Skip to content

Commit 16e6f7f

Browse files
committed
Add forceStepTimeout
1 parent 9ab0943 commit 16e6f7f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/workflows-shared/src/context.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,21 @@ export class Context extends RpcTarget {
240240
const { accountId, instance } = instanceMetadata;
241241

242242
try {
243+
const forceStepTimeoutKey = `force-step-timeout-${valueKey}`;
244+
const forceStepTimeout =
245+
(await this.#state.storage.get(forceStepTimeoutKey)) ||
246+
(await this.#state.storage.get(
247+
`${forceStepTimeoutKey}-${stepState.attemptedCount}`
248+
));
249+
243250
const timeoutPromise = async () => {
244251
const priorityQueueHash = `${cacheKey}-${stepState.attemptedCount}`;
245-
const timeout = ms(config.timeout);
252+
// inserir aqui
253+
let timeout = ms(config.timeout);
254+
if (forceStepTimeout) {
255+
timeout = 0;
256+
console.log("[Context] I will timeout");
257+
}
246258
// @ts-expect-error priorityQueue is initiated in init
247259
await this.#engine.priorityQueue.add({
248260
hash: priorityQueueHash,
@@ -304,6 +316,9 @@ export class Context extends RpcTarget {
304316
replaceResult
305317
);
306318
result = replaceResult;
319+
// if there is a timeout to be forced we dont want to race with closure
320+
} else if (forceStepTimeout) {
321+
result = await timeoutPromise();
307322
} else {
308323
result = await Promise.race([doWrapperClosure(), timeoutPromise()]);
309324
}
@@ -426,6 +441,8 @@ export class Context extends RpcTarget {
426441
// TODO (WOR-71): Think through if every Error should transition
427442
const durationMs = calcRetryDuration(config, stepState);
428443

444+
console.log("Vou esperar", durationMs);
445+
429446
const priorityQueueHash = `${cacheKey}-${stepState.attemptedCount}`;
430447
// @ts-expect-error priorityQueue is initiated in init
431448
await this.#engine.priorityQueue.add({

packages/workflows-shared/src/modifier.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ export class InstanceModifier extends RpcTarget {
119119
}
120120
}
121121

122+
public async forceStepTimeout(step: StepSelector, times?: number) {
123+
const valueKey = await this.#getStepCacheKey(step);
124+
if (times) {
125+
for (let time = 1; time <= times; time++) {
126+
const forceStepTimeoutKey = `force-step-timeout-${valueKey}-${time}`;
127+
await this.#state.storage.put(forceStepTimeoutKey, true);
128+
}
129+
} else {
130+
const forceStepTimeoutKey = `force-step-timeout-${valueKey}`;
131+
await this.#state.storage.put(forceStepTimeoutKey, true);
132+
}
133+
}
134+
122135
public async mockEvent(event: UserEvent): Promise<void> {
123136
// could maybe:
124137
// WorkflowInstance.sendEvent()

0 commit comments

Comments
 (0)