@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 117 lines 3.25 KB
Raw
1 import type { Input } from "alchemy/Input";
2 import * as Scheduler from "../Scheduler/index.ts";
3 import type { Cluster } from "./Cluster.ts";
4
5 export interface ScheduleProps {
6 /**
7 * ECS cluster that the scheduled task should run on.
8 */
9 cluster: Cluster;
10
11 /**
12 * ECS task definition and roles to invoke on each schedule tick.
13 *
14 * This is typically an `AWS.ECS.Task` resource (whose attributes are
15 * `Output`s — the fields are `Input`-typed because `every` is a helper
16 * function, not resource Props).
17 */
18 task: {
19 /**
20 * Task definition ARN to run.
21 */
22 taskDefinitionArn: Input<string>;
23 /**
24 * Task role ARN passed through EventBridge `iam:PassRole`.
25 */
26 taskRoleArn: Input<string>;
27 /**
28 * Execution role ARN passed through EventBridge `iam:PassRole`.
29 */
30 executionRoleArn: Input<string>;
31 };
32
33 /**
34 * Subnets used for the Fargate task network configuration.
35 */
36 subnets: Input<string[]>;
37
38 /**
39 * Security groups attached to the scheduled task ENIs.
40 */
41 securityGroups?: Input<string[]>;
42
43 /**
44 * Whether the scheduled task should receive a public IP.
45 * @default false
46 */
47 assignPublicIp?: boolean;
48
49 /**
50 * Number of task copies to start on each invocation.
51 * @default 1
52 */
53 taskCount?: number;
54
55 /**
56 * Static JSON payload forwarded to the EventBridge ECS target.
57 */
58 input?: string;
59 }
60
61 const toScheduleExpression = (value: string) =>
62 value.startsWith("rate(") || value.startsWith("cron(") ? value : `rate(${value})`;
63
64 /**
65 * Creates a scheduled EventBridge rule that runs an ECS Fargate task.
66 *
67 * `every` is the high-level ECS scheduling helper for phase 1. It provisions
68 * the EventBridge rule plus the invoke role required to call `ecs:RunTask` and
69 * `iam:PassRole` for the target task's execution roles.
70 *
71 * Plain English durations like `"1 hour"` are normalized to `rate(...)`
72 * expressions automatically. Full `rate(...)` and `cron(...)` expressions are
73 * also accepted as-is.
74 * **Example:** Run a task every hour
75 * ```typescript
76 * yield* AWS.ECS.every("HourlyJob", "1 hour", {
77 * cluster,
78 * task: jobTask,
79 * subnets: [privateSubnet1.subnetId, privateSubnet2.subnetId],
80 * securityGroups: [jobSecurityGroup.groupId],
81 * });
82 * ```
83 *
84 * **Example:** Use an explicit cron expression
85 * ```typescript
86 * yield* AWS.ECS.every("NightlyJob", "cron(0 3 * * ? *)", {
87 * cluster,
88 * task: nightlyTask,
89 * subnets: [privateSubnet1.subnetId, privateSubnet2.subnetId],
90 * securityGroups: [jobSecurityGroup.groupId],
91 * });
92 * ```
93 *
94 * **Example:** Run multiple copies with static input
95 * ```typescript
96 * yield* AWS.ECS.every("BatchJob", "30 minutes", {
97 * cluster,
98 * task: batchTask,
99 * subnets: [privateSubnet1.subnetId, privateSubnet2.subnetId],
100 * securityGroups: [jobSecurityGroup.groupId],
101 * taskCount: 3,
102 * input: JSON.stringify({ source: "scheduler" }),
103 * });
104 * ```
105 *
106 * @binding
107 */
108 export const every = (id: string, schedule: string, props: ScheduleProps) =>
109 Scheduler.every(toScheduleExpression(schedule)).named(id).toEcsTask({
110 cluster: props.cluster,
111 task: props.task,
112 subnets: props.subnets,
113 securityGroups: props.securityGroups,
114 assignPublicIp: props.assignPublicIp,
115 taskCount: props.taskCount,
116 input: props.input,
117 });