1- import {
2- Or ,
3- SleepCondition ,
4- UserEventCondition ,
5- } from "@hatchet-dev/typescript-sdk/v1/conditions" ;
6- import { NonDeterminismError } from "@hatchet-dev/typescript-sdk/v1/errors/non-determinism-error" ;
7- import sleep from "@hatchet-dev/typescript-sdk/util/sleep" ;
8- import { hatchet } from "../hatchet-client" ;
9-
10- export const EVENT_KEY = "durable-example:event" ;
1+ import { Or , SleepCondition , UserEventCondition } from '@hatchet-dev/typescript-sdk/v1/conditions' ;
2+ import { NonDeterminismError } from '@hatchet-dev/typescript-sdk/util/errors/non-determinism-error' ;
3+ import sleep from '@hatchet-dev/typescript-sdk/util/sleep' ;
4+ import { hatchet } from '../hatchet-client' ;
5+
6+ export const EVENT_KEY = 'durable-example:event' ;
117export const SLEEP_TIME_SECONDS = 2 ;
128export const SLEEP_TIME = `${ SLEEP_TIME_SECONDS } s` as const ;
139
1410// > Create a durable workflow
1511export const durableWorkflow = hatchet . workflow ( {
16- name : " durable-workflow" ,
12+ name : ' durable-workflow' ,
1713} ) ;
1814
1915durableWorkflow . task ( {
20- name : " ephemeral_task" ,
16+ name : ' ephemeral_task' ,
2117 fn : async ( ) => {
22- console . log ( " Running non-durable task" ) ;
18+ console . log ( ' Running non-durable task' ) ;
2319 } ,
2420} ) ;
2521
2622durableWorkflow . durableTask ( {
27- name : " durable_task" ,
28- executionTimeout : " 10m" ,
23+ name : ' durable_task' ,
24+ executionTimeout : ' 10m' ,
2925 fn : async ( _input , ctx ) => {
30- console . log ( " Waiting for sleep" ) ;
26+ console . log ( ' Waiting for sleep' ) ;
3127 await ctx . sleepFor ( SLEEP_TIME ) ;
32- console . log ( " Sleep finished" ) ;
28+ console . log ( ' Sleep finished' ) ;
3329
34- console . log ( " Waiting for event" ) ;
30+ console . log ( ' Waiting for event' ) ;
3531 await ctx . waitFor ( { eventKey : EVENT_KEY } ) ;
36- console . log ( " Event received" ) ;
32+ console . log ( ' Event received' ) ;
3733
38- return { status : " success" } ;
34+ return { status : ' success' } ;
3935 } ,
4036} ) ;
4137
42- function extractKeyAndEventId ( waitResult : unknown ) : {
43- key : string ;
44- eventId : string ;
45- } {
38+ function extractKeyAndEventId ( waitResult : unknown ) : { key : string ; eventId : string } {
4639 // DurableContext.waitFor currently returns the CREATE payload directly.
4740 // The shape is typically `{ [readableDataKey]: { [eventId]: ... } }`.
4841 const obj = waitResult as Record < string , Record < string , unknown > > ;
49- if ( obj && typeof obj === " object" ) {
42+ if ( obj && typeof obj === ' object' ) {
5043 const [ key ] = Object . keys ( obj ) ;
5144 const inner = obj [ key ] ;
52- if ( inner && typeof inner === " object" && ! Array . isArray ( inner ) ) {
45+ if ( inner && typeof inner === ' object' && ! Array . isArray ( inner ) ) {
5346 const [ eventId ] = Object . keys ( inner ) ;
5447 if ( eventId ) {
5548 return { key, eventId } ;
5649 }
5750 }
5851 if ( key ) {
59- return { key : " CREATE" , eventId : key } ;
52+ return { key : ' CREATE' , eventId : key } ;
6053 }
6154 }
6255
63- return { key : " CREATE" , eventId : "" } ;
56+ return { key : ' CREATE' , eventId : '' } ;
6457}
6558
6659durableWorkflow . durableTask ( {
67- name : " wait_for_or_group_1" ,
68- executionTimeout : " 10m" ,
60+ name : ' wait_for_or_group_1' ,
61+ executionTimeout : ' 10m' ,
6962 fn : async ( _input , ctx ) => {
7063 const start = Date . now ( ) ;
7164 const waitResult = await ctx . waitFor (
72- Or (
73- new SleepCondition ( SLEEP_TIME , "sleep" ) ,
74- new UserEventCondition ( EVENT_KEY , "" , "event" ) ,
75- ) ,
65+ Or ( new SleepCondition ( SLEEP_TIME , 'sleep' ) , new UserEventCondition ( EVENT_KEY , '' , 'event' ) )
7666 ) ;
7767 const { key, eventId } = extractKeyAndEventId ( waitResult ) ;
7868 return {
@@ -84,15 +74,15 @@ durableWorkflow.durableTask({
8474} ) ;
8575
8676durableWorkflow . durableTask ( {
87- name : " wait_for_or_group_2" ,
88- executionTimeout : " 10m" ,
77+ name : ' wait_for_or_group_2' ,
78+ executionTimeout : ' 10m' ,
8979 fn : async ( _input , ctx ) => {
9080 const start = Date . now ( ) ;
9181 const waitResult = await ctx . waitFor (
9282 Or (
93- new SleepCondition ( `${ 6 * SLEEP_TIME_SECONDS } s` , " sleep" ) ,
94- new UserEventCondition ( EVENT_KEY , "" , " event" ) ,
95- ) ,
83+ new SleepCondition ( `${ 6 * SLEEP_TIME_SECONDS } s` , ' sleep' ) ,
84+ new UserEventCondition ( EVENT_KEY , '' , ' event' )
85+ )
9686 ) ;
9787 const { key, eventId } = extractKeyAndEventId ( waitResult ) ;
9888 return {
@@ -104,8 +94,8 @@ durableWorkflow.durableTask({
10494} ) ;
10595
10696durableWorkflow . durableTask ( {
107- name : " wait_for_multi_sleep" ,
108- executionTimeout : " 10m" ,
97+ name : ' wait_for_multi_sleep' ,
98+ executionTimeout : ' 10m' ,
10999 fn : async ( _input , ctx ) => {
110100 const start = Date . now ( ) ;
111101 // sleep 3 times
@@ -118,8 +108,8 @@ durableWorkflow.durableTask({
118108} ) ;
119109
120110export const waitForSleepTwice = hatchet . durableTask ( {
121- name : " wait-for-sleep-twice" ,
122- executionTimeout : " 10m" ,
111+ name : ' wait-for-sleep-twice' ,
112+ executionTimeout : ' 10m' ,
123113 fn : async ( _input , ctx ) => {
124114 try {
125115 const start = Date . now ( ) ;
@@ -134,24 +124,24 @@ export const waitForSleepTwice = hatchet.durableTask({
134124// --- Spawn child from durable task ---
135125
136126export const spawnChildTask = hatchet . task ( {
137- name : " spawn-child-task" ,
127+ name : ' spawn-child-task' ,
138128 fn : async ( input : { n ?: number } ) => {
139129 return { message : `hello from child ${ input . n ?? 1 } ` } ;
140130 } ,
141131} ) ;
142132
143133export const durableWithSpawn = hatchet . durableTask ( {
144- name : " durable-with-spawn" ,
145- executionTimeout : " 10s" ,
134+ name : ' durable-with-spawn' ,
135+ executionTimeout : ' 10s' ,
146136 fn : async ( _input , ctx ) => {
147137 const childResult = await spawnChildTask . run ( { } ) ;
148138 return { child_output : childResult } ;
149139 } ,
150140} ) ;
151141
152142export const durableWithBulkSpawn = hatchet . durableTask ( {
153- name : " durable-with-bulk-spawn" ,
154- executionTimeout : " 10m" ,
143+ name : ' durable-with-bulk-spawn' ,
144+ executionTimeout : ' 10m' ,
155145 fn : async ( input : { n ?: number } , ctx ) => {
156146 const n = input . n ?? 10 ;
157147 const inputs = Array . from ( { length : n } , ( _ , i ) => ( { n : i } ) ) ;
@@ -161,8 +151,8 @@ export const durableWithBulkSpawn = hatchet.durableTask({
161151} ) ;
162152
163153export const durableSleepEventSpawn = hatchet . durableTask ( {
164- name : " durable-sleep-event-spawn" ,
165- executionTimeout : " 10m" ,
154+ name : ' durable-sleep-event-spawn' ,
155+ executionTimeout : ' 10m' ,
166156 fn : async ( _input , ctx ) => {
167157 const start = Date . now ( ) ;
168158
@@ -182,8 +172,8 @@ export const durableSleepEventSpawn = hatchet.durableTask({
182172// --- Spawn child using explicit ctx.spawnChild ---
183173
184174export const durableWithExplicitSpawn = hatchet . durableTask ( {
185- name : " durable-with-explicit-spawn" ,
186- executionTimeout : " 10m" ,
175+ name : ' durable-with-explicit-spawn' ,
176+ executionTimeout : ' 10m' ,
187177 fn : async ( _input , ctx ) => {
188178 const childResult = await ctx . spawnChild ( spawnChildTask , { } ) ;
189179 return { child_output : childResult } ;
@@ -193,8 +183,8 @@ export const durableWithExplicitSpawn = hatchet.durableTask({
193183// --- Non-determinism detection ---
194184
195185export const durableNonDeterminism = hatchet . durableTask ( {
196- name : " durable-non-determinism" ,
197- executionTimeout : " 10s" ,
186+ name : ' durable-non-determinism' ,
187+ executionTimeout : ' 10s' ,
198188 fn : async ( _input , ctx ) => {
199189 const sleepTime = ctx . invocationCount * 2 ;
200190
@@ -225,8 +215,8 @@ export const durableNonDeterminism = hatchet.durableTask({
225215const MEMO_SLEEP_MS = 2000 ;
226216
227217export const memoTask = hatchet . durableTask ( {
228- name : " memo-task" ,
229- executionTimeout : " 10m" ,
218+ name : ' memo-task' ,
219+ executionTimeout : ' 10m' ,
230220 fn : async ( input : { message : string } , ctx ) => {
231221 const start = Date . now ( ) ;
232222 const res = await ctx . memo ( async ( ) => {
@@ -245,8 +235,8 @@ export const REPLAY_RESET_MEMOIZED_MAX_SECONDS = 5;
245235const REPLAY_RESET_SLEEP = `${ REPLAY_RESET_SLEEP_SECONDS } s` as const ;
246236
247237export const durableReplayReset = hatchet . durableTask ( {
248- name : " durable-replay-reset" ,
249- executionTimeout : " 20s" ,
238+ name : ' durable-replay-reset' ,
239+ executionTimeout : ' 20s' ,
250240 fn : async ( _input , ctx ) => {
251241 let start = Date . now ( ) ;
252242 await ctx . sleepFor ( REPLAY_RESET_SLEEP ) ;
@@ -271,29 +261,29 @@ export const durableReplayReset = hatchet.durableTask({
271261// --- Spawn DAG from durable task ---
272262
273263export const dagChildWorkflow = hatchet . workflow ( {
274- name : " dag-child-workflow-ts" ,
264+ name : ' dag-child-workflow-ts' ,
275265} ) ;
276266
277267const dagChild1 = dagChildWorkflow . task ( {
278- name : " dag-child-1" ,
268+ name : ' dag-child-1' ,
279269 fn : async ( ) => {
280270 await sleep ( 1000 ) ;
281- return { result : " child1" } ;
271+ return { result : ' child1' } ;
282272 } ,
283273} ) ;
284274
285275dagChildWorkflow . task ( {
286- name : " dag-child-2" ,
276+ name : ' dag-child-2' ,
287277 parents : [ dagChild1 ] ,
288278 fn : async ( ) => {
289279 await sleep ( 2000 ) ;
290- return { result : " child2" } ;
280+ return { result : ' child2' } ;
291281 } ,
292282} ) ;
293283
294284export const durableSpawnDag = hatchet . durableTask ( {
295- name : " durable-spawn-dag" ,
296- executionTimeout : " 10s" ,
285+ name : ' durable-spawn-dag' ,
286+ executionTimeout : ' 10s' ,
297287 fn : async ( _input , ctx ) => {
298288 const sleepStart = Date . now ( ) ;
299289 const sleepResult = await ctx . sleepFor ( SLEEP_TIME ) ;
0 commit comments