Sleep & Delays
Sleep pauses a task for a specified duration while freeing the worker slot. No resources are consumed during the wait, whether the pause lasts seconds or weeks.
Both durable tasks and DAGs support sleeping, but the API differs: durable tasks call SleepFor dynamically at runtime, while DAGs declare a sleep condition upfront on the task definition.
Durable Sleep
Durable sleep pauses execution for a specified amount of time and frees the worker slot until the sleep expires.
Sleeping puts the task into an evictable state, the worker slot is freed and the task is re-queued when the sleep expires.
Unlike a language-level sleep (e.g. time.sleep in Python or setTimeout in Node), durable sleep is guaranteed to respect the original duration across interruptions. A language-level sleep ties the wait to the local process, so if the process restarts, the sleep starts over from zero.
For example, say you’d like to send a notification to a user after 24 hours. With time.sleep, if the task is interrupted after 23 hours, it will restart and sleep for 24 hours again (47 hours total). With durable sleep, Hatchet tracks the original deadline server-side, so the task will only sleep for 1 more hour on restart.
Using durable sleep
Durable sleep can be used by calling the SleepFor method on the DurableContext object. This method takes a duration as an argument and will sleep for that duration.