File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ( / W o r k f l o w r u n n o n - e x i s t e n t - i d d o e s n o t e x i s t / ) ;
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 ( ) ;
Original file line number Diff line number Diff 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/**
You can’t perform that action at this time.
0 commit comments