Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/openworkflow/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,33 @@ describe("OpenWorkflow", () => {
expect(workflowRun?.finishedAt).not.toBeNull();
});

test("cancels workflow run via client by ID", async () => {
const backend = await createBackend();
const client = new OpenWorkflow({ backend });

const workflow = client.defineWorkflow({ name: "cancel-test" }, noopFn);
const handle = await workflow.run({ value: 1 });

await client.cancelWorkflowRun(handle.workflowRun.id);

const workflowRun = await backend.getWorkflowRun({
workflowRunId: handle.workflowRun.id,
});
expect(workflowRun?.status).toBe("canceled");
expect(workflowRun?.finishedAt).not.toBeNull();
});

test("throws when canceling a non-existent workflow run", async () => {
const backend = await createBackend();
const client = new OpenWorkflow({ backend });

const nonExistentId = randomUUID();

await expect(client.cancelWorkflowRun(nonExistentId)).rejects.toThrow(
`Workflow run ${nonExistentId} does not exist`,
);
});

describe("defineWorkflowSpec / implementWorkflow API", () => {
test("defineWorkflowSpec returns a spec that can be used to schedule runs", async () => {
const backend = await createBackend();
Expand Down
14 changes: 14 additions & 0 deletions packages/openworkflow/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ export class OpenWorkflow {

return new RunnableWorkflow(this, workflow);
}

/**
* Cancels the workflow run with the given ID. Only workflow runs in pending, running, or sleeping
* status can be canceled.
* @param workflowRunId - The ID of the workflow run to cancel
* @returns Promise<void>
* @example
* ```ts
* await ow.cancelWorkflowRun("123");
* ```
*/
async cancelWorkflowRun(workflowRunId: string): Promise<void> {
await this.backend.cancelWorkflowRun({ workflowRunId });
}
}

/**
Expand Down