Task and scheduling controllers
Task and scheduling controllers move workflow work from a trigger to a worker and back to durable workflow state. They combine event handling with time-based checks so retries, timeouts, sleeps, callbacks, cleanup, queue capacity, and recovery continue across process restarts. This page complements Service runtime and control plane, Dispatcher and worker coordination, OLAP and observability services, and Run dispatch and queue execution.
Control loop model
The task controller is the main state transition point for a tenant's tasks. It consumes trigger and worker messages, records the durable result, and sends focused signals to scheduling, dispatch, durable workflow execution, and observability. Periodic operations provide a second way to find work when an event arrives late or a process stops.
This diagram shows how event paths and timer paths converge on durable task state before they wake scheduling and downstream workflow work.
flowchart LR
A[Trigger or worker event] --> B[Task controller]
T[Periodic task loop] --> B
B --> C[Durable task state]
C --> D[Scheduler wakeup]
D --> E[Worker dispatch]
E --> F[Completion failure or cancellation]
F --> B
B --> G[Sleep callback or child trigger]
B --> H[Status and observability]
Controller boundaries
The task controller owns task state transitions and the signals that follow them. The scheduler owns queue selection and capacity. The dispatcher delivers work and reports worker results. This boundary lets a durable state change be retried without tying it to one worker assignment.
References: internal/…/controller.go• L493-535 internal/…/controller.go• L719-776 internal/…/scheduler.go• L375-424
Tenant operation loops
Maintenance work runs as tenant-scoped operations. Each operation limits overlapping work for one tenant and can continue through pages until no work remains. Shared activity signals and staggered polling reduce duplicate database pressure when many controller instances run together.
References: internal/…/controller.go• L273-350 internal/…/pool.go• L17-32 internal/…/pool.go• L113-170
Task state coordination
The task store is the durable source of truth. The controller records a state change first and then publishes the signals that let other services catch up. This order supports redelivery because a repeated message can observe an existing result instead of creating a second one.
Trigger and event creation
Triggers enter through workflow names, user events, internal events, or durable child-run requests. The controller turns those inputs into matching tasks or workflow runs, then signals created work and expression failures. Durable event matches can also wake a waiting task without starting a new worker directly.
References: internal/…/controller.go• L1388-1513 internal/…/controller.go• L1604-1684 internal/…/cron_v1.go• L19-63
Completion failure and cancellation
Worker completion, failure, and cancellation use the same durable release model. Completion stores output. Failure records whether a later attempt can run. Cancellation releases the task and can notify the worker that still owns it. A released task then wakes scheduling and emits workflow and observability signals.
Durable orchestrator tasks have an extra terminal-state rule. A failed attempt that will retry is not terminal for the orchestrator. A terminal event is sent only for the current retry that ended. If an orchestrator is released without a worker, the controller also cancels its unfinished children because no worker remains to perform that action. A worker-owned orchestrator does not use this second child-cancellation path.
References: internal/…/controller.go• L538-698 internal/…/controller.go• L719-901 internal/…/controller.go• L989-1125
Retry and reassignment handling
Retries create a later attempt after an application or internal failure. A requested delay is kept within a safe non-negative range and a configured maximum. The retry path makes a due attempt runnable again and reports its queued state. Reassignment handles a worker that stops responding or never accepts an assignment. It can retry the task or make the loss final before scheduling continues.
References: internal/…/controller.go• L792-986 internal/…/controller.go• L1687-1778 internal/…/scheduler.go• L909-935
Time-based task control
Time-based loops turn stored deadlines into normal task events. A timeout, retry delay, or durable sleep can therefore advance workflow state without keeping a worker process open.
Timeout processing
The timeout loop finds attempts that exceed their allowed run time. It marks them timed out and applies the normal failure rules to decide whether to retry. It also routes cancellation to the affected worker and publishes the result for observability. A retry remains non-terminal even though its current attempt timed out.
References: internal/…/controller.go• L280-287 internal/…/controller.go• L355-490 internal/…/process_timeouts.go• L16-97
Durable sleep processing
A durable sleep stores a future workflow point instead of holding a worker open. When that point is due, the sleep loop resolves the matching durable entries. The controller then creates newly enabled tasks and dispatches satisfied callbacks. The wait survives a process restart while worker capacity stays free.
References: internal/…/controller.go• L289-296 internal/…/process_sleeps.go• L12-43 internal/…/durable_callbacks.go• L17-115
Callback delivery
Durable callbacks connect a completed child or event to a waiting durable task. The controller converts satisfied entries into callback messages. The durable runtime uses those messages to wake child tasks, branches, or other durable nodes.
References: internal/…/controller.go• L741-790 internal/…/durable_callbacks.go• L21-115 internal/…/dispatch.go• L1-200
Schedule ownership and queue wakeups
The task controller changes durable queue state while the scheduler keeps the live view of queues, workers, and capacity. When a task becomes runnable or leaves a queue, the controller sends a focused wakeup for the affected queues or concurrency strategies. The scheduler refreshes that state instead of waiting for a broad poll.
Partition recreation
Scheduler partitions assign tenants to scheduler instances and carry the wake-up path for their queues. If a stale partition is removed during recovery, the scheduler recreates it and reassigns the tenant. New work must still receive a wake-up after that change so it does not wait for a queue poll that may have backed off; heartbeats then keep the recreated partition active during idle periods.
References: internal/…/doc.go• L1-2 internal/…/partition_recreate_test.go• L25-64 internal/…/partition_recreate_test.go• L100-125
Queue release notifications
Task completion, failure, cancellation, retry release, unpause, and recovery all use the same handoff idea. The durable change happens first. The controller then publishes the tenant's scheduler partition and identifies the queues or shared limits that need attention. It also publishes workflow-run completion candidates for downstream subscribers.
References: internal/…/controller.go• L1327-1386 internal/…/controller.go• L1516-1600 internal/…/process_retry_queue_items.go• L80-115
Queue assignment and dispatch
The scheduler turns queue results into worker messages. It groups assignments by dispatcher, emits batch work to the right worker, and reports assigned work to observability. If a worker has no dispatcher or a dispatch send fails, the scheduler removes or retries the assignment rather than leaving it silently claimed.
References: internal/…/scheduler.go• L522-673 internal/…/scheduler.go• L1072-1327
Capacity and concurrency changes
Scheduler notifications refresh queue work, worker capacity, and shared concurrency state. Concurrency results wake the next queues and strategies. They also send cancellation directly to workers for tasks whose runtime was released during the concurrency update, because the later task-cancellation path may no longer find the worker.
References: internal/…/scheduler.go• L404-424 internal/…/scheduler.go• L938-1070
Rate limit and batch state
Rate-limited work remains outside active scheduling until its limit allows progress. The scheduler reports rate-limit outcomes and the task controller requeues work when a pause ends. Batch work stays buffered until its size or time rule flushes it. The scheduler sends one batch command to a dispatcher and releases the batch reservation if delivery fails.
References: internal/…/controller.go• L1549-1600 internal/…/scheduler.go• L675-754 internal/…/scheduler.go• L801-907 internal/…/scheduler.go• L1072-1327
Trigger scheduling and maintenance
The ticker owns time-based trigger creation and small maintenance jobs that are not part of a worker attempt. It keeps database-backed cron assignments in sync with local scheduled jobs. It then sends each due workflow trigger to the task controller, where normal task creation and scheduling rules apply.
Cron schedule ownership
The ticker claims a set of cron schedules through a database-backed ticker identity. It refreshes that set on a short interval and also reacts to an immediate refresh notification. New schedules are added. Schedules no longer assigned to the ticker are removed. A lock prevents overlapping refreshes from changing the local schedule map at the same time.
References: internal/…/ticker.go• L157-224 internal/…/cron.go• L18-76 internal/…/cron.go• L172-209
Cron workflow triggering
When a cron fires, the ticker loads the selected workflow version and sends a trigger message to the task-processing queue. The message includes the scheduled time and cron metadata. The task controller then handles it like any other workflow-name trigger, so cron work receives the same durable state and retry behavior as event-driven work.
References: internal/…/cron.go• L78-169 internal/…/cron_v1.go• L19-69 internal/…/controller.go• L1454-1457
Controller maintenance and cleanup
The task controller starts periodic loops for timeouts, sleeps, retries, reassignments, paused-workflow expiry, stale state cleanup, and stuck durable orchestrator recovery. It also creates task partitions and runs other bounded maintenance jobs. On shutdown it stops message buffering and every tenant operation before it stops its scheduler, so background work does not outlive the controller.
References: internal/…/controller.go• L273-350 internal/…/controller.go• L355-490 internal/…/cleanup.go• L8-26 internal/…/create_partitions.go• L14-53
Recovery paths
Recovery paths repair work that can be lost between durable state and a message or worker session. They use delayed redelivery, bounded retries, or periodic scans. Each path returns work to the normal task or scheduler flow rather than creating a separate execution model.
Durable orchestration recovery
Durable orchestration can miss an edge-triggered restore after an engine change or worker disconnect. A periodic pass finds an evicted orchestrator that has waited long enough and whose durable events are satisfied. It sends a restore request through the task queue. The restore path requeues eligible work, reports the restore, and wakes the affected scheduler state.
References: internal/…/controller.go• L343-350 internal/…/restore_evicted_orchestrators.go• L16-78 internal/…/durable_callbacks.go• L21-150
Dead-letter callback recovery
When a durable callback cannot reach a dispatcher, the scheduler resolves the task again and retries delivery after a delay. The delay grows up to a small limit. A redelivery cap prevents a disconnected worker from causing an endless message loop. After the cap, task reassignment or reconciliation must provide progress.
References: internal/…/scheduler.go• L1400-1427 internal/…/scheduler.go• L1532-1615
Dead-letter dispatch recovery
A dead-lettered assignment or batch-start message means a dispatcher may have stopped before the worker received its work. The scheduler lists the affected tasks and sends failure messages back to the task controller. The task controller can then apply normal retry or terminal-failure rules. Dead-lettered cancellations are looked up against current worker-to-dispatcher links before resend.
References: internal/…/scheduler.go• L1430-1529 internal/…/scheduler.go• L1617-1674
Change checklist
When these files or sections are changed, remember to consider these:
- Keep durable release, dispatcher cancellation, scheduler wakeup, and observability behavior aligned for completion, failure, timeout, cancellation, and concurrency cancellation.
- Keep terminal orchestrator events limited to the current retry that reached a terminal state. Keep workerless child cancellation and publish retry behavior aligned with that rule.
- Keep retry-delay bounds compatible with retry queue timing and preserve the handoff from retry state to normal scheduling.
- Keep queue results compatible with dispatcher grouping, batch reservations, rate-limit state, scheduling timeouts, and internal retry behavior.
- When queue state changes outside normal completion, preserve the notification for the affected queue and shared concurrency strategy.
- Keep pause and rate-limit resume paths aligned with both queue movement and scheduler notification.
- Keep scheduler partition recreation aligned with tenant reassignment, wake-up subscriptions, partition heartbeats, and backed-off queue polling.
- Keep cron refresh, local job removal, ticker ownership, and trigger-message metadata aligned when cron identity or timing changes.
- Keep callback redelivery delay, retry cap, task lookup, and fallback reassignment aligned when callback delivery changes.
- Keep controller operation cleanup aligned with scheduler shutdown so no tenant loop or buffered publication continues after service stop.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A task is released but its worker does not receive cancellation | The worker can continue after the durable task is terminal | Route cancellation to the current dispatcher and allow dead-letter resend. |
| A retriable failure is recorded without a later queue handoff | The next attempt stays delayed or invisible | Process due retry state and notify the affected scheduler queue. |
| A terminal orchestrator event is sent for an attempt that will retry | Observability can show a live orchestrator as finished | Emit terminal status only for the current retry that ended. |
| A workerless orchestrator is released without child cancellation | Its unfinished children can remain active | Cancel unfinished children only when no worker owns the orchestrator. |
| A scheduler cannot map an assignment to a dispatcher | Work can remain claimed without reaching a worker | Remove the worker from scheduler state and send the assignment through internal retry. |
| A due queue or concurrency change is committed without a scheduler wakeup | Runnable work waits for a later poll | Publish the affected queues and strategies after the durable change. |
| A concurrency update releases a running task before cancellation mapping | The normal task-cancel path cannot find its worker | Signal the worker's dispatcher from the worker identity captured in the concurrency result. |
| A callback is redelivered without delay or a cap | A disconnected worker can cause a message storm | Use bounded delayed redelivery and rely on reassignment or reconciliation after the cap. |
| A cron schedule is removed from the database but remains local | The workflow can keep triggering after ownership ends | Refresh the schedule set and remove local jobs that are no longer assigned. |
| A recreated scheduler partition does not restore its wake-up path | New work can wait for a backed-off queue poll after tenant reassignment | Recreate the tenant assignment, subscribe the scheduler to the current partition, and keep the partition alive with heartbeats. |
| Controller shutdown leaves operation loops or message buffers running | Background work can publish after service stop | Stop tenant operations, buffers, subscriptions, and schedulers during cleanup. |
Key files
| File | Why to read it |
|---|---|
| internal/…/controller.go | Shows task message entry points, durable release coordination, trigger handling, scheduler notifications, retry bounds, and controller lifecycle. |
| internal/…/process_timeouts.go | Shows how stored run deadlines become failure, cancellation, and retry state. |
| internal/…/process_sleeps.go | Shows how durable waits resume workflow progress. |
| internal/…/durable_callbacks.go | Shows callback dispatch, durable restoration, and scheduler notification. |
| internal/…/restore_evicted_orchestrators.go | Shows periodic recovery for stuck durable orchestrators. |
| internal/…/process_retry_queue_items.go | Shows how due retries return to normal scheduling. |
| internal/…/scheduler.go | Shows queue wakeups, worker dispatch, batch handling, concurrency cancellation, and dead-letter recovery. |
| internal/…/partition_recreate_test.go | Shows the scheduler contract that tenant work still starts promptly after partition recreation and that heartbeats keep the partition alive. |
| internal/…/doc.go | Defines the in-process scheduler test package used to check end-to-end scheduling behavior. |
| internal/…/ticker.go | Shows ticker ownership, cron refresh notifications, periodic trigger polling, and shutdown. |
| internal/…/cron.go | Shows database-backed cron ownership and local schedule refresh. |
| internal/…/cron_v1.go | Shows how a due cron becomes a normal task-processing trigger. |
| internal/…/trigger.go | Shows how workflow and event triggers become durable task signals. |
| pkg/…/scheduler_queue.go | Shows the durable queue outcomes that the scheduler must commit and report. |
| internal/…/pool.go | Explains tenant-scoped operation scheduling and continuation. |