Human-Timed Durable Workflows: Sleep Timers, External Signals, Queries, and Cancellation
Business processes move at human speed: a customer clicks a link at 14:00, forgets to check out for 3 hours, replies with a question at 17:10, and pays the next morning. Durable workflows model these multi-day arcs cleanly with four primitives: Timers, Signals, Queries, and Cancellation Scopes.
The Four Control Primitives
| Primitive | Direction | Mutates State? | Typical Use Case |
|---|---|---|---|
Durable Timer (sleep) | Internal clock | Advances workflow time | Wait 2 hours before sending an abandoned-cart nudge |
| Signal | External -> Workflow (async) | Yes | Deliver inbound customer reply, payment webhook, or stop command |
| Query | External -> Workflow (sync read) | No (read-only) | Inspect current funnel step and scheduled wake-up time for UI |
| Cancellation | External -> Workflow | Triggers cleanup block | Cancel follow-up cleanly when customer opts out or human takes over |
Respecting Quiet Hours and Timezones
Sending a scheduled reminder at 03:15 AM destroys engagement and triggers spam blocks. Compute timer durations through a pure, deterministic helper that shifts wake-ups inside the recipient's local quiet window forward to the next allowed morning hour:
export function computeNextAllowedSendTime(
targetUtcMs: number,
tzOffsetMinutes: number,
quietStartHour = 21,
quietEndHour = 9
): number {
const localMs = targetUtcMs + tzOffsetMinutes * 60_000;
const localDate = new Date(localMs);
const hour = localDate.getUTCHours();
if (hour >= quietStartHour) {
localDate.setUTCDate(localDate.getUTCDate() + 1);
localDate.setUTCHours(quietEndHour, 0, 0, 0);
return localDate.getTime() - tzOffsetMinutes * 60_000;
}
if (hour < quietEndHour) {
localDate.setUTCHours(quietEndHour, 0, 0, 0);
return localDate.getTime() - tzOffsetMinutes * 60_000;
}
return targetUtcMs;
}
Safe Cleanup on Cancellation
When a workflow is cancelled externally, run cleanup inside a non-cancellable scope if you must write a final audit status ("cancelled_by_reply") to your reporting ledger so analytics stay consistent with actual customer experience.
Next: protect downstream APIs and sender reputation during high-volume bursts in Rate Limiting, Token Buckets, and Circuit Breakers.