| 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) => |
| 17 | arn?.split("/").pop()?.split(":")[0]; |
| 18 | |
| 19 | /** |
| 20 | * Restart every RUNNING task of `family` that is not yet on |
| 21 | * `nextTaskDefinitionArn`: |
| 22 | * |
| 23 | * - **standalone** tasks (launched via `RunTask`, `startedBy` ≠ the service |
| 24 | * scheduler) are stopped and re-run on the new revision in the same |
| 25 | * cluster/launch type; |
| 26 | * - **service-managed** tasks (`startedBy: "ecs-svc"` in floci) are only |
| 27 | * stopped — the service scheduler relaunches them on the service's |
| 28 | * current (freshly-updated) task definition. |
| 29 | * |
| 30 | * Bounded: one `listClusters` page, one `listTasks`/`describeTasks` round |
| 31 | * per cluster. |
| 32 | */ |
| 33 | export const restartFamilyTasks = Effect.fn(function* (options: { |
| 34 | family: string; |
| 35 | nextTaskDefinitionArn: string; |
| 36 | /** Restart the service scheduler's tasks instead of standalone ones. */ |
| 37 | serviceManaged: boolean; |
| 38 | }) { |
| 39 | const clusters = yield* ecs.listClusters({}); |
| 40 | let restarted = 0; |
| 41 | for (const cluster of clusters.clusterArns ?? []) { |
| 42 | const listed = yield* ecs |
| 43 | .listTasks({ |
| 44 | cluster, |
| 45 | family: options.family, |
| 46 | desiredStatus: "RUNNING", |
| 47 | }) |
| 48 | .pipe( |
| 49 | Effect.catchTag("ClusterNotFoundException", () => |
| 50 | Effect.succeed({ taskArns: [] as string[] }), |
| 51 | ), |
| 52 | ); |
| 53 | const taskArns = listed.taskArns ?? []; |
| 54 | if (taskArns.length === 0) continue; |
| 55 | const described = yield* ecs.describeTasks({ cluster, tasks: taskArns }); |
| 56 | for (const task of described.tasks ?? []) { |
| 57 | if (task.taskArn === undefined) continue; |
| 58 | // Defensive re-filters: an emulator that ignores the list filters |
| 59 | // still only restarts RUNNING tasks of exactly this family. |
| 60 | if (task.lastStatus !== "RUNNING") continue; |
| 61 | if (familyOfArn(task.taskDefinitionArn) !== options.family) continue; |
| 62 | if (task.taskDefinitionArn === options.nextTaskDefinitionArn) continue; |
| 63 | const isServiceManaged = task.startedBy === "ecs-svc"; |
| 64 | if (isServiceManaged !== options.serviceManaged) continue; |
| 65 | yield* ecs.stopTask({ |
| 66 | cluster, |
| 67 | task: task.taskArn, |
| 68 | reason: "alchemy dev hot swap", |
| 69 | }); |
| 70 | if (!options.serviceManaged) { |
| 71 | yield* ecs.runTask({ |
| 72 | cluster, |
| 73 | taskDefinition: options.nextTaskDefinitionArn, |
| 74 | count: 1, |
| 75 | launchType: task.launchType ?? "EC2", |
| 76 | }); |
| 77 | } |
| 78 | restarted++; |
| 79 | } |
| 80 | } |
| 81 | return restarted; |
| 82 | }); |