Sleep
Durable sleep is a step that can be used to pause a command for a specified amount of time. This can be useful when you want to wait for a specific amount of time before continuing with the command. For example when onboarding a new user after a purchase of a product, you might want to wait for a few days before sending a follow-up email.
The sleep is durable, meaning that the command can be restarted or resumed after a failure or a restart and the sleep will continue from the last saved state. The sleep step will never run longer that the first time the sleep was started.
API reference:
- SleepAsync: Sleep for a specified number of seconds.
- SleepAsync: Sleep until a specified date and time.
Required scopes:
Data/Buildby.Taskurai/steps/create
Prerequisites
- You have completed the setup of a worker in Taskurai, see Workers and Commands.
Sleeping for a specified amount of time
It is possible to sleep for a specified amount of time.
- C#
[Command]
[Version(1)]
public async Task<TaskCommandResult> StepsSample(TaskuraiTaskContext context, CancellationToken cancellationToken)
{
...
// Wait for 2 hours before sending a follow-up email, command will be suspended for 2 hours
await context.SleepAsync(
id: "initial-delay-1-step",
seconds: 7200, // Wait for 2 hours
);
...
return Succeeded(
$"Successfully completed sample."
);
}
Sleeping until a specified date and time
It is possible to sleep until a specified date and time.
- C#
[Command]
[Version(1)]
public async Task<TaskCommandResult> StepsSample(
TaskuraiTaskContext context,
CancellationToken cancellationToken,
DateTimeOffset orderDate)
{
...
// Wait for 10 days sending a follow-up email after an order has been placed
// Durable sleep and wait
await context.SleepAsync(
id: "initial-delay-1-step",
dateTime: orderDate.AddDays(10)
);
...
return Succeeded(
$"Successfully completed sample."
);
}