-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathrunEngine.ts
More file actions
297 lines (260 loc) · 8.32 KB
/
runEngine.ts
File metadata and controls
297 lines (260 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { z } from "zod";
import { Enum, MachinePreset, RuntimeEnvironmentType, TaskRunExecution } from "./common.js";
import { EnvironmentType } from "./schemas.js";
import type * as DB_TYPES from "@trigger.dev/database";
const anyString = z.custom<string & {}>((v) => typeof v === "string");
export const TriggerSource = z
.enum(["sdk", "api", "dashboard", "cli", "mcp", "schedule"])
.or(anyString);
export type TriggerSource = z.infer<typeof TriggerSource>;
export const TriggerAction = z.enum(["trigger", "replay", "test"]).or(anyString);
export type TriggerAction = z.infer<typeof TriggerAction>;
export const RunAnnotations = z.object({
triggerSource: TriggerSource,
triggerAction: TriggerAction,
rootTriggerSource: TriggerSource,
rootScheduleId: z.string().optional(),
});
export type RunAnnotations = z.infer<typeof RunAnnotations>;
export const TaskRunExecutionStatus = {
RUN_CREATED: "RUN_CREATED",
QUEUED: "QUEUED",
QUEUED_EXECUTING: "QUEUED_EXECUTING",
PENDING_EXECUTING: "PENDING_EXECUTING",
EXECUTING: "EXECUTING",
EXECUTING_WITH_WAITPOINTS: "EXECUTING_WITH_WAITPOINTS",
SUSPENDED: "SUSPENDED",
PENDING_CANCEL: "PENDING_CANCEL",
FINISHED: "FINISHED",
DELAYED: "DELAYED",
} satisfies Enum<DB_TYPES.TaskRunExecutionStatus>;
export type TaskRunExecutionStatus =
(typeof TaskRunExecutionStatus)[keyof typeof TaskRunExecutionStatus];
export const TaskRunStatus = {
DELAYED: "DELAYED",
PENDING: "PENDING",
PENDING_VERSION: "PENDING_VERSION",
WAITING_FOR_DEPLOY: "WAITING_FOR_DEPLOY",
DEQUEUED: "DEQUEUED",
EXECUTING: "EXECUTING",
WAITING_TO_RESUME: "WAITING_TO_RESUME",
RETRYING_AFTER_FAILURE: "RETRYING_AFTER_FAILURE",
PAUSED: "PAUSED",
CANCELED: "CANCELED",
INTERRUPTED: "INTERRUPTED",
COMPLETED_SUCCESSFULLY: "COMPLETED_SUCCESSFULLY",
COMPLETED_WITH_ERRORS: "COMPLETED_WITH_ERRORS",
SYSTEM_FAILURE: "SYSTEM_FAILURE",
CRASHED: "CRASHED",
EXPIRED: "EXPIRED",
TIMED_OUT: "TIMED_OUT",
} satisfies Enum<DB_TYPES.TaskRunStatus>;
export type TaskRunStatus = (typeof TaskRunStatus)[keyof typeof TaskRunStatus];
export const WaitpointType = {
RUN: "RUN",
DATETIME: "DATETIME",
MANUAL: "MANUAL",
BATCH: "BATCH",
} satisfies Enum<DB_TYPES.WaitpointType>;
export type WaitpointType = (typeof WaitpointType)[keyof typeof WaitpointType];
const WaitpointStatusValues = {
PENDING: "PENDING",
COMPLETED: "COMPLETED",
} satisfies Enum<DB_TYPES.WaitpointStatus>;
export const WaitpointStatus = z.enum(
Object.values(WaitpointStatusValues) as [DB_TYPES.WaitpointStatus]
);
export type WaitpointStatus = z.infer<typeof WaitpointStatus>;
export type TaskEventEnvironment = {
id: string;
type: RuntimeEnvironmentType;
organizationId: string;
projectId: string;
project: {
externalRef: string;
};
};
export const CompletedWaitpoint = z.object({
id: z.string(),
index: z.number().optional(),
friendlyId: z.string(),
type: z.enum(Object.values(WaitpointType) as [WaitpointType]),
completedAt: z.coerce.date(),
idempotencyKey: z.string().optional(),
/** For type === "RUN" */
completedByTaskRun: z
.object({
id: z.string(),
friendlyId: z.string(),
/** If the run has an associated batch */
batch: z
.object({
id: z.string(),
friendlyId: z.string(),
})
.optional(),
})
.optional(),
/** For type === "DATETIME" */
completedAfter: z.coerce.date().optional(),
/** For type === "BATCH" */
completedByBatch: z
.object({
id: z.string(),
friendlyId: z.string(),
})
.optional(),
output: z.string().optional(),
outputType: z.string().optional(),
outputIsError: z.boolean(),
});
export type CompletedWaitpoint = z.infer<typeof CompletedWaitpoint>;
const ExecutionSnapshot = z.object({
id: z.string(),
friendlyId: z.string(),
executionStatus: z.enum(Object.values(TaskRunExecutionStatus) as [TaskRunExecutionStatus]),
description: z.string(),
createdAt: z.coerce.date(),
});
const BaseRunMetadata = z.object({
id: z.string(),
friendlyId: z.string(),
status: z.enum(Object.values(TaskRunStatus) as [TaskRunStatus]),
attemptNumber: z.number().nullish(),
taskEventStore: z.string().optional(),
});
export const ExecutionResult = z.object({
snapshot: ExecutionSnapshot,
run: BaseRunMetadata,
});
export type ExecutionResult = z.infer<typeof ExecutionResult>;
/** The response to the Worker when starting an attempt */
export const StartRunAttemptResult = ExecutionResult.and(
z.object({
execution: TaskRunExecution,
})
);
export type StartRunAttemptResult = z.infer<typeof StartRunAttemptResult>;
/** The response to the Worker when completing an attempt */
const CompleteAttemptStatus = z.enum([
"RUN_FINISHED",
"RUN_PENDING_CANCEL",
"RETRY_QUEUED",
"RETRY_IMMEDIATELY",
]);
export type CompleteAttemptStatus = z.infer<typeof CompleteAttemptStatus>;
export const CompleteRunAttemptResult = z
.object({
attemptStatus: CompleteAttemptStatus,
})
.and(ExecutionResult);
export type CompleteRunAttemptResult = z.infer<typeof CompleteRunAttemptResult>;
export const CheckpointTypeEnum = {
DOCKER: "DOCKER",
KUBERNETES: "KUBERNETES",
} satisfies Enum<DB_TYPES.CheckpointType>;
export type CheckpointTypeEnum = (typeof CheckpointTypeEnum)[keyof typeof CheckpointTypeEnum];
export const CheckpointType = z.enum(Object.values(CheckpointTypeEnum) as [CheckpointTypeEnum]);
export type CheckpointType = z.infer<typeof CheckpointType>;
export const CheckpointInput = z.object({
type: CheckpointType,
location: z.string(),
imageRef: z.string().nullish(),
reason: z.string().nullish(),
});
export type CheckpointInput = z.infer<typeof CheckpointInput>;
export const TaskRunCheckpoint = CheckpointInput.merge(
z.object({
id: z.string(),
friendlyId: z.string(),
})
);
export type TaskRunCheckpoint = z.infer<typeof TaskRunCheckpoint>;
/** The response when a Worker asks for the latest execution state */
export const RunExecutionData = z.object({
version: z.literal("1"),
snapshot: ExecutionSnapshot,
run: BaseRunMetadata,
batch: z
.object({
id: z.string(),
friendlyId: z.string(),
})
.optional(),
checkpoint: TaskRunCheckpoint.optional(),
completedWaitpoints: z.array(CompletedWaitpoint),
});
export type RunExecutionData = z.infer<typeof RunExecutionData>;
export const CreateCheckpointResult = z.discriminatedUnion("ok", [
z
.object({
ok: z.literal(true),
checkpoint: TaskRunCheckpoint,
})
.merge(ExecutionResult),
z.object({
ok: z.literal(false),
error: z.string(),
}),
]);
export type CreateCheckpointResult = z.infer<typeof CreateCheckpointResult>;
export const MachineResources = z.object({
cpu: z.number(),
memory: z.number(),
});
export type MachineResources = z.infer<typeof MachineResources>;
export const DequeueMessageCheckpoint = z.object({
id: z.string(),
type: CheckpointType,
location: z.string(),
imageRef: z.string().nullish(),
reason: z.string().nullish(),
});
export type DequeueMessageCheckpoint = z.infer<typeof DequeueMessageCheckpoint>;
export const PlacementTag = z.object({
key: z.string(),
values: z.array(z.string()).optional(),
});
export type PlacementTag = z.infer<typeof PlacementTag>;
/** This is sent to a Worker when a run is dequeued (a new run or continuing run) */
export const DequeuedMessage = z.object({
version: z.literal("1"),
snapshot: ExecutionSnapshot,
dequeuedAt: z.coerce.date(),
workerQueueLength: z.number().optional(),
image: z.string().optional(),
checkpoint: DequeueMessageCheckpoint.optional(),
completedWaitpoints: z.array(CompletedWaitpoint),
backgroundWorker: z.object({
id: z.string(),
friendlyId: z.string(),
version: z.string(),
}),
deployment: z.object({
id: z.string().optional(),
friendlyId: z.string().optional(),
imagePlatform: z.string().optional(),
}),
run: z.object({
id: z.string(),
friendlyId: z.string(),
isTest: z.boolean(),
machine: MachinePreset,
attemptNumber: z.number(),
masterQueue: z.string(),
traceContext: z.record(z.unknown()),
annotations: RunAnnotations.optional(),
}),
environment: z.object({
id: z.string(),
type: EnvironmentType,
}),
organization: z.object({
id: z.string(),
}),
project: z.object({
id: z.string(),
}),
placementTags: z.array(PlacementTag).optional(),
});
export type DequeuedMessage = z.infer<typeof DequeuedMessage>;