@sonamu-kit/tasks 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.swcrc +17 -0
- package/README.md +7 -0
- package/dist/backend.d.ts +107 -0
- package/dist/backend.d.ts.map +1 -0
- package/dist/backend.js +3 -0
- package/dist/backend.js.map +1 -0
- package/dist/chaos.test.d.ts +2 -0
- package/dist/chaos.test.d.ts.map +1 -0
- package/dist/chaos.test.js +92 -0
- package/dist/chaos.test.js.map +1 -0
- package/dist/client.d.ts +178 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +223 -0
- package/dist/client.js.map +1 -0
- package/dist/client.test.d.ts +2 -0
- package/dist/client.test.d.ts.map +1 -0
- package/dist/client.test.js +339 -0
- package/dist/client.test.js.map +1 -0
- package/dist/config.d.ts +22 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +23 -0
- package/dist/config.js.map +1 -0
- package/dist/config.test.d.ts +2 -0
- package/dist/config.test.d.ts.map +1 -0
- package/dist/config.test.js +24 -0
- package/dist/config.test.js.map +1 -0
- package/dist/core/duration.d.ts +22 -0
- package/dist/core/duration.d.ts.map +1 -0
- package/dist/core/duration.js +64 -0
- package/dist/core/duration.js.map +1 -0
- package/dist/core/duration.test.d.ts +2 -0
- package/dist/core/duration.test.d.ts.map +1 -0
- package/dist/core/duration.test.js +265 -0
- package/dist/core/duration.test.js.map +1 -0
- package/dist/core/error.d.ts +15 -0
- package/dist/core/error.d.ts.map +1 -0
- package/dist/core/error.js +25 -0
- package/dist/core/error.js.map +1 -0
- package/dist/core/error.test.d.ts +2 -0
- package/dist/core/error.test.d.ts.map +1 -0
- package/dist/core/error.test.js +63 -0
- package/dist/core/error.test.js.map +1 -0
- package/dist/core/json.d.ts +5 -0
- package/dist/core/json.d.ts.map +1 -0
- package/dist/core/json.js +3 -0
- package/dist/core/json.js.map +1 -0
- package/dist/core/result.d.ts +22 -0
- package/dist/core/result.d.ts.map +1 -0
- package/dist/core/result.js +22 -0
- package/dist/core/result.js.map +1 -0
- package/dist/core/result.test.d.ts +2 -0
- package/dist/core/result.test.d.ts.map +1 -0
- package/dist/core/result.test.js +19 -0
- package/dist/core/result.test.js.map +1 -0
- package/dist/core/retry.d.ts +21 -0
- package/dist/core/retry.d.ts.map +1 -0
- package/dist/core/retry.js +25 -0
- package/dist/core/retry.js.map +1 -0
- package/dist/core/retry.test.d.ts +2 -0
- package/dist/core/retry.test.d.ts.map +1 -0
- package/dist/core/retry.test.js +37 -0
- package/dist/core/retry.test.js.map +1 -0
- package/dist/core/schema.d.ts +57 -0
- package/dist/core/schema.d.ts.map +1 -0
- package/dist/core/schema.js +4 -0
- package/dist/core/schema.js.map +1 -0
- package/dist/core/step.d.ts +96 -0
- package/dist/core/step.d.ts.map +1 -0
- package/dist/core/step.js +78 -0
- package/dist/core/step.js.map +1 -0
- package/dist/core/step.test.d.ts +2 -0
- package/dist/core/step.test.d.ts.map +1 -0
- package/dist/core/step.test.js +356 -0
- package/dist/core/step.test.js.map +1 -0
- package/dist/core/workflow.d.ts +78 -0
- package/dist/core/workflow.d.ts.map +1 -0
- package/dist/core/workflow.js +46 -0
- package/dist/core/workflow.js.map +1 -0
- package/dist/core/workflow.test.d.ts +2 -0
- package/dist/core/workflow.test.d.ts.map +1 -0
- package/dist/core/workflow.test.js +172 -0
- package/dist/core/workflow.test.js.map +1 -0
- package/dist/database/backend.d.ts +60 -0
- package/dist/database/backend.d.ts.map +1 -0
- package/dist/database/backend.js +387 -0
- package/dist/database/backend.js.map +1 -0
- package/dist/database/backend.test.d.ts +2 -0
- package/dist/database/backend.test.d.ts.map +1 -0
- package/dist/database/backend.test.js +17 -0
- package/dist/database/backend.test.js.map +1 -0
- package/dist/database/backend.testsuite.d.ts +20 -0
- package/dist/database/backend.testsuite.d.ts.map +1 -0
- package/dist/database/backend.testsuite.js +1174 -0
- package/dist/database/backend.testsuite.js.map +1 -0
- package/dist/database/base.d.ts +12 -0
- package/dist/database/base.d.ts.map +1 -0
- package/dist/database/base.js +19 -0
- package/dist/database/base.js.map +1 -0
- package/dist/database/migrations/20251212000000_0_init.js +9 -0
- package/dist/database/migrations/20251212000000_0_init.js.map +1 -0
- package/dist/database/migrations/20251212000000_1_tables.js +88 -0
- package/dist/database/migrations/20251212000000_1_tables.js.map +1 -0
- package/dist/database/migrations/20251212000000_2_fk.js +48 -0
- package/dist/database/migrations/20251212000000_2_fk.js.map +1 -0
- package/dist/database/migrations/20251212000000_3_indexes.js +107 -0
- package/dist/database/migrations/20251212000000_3_indexes.js.map +1 -0
- package/dist/database/pubsub.d.ts +17 -0
- package/dist/database/pubsub.d.ts.map +1 -0
- package/dist/database/pubsub.js +70 -0
- package/dist/database/pubsub.js.map +1 -0
- package/dist/database/pubsub.test.d.ts +2 -0
- package/dist/database/pubsub.test.d.ts.map +1 -0
- package/dist/database/pubsub.test.js +86 -0
- package/dist/database/pubsub.test.js.map +1 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +21 -0
- package/dist/errors.js.map +1 -0
- package/dist/execution.d.ts +82 -0
- package/dist/execution.d.ts.map +1 -0
- package/dist/execution.js +182 -0
- package/dist/execution.js.map +1 -0
- package/dist/execution.test.d.ts +2 -0
- package/dist/execution.test.d.ts.map +1 -0
- package/dist/execution.test.js +556 -0
- package/dist/execution.test.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +12 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +5 -0
- package/dist/internal.js.map +1 -0
- package/dist/practices/01-remote-workflow.d.ts +2 -0
- package/dist/practices/01-remote-workflow.d.ts.map +1 -0
- package/dist/practices/01-remote-workflow.js +69 -0
- package/dist/practices/01-remote-workflow.js.map +1 -0
- package/dist/practices/01-remote.d.ts +2 -0
- package/dist/practices/01-remote.d.ts.map +1 -0
- package/dist/practices/01-remote.js +87 -0
- package/dist/practices/01-remote.js.map +1 -0
- package/dist/practices/02-local.d.ts +2 -0
- package/dist/practices/02-local.d.ts.map +1 -0
- package/dist/practices/02-local.js +84 -0
- package/dist/practices/02-local.js.map +1 -0
- package/dist/practices/03-local-retry.d.ts +2 -0
- package/dist/practices/03-local-retry.d.ts.map +1 -0
- package/dist/practices/03-local-retry.js +85 -0
- package/dist/practices/03-local-retry.js.map +1 -0
- package/dist/practices/04-scheduler-dispose.d.ts +2 -0
- package/dist/practices/04-scheduler-dispose.d.ts.map +1 -0
- package/dist/practices/04-scheduler-dispose.js +65 -0
- package/dist/practices/04-scheduler-dispose.js.map +1 -0
- package/dist/practices/05-router.d.ts +2 -0
- package/dist/practices/05-router.d.ts.map +1 -0
- package/dist/practices/05-router.js +80 -0
- package/dist/practices/05-router.js.map +1 -0
- package/dist/registry.d.ts +33 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +54 -0
- package/dist/registry.js.map +1 -0
- package/dist/registry.test.d.ts +2 -0
- package/dist/registry.test.d.ts.map +1 -0
- package/dist/registry.test.js +95 -0
- package/dist/registry.test.js.map +1 -0
- package/dist/scheduler.d.ts +22 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +117 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/tasks/index.d.ts +4 -0
- package/dist/tasks/index.d.ts.map +1 -0
- package/dist/tasks/index.js +5 -0
- package/dist/tasks/index.js.map +1 -0
- package/dist/tasks/local-task.d.ts +6 -0
- package/dist/tasks/local-task.d.ts.map +1 -0
- package/dist/tasks/local-task.js +95 -0
- package/dist/tasks/local-task.js.map +1 -0
- package/dist/tasks/remote-task.d.ts +11 -0
- package/dist/tasks/remote-task.d.ts.map +1 -0
- package/dist/tasks/remote-task.js +213 -0
- package/dist/tasks/remote-task.js.map +1 -0
- package/dist/tasks/shared.d.ts +8 -0
- package/dist/tasks/shared.d.ts.map +1 -0
- package/dist/tasks/shared.js +41 -0
- package/dist/tasks/shared.js.map +1 -0
- package/dist/testing/connection.d.ts +7 -0
- package/dist/testing/connection.d.ts.map +1 -0
- package/dist/testing/connection.js +38 -0
- package/dist/testing/connection.js.map +1 -0
- package/dist/types/config.d.ts +44 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +3 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/context.d.ts +18 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/context.js +4 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/events.d.ts +43 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +3 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/task-items.d.ts +12 -0
- package/dist/types/task-items.d.ts.map +1 -0
- package/dist/types/task-items.js +3 -0
- package/dist/types/task-items.js.map +1 -0
- package/dist/types/utils.d.ts +4 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/utils.js +8 -0
- package/dist/types/utils.js.map +1 -0
- package/dist/worker.d.ts +61 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +206 -0
- package/dist/worker.js.map +1 -0
- package/dist/worker.test.d.ts +2 -0
- package/dist/worker.test.d.ts.map +1 -0
- package/dist/worker.test.js +1163 -0
- package/dist/worker.test.js.map +1 -0
- package/dist/workflow.d.ts +44 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +21 -0
- package/dist/workflow.js.map +1 -0
- package/dist/workflow.test.d.ts +2 -0
- package/dist/workflow.test.d.ts.map +1 -0
- package/dist/workflow.test.js +73 -0
- package/dist/workflow.test.js.map +1 -0
- package/nodemon.json +6 -0
- package/package.json +63 -0
- package/scripts/migrate.ts +11 -0
- package/src/backend.ts +133 -0
- package/src/chaos.test.ts +108 -0
- package/src/client.test.ts +297 -0
- package/src/client.ts +331 -0
- package/src/config.test.ts +23 -0
- package/src/config.ts +35 -0
- package/src/core/duration.test.ts +326 -0
- package/src/core/duration.ts +86 -0
- package/src/core/error.test.ts +77 -0
- package/src/core/error.ts +30 -0
- package/src/core/json.ts +2 -0
- package/src/core/result.test.ts +13 -0
- package/src/core/result.ts +29 -0
- package/src/core/retry.test.ts +41 -0
- package/src/core/retry.ts +29 -0
- package/src/core/schema.ts +74 -0
- package/src/core/step.test.ts +362 -0
- package/src/core/step.ts +152 -0
- package/src/core/workflow.test.ts +184 -0
- package/src/core/workflow.ts +127 -0
- package/src/database/backend.test.ts +16 -0
- package/src/database/backend.testsuite.ts +1376 -0
- package/src/database/backend.ts +655 -0
- package/src/database/base.ts +23 -0
- package/src/database/migrations/20251212000000_0_init.ts +10 -0
- package/src/database/migrations/20251212000000_1_tables.ts +54 -0
- package/src/database/migrations/20251212000000_2_fk.ts +46 -0
- package/src/database/migrations/20251212000000_3_indexes.ts +82 -0
- package/src/database/pubsub.test.ts +92 -0
- package/src/database/pubsub.ts +92 -0
- package/src/execution.test.ts +508 -0
- package/src/execution.ts +291 -0
- package/src/index.ts +7 -0
- package/src/internal.ts +11 -0
- package/src/practices/01-remote-workflow.ts +61 -0
- package/src/registry.test.ts +122 -0
- package/src/registry.ts +65 -0
- package/src/testing/connection.ts +44 -0
- package/src/worker.test.ts +1138 -0
- package/src/worker.ts +281 -0
- package/src/workflow.test.ts +68 -0
- package/src/workflow.ts +84 -0
- package/table_ddl.sql +60 -0
- package/templates/openworkflow.config.ts +22 -0
- package/tsconfig.json +40 -0
- package/tsconfig.test.json +4 -0
- package/vite.config.ts +13 -0
|
@@ -0,0 +1,1376 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { afterAll, beforeAll, describe, expect, test } from "vitest";
|
|
3
|
+
import type { Backend } from "..//backend";
|
|
4
|
+
import type { StepAttempt } from "../core/step";
|
|
5
|
+
import type { WorkflowRun } from "../core/workflow";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for the Backend test suite.
|
|
9
|
+
*/
|
|
10
|
+
export interface TestBackendOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new isolated Backend instance.
|
|
13
|
+
*/
|
|
14
|
+
setup: () => Promise<Backend>;
|
|
15
|
+
/**
|
|
16
|
+
* Cleans up a Backend instance.
|
|
17
|
+
*/
|
|
18
|
+
teardown: (backend: Backend) => Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Runs the Backend test suite.
|
|
23
|
+
* @param options - Test suite options
|
|
24
|
+
*/
|
|
25
|
+
export function testBackend(options: TestBackendOptions): void {
|
|
26
|
+
const { setup, teardown } = options;
|
|
27
|
+
describe("Backend", () => {
|
|
28
|
+
let backend: Backend;
|
|
29
|
+
|
|
30
|
+
beforeAll(async () => {
|
|
31
|
+
backend = await setup();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
afterAll(async () => {
|
|
35
|
+
await teardown(backend);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("createWorkflowRun()", () => {
|
|
39
|
+
test("creates a workflow run", async () => {
|
|
40
|
+
const expected: WorkflowRun = {
|
|
41
|
+
namespaceId: "", // -
|
|
42
|
+
id: "", // -
|
|
43
|
+
workflowName: randomUUID(),
|
|
44
|
+
version: randomUUID(),
|
|
45
|
+
status: "pending",
|
|
46
|
+
idempotencyKey: randomUUID(),
|
|
47
|
+
config: { key: "val" },
|
|
48
|
+
context: { key: "val" },
|
|
49
|
+
input: { key: "val" },
|
|
50
|
+
output: null,
|
|
51
|
+
error: null,
|
|
52
|
+
attempts: 0,
|
|
53
|
+
parentStepAttemptNamespaceId: null,
|
|
54
|
+
parentStepAttemptId: null,
|
|
55
|
+
workerId: null,
|
|
56
|
+
availableAt: newDateInOneYear(), // -
|
|
57
|
+
deadlineAt: newDateInOneYear(),
|
|
58
|
+
startedAt: null,
|
|
59
|
+
finishedAt: null,
|
|
60
|
+
createdAt: new Date(), // -
|
|
61
|
+
updatedAt: new Date(), // -
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Create with all fields
|
|
65
|
+
const created = await backend.createWorkflowRun({
|
|
66
|
+
workflowName: expected.workflowName,
|
|
67
|
+
version: expected.version,
|
|
68
|
+
idempotencyKey: expected.idempotencyKey,
|
|
69
|
+
input: expected.input,
|
|
70
|
+
config: expected.config,
|
|
71
|
+
context: expected.context,
|
|
72
|
+
availableAt: expected.availableAt,
|
|
73
|
+
deadlineAt: expected.deadlineAt,
|
|
74
|
+
});
|
|
75
|
+
expect(created.namespaceId).toHaveLength(36);
|
|
76
|
+
expect(created.id).toHaveLength(36);
|
|
77
|
+
expect(deltaSeconds(created.availableAt)).toBeGreaterThan(1);
|
|
78
|
+
expect(deltaSeconds(created.createdAt)).toBeLessThan(1);
|
|
79
|
+
expect(deltaSeconds(created.updatedAt)).toBeLessThan(1);
|
|
80
|
+
|
|
81
|
+
expected.namespaceId = created.namespaceId;
|
|
82
|
+
expected.id = created.id;
|
|
83
|
+
expected.availableAt = created.availableAt;
|
|
84
|
+
expected.createdAt = created.createdAt;
|
|
85
|
+
expected.updatedAt = created.updatedAt;
|
|
86
|
+
expect(created).toEqual(expected);
|
|
87
|
+
|
|
88
|
+
// Create with minimal fields
|
|
89
|
+
const createdMin = await backend.createWorkflowRun({
|
|
90
|
+
workflowName: expected.workflowName,
|
|
91
|
+
version: null,
|
|
92
|
+
idempotencyKey: null,
|
|
93
|
+
input: null,
|
|
94
|
+
config: {},
|
|
95
|
+
context: null,
|
|
96
|
+
availableAt: null,
|
|
97
|
+
deadlineAt: null,
|
|
98
|
+
});
|
|
99
|
+
expect(createdMin.version).toBeNull();
|
|
100
|
+
expect(createdMin.idempotencyKey).toBeNull();
|
|
101
|
+
expect(createdMin.input).toBeNull();
|
|
102
|
+
expect(createdMin.context).toBeNull();
|
|
103
|
+
expect(deltaSeconds(createdMin.availableAt)).toBeLessThan(1); // defaults to NOW()
|
|
104
|
+
expect(createdMin.deadlineAt).toBeNull();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("listWorkflowRuns()", () => {
|
|
109
|
+
test("lists workflow runs ordered by creation time", async () => {
|
|
110
|
+
const backend = await setup();
|
|
111
|
+
const first = await createPendingWorkflowRun(backend);
|
|
112
|
+
await sleep(10); // ensure timestamp difference
|
|
113
|
+
const second = await createPendingWorkflowRun(backend);
|
|
114
|
+
|
|
115
|
+
const listed = await backend.listWorkflowRuns({});
|
|
116
|
+
const listedIds = listed.data.map((run) => run.id);
|
|
117
|
+
expect(listedIds).toEqual([first.id, second.id]);
|
|
118
|
+
await teardown(backend);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("paginates workflow runs", async () => {
|
|
122
|
+
const backend = await setup();
|
|
123
|
+
const runs: WorkflowRun[] = [];
|
|
124
|
+
for (let i = 0; i < 5; i++) {
|
|
125
|
+
runs.push(await createPendingWorkflowRun(backend));
|
|
126
|
+
await sleep(10);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// p1
|
|
130
|
+
const page1 = await backend.listWorkflowRuns({ limit: 2 });
|
|
131
|
+
expect(page1.data).toHaveLength(2);
|
|
132
|
+
expect(page1.data[0]?.id).toBe(runs[0]?.id);
|
|
133
|
+
expect(page1.data[1]?.id).toBe(runs[1]?.id);
|
|
134
|
+
expect(page1.pagination.next).not.toBeNull();
|
|
135
|
+
expect(page1.pagination.prev).toBeNull();
|
|
136
|
+
|
|
137
|
+
// p2
|
|
138
|
+
const page2 = await backend.listWorkflowRuns({
|
|
139
|
+
limit: 2,
|
|
140
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
141
|
+
after: page1.pagination.next!,
|
|
142
|
+
});
|
|
143
|
+
expect(page2.data).toHaveLength(2);
|
|
144
|
+
expect(page2.data[0]?.id).toBe(runs[2]?.id);
|
|
145
|
+
expect(page2.data[1]?.id).toBe(runs[3]?.id);
|
|
146
|
+
expect(page2.pagination.next).not.toBeNull();
|
|
147
|
+
expect(page2.pagination.prev).not.toBeNull();
|
|
148
|
+
|
|
149
|
+
// p3
|
|
150
|
+
const page3 = await backend.listWorkflowRuns({
|
|
151
|
+
limit: 2,
|
|
152
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
153
|
+
after: page2.pagination.next!,
|
|
154
|
+
});
|
|
155
|
+
expect(page3.data).toHaveLength(1);
|
|
156
|
+
expect(page3.data[0]?.id).toBe(runs[4]?.id);
|
|
157
|
+
expect(page3.pagination.next).toBeNull();
|
|
158
|
+
expect(page3.pagination.prev).not.toBeNull();
|
|
159
|
+
|
|
160
|
+
// p2 again
|
|
161
|
+
const page2Back = await backend.listWorkflowRuns({
|
|
162
|
+
limit: 2,
|
|
163
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
164
|
+
before: page3.pagination.prev!,
|
|
165
|
+
});
|
|
166
|
+
expect(page2Back.data).toHaveLength(2);
|
|
167
|
+
expect(page2Back.data[0]?.id).toBe(runs[2]?.id);
|
|
168
|
+
expect(page2Back.data[1]?.id).toBe(runs[3]?.id);
|
|
169
|
+
expect(page2Back.pagination.next).toEqual(page2.pagination.next);
|
|
170
|
+
expect(page2Back.pagination.prev).toEqual(page2.pagination.prev);
|
|
171
|
+
await teardown(backend);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("handles empty results", async () => {
|
|
175
|
+
const backend = await setup();
|
|
176
|
+
const listed = await backend.listWorkflowRuns({});
|
|
177
|
+
expect(listed.data).toHaveLength(0);
|
|
178
|
+
expect(listed.pagination.next).toBeNull();
|
|
179
|
+
expect(listed.pagination.prev).toBeNull();
|
|
180
|
+
await teardown(backend);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("paginates correctly with id as tiebreaker when multiple items have the same created_at timestamp", async () => {
|
|
184
|
+
const backend = await setup();
|
|
185
|
+
|
|
186
|
+
const runs: WorkflowRun[] = [];
|
|
187
|
+
for (let i = 0; i < 5; i++) {
|
|
188
|
+
runs.push(await createPendingWorkflowRun(backend));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
runs.sort((a, b) => {
|
|
192
|
+
const timeDiff = a.createdAt.getTime() - b.createdAt.getTime();
|
|
193
|
+
if (timeDiff !== 0) return timeDiff;
|
|
194
|
+
return a.id.localeCompare(b.id);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const page1 = await backend.listWorkflowRuns({ limit: 2 });
|
|
198
|
+
expect(page1.data).toHaveLength(2);
|
|
199
|
+
expect(page1.data[0]?.id).toBe(runs[0]?.id);
|
|
200
|
+
expect(page1.data[1]?.id).toBe(runs[1]?.id);
|
|
201
|
+
expect(page1.pagination.next).not.toBeNull();
|
|
202
|
+
|
|
203
|
+
const page2 = await backend.listWorkflowRuns({
|
|
204
|
+
limit: 2,
|
|
205
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
206
|
+
after: page1.pagination.next!,
|
|
207
|
+
});
|
|
208
|
+
expect(page2.data).toHaveLength(2);
|
|
209
|
+
expect(page2.data[0]?.id).toBe(runs[2]?.id);
|
|
210
|
+
expect(page2.data[1]?.id).toBe(runs[3]?.id);
|
|
211
|
+
expect(page2.pagination.next).not.toBeNull();
|
|
212
|
+
|
|
213
|
+
const page3 = await backend.listWorkflowRuns({
|
|
214
|
+
limit: 2,
|
|
215
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
216
|
+
after: page2.pagination.next!,
|
|
217
|
+
});
|
|
218
|
+
expect(page3.data).toHaveLength(1);
|
|
219
|
+
expect(page3.data[0]?.id).toBe(runs[4]?.id);
|
|
220
|
+
expect(page3.pagination.next).toBeNull();
|
|
221
|
+
|
|
222
|
+
const page2Back = await backend.listWorkflowRuns({
|
|
223
|
+
limit: 2,
|
|
224
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
225
|
+
before: page3.pagination.prev!,
|
|
226
|
+
});
|
|
227
|
+
expect(page2Back.data).toHaveLength(2);
|
|
228
|
+
expect(page2Back.data[0]?.id).toBe(runs[2]?.id);
|
|
229
|
+
expect(page2Back.data[1]?.id).toBe(runs[3]?.id);
|
|
230
|
+
|
|
231
|
+
await teardown(backend);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
describe("claimWorkflowRun()", () => {
|
|
236
|
+
// because claims involve timing and leases, we create and teardown a new
|
|
237
|
+
// namespaced backend instance for each test
|
|
238
|
+
|
|
239
|
+
test("claims workflow runs and respects leases, reclaiming if lease expires", async () => {
|
|
240
|
+
const backend = await setup();
|
|
241
|
+
|
|
242
|
+
await createPendingWorkflowRun(backend);
|
|
243
|
+
|
|
244
|
+
const firstLeaseMs = 30;
|
|
245
|
+
const firstWorker = randomUUID();
|
|
246
|
+
const claimed = await backend.claimWorkflowRun({
|
|
247
|
+
workerId: firstWorker,
|
|
248
|
+
leaseDurationMs: firstLeaseMs,
|
|
249
|
+
});
|
|
250
|
+
expect(claimed?.status).toBe("running");
|
|
251
|
+
expect(claimed?.workerId).toBe(firstWorker);
|
|
252
|
+
expect(claimed?.attempts).toBe(1);
|
|
253
|
+
expect(claimed?.startedAt).not.toBeNull();
|
|
254
|
+
|
|
255
|
+
const secondWorker = randomUUID();
|
|
256
|
+
const blocked = await backend.claimWorkflowRun({
|
|
257
|
+
workerId: secondWorker,
|
|
258
|
+
leaseDurationMs: 10,
|
|
259
|
+
});
|
|
260
|
+
expect(blocked).toBeNull();
|
|
261
|
+
|
|
262
|
+
await sleep(firstLeaseMs + 5); // small buffer for timing variability
|
|
263
|
+
|
|
264
|
+
const reclaimed = await backend.claimWorkflowRun({
|
|
265
|
+
workerId: secondWorker,
|
|
266
|
+
leaseDurationMs: 10,
|
|
267
|
+
});
|
|
268
|
+
expect(reclaimed?.id).toBe(claimed?.id);
|
|
269
|
+
expect(reclaimed?.attempts).toBe(2);
|
|
270
|
+
expect(reclaimed?.workerId).toBe(secondWorker);
|
|
271
|
+
expect(reclaimed?.startedAt?.getTime()).toBe(claimed?.startedAt?.getTime());
|
|
272
|
+
|
|
273
|
+
await teardown(backend);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("prioritizes pending workflow runs over expired running ones", async () => {
|
|
277
|
+
const backend = await setup();
|
|
278
|
+
|
|
279
|
+
const running = await createPendingWorkflowRun(backend);
|
|
280
|
+
const runningClaim = await backend.claimWorkflowRun({
|
|
281
|
+
workerId: "worker-running",
|
|
282
|
+
leaseDurationMs: 5,
|
|
283
|
+
});
|
|
284
|
+
if (!runningClaim) throw new Error("expected claim");
|
|
285
|
+
expect(runningClaim.id).toBe(running.id);
|
|
286
|
+
|
|
287
|
+
await sleep(10); // wait for running's lease to expire
|
|
288
|
+
|
|
289
|
+
// pending claimed first, even though running expired
|
|
290
|
+
const pending = await createPendingWorkflowRun(backend);
|
|
291
|
+
const claimedFirst = await backend.claimWorkflowRun({
|
|
292
|
+
workerId: "worker-second",
|
|
293
|
+
leaseDurationMs: 100,
|
|
294
|
+
});
|
|
295
|
+
expect(claimedFirst?.id).toBe(pending.id);
|
|
296
|
+
|
|
297
|
+
// running claimed second
|
|
298
|
+
const claimedSecond = await backend.claimWorkflowRun({
|
|
299
|
+
workerId: "worker-third",
|
|
300
|
+
leaseDurationMs: 100,
|
|
301
|
+
});
|
|
302
|
+
expect(claimedSecond?.id).toBe(running.id);
|
|
303
|
+
|
|
304
|
+
await teardown(backend);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test("returns null when no workflow runs are available", async () => {
|
|
308
|
+
const backend = await setup();
|
|
309
|
+
|
|
310
|
+
const claimed = await backend.claimWorkflowRun({
|
|
311
|
+
workerId: randomUUID(),
|
|
312
|
+
leaseDurationMs: 10,
|
|
313
|
+
});
|
|
314
|
+
expect(claimed).toBeNull();
|
|
315
|
+
|
|
316
|
+
await teardown(backend);
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
describe("extendWorkflowRunLease()", () => {
|
|
321
|
+
test("extends the lease for running workflow runs", async () => {
|
|
322
|
+
const workerId = randomUUID();
|
|
323
|
+
await createPendingWorkflowRun(backend);
|
|
324
|
+
|
|
325
|
+
const claimed = await backend.claimWorkflowRun({
|
|
326
|
+
workerId,
|
|
327
|
+
leaseDurationMs: 20,
|
|
328
|
+
});
|
|
329
|
+
if (!claimed) throw new Error("Expected workflow run to be claimed"); // for type narrowing
|
|
330
|
+
|
|
331
|
+
const previousExpiry = claimed.availableAt;
|
|
332
|
+
const extended = await backend.extendWorkflowRunLease({
|
|
333
|
+
workflowRunId: claimed.id,
|
|
334
|
+
workerId,
|
|
335
|
+
leaseDurationMs: 200,
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
expect(extended.availableAt?.getTime()).toBeGreaterThan(
|
|
339
|
+
previousExpiry?.getTime() ?? Infinity,
|
|
340
|
+
);
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
describe("sleepWorkflowRun()", () => {
|
|
345
|
+
test("sets a running workflow to sleeping status until a future time", async () => {
|
|
346
|
+
const workerId = randomUUID();
|
|
347
|
+
await createPendingWorkflowRun(backend);
|
|
348
|
+
|
|
349
|
+
const claimed = await backend.claimWorkflowRun({
|
|
350
|
+
workerId,
|
|
351
|
+
leaseDurationMs: 100,
|
|
352
|
+
});
|
|
353
|
+
if (!claimed) throw new Error("Expected workflow run to be claimed");
|
|
354
|
+
|
|
355
|
+
const sleepUntil = new Date(Date.now() + 5000); // 5 seconds from now
|
|
356
|
+
|
|
357
|
+
await backend.sleepWorkflowRun({
|
|
358
|
+
workflowRunId: claimed.id,
|
|
359
|
+
workerId,
|
|
360
|
+
availableAt: sleepUntil,
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
const fetched = await backend.getWorkflowRun({
|
|
364
|
+
workflowRunId: claimed.id,
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
expect(fetched).not.toBeNull();
|
|
368
|
+
expect(fetched?.availableAt?.getTime()).toBe(sleepUntil.getTime());
|
|
369
|
+
expect(fetched?.workerId).toBeNull();
|
|
370
|
+
expect(fetched?.status).toBe("sleeping");
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test("fails when trying to sleep a canceled workflow", async () => {
|
|
374
|
+
const backend = await setup();
|
|
375
|
+
|
|
376
|
+
// completed run
|
|
377
|
+
let claimed = await createClaimedWorkflowRun(backend);
|
|
378
|
+
await backend.completeWorkflowRun({
|
|
379
|
+
workflowRunId: claimed.id,
|
|
380
|
+
workerId: claimed.workerId ?? "",
|
|
381
|
+
output: null,
|
|
382
|
+
});
|
|
383
|
+
await expect(
|
|
384
|
+
backend.sleepWorkflowRun({
|
|
385
|
+
workflowRunId: claimed.id,
|
|
386
|
+
workerId: claimed.workerId ?? "",
|
|
387
|
+
availableAt: new Date(Date.now() + 60_000),
|
|
388
|
+
}),
|
|
389
|
+
).rejects.toThrow("Failed to sleep workflow run");
|
|
390
|
+
|
|
391
|
+
// failed run
|
|
392
|
+
claimed = await createClaimedWorkflowRun(backend);
|
|
393
|
+
await backend.failWorkflowRun({
|
|
394
|
+
workflowRunId: claimed.id,
|
|
395
|
+
workerId: claimed.workerId ?? "",
|
|
396
|
+
error: { message: "failed" },
|
|
397
|
+
});
|
|
398
|
+
await expect(
|
|
399
|
+
backend.sleepWorkflowRun({
|
|
400
|
+
workflowRunId: claimed.id,
|
|
401
|
+
workerId: claimed.workerId ?? "",
|
|
402
|
+
availableAt: new Date(Date.now() + 60_000),
|
|
403
|
+
}),
|
|
404
|
+
).rejects.toThrow("Failed to sleep workflow run");
|
|
405
|
+
|
|
406
|
+
// canceled run
|
|
407
|
+
claimed = await createClaimedWorkflowRun(backend);
|
|
408
|
+
await backend.cancelWorkflowRun({
|
|
409
|
+
workflowRunId: claimed.id,
|
|
410
|
+
});
|
|
411
|
+
await expect(
|
|
412
|
+
backend.sleepWorkflowRun({
|
|
413
|
+
workflowRunId: claimed.id,
|
|
414
|
+
workerId: claimed.workerId ?? "",
|
|
415
|
+
availableAt: new Date(Date.now() + 60_000),
|
|
416
|
+
}),
|
|
417
|
+
).rejects.toThrow("Failed to sleep workflow run");
|
|
418
|
+
|
|
419
|
+
await teardown(backend);
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
describe("completeWorkflowRun()", () => {
|
|
424
|
+
test("marks running workflow runs as completed", async () => {
|
|
425
|
+
const workerId = randomUUID();
|
|
426
|
+
await createPendingWorkflowRun(backend);
|
|
427
|
+
|
|
428
|
+
const claimed = await backend.claimWorkflowRun({
|
|
429
|
+
workerId,
|
|
430
|
+
leaseDurationMs: 20,
|
|
431
|
+
});
|
|
432
|
+
if (!claimed) throw new Error("Expected workflow run to be claimed"); // for type narrowing
|
|
433
|
+
|
|
434
|
+
const output = { ok: true };
|
|
435
|
+
const completed = await backend.completeWorkflowRun({
|
|
436
|
+
workflowRunId: claimed.id,
|
|
437
|
+
workerId,
|
|
438
|
+
output,
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
expect(completed.status).toBe("completed");
|
|
442
|
+
expect(completed.output).toEqual(output);
|
|
443
|
+
expect(completed.error).toBeNull();
|
|
444
|
+
expect(completed.finishedAt).not.toBeNull();
|
|
445
|
+
expect(completed.availableAt).toBeNull();
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
describe("failWorkflowRun()", () => {
|
|
450
|
+
test("reschedules workflow runs with exponential backoff on first failure", async () => {
|
|
451
|
+
const workerId = randomUUID();
|
|
452
|
+
await createPendingWorkflowRun(backend);
|
|
453
|
+
|
|
454
|
+
const claimed = await backend.claimWorkflowRun({
|
|
455
|
+
workerId,
|
|
456
|
+
leaseDurationMs: 20,
|
|
457
|
+
});
|
|
458
|
+
if (!claimed) throw new Error("Expected workflow run to be claimed");
|
|
459
|
+
|
|
460
|
+
const beforeFailTime = Date.now();
|
|
461
|
+
|
|
462
|
+
const error = { message: "boom" };
|
|
463
|
+
const failed = await backend.failWorkflowRun({
|
|
464
|
+
workflowRunId: claimed.id,
|
|
465
|
+
workerId,
|
|
466
|
+
error,
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
// rescheduled, not permanently failed
|
|
470
|
+
expect(failed.status).toBe("pending");
|
|
471
|
+
expect(failed.error).toEqual(error);
|
|
472
|
+
expect(failed.output).toBeNull();
|
|
473
|
+
expect(failed.finishedAt).toBeNull();
|
|
474
|
+
expect(failed.workerId).toBeNull();
|
|
475
|
+
expect(failed.startedAt).toBeNull(); // cleared on failure for retry
|
|
476
|
+
|
|
477
|
+
expect(failed.availableAt).not.toBeNull();
|
|
478
|
+
if (!failed.availableAt) throw new Error("Expected availableAt");
|
|
479
|
+
const delayMs = failed.availableAt.getTime() - beforeFailTime;
|
|
480
|
+
expect(delayMs).toBeGreaterThanOrEqual(900); // ~1s with some tolerance
|
|
481
|
+
expect(delayMs).toBeLessThan(1500);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
test("reschedules with increasing backoff on multiple failures (known slow test)", async () => {
|
|
485
|
+
// this test needs isolated namespace
|
|
486
|
+
const backend = await setup();
|
|
487
|
+
|
|
488
|
+
await createPendingWorkflowRun(backend);
|
|
489
|
+
|
|
490
|
+
// fail first attempt
|
|
491
|
+
let workerId = randomUUID();
|
|
492
|
+
let claimed = await backend.claimWorkflowRun({
|
|
493
|
+
workerId,
|
|
494
|
+
leaseDurationMs: 20,
|
|
495
|
+
});
|
|
496
|
+
if (!claimed) throw new Error("Expected workflow run to be claimed");
|
|
497
|
+
expect(claimed.attempts).toBe(1);
|
|
498
|
+
|
|
499
|
+
const firstFailed = await backend.failWorkflowRun({
|
|
500
|
+
workflowRunId: claimed.id,
|
|
501
|
+
workerId,
|
|
502
|
+
error: { message: "first failure" },
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
expect(firstFailed.status).toBe("pending");
|
|
506
|
+
|
|
507
|
+
await sleep(1100); // wait for first backoff (~1s)
|
|
508
|
+
|
|
509
|
+
// fail second attempt
|
|
510
|
+
workerId = randomUUID();
|
|
511
|
+
claimed = await backend.claimWorkflowRun({
|
|
512
|
+
workerId,
|
|
513
|
+
leaseDurationMs: 20,
|
|
514
|
+
});
|
|
515
|
+
if (!claimed) throw new Error("Expected workflow run to be claimed");
|
|
516
|
+
expect(claimed.attempts).toBe(2);
|
|
517
|
+
|
|
518
|
+
const beforeSecondFail = Date.now();
|
|
519
|
+
const secondFailed = await backend.failWorkflowRun({
|
|
520
|
+
workflowRunId: claimed.id,
|
|
521
|
+
workerId,
|
|
522
|
+
error: { message: "second failure" },
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
expect(secondFailed.status).toBe("pending");
|
|
526
|
+
|
|
527
|
+
// second attempt should have ~2s backoff (1s * 2^1)
|
|
528
|
+
if (!secondFailed.availableAt) throw new Error("Expected availableAt");
|
|
529
|
+
const delayMs = secondFailed.availableAt.getTime() - beforeSecondFail;
|
|
530
|
+
expect(delayMs).toBeGreaterThanOrEqual(1900); // ~2s with some tolerance
|
|
531
|
+
expect(delayMs).toBeLessThan(2500);
|
|
532
|
+
|
|
533
|
+
await teardown(backend);
|
|
534
|
+
});
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
describe("createStepAttempt()", () => {
|
|
538
|
+
test("creates a step attempt", async () => {
|
|
539
|
+
const workflowRun = await createClaimedWorkflowRun(backend);
|
|
540
|
+
|
|
541
|
+
const expected: StepAttempt = {
|
|
542
|
+
namespaceId: workflowRun.namespaceId,
|
|
543
|
+
id: "", // -
|
|
544
|
+
workflowRunId: workflowRun.id,
|
|
545
|
+
stepName: randomUUID(),
|
|
546
|
+
kind: "function",
|
|
547
|
+
status: "running",
|
|
548
|
+
config: { key: "val" },
|
|
549
|
+
context: null,
|
|
550
|
+
output: null,
|
|
551
|
+
error: null,
|
|
552
|
+
childWorkflowRunNamespaceId: null,
|
|
553
|
+
childWorkflowRunId: null,
|
|
554
|
+
startedAt: null,
|
|
555
|
+
finishedAt: null,
|
|
556
|
+
createdAt: new Date(), // -
|
|
557
|
+
updatedAt: new Date(), // -
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
const created = await backend.createStepAttempt({
|
|
561
|
+
workflowRunId: expected.workflowRunId,
|
|
562
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
563
|
+
workerId: workflowRun.workerId!,
|
|
564
|
+
stepName: expected.stepName,
|
|
565
|
+
kind: expected.kind,
|
|
566
|
+
config: expected.config,
|
|
567
|
+
context: expected.context,
|
|
568
|
+
});
|
|
569
|
+
expect(created.id).toHaveLength(36);
|
|
570
|
+
expect(deltaSeconds(created.startedAt)).toBeLessThan(1);
|
|
571
|
+
expect(deltaSeconds(created.createdAt)).toBeLessThan(1);
|
|
572
|
+
expect(deltaSeconds(created.updatedAt)).toBeLessThan(1);
|
|
573
|
+
|
|
574
|
+
expected.id = created.id;
|
|
575
|
+
expected.startedAt = created.startedAt;
|
|
576
|
+
expected.createdAt = created.createdAt;
|
|
577
|
+
expected.updatedAt = created.updatedAt;
|
|
578
|
+
expect(created).toEqual(expected);
|
|
579
|
+
});
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
describe("getStepAttempt()", () => {
|
|
583
|
+
test("returns a persisted step attempt", async () => {
|
|
584
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
585
|
+
|
|
586
|
+
const created = await backend.createStepAttempt({
|
|
587
|
+
workflowRunId: claimed.id,
|
|
588
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
589
|
+
workerId: claimed.workerId!,
|
|
590
|
+
stepName: randomUUID(),
|
|
591
|
+
kind: "function",
|
|
592
|
+
config: {},
|
|
593
|
+
context: null,
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
const got = await backend.getStepAttempt({
|
|
597
|
+
stepAttemptId: created.id,
|
|
598
|
+
});
|
|
599
|
+
expect(got).toEqual(created);
|
|
600
|
+
});
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
describe("listStepAttempts()", () => {
|
|
604
|
+
beforeAll(async () => {
|
|
605
|
+
backend = await setup();
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
afterAll(async () => {
|
|
609
|
+
await teardown(backend);
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
test("lists step attempts ordered by creation time", async () => {
|
|
613
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
614
|
+
|
|
615
|
+
const first = await backend.createStepAttempt({
|
|
616
|
+
workflowRunId: claimed.id,
|
|
617
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
618
|
+
workerId: claimed.workerId!,
|
|
619
|
+
stepName: randomUUID(),
|
|
620
|
+
kind: "function",
|
|
621
|
+
config: {},
|
|
622
|
+
context: null,
|
|
623
|
+
});
|
|
624
|
+
await backend.completeStepAttempt({
|
|
625
|
+
workflowRunId: claimed.id,
|
|
626
|
+
stepAttemptId: first.id,
|
|
627
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
628
|
+
workerId: claimed.workerId!,
|
|
629
|
+
output: { ok: true },
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
await sleep(10); // ensure timestamp difference
|
|
633
|
+
|
|
634
|
+
const second = await backend.createStepAttempt({
|
|
635
|
+
workflowRunId: claimed.id,
|
|
636
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
637
|
+
workerId: claimed.workerId!,
|
|
638
|
+
stepName: randomUUID(),
|
|
639
|
+
kind: "function",
|
|
640
|
+
config: {},
|
|
641
|
+
context: null,
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
const listed = await backend.listStepAttempts({
|
|
645
|
+
workflowRunId: claimed.id,
|
|
646
|
+
});
|
|
647
|
+
const listedStepNames = listed.data.map((step) => step.stepName);
|
|
648
|
+
expect(listedStepNames).toEqual([first.stepName, second.stepName]);
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
test("paginates step attempts", async () => {
|
|
652
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
653
|
+
|
|
654
|
+
for (let i = 0; i < 5; i++) {
|
|
655
|
+
await backend.createStepAttempt({
|
|
656
|
+
workflowRunId: claimed.id,
|
|
657
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
658
|
+
workerId: claimed.workerId!,
|
|
659
|
+
stepName: `step-${String(i)}`,
|
|
660
|
+
kind: "function",
|
|
661
|
+
config: {},
|
|
662
|
+
context: null,
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
await sleep(10); // ensure createdAt differs
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// p1
|
|
669
|
+
const page1 = await backend.listStepAttempts({
|
|
670
|
+
workflowRunId: claimed.id,
|
|
671
|
+
limit: 2,
|
|
672
|
+
});
|
|
673
|
+
expect(page1.data).toHaveLength(2);
|
|
674
|
+
expect(page1.data[0]?.stepName).toBe("step-0");
|
|
675
|
+
expect(page1.data[1]?.stepName).toBe("step-1");
|
|
676
|
+
expect(page1.pagination.next).not.toBeNull();
|
|
677
|
+
expect(page1.pagination.prev).toBeNull();
|
|
678
|
+
|
|
679
|
+
// p2
|
|
680
|
+
const page2 = await backend.listStepAttempts({
|
|
681
|
+
workflowRunId: claimed.id,
|
|
682
|
+
limit: 2,
|
|
683
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
684
|
+
after: page1.pagination.next!,
|
|
685
|
+
});
|
|
686
|
+
expect(page2.data).toHaveLength(2);
|
|
687
|
+
expect(page2.data[0]?.stepName).toBe("step-2");
|
|
688
|
+
expect(page2.data[1]?.stepName).toBe("step-3");
|
|
689
|
+
expect(page2.pagination.next).not.toBeNull();
|
|
690
|
+
expect(page2.pagination.prev).not.toBeNull();
|
|
691
|
+
|
|
692
|
+
// p3
|
|
693
|
+
const page3 = await backend.listStepAttempts({
|
|
694
|
+
workflowRunId: claimed.id,
|
|
695
|
+
limit: 2,
|
|
696
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
697
|
+
after: page2.pagination.next!,
|
|
698
|
+
});
|
|
699
|
+
expect(page3.data).toHaveLength(1);
|
|
700
|
+
expect(page3.data[0]?.stepName).toBe("step-4");
|
|
701
|
+
expect(page3.pagination.next).toBeNull();
|
|
702
|
+
expect(page3.pagination.prev).not.toBeNull();
|
|
703
|
+
|
|
704
|
+
// p2 again
|
|
705
|
+
const page2Back = await backend.listStepAttempts({
|
|
706
|
+
workflowRunId: claimed.id,
|
|
707
|
+
limit: 2,
|
|
708
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
709
|
+
before: page3.pagination.prev!,
|
|
710
|
+
});
|
|
711
|
+
expect(page2Back.data).toHaveLength(2);
|
|
712
|
+
expect(page2Back.data[0]?.stepName).toBe("step-2");
|
|
713
|
+
expect(page2Back.data[1]?.stepName).toBe("step-3");
|
|
714
|
+
expect(page2Back.pagination.next).toEqual(page2.pagination.next);
|
|
715
|
+
expect(page2Back.pagination.prev).toEqual(page2.pagination.prev);
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
test("handles empty results", async () => {
|
|
719
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
720
|
+
const listed = await backend.listStepAttempts({
|
|
721
|
+
workflowRunId: claimed.id,
|
|
722
|
+
});
|
|
723
|
+
expect(listed.data).toHaveLength(0);
|
|
724
|
+
expect(listed.pagination.next).toBeNull();
|
|
725
|
+
expect(listed.pagination.prev).toBeNull();
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
test("handles exact limit match", async () => {
|
|
729
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
730
|
+
await backend.createStepAttempt({
|
|
731
|
+
workflowRunId: claimed.id,
|
|
732
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
733
|
+
workerId: claimed.workerId!,
|
|
734
|
+
stepName: "step-1",
|
|
735
|
+
kind: "function",
|
|
736
|
+
config: {},
|
|
737
|
+
context: null,
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
const listed = await backend.listStepAttempts({
|
|
741
|
+
workflowRunId: claimed.id,
|
|
742
|
+
limit: 1,
|
|
743
|
+
});
|
|
744
|
+
expect(listed.data).toHaveLength(1);
|
|
745
|
+
expect(listed.pagination.next).toBeNull();
|
|
746
|
+
expect(listed.pagination.prev).toBeNull();
|
|
747
|
+
});
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
describe("completeStepAttempt()", () => {
|
|
751
|
+
beforeAll(async () => {
|
|
752
|
+
backend = await setup();
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
afterAll(async () => {
|
|
756
|
+
await teardown(backend);
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
test("marks running step attempts as completed", async () => {
|
|
760
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
761
|
+
|
|
762
|
+
const created = await backend.createStepAttempt({
|
|
763
|
+
workflowRunId: claimed.id,
|
|
764
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
765
|
+
workerId: claimed.workerId!,
|
|
766
|
+
stepName: randomUUID(),
|
|
767
|
+
kind: "function",
|
|
768
|
+
config: {},
|
|
769
|
+
context: null,
|
|
770
|
+
});
|
|
771
|
+
const output = { foo: "bar" };
|
|
772
|
+
|
|
773
|
+
const completed = await backend.completeStepAttempt({
|
|
774
|
+
workflowRunId: claimed.id,
|
|
775
|
+
stepAttemptId: created.id,
|
|
776
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
777
|
+
workerId: claimed.workerId!,
|
|
778
|
+
output,
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
expect(completed.status).toBe("completed");
|
|
782
|
+
expect(completed.output).toEqual(output);
|
|
783
|
+
expect(completed.error).toBeNull();
|
|
784
|
+
expect(completed.finishedAt).not.toBeNull();
|
|
785
|
+
|
|
786
|
+
const fetched = await backend.getStepAttempt({
|
|
787
|
+
stepAttemptId: created.id,
|
|
788
|
+
});
|
|
789
|
+
expect(fetched?.status).toBe("completed");
|
|
790
|
+
expect(fetched?.output).toEqual(output);
|
|
791
|
+
expect(fetched?.error).toBeNull();
|
|
792
|
+
expect(fetched?.finishedAt).not.toBeNull();
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
test("throws when workflow is not running", async () => {
|
|
796
|
+
const backend = await setup();
|
|
797
|
+
await createPendingWorkflowRun(backend);
|
|
798
|
+
|
|
799
|
+
// create a step attempt by first claiming the workflow
|
|
800
|
+
const claimed = await backend.claimWorkflowRun({
|
|
801
|
+
workerId: randomUUID(),
|
|
802
|
+
leaseDurationMs: 100,
|
|
803
|
+
});
|
|
804
|
+
if (!claimed) throw new Error("Failed to claim workflow run");
|
|
805
|
+
|
|
806
|
+
const stepAttempt = await backend.createStepAttempt({
|
|
807
|
+
workflowRunId: claimed.id,
|
|
808
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
809
|
+
workerId: claimed.workerId!,
|
|
810
|
+
stepName: randomUUID(),
|
|
811
|
+
kind: "function",
|
|
812
|
+
config: {},
|
|
813
|
+
context: null,
|
|
814
|
+
});
|
|
815
|
+
|
|
816
|
+
// complete the workflow so it's no longer running
|
|
817
|
+
await backend.completeWorkflowRun({
|
|
818
|
+
workflowRunId: claimed.id,
|
|
819
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
820
|
+
workerId: claimed.workerId!,
|
|
821
|
+
output: null,
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
// try to complete the step attempt
|
|
825
|
+
await expect(
|
|
826
|
+
backend.completeStepAttempt({
|
|
827
|
+
workflowRunId: claimed.id,
|
|
828
|
+
stepAttemptId: stepAttempt.id,
|
|
829
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
830
|
+
workerId: claimed.workerId!,
|
|
831
|
+
output: { foo: "bar" },
|
|
832
|
+
}),
|
|
833
|
+
).rejects.toThrow("Failed to mark step attempt completed");
|
|
834
|
+
|
|
835
|
+
await teardown(backend);
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
test("throws when step attempt does not exist", async () => {
|
|
839
|
+
const backend = await setup();
|
|
840
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
841
|
+
|
|
842
|
+
await expect(
|
|
843
|
+
backend.completeStepAttempt({
|
|
844
|
+
workflowRunId: claimed.id,
|
|
845
|
+
stepAttemptId: randomUUID(),
|
|
846
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
847
|
+
workerId: claimed.workerId!,
|
|
848
|
+
output: { foo: "bar" },
|
|
849
|
+
}),
|
|
850
|
+
).rejects.toThrow("Failed to mark step attempt completed");
|
|
851
|
+
|
|
852
|
+
await teardown(backend);
|
|
853
|
+
});
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
describe("failStepAttempt()", () => {
|
|
857
|
+
beforeAll(async () => {
|
|
858
|
+
backend = await setup();
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
afterAll(async () => {
|
|
862
|
+
await teardown(backend);
|
|
863
|
+
});
|
|
864
|
+
|
|
865
|
+
test("marks running step attempts as failed", async () => {
|
|
866
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
867
|
+
|
|
868
|
+
const created = await backend.createStepAttempt({
|
|
869
|
+
workflowRunId: claimed.id,
|
|
870
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
871
|
+
workerId: claimed.workerId!,
|
|
872
|
+
stepName: randomUUID(),
|
|
873
|
+
kind: "function",
|
|
874
|
+
config: {},
|
|
875
|
+
context: null,
|
|
876
|
+
});
|
|
877
|
+
const error = { message: "nope" };
|
|
878
|
+
|
|
879
|
+
const failed = await backend.failStepAttempt({
|
|
880
|
+
workflowRunId: claimed.id,
|
|
881
|
+
stepAttemptId: created.id,
|
|
882
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
883
|
+
workerId: claimed.workerId!,
|
|
884
|
+
error,
|
|
885
|
+
});
|
|
886
|
+
|
|
887
|
+
expect(failed.status).toBe("failed");
|
|
888
|
+
expect(failed.error).toEqual(error);
|
|
889
|
+
expect(failed.output).toBeNull();
|
|
890
|
+
expect(failed.finishedAt).not.toBeNull();
|
|
891
|
+
|
|
892
|
+
const fetched = await backend.getStepAttempt({
|
|
893
|
+
stepAttemptId: created.id,
|
|
894
|
+
});
|
|
895
|
+
expect(fetched?.status).toBe("failed");
|
|
896
|
+
expect(fetched?.error).toEqual(error);
|
|
897
|
+
expect(fetched?.output).toBeNull();
|
|
898
|
+
expect(fetched?.finishedAt).not.toBeNull();
|
|
899
|
+
});
|
|
900
|
+
|
|
901
|
+
test("throws when workflow is not running", async () => {
|
|
902
|
+
const backend = await setup();
|
|
903
|
+
await createPendingWorkflowRun(backend);
|
|
904
|
+
|
|
905
|
+
// create a step attempt by first claiming the workflow
|
|
906
|
+
const claimed = await backend.claimWorkflowRun({
|
|
907
|
+
workerId: randomUUID(),
|
|
908
|
+
leaseDurationMs: 100,
|
|
909
|
+
});
|
|
910
|
+
if (!claimed) throw new Error("Failed to claim workflow run");
|
|
911
|
+
|
|
912
|
+
const stepAttempt = await backend.createStepAttempt({
|
|
913
|
+
workflowRunId: claimed.id,
|
|
914
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
915
|
+
workerId: claimed.workerId!,
|
|
916
|
+
stepName: randomUUID(),
|
|
917
|
+
kind: "function",
|
|
918
|
+
config: {},
|
|
919
|
+
context: null,
|
|
920
|
+
});
|
|
921
|
+
|
|
922
|
+
// complete the workflow so it's no longer running
|
|
923
|
+
await backend.completeWorkflowRun({
|
|
924
|
+
workflowRunId: claimed.id,
|
|
925
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
926
|
+
workerId: claimed.workerId!,
|
|
927
|
+
output: null,
|
|
928
|
+
});
|
|
929
|
+
|
|
930
|
+
// try to fail the step attempt
|
|
931
|
+
await expect(
|
|
932
|
+
backend.failStepAttempt({
|
|
933
|
+
workflowRunId: claimed.id,
|
|
934
|
+
stepAttemptId: stepAttempt.id,
|
|
935
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
936
|
+
workerId: claimed.workerId!,
|
|
937
|
+
error: { message: "nope" },
|
|
938
|
+
}),
|
|
939
|
+
).rejects.toThrow("Failed to mark step attempt failed");
|
|
940
|
+
|
|
941
|
+
await teardown(backend);
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
test("throws when step attempt does not exist", async () => {
|
|
945
|
+
const backend = await setup();
|
|
946
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
947
|
+
|
|
948
|
+
await expect(
|
|
949
|
+
backend.failStepAttempt({
|
|
950
|
+
workflowRunId: claimed.id,
|
|
951
|
+
stepAttemptId: randomUUID(),
|
|
952
|
+
// biome-ignore lint/style/noNonNullAssertion: for test
|
|
953
|
+
workerId: claimed.workerId!,
|
|
954
|
+
error: { message: "nope" },
|
|
955
|
+
}),
|
|
956
|
+
).rejects.toThrow("Failed to mark step attempt failed");
|
|
957
|
+
|
|
958
|
+
await teardown(backend);
|
|
959
|
+
});
|
|
960
|
+
});
|
|
961
|
+
|
|
962
|
+
describe("deadline_at", () => {
|
|
963
|
+
beforeAll(async () => {
|
|
964
|
+
backend = await setup();
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
afterAll(async () => {
|
|
968
|
+
await teardown(backend);
|
|
969
|
+
});
|
|
970
|
+
|
|
971
|
+
test("creates a workflow run with a deadline", async () => {
|
|
972
|
+
const deadline = new Date(Date.now() + 60_000); // in 1 minute
|
|
973
|
+
const created = await backend.createWorkflowRun({
|
|
974
|
+
workflowName: randomUUID(),
|
|
975
|
+
version: null,
|
|
976
|
+
idempotencyKey: null,
|
|
977
|
+
input: null,
|
|
978
|
+
config: {},
|
|
979
|
+
context: null,
|
|
980
|
+
availableAt: null,
|
|
981
|
+
deadlineAt: deadline,
|
|
982
|
+
});
|
|
983
|
+
|
|
984
|
+
expect(created.deadlineAt).not.toBeNull();
|
|
985
|
+
expect(created.deadlineAt?.getTime()).toBe(deadline.getTime());
|
|
986
|
+
});
|
|
987
|
+
|
|
988
|
+
test("does not claim workflow runs past their deadline", async () => {
|
|
989
|
+
const backend = await setup();
|
|
990
|
+
|
|
991
|
+
const pastDeadline = new Date(Date.now() - 1000);
|
|
992
|
+
await backend.createWorkflowRun({
|
|
993
|
+
workflowName: randomUUID(),
|
|
994
|
+
version: null,
|
|
995
|
+
idempotencyKey: null,
|
|
996
|
+
input: null,
|
|
997
|
+
config: {},
|
|
998
|
+
context: null,
|
|
999
|
+
availableAt: null,
|
|
1000
|
+
deadlineAt: pastDeadline,
|
|
1001
|
+
});
|
|
1002
|
+
|
|
1003
|
+
const claimed = await backend.claimWorkflowRun({
|
|
1004
|
+
workerId: randomUUID(),
|
|
1005
|
+
leaseDurationMs: 1000,
|
|
1006
|
+
});
|
|
1007
|
+
|
|
1008
|
+
expect(claimed).toBeNull();
|
|
1009
|
+
|
|
1010
|
+
await teardown(backend);
|
|
1011
|
+
});
|
|
1012
|
+
|
|
1013
|
+
test("marks deadline-expired workflow runs as failed when claiming", async () => {
|
|
1014
|
+
const backend = await setup();
|
|
1015
|
+
|
|
1016
|
+
const pastDeadline = new Date(Date.now() - 1000);
|
|
1017
|
+
const created = await backend.createWorkflowRun({
|
|
1018
|
+
workflowName: randomUUID(),
|
|
1019
|
+
version: null,
|
|
1020
|
+
idempotencyKey: null,
|
|
1021
|
+
input: null,
|
|
1022
|
+
config: {},
|
|
1023
|
+
context: null,
|
|
1024
|
+
availableAt: null,
|
|
1025
|
+
deadlineAt: pastDeadline,
|
|
1026
|
+
});
|
|
1027
|
+
|
|
1028
|
+
// attempt to claim triggers deadline check
|
|
1029
|
+
const claimed = await backend.claimWorkflowRun({
|
|
1030
|
+
workerId: randomUUID(),
|
|
1031
|
+
leaseDurationMs: 1000,
|
|
1032
|
+
});
|
|
1033
|
+
expect(claimed).toBeNull();
|
|
1034
|
+
|
|
1035
|
+
// verify it was marked as failed
|
|
1036
|
+
const failed = await backend.getWorkflowRun({
|
|
1037
|
+
workflowRunId: created.id,
|
|
1038
|
+
});
|
|
1039
|
+
expect(failed?.status).toBe("failed");
|
|
1040
|
+
expect(failed?.error).toEqual({
|
|
1041
|
+
message: "Workflow run deadline exceeded",
|
|
1042
|
+
});
|
|
1043
|
+
expect(failed?.finishedAt).not.toBeNull();
|
|
1044
|
+
expect(failed?.availableAt).toBeNull();
|
|
1045
|
+
|
|
1046
|
+
await teardown(backend);
|
|
1047
|
+
});
|
|
1048
|
+
|
|
1049
|
+
test("does not reschedule failed workflow runs if next retry would exceed deadline", async () => {
|
|
1050
|
+
const backend = await setup();
|
|
1051
|
+
|
|
1052
|
+
const deadline = new Date(Date.now() + 500); // 500ms from now
|
|
1053
|
+
const created = await backend.createWorkflowRun({
|
|
1054
|
+
workflowName: randomUUID(),
|
|
1055
|
+
version: null,
|
|
1056
|
+
idempotencyKey: null,
|
|
1057
|
+
input: null,
|
|
1058
|
+
config: {},
|
|
1059
|
+
context: null,
|
|
1060
|
+
availableAt: null,
|
|
1061
|
+
deadlineAt: deadline,
|
|
1062
|
+
});
|
|
1063
|
+
|
|
1064
|
+
const workerId = randomUUID();
|
|
1065
|
+
const claimed = await backend.claimWorkflowRun({
|
|
1066
|
+
workerId,
|
|
1067
|
+
leaseDurationMs: 100,
|
|
1068
|
+
});
|
|
1069
|
+
expect(claimed).not.toBeNull();
|
|
1070
|
+
|
|
1071
|
+
// should mark as permanently failed since retry backoff (1s) would exceed deadline (500ms)
|
|
1072
|
+
const failed = await backend.failWorkflowRun({
|
|
1073
|
+
workflowRunId: created.id,
|
|
1074
|
+
workerId,
|
|
1075
|
+
error: { message: "test error" },
|
|
1076
|
+
});
|
|
1077
|
+
|
|
1078
|
+
expect(failed.status).toBe("failed");
|
|
1079
|
+
expect(failed.availableAt).toBeNull();
|
|
1080
|
+
expect(failed.finishedAt).not.toBeNull();
|
|
1081
|
+
expect(failed.startedAt).toBeNull(); // cleared on permanent failure
|
|
1082
|
+
|
|
1083
|
+
await teardown(backend);
|
|
1084
|
+
});
|
|
1085
|
+
|
|
1086
|
+
test("reschedules failed workflow runs if retry would complete before deadline", async () => {
|
|
1087
|
+
const backend = await setup();
|
|
1088
|
+
|
|
1089
|
+
const deadline = new Date(Date.now() + 5000); // in 5 seconds
|
|
1090
|
+
const created = await backend.createWorkflowRun({
|
|
1091
|
+
workflowName: randomUUID(),
|
|
1092
|
+
version: null,
|
|
1093
|
+
idempotencyKey: null,
|
|
1094
|
+
input: null,
|
|
1095
|
+
config: {},
|
|
1096
|
+
context: null,
|
|
1097
|
+
availableAt: null,
|
|
1098
|
+
deadlineAt: deadline,
|
|
1099
|
+
});
|
|
1100
|
+
|
|
1101
|
+
const workerId = randomUUID();
|
|
1102
|
+
const claimed = await backend.claimWorkflowRun({
|
|
1103
|
+
workerId,
|
|
1104
|
+
leaseDurationMs: 100,
|
|
1105
|
+
});
|
|
1106
|
+
expect(claimed).not.toBeNull();
|
|
1107
|
+
|
|
1108
|
+
// should reschedule since retry backoff (1s) is before deadline (5s
|
|
1109
|
+
const failed = await backend.failWorkflowRun({
|
|
1110
|
+
workflowRunId: created.id,
|
|
1111
|
+
workerId,
|
|
1112
|
+
error: { message: "test error" },
|
|
1113
|
+
});
|
|
1114
|
+
|
|
1115
|
+
expect(failed.status).toBe("pending");
|
|
1116
|
+
expect(failed.availableAt).not.toBeNull();
|
|
1117
|
+
expect(failed.finishedAt).toBeNull();
|
|
1118
|
+
|
|
1119
|
+
await teardown(backend);
|
|
1120
|
+
});
|
|
1121
|
+
});
|
|
1122
|
+
|
|
1123
|
+
describe("cancelWorkflowRun()", () => {
|
|
1124
|
+
test("cancels a pending workflow run", async () => {
|
|
1125
|
+
const backend = await setup();
|
|
1126
|
+
|
|
1127
|
+
const created = await createPendingWorkflowRun(backend);
|
|
1128
|
+
expect(created.status).toBe("pending");
|
|
1129
|
+
|
|
1130
|
+
const canceled = await backend.cancelWorkflowRun({
|
|
1131
|
+
workflowRunId: created.id,
|
|
1132
|
+
});
|
|
1133
|
+
|
|
1134
|
+
expect(canceled.status).toBe("canceled");
|
|
1135
|
+
expect(canceled.workerId).toBeNull();
|
|
1136
|
+
expect(canceled.availableAt).toBeNull();
|
|
1137
|
+
expect(canceled.finishedAt).not.toBeNull();
|
|
1138
|
+
expect(deltaSeconds(canceled.finishedAt)).toBeLessThan(1);
|
|
1139
|
+
|
|
1140
|
+
await teardown(backend);
|
|
1141
|
+
});
|
|
1142
|
+
|
|
1143
|
+
test("cancels a running workflow run", async () => {
|
|
1144
|
+
const backend = await setup();
|
|
1145
|
+
|
|
1146
|
+
const created = await createClaimedWorkflowRun(backend);
|
|
1147
|
+
expect(created.status).toBe("running");
|
|
1148
|
+
expect(created.workerId).not.toBeNull();
|
|
1149
|
+
|
|
1150
|
+
const canceled = await backend.cancelWorkflowRun({
|
|
1151
|
+
workflowRunId: created.id,
|
|
1152
|
+
});
|
|
1153
|
+
|
|
1154
|
+
expect(canceled.status).toBe("canceled");
|
|
1155
|
+
expect(canceled.workerId).toBeNull();
|
|
1156
|
+
expect(canceled.availableAt).toBeNull();
|
|
1157
|
+
expect(canceled.finishedAt).not.toBeNull();
|
|
1158
|
+
|
|
1159
|
+
await teardown(backend);
|
|
1160
|
+
});
|
|
1161
|
+
|
|
1162
|
+
test("cancels a sleeping workflow run", async () => {
|
|
1163
|
+
const backend = await setup();
|
|
1164
|
+
|
|
1165
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
1166
|
+
|
|
1167
|
+
// put workflow to sleep
|
|
1168
|
+
const sleepUntil = new Date(Date.now() + 60_000); // 1 minute from now
|
|
1169
|
+
const sleeping = await backend.sleepWorkflowRun({
|
|
1170
|
+
workflowRunId: claimed.id,
|
|
1171
|
+
workerId: claimed.workerId ?? "",
|
|
1172
|
+
availableAt: sleepUntil,
|
|
1173
|
+
});
|
|
1174
|
+
expect(sleeping.status).toBe("sleeping");
|
|
1175
|
+
|
|
1176
|
+
const canceled = await backend.cancelWorkflowRun({
|
|
1177
|
+
workflowRunId: sleeping.id,
|
|
1178
|
+
});
|
|
1179
|
+
|
|
1180
|
+
expect(canceled.status).toBe("canceled");
|
|
1181
|
+
expect(canceled.workerId).toBeNull();
|
|
1182
|
+
expect(canceled.availableAt).toBeNull();
|
|
1183
|
+
expect(canceled.finishedAt).not.toBeNull();
|
|
1184
|
+
|
|
1185
|
+
await teardown(backend);
|
|
1186
|
+
});
|
|
1187
|
+
|
|
1188
|
+
test("throws error when canceling a completed workflow run", async () => {
|
|
1189
|
+
const backend = await setup();
|
|
1190
|
+
|
|
1191
|
+
const claimed = await createClaimedWorkflowRun(backend);
|
|
1192
|
+
|
|
1193
|
+
// mark as completed
|
|
1194
|
+
await backend.completeWorkflowRun({
|
|
1195
|
+
workflowRunId: claimed.id,
|
|
1196
|
+
workerId: claimed.workerId ?? "",
|
|
1197
|
+
output: { result: "success" },
|
|
1198
|
+
});
|
|
1199
|
+
|
|
1200
|
+
await expect(
|
|
1201
|
+
backend.cancelWorkflowRun({
|
|
1202
|
+
workflowRunId: claimed.id,
|
|
1203
|
+
}),
|
|
1204
|
+
).rejects.toThrow(/Cannot cancel workflow run .* with status completed/);
|
|
1205
|
+
|
|
1206
|
+
await teardown(backend);
|
|
1207
|
+
});
|
|
1208
|
+
|
|
1209
|
+
test("throws error when canceling a failed workflow run", async () => {
|
|
1210
|
+
const backend = await setup();
|
|
1211
|
+
|
|
1212
|
+
// create with deadline that's already passed to make it fail
|
|
1213
|
+
const workflowWithDeadline = await backend.createWorkflowRun({
|
|
1214
|
+
workflowName: randomUUID(),
|
|
1215
|
+
version: null,
|
|
1216
|
+
idempotencyKey: null,
|
|
1217
|
+
input: null,
|
|
1218
|
+
config: {},
|
|
1219
|
+
context: null,
|
|
1220
|
+
availableAt: null,
|
|
1221
|
+
deadlineAt: new Date(Date.now() - 1000), // deadline in the past
|
|
1222
|
+
});
|
|
1223
|
+
|
|
1224
|
+
// try to claim it, which should mark it as failed due to deadline
|
|
1225
|
+
const claimed = await backend.claimWorkflowRun({
|
|
1226
|
+
workerId: randomUUID(),
|
|
1227
|
+
leaseDurationMs: 100,
|
|
1228
|
+
});
|
|
1229
|
+
|
|
1230
|
+
// if claim succeeds, manually fail it
|
|
1231
|
+
if (claimed?.workerId) {
|
|
1232
|
+
await backend.failWorkflowRun({
|
|
1233
|
+
workflowRunId: claimed.id,
|
|
1234
|
+
workerId: claimed.workerId,
|
|
1235
|
+
error: { message: "test error" },
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
// get a workflow that's definitely failed
|
|
1240
|
+
const failedRun = await backend.getWorkflowRun({
|
|
1241
|
+
workflowRunId: workflowWithDeadline.id,
|
|
1242
|
+
});
|
|
1243
|
+
|
|
1244
|
+
if (failedRun?.status === "failed") {
|
|
1245
|
+
await expect(
|
|
1246
|
+
backend.cancelWorkflowRun({
|
|
1247
|
+
workflowRunId: failedRun.id,
|
|
1248
|
+
}),
|
|
1249
|
+
).rejects.toThrow(/Cannot cancel workflow run .* with status failed/);
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
await teardown(backend);
|
|
1253
|
+
});
|
|
1254
|
+
|
|
1255
|
+
test("is idempotent when canceling an already canceled workflow run", async () => {
|
|
1256
|
+
const backend = await setup();
|
|
1257
|
+
|
|
1258
|
+
const created = await createPendingWorkflowRun(backend);
|
|
1259
|
+
|
|
1260
|
+
const firstCancel = await backend.cancelWorkflowRun({
|
|
1261
|
+
workflowRunId: created.id,
|
|
1262
|
+
});
|
|
1263
|
+
expect(firstCancel.status).toBe("canceled");
|
|
1264
|
+
|
|
1265
|
+
const secondCancel = await backend.cancelWorkflowRun({
|
|
1266
|
+
workflowRunId: created.id,
|
|
1267
|
+
});
|
|
1268
|
+
expect(secondCancel.status).toBe("canceled");
|
|
1269
|
+
expect(secondCancel.id).toBe(firstCancel.id);
|
|
1270
|
+
|
|
1271
|
+
await teardown(backend);
|
|
1272
|
+
});
|
|
1273
|
+
|
|
1274
|
+
test("throws error when canceling a non-existent workflow run", async () => {
|
|
1275
|
+
const backend = await setup();
|
|
1276
|
+
|
|
1277
|
+
const nonExistentId = randomUUID();
|
|
1278
|
+
|
|
1279
|
+
await expect(
|
|
1280
|
+
backend.cancelWorkflowRun({
|
|
1281
|
+
workflowRunId: nonExistentId,
|
|
1282
|
+
}),
|
|
1283
|
+
).rejects.toThrow(`Workflow run ${nonExistentId} does not exist`);
|
|
1284
|
+
|
|
1285
|
+
await teardown(backend);
|
|
1286
|
+
});
|
|
1287
|
+
|
|
1288
|
+
test("canceled workflow is not claimed by workers", async () => {
|
|
1289
|
+
const backend = await setup();
|
|
1290
|
+
|
|
1291
|
+
const created = await createPendingWorkflowRun(backend);
|
|
1292
|
+
|
|
1293
|
+
// cancel the workflow
|
|
1294
|
+
await backend.cancelWorkflowRun({
|
|
1295
|
+
workflowRunId: created.id,
|
|
1296
|
+
});
|
|
1297
|
+
|
|
1298
|
+
// try to claim work
|
|
1299
|
+
const claimed = await backend.claimWorkflowRun({
|
|
1300
|
+
workerId: randomUUID(),
|
|
1301
|
+
leaseDurationMs: 100,
|
|
1302
|
+
});
|
|
1303
|
+
|
|
1304
|
+
// should not claim the canceled workflow
|
|
1305
|
+
expect(claimed).toBeNull();
|
|
1306
|
+
|
|
1307
|
+
await teardown(backend);
|
|
1308
|
+
});
|
|
1309
|
+
});
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
/**
|
|
1314
|
+
* Create a pending workflow run for tests.
|
|
1315
|
+
* @param b - Backend
|
|
1316
|
+
* @returns Created workflow run
|
|
1317
|
+
*/
|
|
1318
|
+
async function createPendingWorkflowRun(b: Backend) {
|
|
1319
|
+
return await b.createWorkflowRun({
|
|
1320
|
+
workflowName: randomUUID(),
|
|
1321
|
+
version: null,
|
|
1322
|
+
idempotencyKey: null,
|
|
1323
|
+
input: null,
|
|
1324
|
+
config: {},
|
|
1325
|
+
context: null,
|
|
1326
|
+
availableAt: null,
|
|
1327
|
+
deadlineAt: null,
|
|
1328
|
+
});
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
/**
|
|
1332
|
+
* Create and claim a workflow run for tests.
|
|
1333
|
+
* @param b - Backend
|
|
1334
|
+
* @returns Claimed workflow run
|
|
1335
|
+
*/
|
|
1336
|
+
async function createClaimedWorkflowRun(b: Backend) {
|
|
1337
|
+
await createPendingWorkflowRun(b);
|
|
1338
|
+
|
|
1339
|
+
const claimed = await b.claimWorkflowRun({
|
|
1340
|
+
workerId: randomUUID(),
|
|
1341
|
+
leaseDurationMs: 100,
|
|
1342
|
+
});
|
|
1343
|
+
|
|
1344
|
+
if (!claimed) throw new Error("Failed to claim workflow run");
|
|
1345
|
+
|
|
1346
|
+
return claimed;
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
/**
|
|
1350
|
+
* Get delta in seconds from now.
|
|
1351
|
+
* @param date - Date to compare
|
|
1352
|
+
* @returns Delta in seconds
|
|
1353
|
+
*/
|
|
1354
|
+
function deltaSeconds(date: Date | null | undefined): number {
|
|
1355
|
+
if (!date) return Infinity;
|
|
1356
|
+
return Math.abs((Date.now() - date.getTime()) / 1000);
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
/**
|
|
1360
|
+
* Create a Date one year in the future.
|
|
1361
|
+
* @returns Future Date
|
|
1362
|
+
*/
|
|
1363
|
+
function newDateInOneYear() {
|
|
1364
|
+
const d = new Date();
|
|
1365
|
+
d.setFullYear(d.getFullYear() + 1);
|
|
1366
|
+
return d;
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
/**
|
|
1370
|
+
* Sleep for a given duration.
|
|
1371
|
+
* @param ms - Milliseconds to sleep
|
|
1372
|
+
* @returns Promise resolved after sleeping
|
|
1373
|
+
*/
|
|
1374
|
+
function sleep(ms: number) {
|
|
1375
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1376
|
+
}
|