Skip to content

Commit d9dcaaf

Browse files
committed
feat: Cancel workflow via the client with workflow id
1 parent 40ee9cb commit d9dcaaf

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

packages/openworkflow/client.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,33 @@ describe("OpenWorkflow", () => {
412412
expect(workflowRun?.finishedAt).not.toBeNull();
413413
});
414414

415+
test("cancels workflow run via client by ID", async () => {
416+
const backend = await createBackend();
417+
const client = new OpenWorkflow({ backend });
418+
419+
const workflow = client.defineWorkflow({ name: "cancel-test" }, noopFn);
420+
const handle = await workflow.run({ value: 1 });
421+
422+
await client.cancelWorkflowRun(handle.workflowRun.id);
423+
424+
const workflowRun = await backend.getWorkflowRun({
425+
workflowRunId: handle.workflowRun.id,
426+
});
427+
expect(workflowRun?.status).toBe("canceled");
428+
expect(workflowRun?.finishedAt).not.toBeNull();
429+
});
430+
431+
test("throws when canceling a non-existent workflow run", async () => {
432+
const backend = await createBackend();
433+
const client = new OpenWorkflow({ backend });
434+
435+
const nonExistentId = randomUUID();
436+
437+
await expect(
438+
client.cancelWorkflowRun(nonExistentId),
439+
).rejects.toThrow(`Workflow run ${nonExistentId} does not exist`);
440+
});
441+
415442
describe("defineWorkflowSpec / implementWorkflow API", () => {
416443
test("defineWorkflowSpec returns a spec that can be used to schedule runs", async () => {
417444
const backend = await createBackend();

packages/openworkflow/client.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,20 @@ export class OpenWorkflow {
157157

158158
return new RunnableWorkflow(this, workflow);
159159
}
160+
161+
/**
162+
* Cancels the workflow run with the given ID. Only workflow runs in pending, running, or sleeping
163+
* status can be canceled.
164+
* @param workflowRunId - The ID of the workflow run to cancel
165+
* @returns Promise<void>
166+
* @example
167+
* ```ts
168+
* await ow.cancelWorkflowRun("123");
169+
* ```
170+
*/
171+
async cancelWorkflowRun(workflowRunId: string): Promise<void> {
172+
await this.backend.cancelWorkflowRun({ workflowRunId });
173+
}
160174
}
161175

162176
/**

0 commit comments

Comments
 (0)