The best way to control Claude Code, Codex and more from any computer or phone

main
ts 84 lines 3.08 KB
Raw
1 /** @effect-diagnostics anyUnknownInErrorContext:off */
2
3 /**
4 * INTERNAL — shared change-trigger and task-restart machinery for the floci
5 * ECS dev providers ([FlociTaskProvider](./FlociTaskProvider.ts),
6 * [FlociServiceProvider](./FlociServiceProvider.ts)). NOT exported from the
7 * service `index.ts`.
8 */
9
10 import * as ecs from "@distilled.cloud/aws/ecs";
11 import * as Effect from "effect/Effect";
12
13 export { imageSourceTrigger } from "../Local/ImageSourceTrigger.ts";
14
15 /** The family segment of a task definition revision ARN. */
16 const familyOfArn = (arn: string | undefined) => arn?.split("/").pop()?.split(":")[0];
17
18 /**
19 * Restart every RUNNING task of `family` that is not yet on
20 * `nextTaskDefinitionArn`:
21 *
22 * - **standalone** tasks (launched via `RunTask`, `startedBy` ≠ the service
23 * scheduler) are stopped and re-run on the new revision in the same
24 * cluster/launch type;
25 * - **service-managed** tasks (`startedBy: "ecs-svc/<deployment>"`) are only
26 * stopped — the service scheduler relaunches them on the service's
27 * current (freshly-updated) task definition.
28 *
29 * Bounded: one `listClusters` page, one `listTasks`/`describeTasks` round
30 * per cluster.
31 */
32 export const restartFamilyTasks = Effect.fn(function* (options: {
33 family: string;
34 nextTaskDefinitionArn: string;
35 /** Restart the service scheduler's tasks instead of standalone ones. */
36 serviceManaged: boolean;
37 }) {
38 const clusters = yield* ecs.listClusters({});
39 let restarted = 0;
40 for (const cluster of clusters.clusterArns ?? []) {
41 const listed = yield* ecs
42 .listTasks({
43 cluster,
44 family: options.family,
45 desiredStatus: "RUNNING",
46 })
47 .pipe(
48 Effect.catchTag("ClusterNotFoundException", () =>
49 Effect.succeed({ taskArns: [] as string[] }),
50 ),
51 );
52 const taskArns = listed.taskArns ?? [];
53 if (taskArns.length === 0) continue;
54 const described = yield* ecs.describeTasks({ cluster, tasks: taskArns });
55 for (const task of described.tasks ?? []) {
56 if (task.taskArn === undefined) continue;
57 // Defensive re-filters: an emulator that ignores the list filters
58 // still only restarts RUNNING tasks of exactly this family.
59 if (task.lastStatus !== "RUNNING") continue;
60 if (familyOfArn(task.taskDefinitionArn) !== options.family) continue;
61 if (task.taskDefinitionArn === options.nextTaskDefinitionArn) continue;
62 // ECS stamps service tasks with their deployment id (`ecs-svc/<id>`);
63 // older floci builds used the bare `ecs-svc`.
64 const isServiceManaged =
65 task.startedBy === "ecs-svc" || (task.startedBy?.startsWith("ecs-svc/") ?? false);
66 if (isServiceManaged !== options.serviceManaged) continue;
67 yield* ecs.stopTask({
68 cluster,
69 task: task.taskArn,
70 reason: "alchemy dev hot swap",
71 });
72 if (!options.serviceManaged) {
73 yield* ecs.runTask({
74 cluster,
75 taskDefinition: options.nextTaskDefinitionArn,
76 count: 1,
77 launchType: task.launchType ?? "EC2",
78 });
79 }
80 restarted++;
81 }
82 }
83 return restarted;
84 });