Skip to content

Commit 9b48fbc

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

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

packages/openworkflow/client.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,31 @@ 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+
await expect(
436+
client.cancelWorkflowRun("non-existent-id"),
437+
).rejects.toThrow(/Workflow run non-existent-id does not exist/);
438+
});
439+
415440
describe("defineWorkflowSpec / implementWorkflow API", () => {
416441
test("defineWorkflowSpec returns a spec that can be used to schedule runs", async () => {
417442
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 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)