Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ResourceCleanupService.ts

117 lines4.5 KB

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: Effect.fn("ResourceCleanupService.cleanupScheduledTasks")(function (
51 threadId: ThreadId,
52 ) {
53 return scheduledTasks
54 .deleteForThread(threadId)
55 .pipe(
56 Effect.mapError(
57 (cause) =>
58 new ResourceCleanupError({ operation: "scheduled-tasks", threadId, cause }),
59 ),
60 );
61 }),
62 cleanupTerminals: Effect.fn("ResourceCleanupService.cleanupTerminals")(function (
63 threadId: string,
64 ) {
65 return terminals
66 .close({ threadId, deleteHistory: true })
67 .pipe(
68 Effect.mapError(
69 (cause) => new ResourceCleanupError({ operation: "terminal", threadId, cause }),
70 ),
71 );
72 }),
73 cleanupPreviews: Effect.fn("ResourceCleanupService.cleanupPreviews")(function (
74 threadId: string,
75 ) {
76 return previews
77 .close({ threadId: ThreadId.make(threadId) })
78 .pipe(
79 Effect.mapError(
80 (cause) => new ResourceCleanupError({ operation: "preview", threadId, cause }),
81 ),
82 );
83 }),
84 cleanupAttachments: Effect.fn("ResourceCleanupService.cleanupAttachments")(function (
85 attachmentIds: ReadonlyArray<string>,
86 ) {
87 return Effect.forEach(
88 attachmentIds,
89 (attachmentId) => {
90 const extension = parseAttachmentFileExtension(attachmentId);
91 const path =
92 resolveAttachmentPathById({
93 attachmentsDir: config.attachmentsDir,
94 attachmentId,
95 }) ??
96 (extension
97 ? resolveAttachmentRelativePath({
98 attachmentsDir: config.attachmentsDir,
99 relativePath: `${attachmentId}.${extension}`,
100 })
101 : null);
102 return path === null
103 ? Effect.void
104 : fileSystem.remove(path, { force: true }).pipe(
105 Effect.andThen(fileSystem.remove(`${path}.txt`, { force: true })),
106 Effect.mapError(
107 (cause) =>
108 new ResourceCleanupError({ operation: "attachment", attachmentId, cause }),
109 ),
110 );
111 },
112 { discard: true, concurrency: 4 },
113 );
114 }),
115 };
116 }),
117);