55 WorkflowEvent ,
66 WorkflowStep ,
77} from "cloudflare:workers" ;
8+ import { NonRetryableError } from "cloudflare:workflows" ;
89
910type Params = {
1011 name : string ;
@@ -56,14 +57,47 @@ export class Demo2 extends WorkflowEntrypoint<{}, Params> {
5657 }
5758}
5859
60+ export class Demo3 extends WorkflowEntrypoint < { } , Params > {
61+ async run (
62+ event : WorkflowEvent < Params & { doRetry : boolean ; errorMessage : string } > ,
63+ step : WorkflowStep
64+ ) {
65+ let runs = - 1 ;
66+ try {
67+ await step . do (
68+ "First (and only step) step" ,
69+ {
70+ retries : {
71+ limit : 3 ,
72+ delay : 100 ,
73+ } ,
74+ } ,
75+ async function ( ) {
76+ runs ++ ;
77+ if ( event . payload . doRetry ) {
78+ throw new Error ( event . payload . errorMessage ) ;
79+ } else {
80+ throw new NonRetryableError ( event . payload . errorMessage ) ;
81+ }
82+ }
83+ ) ;
84+ } catch { }
85+
86+ return `The step was retried ${ runs } time${ runs === 1 ? "" : "s" } ` ;
87+ }
88+ }
89+
5990type Env = {
6091 WORKFLOW : Workflow ;
6192 WORKFLOW2 : Workflow ;
93+ WORKFLOW3 : Workflow < { doRetry : boolean ; errorMessage : string } > ;
6294} ;
6395export default class extends WorkerEntrypoint < Env > {
6496 async fetch ( req : Request ) {
6597 const url = new URL ( req . url ) ;
6698 const id = url . searchParams . get ( "workflowName" ) ;
99+ const doRetry = url . searchParams . get ( "doRetry" ) ;
100+ const errorMessage = url . searchParams . get ( "errorMessage" ) ;
67101
68102 if ( url . pathname === "/favicon.ico" ) {
69103 return new Response ( null , { status : 404 } ) ;
@@ -89,6 +123,18 @@ export default class extends WorkerEntrypoint<Env> {
89123 } else {
90124 handle = await this . env . WORKFLOW2 . create ( { id } ) ;
91125 }
126+ } else if ( url . pathname === "/createDemo3" ) {
127+ if ( id === null ) {
128+ handle = await this . env . WORKFLOW3 . create ( ) ;
129+ } else {
130+ handle = await this . env . WORKFLOW3 . create ( {
131+ id,
132+ params : {
133+ doRetry : doRetry === "false" ? false : true ,
134+ errorMessage : errorMessage ?? "" ,
135+ } ,
136+ } ) ;
137+ }
92138 } else if ( url . pathname === "/sendEvent" ) {
93139 handle = await this . env . WORKFLOW2 . get ( id ) ;
94140
@@ -98,6 +144,8 @@ export default class extends WorkerEntrypoint<Env> {
98144 } ) ;
99145 } else if ( url . pathname === "/get2" ) {
100146 handle = await this . env . WORKFLOW2 . get ( id ) ;
147+ } else if ( url . pathname === "/get3" ) {
148+ handle = await this . env . WORKFLOW3 . get ( id ) ;
101149 } else {
102150 handle = await this . env . WORKFLOW . get ( id ) ;
103151 }
0 commit comments