Task orchestration and state
Task orchestration turns a validated task request into one controlled run. It queues work, applies capacity rules, starts a local or remote executor, keeps durable and live state aligned, and completes the task with logs, outputs, alerts, hooks, and cleanup. This page complements Automation execution with the state and control model, and links to Local automation execution and Runner-backed execution for executor-specific behavior.
Task lifecycle
The task lifecycle moves between waiting, active, and terminal states. The task pool owns this lifecycle at the server boundary, while a task runner represents one task and connects its status, logs, inputs, executor, and completion actions.
flowchart LR
A[Task request] --> B[Validate and persist]
B --> C[Waiting queue]
C --> D{Capacity and runner available?}
D -- No --> C
D -- Yes --> E[Starting]
E --> F[Running locally or remotely]
F --> G{Terminal result}
G --> H[Success]
G --> I[Error]
G --> J[Stopped]
H --> K[Persist outputs and complete]
I --> K
J --> K
Task pool
The task pool is the server-side coordinator for queued and running work. It accepts new tasks, checks global and project capacity, prevents disallowed parallel runs, and starts a task only after it owns the queue entry. A task can return to waiting when no remote runner can accept it; this avoids treating temporary capacity pressure as a task failure. Queue entries are claimed and removed by task ID so concurrent nodes cannot remove the wrong task. services/…/TaskPool.go• L355-413, services/…/TaskPool.go• L740-772
The pool also releases active-task records, aliases, claims, and running records when a task ends or is requeued. Workflow completion is notified after the task slot is released, so downstream workflow work does not compete with stale capacity. services/…/TaskPool.go• L522-537, services/…/TaskRunner.go• L421-460
References: services/…/TaskPool.go• L355-413 services/…/TaskPool.go• L522-537 services/…/TaskPool.go• L740-772
Task runner
The task runner is the per-task control object. It loads the template, project settings, repository, inventory, environment, viewers, and task-specific execution choice before the task starts. It then owns status transitions and selects the completion model: local execution returns to the runner, while remote execution hands completion to the runner report path. services/…/TaskRunner.go• L193-370, services/…/TaskRunner.go• L606-700, services/…/TaskPool.go• L1343-1384
An executor exposes one task lifecycle. Preparation builds the run context, execution starts the application, and cleanup releases temporary resources. Local execution now keeps the prepared environment, arguments, inputs, and parameters on the executor so callers can drive preparation, run, and cleanup as separate phases. A local executor is single-use and still runs synchronously for the task runner. services/…/executor.go• L18-46, services/…/executor.go• L66-92, services/…/local_executor.go• L837-878, services/…/local_executor.go• L1057-1272
The runner treats success, error, and stopped as terminal results. A terminal result records the end time, writes the task event and audit record, releases pool state, and advances a workflow when the task belongs to one. A successful task can also create configured autorun tasks. services/…/TaskRunner.go• L421-523, pkg/…/task_logger.go• L8-32
References: services/…/TaskRunner.go• L193-240 services/…/TaskRunner.go• L320-373 services/…/TaskRunner.go• L421-523 services/…/executor.go• L18-92 services/…/local_executor.go• L837-878 services/…/local_executor.go• L1057-1272
State persistence
Task state has two purposes: the database records the durable task result, and the task state store tracks live coordination data. The live store contains the queue, running tasks, active tasks by project, aliases, claims, and transient fields needed to recover a task after a process restart. services/…/task_state_store.go• L16-23, services/…/task_state_store.go• L90-159
Durable and live state
Status, start and end times, runner assignment, commit details, and artifacts are written to the task record. Task output, stage, and stage-result records keep the execution history and structured results separate from the task summary. The live store mirrors the fields needed for dispatch and recovery. In high-availability mode, nodes hydrate task runners from durable state and prefer the database snapshot when live state is stale. services/…/TaskRunner.go• L117-140, services/…/TaskPool.go• L657-720, db/…/task.go• L192-226, db/…/task.go• L240-272
Task requests keep app-specific parameters in a common parameter map. Validation converts that map into the selected app's task parameter shape before execution. Runtime workflow fields can be resolved onto the task for execution without becoming stored task columns. db/Task.go• L52-116, db/Task.go• L118-125, db/Task.go• L209-245
Task history is read within project boundaries and can be narrowed by template, workflow run, status, viewer, or task IDs. Project task lists use an ID cursor for older results, so history remains usable as the number of tasks grows. db/…/task.go• L274-357, db/…/task.go• L381-406
The shared queue uses an ID-based claim and dequeue operation. This ensures that only one node starts a task even when several nodes inspect the same queue. A finalization lock gives one node responsibility for remote completion, including cleanup, finish notifications, and autorun work. services/…/TaskPool.go• L378-413, services/…/TaskPool.go• L539-560
References: services/…/task_state_store.go• L16-23 services/…/task_state_store.go• L90-159 services/…/TaskRunner.go• L117-140 services/…/TaskPool.go• L539-560 services/…/TaskPool.go• L657-720 db/Task.go• L52-125 db/Task.go• L209-245 db/…/task.go• L192-226 db/…/task.go• L240-272 db/…/task.go• L274-357
Runner recovery
A reconciliation pass checks tasks assigned to remote runners. A task that is starting on an offline runner returns to the queue. A task that was running on a lost or restarted runner fails after the recovery rules decide that the runner cannot continue it. These actions use the same finalization guard as normal completion. services/…/runner_reconciler.go• L29-101, services/…/runner_reconciler.go• L122-189, services/…/runner_reconciler.go• L216-320
This split matters in a cluster. A node can disappear without losing the task record, and another node can recover or finalize the task without running the original executor process. services/…/runner_reconciler.go• L322-376
References: services/…/runner_reconciler.go• L29-101 services/…/runner_reconciler.go• L122-189 services/…/runner_reconciler.go• L216-376
Input and execution context
Task input is assembled from the task request and the template's execution context. The system combines template and task parameters with variable groups, survey values, environment variables, repository and inventory data, and task identity values before it gives the final context to an executor. services/…/TaskRunner.go• L606-700, services/…/TaskRunner.go• L703-794, services/…/local_executor.go• L188-331, services/…/local_executor.go• L1057-1253
Input sources
The main input sources have distinct roles:
| Source | Purpose |
|---|---|
| Task parameters | Values for this run, such as task-specific flags or playbook choices. |
| Template parameters | Default application settings for the task type. |
| Environments | Shared JSON, process variables, and protected values merged for the template. Later values replace earlier ones with the same key. |
| Survey values | User-provided values. Values targeted at the process environment are kept out of extra variables so they are passed once. |
| Inventory | Hosts and host variables resolved from the selected inventory. Managed inventory becomes a run-time static inventory containing enabled hosts. |
| Task identity | Project, task, and workflow identifiers exposed to the running automation. |
Task-supplied secret values are stored as a task-bound, expiring access key rather than in the task row or event data. The executor reads them at dispatch time and the pool deletes them at terminal completion, with an expiry sweep as a crash backstop. services/…/TaskPool.go• L1105-1150, services/…/TaskPool.go• L1329-1341, services/…/TaskRunner.go• L259-283, services/…/TaskRunner.go• L432-443
Survey values targeted at the process environment are delivered as environment values and are removed from extra variables. This keeps one input from reaching the task through two channels. Values such as lists and objects keep JSON form when the executor must pass them as one string. services/…/local_executor.go• L188-304, services/…/local_executor_test.go• L166-252
Every app receives project and task identity environment values. Workflow tasks also receive workflow run, workflow template, version, and URL values when those values exist. This gives a task a stable way to identify its run without requiring the executor to query the database. services/…/local_executor.go• L307-332, services/…/local_executor.go• L1225-1227, services/…/local_executor_test.go• L574-655, db/Task.go• L94-103
Agent tasks keep secrets and extra variables out of command-line arguments because process listings can expose them. Other app types receive their supported input channels through their executor-specific context. services/…/local_executor.go• L1165-1197, services/…/local_executor_test.go• L700-724
References: services/…/TaskRunner.go• L606-700 services/…/TaskRunner.go• L703-794 services/…/local_executor.go• L188-331 services/…/local_executor.go• L1057-1253 services/…/local_executor_test.go• L112-252 services/…/local_executor_test.go• L574-724 db/Task.go• L52-116
Inventory context
Managed inventory is a database-backed host list with shared and per-host variables. Before execution, enabled hosts and validated variables are rendered into the static inventory form consumed by the task; disabled hosts do not enter the run. Invalid inventory data stops preparation rather than producing a partial host list. services/…/TaskRunner.go• L659-673, services/…/managed_inventory.go• L10-56
References: services/…/TaskRunner.go• L659-673 services/…/managed_inventory.go• L10-56
Logs and outputs
Logs have a live path and a durable path. Each task message is sent to interested users for near-real-time display, passed to listeners, and placed in the pool's write stream. The pool batches records before it stores them and can update stage information as output arrives. Durable output records preserve the task stream for later reads, while stage records associate structured progress with a part of the run. services/…/TaskRunner_logging.go• L22-60, services/…/TaskPool.go• L416-493, db/…/task.go• L240-272
Log lifecycle
Local command output is read from standard output and standard error as lines. Remote runners keep log records in the running job and send them with progress reports; the server accepts them as part of the remote task's progress. A line that exceeds the reader limit is a task-level execution failure because the system cannot safely continue to read the process output. services/…/TaskRunner_logging.go• L63-89, services/…/TaskRunner_logging.go• L190-242, services/…/running_job.go• L57-81, services/…/running_job.go• L195-232
Workflow outputs
Workflow outputs are separate from human-readable logs. A local workflow task prepares an output capture before the app runs, collects the output document after a successful running result, and exposes it to the runner before the server marks the task successful. A remote output document is validated and stored before the terminal success state is accepted. This ordering lets a downstream workflow task read a complete artifact document as soon as it sees success. services/…/local_executor.go• L935-960, services/…/local_executor.go• L1021-1054, services/…/TaskRunner.go• L358-370, services/…/TaskPool.go• L615-645
An executor that cannot capture workflow outputs leaves no output document. Tasks outside a workflow do not produce workflow outputs. Invalid output data fails the task instead of allowing success without a valid artifact. A stopped local task does not collect outputs after its termination signal. services/…/local_executor.go• L1021-1045, services/…/TaskPool.go• L615-645
Host statistics
Ansible tasks can produce per-host statistics as a separate result from the task log. The task prepares a callback and a private file, the callback writes versioned JSON after the playbook run, and the server parses and stores sorted host rows. Statistics are collected even when the playbook fails, but invalid or oversized statistics do not change the task result. services/…/local_executor.go• L893-933, services/…/local_executor.go• L1225-1227, services/…/semaphore_hosts.py• L4-7, services/…/task_hosts.go• L35-57, services/…/task_hosts.go• L96-174, services/…/TaskRunner.go• L399-419
References: services/…/TaskRunner_logging.go• L22-60 services/…/TaskPool.go• L416-493 services/…/local_executor.go• L893-1054 services/…/local_executor.go• L1218-1227 services/…/semaphore_hosts.py• L14-57 services/…/task_hosts.go• L35-174 services/…/TaskRunner.go• L399-419 db/…/task.go• L13-59 db/…/task.go• L240-272
Alerts and hooks
Status changes can notify users and external chat services. Notifications are limited to configured notifiable states: success, error, and waiting for confirmation. Project alert settings and template suppression settings decide whether a channel sends a message. pkg/…/task_logger.go• L52-54, services/…/TaskRunner_logging.go• L157-178, services/…/alert.go• L43-60
The alert layer can send email and configured chat notifications such as Telegram, Slack, Rocket.Chat, Microsoft Teams, DingTalk, and Gotify. A failed delivery is logged and does not change the task result. services/…/alert.go• L53-127, services/…/alert.go• L130-206, services/…/alert.go• L208-269, services/…/alert.go• L333-393, services/…/alert.go• L395-529
Application hooks run after a task reaches a terminal state. The hook registry selects a hook for the task application, while the task event and audit record remain the common completion signals. An application without a registered hook has no hook side effect. services/…/TaskRunner.go• L146-190, services/…/common.go• L5-7, services/…/factory.go• L7-13
References: pkg/…/task_logger.go• L52-54 services/…/TaskRunner_logging.go• L157-178 services/…/alert.go• L43-60 services/…/TaskRunner.go• L146-190 services/…/factory.go• L7-13
Locks and capacity
The pool applies capacity limits before it dispatches a task. Global limits and project limits bound concurrent work, while a template setting can prevent two tasks from the same template from running together. These checks affect dispatch only; a task that is blocked stays queued. services/…/TaskPool.go• L740-772
Repository work has a separate per-key lock. It protects shared working directories from concurrent Git operations when task execution itself is allowed to run in parallel. The lock covers repository update and checkout only; it does not limit the task count or isolate later work in the shared directory. Inventory repository updates use the same protection. services/…/key_lock.go• L5-34, services/…/local_executor.go• L57-62, services/…/local_executor.go• L1416-1437, services/…/local_executor_inventory.go• L79-107
References: services/…/TaskPool.go• L740-772 services/…/key_lock.go• L5-34 services/…/local_executor.go• L57-62 services/…/local_executor.go• L1416-1437 services/…/local_executor_inventory.go• L79-107
Task cancellation
Cancellation changes the task state and then applies the control needed for the executor type. A normal stop moves an active task to stopping and allows the executor to report a clean stopped result. A force stop marks it stopped at once and releases remote state locally when no remote completion report can arrive. services/…/TaskPool.go• L809-828, services/…/TaskPool.go• L850-879
Local cancellation
For a local task, cancellation closes the executor's stop signal. The running application receives that signal and the task runner checks the kill state independently of the process exit code. This prevents a process that exits with no error after a termination request from being reported as success. The executor also rejects a second run, which prevents a reused executor from starting with stale prepared state. services/…/local_executor.go• L101-125, services/…/local_executor.go• L837-878, services/…/TaskRunner.go• L330-337
Remote cancellation
The server cannot directly kill a remote process through the remote job abstraction. It records the stop request and uses the runner progress contract to tell the runner which jobs are no longer accepted. If the task is force-stopped while the runner is unavailable, server-side finalization still releases shared state and rejects a late terminal report. services/…/RemoteJob.go• L315-327, services/…/types.go• L53-61, services/…/TaskPool.go• L820-827, services/…/TaskPool.go• L972-992
Stopping a queued task removes it before the queue loop can start it. Stopping a workflow run or all tasks for a template also covers tasks that are not present in the current node's memory, which is required for high-availability operation. The database can mark waiting tasks as stopped in bulk for a project and template. services/…/TaskPool.go• L830-848, services/…/TaskPool.go• L881-997, services/…/TaskPool.go• L1000-1097, db/…/task.go• L229-237
References: services/…/TaskPool.go• L809-879 services/…/local_executor.go• L101-125 services/…/local_executor.go• L837-878 services/…/RemoteJob.go• L315-327 services/…/TaskPool.go• L1000-1097 db/…/task.go• L229-237
Change checklist
When these files or sections are changed, remember to consider these:
- Keep terminal status, output persistence, host-stat persistence, finalization, and workflow progression ordered so success never becomes visible before its outputs.
- Keep queue claims and finalization locks aligned across the durable task row and the shared state store. A mismatch can duplicate work or leak capacity.
- Keep stop handling consistent for queued tasks, local processes, remote runner jobs, and tasks owned by another high-availability node.
- Keep input targets distinct. A survey value sent as an environment variable must not also appear in extra variables or command arguments.
- Keep the executor lifecycle contract aligned across preparation, run, cleanup, output capture, and stop signals. Prepared state must not be reused for a second run.
- Keep the host statistics file format, callback counters, size limit, parser, and stored host rows compatible.
- Keep managed inventory validation and enabled-host filtering aligned with the static inventory shape consumed by the executor.
- Keep log batching and remote progress acknowledgement compatible. Otherwise users can see live output that is missing from durable history or receive duplicate lines.
- Keep alert suppression and notifiable status rules aligned with any new terminal or approval state.
- Keep task list filters and cursor rules aligned with project boundaries and workflow-run views when task history queries change.
- Keep task, stage, stage-result, and output records consistent when changing completion data or deleting task history.
- Keep task parameter validation and runtime workflow identity fields aligned with the app context exposed to local and remote executors.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Two cluster nodes remove a queue entry by position | One node can start a different task from the one it inspected | Claim and dequeue by task ID through the shared state store |
| A remote runner disappears while a task is starting | The task can remain assigned and never run | Requeue starting tasks after runner liveness checks |
| A remote runner disappears while a task is running | The task can remain unfinished without a completion report | Fail and finalize the task after the recovery window or restart detection |
| A success state is stored before workflow outputs | Downstream workflow work can start without its input artifacts | Validate and store outputs before persisting success |
| A local task is stopped but its process returns success | The task can be reported as successful after cancellation | Check the executor kill state independently of the exit error |
| A local executor is run twice | Stale prepared state can be used for a second task run | Enforce the single-use executor lifecycle |
| Standard output contains an overlong line | Output reading stops and the task cannot be observed safely | Kill the job and report the output-size failure |
| Managed inventory variables or host variables are invalid | The task starts with no reliable host model or fails later in the application | Validate and render the inventory before execution |
| Host statistics are malformed or larger than the read limit | Host rows are not stored, but the task result remains valid | Bound and validate the statistics document; treat it as optional task metadata |
| Shared repository operations run without the repository lock | Parallel Git actions can corrupt the working directory | Serialize Git work by repository or inventory key |
| A task query accepts IDs without a project scope | A caller can read or act on another project's task | Validate the task through its project and constrain list queries to the project |
| Task output rows are omitted during task history cleanup | Durable logs or result data can remain after the task is deleted | Remove task outputs with the task record as one lifecycle operation |
Key files
| File | Why to read it |
|---|---|
| services/…/TaskPool.go | Main server coordinator for queue admission, live state release, logs, completion, inputs, and cancellation. |
| services/…/TaskRunner.go | Per-task lifecycle, durable status changes, completion ordering, autorun behavior, task context loading, and result persistence. |
| services/…/task_state_store.go | Contract for queue, running, active, claim, alias, runtime, and finalization state. |
| services/…/runner_reconciler.go | Recovery rules for offline, restarted, or lost remote runners. |
| services/…/TaskRunner_logging.go | Status transition, live log delivery, log listeners, alerts, and process output handling. |
| services/…/executor.go | Shared lifecycle contract for task executors and optional output or host-stat results. |
| services/…/local_executor.go | Local lifecycle phases, input preparation, cancellation signal, output and host-stat capture, repository work, and cleanup boundary. |
| services/…/local_executor_test.go | Behavioral checks for input channels, task identity, argument safety, and credential routing. |
| services/…/local_executor_inventory.go | Inventory materialization, inventory repository access, and cleanup of temporary inventory files. |
| services/…/managed_inventory.go | Conversion of managed hosts and variables into the run-time inventory form. |
| services/…/task_hosts.go | Host-stat file capture, document validation, size bound, and conversion to stored host rows. |
| services/…/semaphore_hosts.py | Ansible callback contract that writes per-host counters for the server to read. |
| services/…/RemoteJob.go | Server-side remote dispatch, timeout, and remote cancellation boundary. |
| services/…/job_pool.go | Runner-side job queue, execution handoff, progress reporting, and output transfer. |
| services/…/running_job.go | Runner-side status and log snapshot used for progress reports. |
| services/…/key_lock.go | Per-repository serialization rule for shared working directories. |
| pkg/…/task_logger.go | Shared task states and terminal/notifiable state rules. |
| db/Task.go | Task request shape, app parameter validation, workflow identity fields, and durable task metadata. |
| db/…/task.go | Durable task status, stage, output, history query, deletion, and bulk waiting-task state operations. |