Open History to see the last commit
1import { ThreadId } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Schema from "effect/Schema";
7
8import { resolveAttachmentRelativePath } from "../attachmentPaths.ts";
9import { parseAttachmentFileExtension, resolveAttachmentPathById } from "../attachmentStore.ts";
10import * as ServerConfig from "../config.ts";
11import * as PreviewManager from "../preview/Manager.ts";
12import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts";
13import * as TerminalManager from "../terminal/Manager.ts";
14
15export class ResourceCleanupError extends Schema.TaggedError<ResourceCleanupError>()(
16 "ResourceCleanupError",
17 {
18 operation: Schema.Literals(["terminal", "preview", "attachment", "scheduled-tasks"]),
19 threadId: Schema.optional(Schema.String),
20 attachmentId: Schema.optional(Schema.String),
21 cause: Schema.Defect(),
22 },
23) {}
24
25export class ResourceCleanupService extends Context.Reference<{
26 readonly cleanupScheduledTasks: (threadId: ThreadId) => Effect.Effect<void, ResourceCleanupError>;
27 readonly cleanupTerminals: (threadId: string) => Effect.Effect<void, ResourceCleanupError>;
28 readonly cleanupPreviews: (threadId: string) => Effect.Effect<void, ResourceCleanupError>;
29 readonly cleanupAttachments: (
30 attachmentIds: ReadonlyArray<string>,
31 ) => Effect.Effect<void, ResourceCleanupError>;
32}>("t3/orchestration-v2/ResourceCleanupService", {
33 defaultValue: () => ({
34 cleanupScheduledTasks: () => Effect.void,
35 cleanupTerminals: () => Effect.void,
36 cleanupPreviews: () => Effect.void,
37 cleanupAttachments: () => Effect.void,
38 }),
39}) {}
40
41export const layer = Layer.effect(
42 ResourceCleanupService,
43 Effect.gen(function* () {
44 const terminals = yield* TerminalManager.TerminalManager;
45 const previews = yield* PreviewManager.PreviewManager;
46 const fileSystem = yield* FileSystem.FileSystem;
47 const config = yield* ServerConfig.ServerConfig;
48 const scheduledTasks = yield* ScheduledTaskService.ScheduledTaskService;
49 return {
50 cleanupScheduledTasks: (threadId: ThreadId) =>
51 scheduledTasks
52 .deleteForThread(threadId)
53 .pipe(
54 Effect.mapError(
55 (cause) =>
56 new ResourceCleanupError({ operation: "scheduled-tasks", threadId, cause }),
57 ),
58 ),
59 cleanupTerminals: (threadId: string) =>
60 terminals
61 .close({ threadId, deleteHistory: true })
62 .pipe(
63 Effect.mapError(
64 (cause) => new ResourceCleanupError({ operation: "terminal", threadId, cause }),
65 ),
66 ),
67 cleanupPreviews: (threadId: string) =>
68 previews
69 .close({ threadId: ThreadId.make(threadId) })
70 .pipe(
71 Effect.mapError(
72 (cause) => new ResourceCleanupError({ operation: "preview", threadId, cause }),
73 ),
74 ),
75 cleanupAttachments: (attachmentIds: ReadonlyArray<string>) =>
76 Effect.forEach(
77 attachmentIds,
78 (attachmentId) => {
79 const extension = parseAttachmentFileExtension(attachmentId);
80 const path =
81 resolveAttachmentPathById({
82 attachmentsDir: config.attachmentsDir,
83 attachmentId,
84 }) ??
85 (extension
86 ? resolveAttachmentRelativePath({
87 attachmentsDir: config.attachmentsDir,
88 relativePath: `${attachmentId}.${extension}`,
89 })
90 : null);
91 return path === null
92 ? Effect.void
93 : fileSystem.remove(path, { force: true }).pipe(
94 Effect.andThen(fileSystem.remove(`${path}.txt`, { force: true })),
95 Effect.mapError(
96 (cause) =>
97 new ResourceCleanupError({ operation: "attachment", attachmentId, cause }),
98 ),
99 );
100 },
101 { discard: true, concurrency: 4 },
102 ),
103 };
104 }),
105);