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