-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcontext.ts
More file actions
1175 lines (1055 loc) · 33.8 KB
/
context.ts
File metadata and controls
1175 lines (1055 loc) · 33.8 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { RpcTarget } from "cloudflare:workers";
import { ms } from "itty-time";
import { INSTANCE_METADATA, InstanceEvent, InstanceStatus } from "./instance";
import { computeHash } from "./lib/cache";
import {
ABORT_REASONS,
InvalidStepReadableStreamError,
OversizedStreamChunkError,
StreamOutputStorageLimitError,
UnsupportedStreamChunkError,
WorkflowFatalError,
WorkflowInternalError,
WorkflowTimeoutError,
} from "./lib/errors";
import { calcRetryDuration } from "./lib/retries";
import {
cleanupPendingStreamOutput,
createReplayReadableStream,
getInvalidStoredStreamOutputError,
getStreamOutputMetaKey,
isReadableStreamLike,
rollbackStreamOutput,
StreamOutputState,
writeStreamOutput,
} from "./lib/streams";
import {
isValidStepConfig,
isValidStepName,
MAX_STEP_NAME_LENGTH,
} from "./lib/validators";
import { MODIFIER_KEYS } from "./modifier";
import type { Engine } from "./engine";
import type { InstanceMetadata } from "./instance";
import type { StreamOutputMeta } from "./lib/streams";
import type {
WorkflowSleepDuration,
WorkflowStepConfig,
WorkflowStepEvent,
} from "cloudflare:workers";
export type Event = {
timestamp: Date;
payload: unknown;
type: string;
};
export type ResolvedStepConfig = Required<WorkflowStepConfig>;
const defaultConfig: Required<WorkflowStepConfig> = {
retries: {
limit: 5,
delay: 1000,
backoff: "exponential",
},
timeout: "10 minutes",
};
export interface UserErrorField {
isUserError?: boolean;
}
export type StepState = {
attemptedCount: number;
};
export type WorkflowStepContext = {
step: {
name: string;
count: number;
};
attempt: number;
config: ResolvedStepConfig;
};
const PAUSE_DATETIME = "PAUSE_DATETIME";
export class Context extends RpcTarget {
#engine: Engine;
#state: DurableObjectState;
#counters: Map<string, number> = new Map();
#lifetimeStepCounter: number = 0;
constructor(engine: Engine, state: DurableObjectState) {
super();
this.#engine = engine;
this.#state = state;
}
async #checkForPendingPause(): Promise<void> {
if (this.#engine.timeoutHandler.isRunningStep()) {
return;
}
const status = await this.#engine.getStatus();
if (status === InstanceStatus.Paused) {
throw new Error(ABORT_REASONS.USER_PAUSE);
}
if (status === InstanceStatus.WaitingForPause) {
await this.#state.storage.put(PAUSE_DATETIME, new Date());
const metadata =
await this.#state.storage.get<InstanceMetadata>(INSTANCE_METADATA);
if (metadata) {
await this.#engine.setStatus(
metadata.accountId,
metadata.instance.id,
InstanceStatus.Paused
);
}
throw new Error(ABORT_REASONS.USER_PAUSE);
}
}
#getCount(name: string): number {
let val = this.#counters.get(name) ?? 0;
// 1-indexed, as we're increasing the value before write
val++;
this.#counters.set(name, val);
return val;
}
do(
name: string,
callback: (ctx: WorkflowStepContext) => Promise<unknown>
): Promise<unknown>;
do(
name: string,
config: WorkflowStepConfig,
callback: (ctx: WorkflowStepContext) => Promise<unknown>
): Promise<unknown>;
async do<T>(
name: string,
configOrCallback:
| WorkflowStepConfig
| ((ctx: WorkflowStepContext) => Promise<T>),
callback?: (ctx: WorkflowStepContext) => Promise<T>
): Promise<unknown | void | undefined> {
let closure: (ctx: WorkflowStepContext) => Promise<T>, stepConfig;
// If a user passes in a config, we'd like it to be the second arg so the callback is always last
if (callback) {
closure = callback;
stepConfig = configOrCallback as WorkflowStepConfig;
} else {
closure = configOrCallback as (ctx: WorkflowStepContext) => Promise<T>;
stepConfig = {};
}
this.#lifetimeStepCounter++;
const stepLimit = this.#engine.stepLimit;
if (this.#lifetimeStepCounter > stepLimit) {
throw new WorkflowFatalError(
`The limit of ${stepLimit} steps has been reached. This limit can be changed in your worker configuration.`
);
}
if (!isValidStepName(name)) {
// NOTE(lduarte): marking errors as user error allows the observability layer to avoid leaking
// user errors to sentry while making everything more observable. `isUserError` is not serialized
// into userland code due to how workerd serialzises errors over RPC - we also set it as undefined
// in the obs layer in case changes to workerd happen
const error = new WorkflowFatalError(
`Step name "${name}" exceeds max length (${MAX_STEP_NAME_LENGTH} chars) or invalid characters found`
) as Error & UserErrorField;
error.isUserError = true;
throw error;
}
if (!isValidStepConfig(stepConfig)) {
const error = new WorkflowFatalError(
`Step config for "${name}" is in a invalid format. See https://developers.cloudflare.com/workflows/build/sleeping-and-retrying/`
) as Error & UserErrorField;
error.isUserError = true;
throw error;
}
let config: ResolvedStepConfig = {
...defaultConfig,
...stepConfig,
retries: {
...defaultConfig.retries,
...stepConfig.retries,
},
};
const hash = await computeHash(name);
const count = this.#getCount("run-" + name);
const cacheKey = `${hash}-${count}`;
const valueKey = `${cacheKey}-value`;
const streamMetaKey = getStreamOutputMetaKey(cacheKey);
const configKey = `${cacheKey}-config`;
const errorKey = `${cacheKey}-error`;
const stepNameWithCounter = `${name}-${count}`;
const stepStateKey = `${cacheKey}-metadata`;
const retryDelayDisableKey = `${MODIFIER_KEYS.DISABLE_RETRY_DELAY}${valueKey}`;
const maybeMap = await this.#state.storage.get([
valueKey,
streamMetaKey,
configKey,
errorKey,
]);
// Check cache -- streams first, then plain values
const maybeStreamMeta = maybeMap.get(streamMetaKey) as
| StreamOutputMeta
| undefined
| null;
if (maybeStreamMeta?.state === StreamOutputState.Complete) {
const maybeOutputError = getInvalidStoredStreamOutputError(
this.#state.storage,
cacheKey,
maybeStreamMeta
);
if (maybeOutputError !== undefined) {
throw new WorkflowInternalError(
`Stored output for ${stepNameWithCounter} is corrupt or incomplete.`
);
}
return createReplayReadableStream({
storage: this.#state.storage,
cacheKey,
meta: maybeStreamMeta,
}) as T;
} else if (maybeStreamMeta !== undefined && maybeStreamMeta !== null) {
// We're not in a complete state - means we crashed while persisting a stream on a previous invocation - need to cleanup
await cleanupPendingStreamOutput(this.#state.storage, cacheKey).catch(
() => {}
);
}
const maybeResult = maybeMap.get(valueKey);
if (maybeResult) {
return (maybeResult as { value: T }).value;
}
const maybeError: (Error & UserErrorField) | undefined = maybeMap.get(
errorKey
) as Error | undefined;
if (maybeError) {
maybeError.isUserError = true;
throw maybeError;
}
// Persist initial config because user can pass in dynamic config
if (!maybeMap.has(configKey)) {
await this.#state.storage.put(configKey, config);
} else {
config = maybeMap.get(configKey) as ResolvedStepConfig;
}
const attemptLogs = this.#engine
.readLogsFromStep(cacheKey)
.filter((val) =>
[
InstanceEvent.ATTEMPT_SUCCESS,
InstanceEvent.ATTEMPT_FAILURE,
InstanceEvent.ATTEMPT_START,
].includes(val.event)
);
// this means that the the engine died while executing this step - we can mark the latest attempt as failed
if (
attemptLogs.length > 0 &&
attemptLogs.at(-1)?.event === InstanceEvent.ATTEMPT_START
) {
// TODO: We should get this from SQL
const stepState = ((await this.#state.storage.get(
stepStateKey
)) as StepState) ?? {
attemptedCount: 1,
};
const priorityQueueHash = `${cacheKey}-${stepState.attemptedCount}`;
// @ts-expect-error priorityQueue is initiated in init
const timeoutEntryPQ = this.#engine.priorityQueue.getFirst(
(a) => a.hash === priorityQueueHash && a.type === "timeout"
);
// if there's a timeout on the PQ we pop it, because we wont need it
if (timeoutEntryPQ !== undefined) {
// @ts-expect-error priorityQueue is initiated in init
this.#engine.priorityQueue.remove(timeoutEntryPQ);
}
this.#engine.writeLog(
InstanceEvent.ATTEMPT_FAILURE,
cacheKey,
stepNameWithCounter,
{
attempt: stepState.attemptedCount,
error: {
name: "WorkflowInternalError",
message: `Attempt failed due to internal workflows error`,
},
}
);
await this.#state.storage.put(stepStateKey, stepState);
}
const doWrapper = async (
doWrapperClosure: (ctx: WorkflowStepContext) => Promise<unknown>
): Promise<unknown | void | undefined> => {
const stepState = ((await this.#state.storage.get(
stepStateKey
)) as StepState) ?? {
attemptedCount: 0,
};
// NOTE(caio): this might be a stream returning step - if so cleanup stale data from previous lifetimes
await cleanupPendingStreamOutput(this.#state.storage, cacheKey).catch(
() => {}
);
await this.#engine.timeoutHandler.acquire(this.#engine);
if (stepState.attemptedCount == 0) {
this.#engine.writeLog(
InstanceEvent.STEP_START,
cacheKey,
stepNameWithCounter,
{
config,
}
);
} else {
// in case the engine dies while retrying and wakes up before the retry period
const priorityQueueHash = `${cacheKey}-${stepState.attemptedCount}`;
// @ts-expect-error priorityQueue is initiated in init
const retryEntryPQ = this.#engine.priorityQueue.getFirst(
(a) => a.hash === priorityQueueHash && a.type === "retry"
);
// complete sleep if it didn't finish for some reason
if (retryEntryPQ !== undefined) {
const disableAllRetryDelays = await this.#state.storage.get(
MODIFIER_KEYS.DISABLE_ALL_RETRY_DELAYS
);
const disableThisRetryDelay =
await this.#state.storage.get(retryDelayDisableKey);
const disableRetryDelay =
disableAllRetryDelays || disableThisRetryDelay;
await this.#engine.timeoutHandler.release(this.#engine);
await scheduler.wait(
disableRetryDelay ? 0 : retryEntryPQ.targetTimestamp - Date.now()
);
await this.#engine.timeoutHandler.acquire(this.#engine);
// @ts-expect-error priorityQueue is initiated in init
this.#engine.priorityQueue.remove({
hash: priorityQueueHash,
type: "retry",
});
}
}
let result;
const instanceMetadata =
await this.#state.storage.get<InstanceMetadata>(INSTANCE_METADATA);
if (!instanceMetadata) {
throw new Error("instanceMetadata is undefined");
}
const { accountId, instance } = instanceMetadata;
let streamResultSeen = false;
let lastStreamMeta: StreamOutputMeta | undefined;
const abortController = new AbortController();
const stepExecutionSignal = AbortSignal.any([
abortController.signal,
this.#engine.engineAbortController.signal,
]);
try {
const timeoutPromise = async (): Promise<never> => {
const priorityQueueHash = `${cacheKey}-${stepState.attemptedCount}`;
let timeout = ms(config.timeout);
if (forceStepTimeout) {
timeout = 0;
}
// @ts-expect-error priorityQueue is initiated in init
await this.#engine.priorityQueue.add({
hash: priorityQueueHash,
targetTimestamp: Date.now() + timeout,
type: "timeout",
});
await scheduler.wait(timeout);
// if we reach here, means that we can try to delete the timeout from the PQ
// because we managed to wait in the same lifetime
// @ts-expect-error priorityQueue is initiated in init
await this.#engine.priorityQueue.remove({
hash: priorityQueueHash,
type: "timeout",
});
const error = new WorkflowTimeoutError(
`Execution timed out after ${timeout}ms`
);
abortController.abort(error);
throw error;
};
this.#engine.writeLog(
InstanceEvent.ATTEMPT_START,
cacheKey,
stepNameWithCounter,
{
attempt: stepState.attemptedCount + 1,
}
);
stepState.attemptedCount++;
await this.#state.storage.put(stepStateKey, stepState);
const priorityQueueHash = `${cacheKey}-${stepState.attemptedCount}`;
const mockErrorKey = `${MODIFIER_KEYS.MOCK_STEP_ERROR}${valueKey}`;
const persistentMockError = await this.#state.storage.get<{
name: string;
message: string;
}>(mockErrorKey);
const transientMockError = await this.#state.storage.get<{
name: string;
message: string;
}>(`${mockErrorKey}-${stepState.attemptedCount}`);
const mockErrorPayload = persistentMockError || transientMockError;
// if a mocked error exists, throw it immediately
if (mockErrorPayload) {
const errorToThrow = new Error(mockErrorPayload.message);
errorToThrow.name = mockErrorPayload.name;
throw errorToThrow;
}
const replaceResult = await this.#state.storage.get(
`${MODIFIER_KEYS.REPLACE_RESULT}${valueKey}`
);
const forceStepTimeoutKey = `${MODIFIER_KEYS.FORCE_STEP_TIMEOUT}${valueKey}`;
const persistentStepTimeout =
await this.#state.storage.get(forceStepTimeoutKey);
const transientStepTimeout = await this.#state.storage.get(
`${forceStepTimeoutKey}-${stepState.attemptedCount}`
);
const forceStepTimeout = persistentStepTimeout || transientStepTimeout;
let timeoutTask: Promise<never> | undefined;
const persistStepResult = async (
value: unknown,
activeTimeoutTask?: Promise<never>
): Promise<unknown> => {
if (!isReadableStreamLike(value)) {
await this.#state.storage.put(valueKey, { value });
abortController.abort("step finished");
// @ts-expect-error priorityQueue is initiated in init
this.#engine.priorityQueue.remove({
hash: priorityQueueHash,
type: "timeout",
});
return value;
}
streamResultSeen = true;
const streamMeta = await writeStreamOutput({
storage: this.#state.storage,
cacheKey,
attempt: stepState.attemptedCount,
stream: value as ReadableStream<unknown>,
signal: stepExecutionSignal,
timeoutTask: activeTimeoutTask,
});
lastStreamMeta = streamMeta;
abortController.abort("step finished");
// @ts-expect-error priorityQueue is initiated in init
this.#engine.priorityQueue.remove({
hash: priorityQueueHash,
type: "timeout",
});
return createReplayReadableStream({
storage: this.#state.storage,
cacheKey,
meta: streamMeta,
});
};
if (forceStepTimeout) {
result = await timeoutPromise();
} else if (replaceResult) {
// Check if the mocked result is a stream sentinel (from mockStepResult with ReadableStream)
if (
replaceResult &&
typeof replaceResult === "object" &&
(replaceResult as Record<string, unknown>).__mockStreamOutput
) {
const sentinel = replaceResult as {
__mockStreamOutput: true;
cacheKey: string;
meta: StreamOutputMeta;
};
result = createReplayReadableStream({
storage: this.#state.storage,
cacheKey: sentinel.cacheKey,
meta: sentinel.meta,
});
} else {
result = replaceResult;
}
} else {
timeoutTask = timeoutPromise();
result = await Promise.race([
doWrapperClosure({
step: { name, count },
attempt: stepState.attemptedCount,
config: structuredClone(config),
}),
timeoutTask,
]);
}
// We store the value of `output` in an object with a `value` property. This allows us to store `undefined`,
// in the case that it's returned from the user's code. This is because DO storage will error if you try to
// store undefined directly.
try {
result = await persistStepResult(result, timeoutTask);
} catch (e) {
abortController.abort("step errored");
// @ts-expect-error priorityQueue is initiated in init
this.#engine.priorityQueue.remove({
hash: priorityQueueHash,
type: "timeout",
});
if (e instanceof WorkflowTimeoutError) {
throw e;
}
// Stream-specific fatal errors
if (
e instanceof InvalidStepReadableStreamError ||
e instanceof OversizedStreamChunkError ||
e instanceof UnsupportedStreamChunkError
) {
this.#engine.writeLog(
InstanceEvent.ATTEMPT_FAILURE,
cacheKey,
stepNameWithCounter,
{
attempt: stepState.attemptedCount,
error: new WorkflowFatalError(e.message),
}
);
this.#engine.writeLog(
InstanceEvent.STEP_FAILURE,
cacheKey,
stepNameWithCounter,
{}
);
this.#engine.writeLog(InstanceEvent.WORKFLOW_FAILURE, null, null, {
error: new WorkflowFatalError(
`The execution of the Workflow instance was terminated, as the step "${name}" returned an invalid ReadableStream output. ${e.message}`
),
});
await this.#engine.setStatus(
accountId,
instance.id,
InstanceStatus.Errored
);
await this.#engine.timeoutHandler.release(this.#engine);
await this.#engine.abort(ABORT_REASONS.NOT_SERIALISABLE);
return;
}
if (e instanceof StreamOutputStorageLimitError) {
this.#engine.writeLog(
InstanceEvent.ATTEMPT_FAILURE,
cacheKey,
stepNameWithCounter,
{
attempt: stepState.attemptedCount,
error: new WorkflowFatalError(e.message),
}
);
this.#engine.writeLog(
InstanceEvent.STEP_FAILURE,
cacheKey,
stepNameWithCounter,
{}
);
this.#engine.writeLog(InstanceEvent.WORKFLOW_FAILURE, null, null, {
error: new WorkflowFatalError(
"The instance has exceeded the 1GiB storage limit"
),
});
await this.#engine.setStatus(
accountId,
instance.id,
InstanceStatus.Errored
);
await this.#engine.timeoutHandler.release(this.#engine);
await this.#engine.abort(ABORT_REASONS.STORAGE_LIMIT_EXCEEDED);
return;
}
// something that cannot be written to storage
if (e instanceof Error && e.name === "DataCloneError") {
this.#engine.writeLog(
InstanceEvent.ATTEMPT_FAILURE,
cacheKey,
stepNameWithCounter,
{
attempt: stepState.attemptedCount,
error: new WorkflowFatalError(
`Value returned from step "${name}" is not serialisable`
),
}
);
this.#engine.writeLog(
InstanceEvent.STEP_FAILURE,
cacheKey,
stepNameWithCounter,
{}
);
this.#engine.writeLog(InstanceEvent.WORKFLOW_FAILURE, null, null, {
error: new WorkflowFatalError(
`The execution of the Workflow instance was terminated, as the step "${name}" returned a value which is not serialisable`
),
});
await this.#engine.setStatus(
accountId,
instance.id,
InstanceStatus.Errored
);
await this.#engine.timeoutHandler.release(this.#engine);
await this.#engine.abort(ABORT_REASONS.NOT_SERIALISABLE);
} else if (
e instanceof Error &&
e.message.includes("string or blob too big: SQLITE_TOOBIG")
) {
throw new WorkflowInternalError(
`Step ${stepNameWithCounter} output is too large. Maximum allowed size is 1MiB.`
);
} else {
// TODO (WOR-77): Send this to Sentry
throw new WorkflowInternalError(
`Storage failure for ${stepNameWithCounter} due to internal error.`
);
}
return;
}
// if we reach here, means that the closure ran successfully and we can remove the timeout from the PQ
// @ts-expect-error priorityQueue is initiated in init
this.#engine.priorityQueue.remove({
hash: priorityQueueHash,
type: "timeout",
});
this.#engine.writeLog(
InstanceEvent.ATTEMPT_SUCCESS,
cacheKey,
stepNameWithCounter,
{
attempt: stepState.attemptedCount,
}
);
} catch (e) {
const error = e as Error;
// if we reach here, means that the closure ran but errored out and we can remove the timeout from the PQ
// @ts-expect-error priorityQueue is initiated in init
this.#engine.priorityQueue.remove({
hash: `${cacheKey}-${stepState.attemptedCount}`,
type: "timeout",
});
// Clean up any partial stream output from this failed attempt
if (streamResultSeen) {
try {
await rollbackStreamOutput(
this.#state.storage,
cacheKey,
stepState.attemptedCount
);
} catch {
// Best-effort cleanup
}
}
if (
e instanceof Error &&
(error.name === "NonRetryableError" ||
error.message.startsWith("NonRetryableError"))
) {
this.#engine.writeLog(
InstanceEvent.ATTEMPT_FAILURE,
cacheKey,
stepNameWithCounter,
{
attempt: stepState.attemptedCount,
error: new WorkflowFatalError(
`Step threw a NonRetryableError with message "${e.message}"`
),
}
);
this.#engine.writeLog(
InstanceEvent.STEP_FAILURE,
cacheKey,
stepNameWithCounter,
{}
);
throw error;
}
this.#engine.writeLog(
InstanceEvent.ATTEMPT_FAILURE,
cacheKey,
stepNameWithCounter,
{
attempt: stepState.attemptedCount,
error: {
name: error.name,
message: error.message,
// TODO (WOR-79): Stacks are all incorrect over RPC and need work
// stack: error.stack,
},
}
);
await this.#state.storage.put(stepStateKey, stepState);
if (stepState.attemptedCount <= config.retries.limit) {
// TODO (WOR-71): Think through if every Error should transition
const durationMs = calcRetryDuration(config, stepState);
const disableAllRetryDelays = await this.#state.storage.get(
MODIFIER_KEYS.DISABLE_ALL_RETRY_DELAYS
);
const disableThisRetryDelay =
await this.#state.storage.get(retryDelayDisableKey);
const disableRetryDelay =
disableAllRetryDelays || disableThisRetryDelay;
const effectiveDuration = disableRetryDelay ? 0 : durationMs;
const priorityQueueHash = `${cacheKey}-${stepState.attemptedCount}`;
// @ts-expect-error priorityQueue is initiated in init
await this.#engine.priorityQueue.add({
hash: priorityQueueHash,
targetTimestamp: Date.now() + effectiveDuration,
type: "retry",
});
await this.#engine.timeoutHandler.release(this.#engine);
// Race retry wait against the pause signal so pause
// takes effect immediately during retries
{
const retryPauseSignal = this.#engine.pauseController.signal;
let pausedDuringRetry = false;
await Promise.race([
scheduler.wait(effectiveDuration),
new Promise<void>((resolve) => {
if (retryPauseSignal.aborted) {
resolve();
return;
}
retryPauseSignal.addEventListener("abort", () => resolve(), {
once: true,
});
}),
]);
const retryStatus = await this.#engine.getStatus();
if (
retryStatus === InstanceStatus.Paused ||
retryStatus === InstanceStatus.WaitingForPause
) {
pausedDuringRetry = true;
}
if (pausedDuringRetry) {
throw new Error(ABORT_REASONS.USER_PAUSE);
}
}
// if it ever reaches here, we can try to remove it from the priority queue since it's no longer useful
// @ts-expect-error priorityQueue is initiated in init
this.#engine.priorityQueue.remove({
hash: priorityQueueHash,
type: "retry",
});
return doWrapper(doWrapperClosure);
} else {
await this.#engine.timeoutHandler.release(this.#engine);
// Clean up any leftover stream chunks on retry exhaustion
try {
await rollbackStreamOutput(
this.#state.storage,
cacheKey,
stepState.attemptedCount
);
} catch {
// Best-effort cleanup
}
this.#engine.writeLog(
InstanceEvent.STEP_FAILURE,
cacheKey,
stepNameWithCounter,
{}
);
await this.#state.storage.put(errorKey, error);
throw error;
}
}
this.#engine.writeLog(
InstanceEvent.STEP_SUCCESS,
cacheKey,
stepNameWithCounter,
{
// TODO (WOR-86): Add limits, figure out serialization
result: lastStreamMeta ? undefined : result,
...(lastStreamMeta && {
streamOutput: { cacheKey, meta: lastStreamMeta },
}),
}
);
await this.#engine.timeoutHandler.release(this.#engine);
return result;
};
const result = await doWrapper(closure);
// Check if a pause was requested while this step was running
await this.#checkForPendingPause();
return result;
}
async sleep(name: string, duration: WorkflowSleepDuration): Promise<void> {
if (typeof duration == "string") {
duration = ms(duration);
}
const hash = await computeHash(name + duration.toString());
const count = this.#getCount("sleep-" + name + duration.toString());
const cacheKey = `${hash}-${count}`;
const sleepNameWithCounter = `${name}-${count}`;
const sleepKey = `${cacheKey}-value`;
const sleepLogWrittenKey = `${cacheKey}-log-written`;
const maybeResult = await this.#state.storage.get(sleepKey);
const sleepNameCountHash = await computeHash(
name + this.#getCount("sleep-" + name)
);
const disableThisSleep = await this.#state.storage.get(
`${MODIFIER_KEYS.DISABLE_SLEEP}${sleepNameCountHash}`
);
const disableAllSleeps = await this.#state.storage.get(
MODIFIER_KEYS.DISABLE_ALL_SLEEPS
);
const disableSleep = disableAllSleeps || disableThisSleep;
if (maybeResult != undefined) {
// @ts-expect-error priorityQueue is initiated in init
const entryPQ = this.#engine.priorityQueue.getFirst(
(a) => a.hash === cacheKey && a.type === "sleep"
);
// in case the engine dies while sleeping and wakes up before the retry period
if (entryPQ !== undefined) {
await scheduler.wait(
disableSleep ? 0 : entryPQ.targetTimestamp - Date.now()
);
// @ts-expect-error priorityQueue is initiated in init
this.#engine.priorityQueue.remove({ hash: cacheKey, type: "sleep" });
}
const shouldWriteLog =
(await this.#state.storage.get(sleepLogWrittenKey)) == undefined;
if (shouldWriteLog) {
this.#engine.writeLog(
InstanceEvent.SLEEP_COMPLETE,
cacheKey,
sleepNameWithCounter,
{}
);
await this.#state.storage.put(sleepLogWrittenKey, true);
}
return;
}
this.#engine.writeLog(
InstanceEvent.SLEEP_START,
cacheKey,
sleepNameWithCounter,
{
durationMs: duration,
}
);
const instanceMetadata =
await this.#state.storage.get<InstanceMetadata>(INSTANCE_METADATA);
if (!instanceMetadata) {
throw new Error("instanceMetadata is undefined");
}
// TODO(lduarte): not sure of this order of operations
await this.#state.storage.put(sleepKey, true); // Any value will do for cache hit
// @ts-expect-error priorityQueue is initiated in init
await this.#engine.priorityQueue.add({
hash: cacheKey,
targetTimestamp: Date.now() + (disableSleep ? 0 : duration),
type: "sleep",
});
// Race the sleep against the pause signal
const pauseSignal = this.#engine.pauseController.signal;
const sleepDuration = disableSleep ? 0 : duration;
let pausedDuringSleep = false;
await Promise.race([
scheduler.wait(sleepDuration),
new Promise<void>((resolve) => {
if (pauseSignal.aborted) {
resolve();
return;
}
pauseSignal.addEventListener("abort", () => resolve(), {
once: true,
});
}),
]);
// Check if we were paused during the sleep
const statusAfterSleep = await this.#engine.getStatus();
if (
statusAfterSleep === InstanceStatus.Paused ||
statusAfterSleep === InstanceStatus.WaitingForPause
) {
pausedDuringSleep = true;
}
if (pausedDuringSleep) {
// Throw pause error
throw new Error(ABORT_REASONS.USER_PAUSE);
}
this.#engine.writeLog(
InstanceEvent.SLEEP_COMPLETE,
cacheKey,
sleepNameWithCounter,
{}
);
await this.#state.storage.put(sleepLogWrittenKey, true);
// @ts-expect-error priorityQueue is initiated in init
this.#engine.priorityQueue.remove({ hash: cacheKey, type: "sleep" });
}
async sleepUntil(name: string, timestamp: Date | number): Promise<void> {
if (timestamp instanceof Date) {
timestamp = timestamp.valueOf();
}
const now = Date.now();
// timestamp needs to be in the future, throw if not
if (timestamp < now) {
throw new Error(
"You can't sleep until a time in the past, time-traveler"
);
}
return this.sleep(name, timestamp - now);
}
async waitForEvent<T>(
name: string,
options: {
type: string;
timeout?: string | number;
}
): Promise<WorkflowStepEvent<T>> {
if (!options.timeout) {
options.timeout = "24 hours";
}
const count = this.#getCount("waitForEvent-" + name);
const waitForEventNameWithCounter = `${name}-${count}`;
const hash = await computeHash(waitForEventNameWithCounter);
const cacheKey = `${hash}-${count}`;
const waitForEventKey = `${cacheKey}-value`;
const errorKey = `${cacheKey}-error`;
const pendingWaiterRegistered = `${cacheKey}-pending`;