Skip to main content
Reference vendor/dbos 1 min read

pattern-delayed-execution


title: Delay Workflow Execution impact: MEDIUM impactDescription: Schedule one-off work for later without polling or external timers tags: pattern, delay, scheduled, queue

Delay Workflow Execution

Use a delay when you need a workflow to run later (a retry timer, a TTL, a scheduled email) and don’t want to write an external scheduler. The workflow is recorded immediately with status ENQUEUED and delay_until populated; the queue runner only dequeues it once that time passes.

This is not the same as a recurring cron schedule. For cron see pattern-scheduled.md. For inline durable waits inside a running workflow, see pattern-sleep.md.

Enqueueing with a delay

Incorrect (sleeping in a goroutine to delay a workflow):

// Lost if the process crashes; no durability, no introspection
go func() {
    time.Sleep(24 * time.Hour)
    dbos.RunWorkflow(ctx, sendReminder, userID)
}()

Correct (WithDelay on enqueue):

// Relative delay on a queued workflow
queue, _ := dbos.RegisterQueue(ctx, "notifications")
handle, _ := dbos.RunWorkflow(ctx, sendReminder, userID,
    dbos.WithQueue(queue),
    dbos.WithDelay(24*time.Hour))

From the client (external app):

handle, _ := dbos.Enqueue[string](client, "notifications", "sendReminder", userID,
    dbos.WithEnqueueDelay(24*time.Hour))

Updating the delay later

SetWorkflowDelay re-targets a workflow whose status is DELAYED. Provide exactly one of WithDelayDuration (relative to now) or WithDelayUntil (absolute):

// Push it back another hour
dbos.SetWorkflowDelay(ctx, workflowID,
    dbos.WithDelayDuration(time.Hour))

// Or set an absolute fire time
dbos.SetWorkflowDelay(ctx, workflowID,
    dbos.WithDelayUntil(time.Date(2026, 6, 1, 9, 0, 0, 0, time.UTC)))

Calling with both options or neither returns an error. SetWorkflowDelay takes a Client, so it works the same from an external dbos.Client.

Cancelling a delayed workflow

CancelWorkflow works on delayed workflows the same as any other status. To run it immediately instead of waiting, use ResumeWorkflow — it bypasses the delay and re-enqueues the workflow.

Reference: Queues