-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathsharedRuntimeManager.ts
More file actions
356 lines (296 loc) · 10.4 KB
/
sharedRuntimeManager.ts
File metadata and controls
356 lines (296 loc) · 10.4 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import { assertExhaustive } from "../../utils.js";
import { clock } from "../clock-api.js";
import { lifecycleHooks } from "../lifecycle-hooks-api.js";
import { DebugLogPropertiesInput } from "../runEngineWorker/index.js";
import {
BatchTaskRunExecutionResult,
CompletedWaitpoint,
TaskRunContext,
TaskRunExecutionResult,
TaskRunFailedExecutionResult,
TaskRunSuccessfulExecutionResult,
WaitpointTokenResult,
} from "../schemas/index.js";
import { tryCatch } from "../tryCatch.js";
import { ExecutorToWorkerProcessConnection } from "../zodIpc.js";
import { RuntimeManager } from "./manager.js";
import { preventMultipleWaits } from "./preventMultipleWaits.js";
/** A function that resolves a waitpoint */
type Resolver = (value: CompletedWaitpoint) => void;
/** Branded type for resolver IDs to keep us from doing anything stupid */
type ResolverId = string & { readonly __brand: unique symbol };
export class SharedRuntimeManager implements RuntimeManager {
/** Maps a resolver ID to a resolver function */
private readonly resolversById = new Map<ResolverId, Resolver>();
/** Stores waitpoints that arrive before their resolvers have been created */
private readonly waitpointsByResolverId = new Map<ResolverId, CompletedWaitpoint>();
private _preventMultipleWaits = preventMultipleWaits();
constructor(
private ipc: ExecutorToWorkerProcessConnection,
private showLogs: boolean
) {
// Log out the runtime status on a long interval to help debug stuck executions
setInterval(() => {
this.debugLog("SharedRuntimeManager status");
}, 300_000);
}
reset(): void {
this.resolversById.clear();
this.waitpointsByResolverId.clear();
}
disable(): void {
// do nothing
}
async waitForTask(params: { id: string; ctx: TaskRunContext }): Promise<TaskRunExecutionResult> {
return this._preventMultipleWaits(async () => {
const promise = new Promise<CompletedWaitpoint>((resolve) => {
this.resolversById.set(params.id as ResolverId, resolve);
});
// Resolve any waitpoints we received before the resolver was created
this.resolvePendingWaitpoints();
await lifecycleHooks.callOnWaitHookListeners({
type: "task",
runId: params.id,
});
const waitpoint = await this.suspendable(promise);
const result = this.waitpointToTaskRunExecutionResult(waitpoint);
await lifecycleHooks.callOnResumeHookListeners({
type: "task",
runId: params.id,
});
return result;
});
}
async waitForBatch(params: {
id: string;
runCount: number;
ctx: TaskRunContext;
}): Promise<BatchTaskRunExecutionResult> {
return this._preventMultipleWaits(async () => {
if (!params.runCount) {
return Promise.resolve({ id: params.id, items: [] });
}
const promises = Array.from({ length: params.runCount }, (_, index) => {
const resolverId = `${params.id}_${index}` as ResolverId;
return new Promise<CompletedWaitpoint>((resolve, reject) => {
this.resolversById.set(resolverId, resolve);
});
});
// Resolve any waitpoints we received before the resolvers were created
this.resolvePendingWaitpoints();
await lifecycleHooks.callOnWaitHookListeners({
type: "batch",
batchId: params.id,
runCount: params.runCount,
});
const waitpoints = await this.suspendable(Promise.all(promises));
await lifecycleHooks.callOnResumeHookListeners({
type: "batch",
batchId: params.id,
runCount: params.runCount,
});
return {
id: params.id,
items: waitpoints.map((wp) => this.waitpointToTaskRunExecutionResult(wp)),
};
});
}
async waitForWaitpoint({
waitpointFriendlyId,
finishDate,
}: {
waitpointFriendlyId: string;
finishDate?: Date;
}): Promise<WaitpointTokenResult> {
return this._preventMultipleWaits(async () => {
const promise = new Promise<CompletedWaitpoint>((resolve) => {
this.resolversById.set(waitpointFriendlyId as ResolverId, resolve);
});
// Resolve any waitpoints we received before the resolver was created
this.resolvePendingWaitpoints();
if (finishDate) {
await lifecycleHooks.callOnWaitHookListeners({
type: "duration",
date: finishDate,
});
} else {
await lifecycleHooks.callOnWaitHookListeners({
type: "token",
token: waitpointFriendlyId,
});
}
const waitpoint = await this.suspendable(promise);
if (finishDate) {
await lifecycleHooks.callOnResumeHookListeners({
type: "duration",
date: finishDate,
});
} else {
await lifecycleHooks.callOnResumeHookListeners({
type: "token",
token: waitpointFriendlyId,
});
}
return {
ok: !waitpoint.outputIsError,
output: waitpoint.output,
outputType: waitpoint.outputType,
};
});
}
async resolveWaitpoints(waitpoints: CompletedWaitpoint[]): Promise<void> {
await Promise.all(waitpoints.map((waitpoint) => this.resolveWaitpoint(waitpoint)));
}
private resolverIdFromWaitpoint(waitpoint: CompletedWaitpoint): ResolverId | null {
let id: string;
switch (waitpoint.type) {
case "RUN": {
if (!waitpoint.completedByTaskRun) {
this.debugLog("no completedByTaskRun for RUN waitpoint", {
waitpoint: this.waitpointForDebugLog(waitpoint),
});
return null;
}
if (waitpoint.completedByTaskRun.batch) {
// This run is part of a batch
id = `${waitpoint.completedByTaskRun.batch.friendlyId}_${waitpoint.index}`;
} else {
// This run is NOT part of a batch
id = waitpoint.completedByTaskRun.friendlyId;
}
break;
}
case "BATCH": {
if (!waitpoint.completedByBatch) {
this.debugLog("no completedByBatch for BATCH waitpoint", {
waitpoint: this.waitpointForDebugLog(waitpoint),
});
return null;
}
id = waitpoint.completedByBatch.friendlyId;
break;
}
case "MANUAL":
case "DATETIME": {
id = waitpoint.friendlyId;
break;
}
default: {
assertExhaustive(waitpoint.type);
}
}
return id as ResolverId;
}
private resolveWaitpoint(waitpoint: CompletedWaitpoint, resolverId?: ResolverId | null): void {
// This is spammy, don't make this a debug log
this.log("resolveWaitpoint", waitpoint);
if (waitpoint.type === "BATCH") {
// We currently ignore these, they're not required to resume after a batch completes
this.debugLog("ignoring BATCH waitpoint", {
waitpoint: this.waitpointForDebugLog(waitpoint),
});
return;
}
resolverId = resolverId ?? this.resolverIdFromWaitpoint(waitpoint);
if (!resolverId) {
this.debugLog("no resolverId for waitpoint", {
waitpoint: this.waitpointForDebugLog(waitpoint),
});
// No need to store the waitpoint, we'll never be able to resolve it
return;
}
const resolve = this.resolversById.get(resolverId);
if (!resolve) {
this.debugLog("no resolver found for resolverId", {
resolverId,
waitpoint: this.waitpointForDebugLog(waitpoint),
});
// Store the waitpoint for later if we can't find a resolver
this.waitpointsByResolverId.set(resolverId, waitpoint);
return;
}
// Ensure current time is accurate before resolving the waitpoint
clock.reset();
resolve(waitpoint);
this.resolversById.delete(resolverId);
this.waitpointsByResolverId.delete(resolverId);
}
private resolvePendingWaitpoints(): void {
for (const [resolverId, waitpoint] of this.waitpointsByResolverId.entries()) {
this.resolveWaitpoint(waitpoint, resolverId);
}
}
private setSuspendable(suspendable: boolean): void {
this.ipc.send("SET_SUSPENDABLE", { suspendable });
}
private async suspendable<T>(promise: Promise<T>): Promise<T> {
this.setSuspendable(true);
const [error, result] = await tryCatch(promise);
this.setSuspendable(false);
if (error) {
this.debugLog("error in suspendable wrapper", { error: String(error) });
throw error;
}
return result;
}
private waitpointToTaskRunExecutionResult(waitpoint: CompletedWaitpoint): TaskRunExecutionResult {
if (!waitpoint.completedByTaskRun?.friendlyId) throw new Error("Missing completedByTaskRun");
if (waitpoint.outputIsError) {
return {
ok: false,
id: waitpoint.completedByTaskRun.friendlyId,
taskIdentifier: waitpoint.completedByTaskRun.taskIdentifier ?? "unknown",
error: waitpoint.output
? JSON.parse(waitpoint.output)
: {
type: "STRING_ERROR",
message: "Missing error output",
},
} satisfies TaskRunFailedExecutionResult;
} else {
return {
ok: true,
id: waitpoint.completedByTaskRun.friendlyId,
taskIdentifier: waitpoint.completedByTaskRun.taskIdentifier ?? "unknown",
output: waitpoint.output,
outputType: waitpoint.outputType ?? "application/json",
} satisfies TaskRunSuccessfulExecutionResult;
}
}
private waitpointForDebugLog(waitpoint: CompletedWaitpoint): DebugLogPropertiesInput {
const { completedAfter, completedAt, output, ...rest } = waitpoint;
return {
...rest,
output: output?.slice(0, 100),
completedAfter: completedAfter?.toISOString(),
completedAt: completedAt?.toISOString(),
completedAfterDate: completedAfter,
completedAtDate: completedAt,
};
}
private debugLog(message: string, properties?: DebugLogPropertiesInput) {
if (this.showLogs) {
console.log(`[${new Date().toISOString()}] ${message}`, {
runtimeStatus: this.status,
...properties,
});
}
this.ipc.send("SEND_DEBUG_LOG", {
message,
properties: {
runtimeStatus: this.status,
...properties,
},
});
}
private log(message: string, ...args: any[]) {
if (!this.showLogs) return;
console.log(`[${new Date().toISOString()}] ${message}`, args);
}
private get status() {
return {
resolversById: Array.from(this.resolversById.keys()),
waitpointsByResolverId: Array.from(this.waitpointsByResolverId.keys()),
};
}
}