К содержанию
Статья ещё не опубликована на языке Russian. Показана версия на English.

Human-Timed Durable Workflows: Sleep Timers, External Signals, Queries, and Cancellation

2 мин на чтение

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

PrimitiveDirectionMutates State?Typical Use Case
Durable Timer (sleep)Internal clockAdvances workflow timeWait 2 hours before sending an abandoned-cart nudge
SignalExternal -> Workflow (async)YesDeliver inbound customer reply, payment webhook, or stop command
QueryExternal -> Workflow (sync read)No (read-only)Inspect current funnel step and scheduled wake-up time for UI
CancellationExternal -> WorkflowTriggers cleanup blockCancel 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:

ts
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.

Было ли это полезно?