Introducing a new way to review your PRs.

Read the announcement

semaphoreui / semaphore

Modern UI and powerful API for Ansible, Terraform/OpenTofu/Terragrunt, PowerShell and other DevOps tools.

View on GitHub

21 documents · Updated Nov 23, 2026

docs / runner-backed-execution

Runner-backed execution

Runner-backed execution lets the server assign a task to a separate runner process while keeping task state, dispatch decisions, and final task lifecycle on the server. The runner pulls work, runs it through a configured executor, and sends status and output back through the runner API. The server-side dispatch and state model is described in Automation execution; this page focuses on the boundary with external runners.

Execution boundary

The server is the authority for task assignment and task state. An external runner is the worker boundary: it receives the task data needed for execution, runs the task in its own environment, and reports progress without owning the server-side task record.

The boundary has these responsibilities:

ServerRunner
Selects a registered runner with matching scope, tags, liveness, and capacityPulls assigned work from the runner API
Builds the task payload and decrypts the access keys needed for that taskHydrates the payload and creates the task executor
Accepts status, logs, commit data, and workflow outputsProduces status, logs, commit data, and workflow outputs
Finalizes the task and starts downstream server workStops execution when the server rejects a stale or finished job

This separation means the server can complete a remote task on any node that receives the runner report. The node that first dispatched the task does not need to stay alive for task completion.

References: services/…/RemoteJob.go• L204-283 api/…/runners.go• L162-180 services/…/TaskPool.go• L539-603

Runner registration

Runner registration creates the server identity that a runner uses for later polling. The command can take a registration token from a flag, a configured file, or standard input, then stores the returned runner token in a token file or the runner configuration.

The start command can auto-register when the runner has no token. It retries registration until the server is available, then starts the job pool. The Docker runner wrapper enables this mode when it receives a registration token and supplies a default token-file location when no runner token location is set.

References: cli/…/runner_register.go• L25-110 cli/…/runner_start.go• L21-50 deployment/…/runner-wrapper• L18-28 services/…/job_pool.go• L614-738

Runner job pool

The runner job pool is the runner-side control loop. It keeps a short queue of accepted jobs, starts each job, tracks jobs that are in progress, polls the server for more work, and sends progress at regular intervals.

The runner uses two server-facing exchanges:

  • A job check returns new jobs and the current server view of assigned jobs.
  • A progress report sends status and new log records, then receives acknowledgements and any jobs that must stop.

The runner includes its process start time in these requests. A changed start time tells the server that the runner restarted and may have lost its in-memory jobs. The pool uses one shared authenticated HTTP client for these exchanges. Connection, handshake, response-header, and response-body waits have a configured limit, so a silent server cannot stop the poll loop and its heartbeat. Progress uploads can continue while logs are sent.

The pool accepts a job only once. It skips jobs already queued or running, then places new work in its queue. It also applies server status changes to current jobs and stops jobs that the server no longer accepts. A one-off runner exits after its accepted jobs finish. Pool state is protected while the run loop, poll loop, and job goroutines access it, so progress reporting can run at the same time as queue and job changes.

This diagram shows how a task moves between the server and the runner job pool.

sequenceDiagram
    participant S as Server task API
    participant P as Runner job pool
    participant E as Runner executor

    P->>S: Check for assigned jobs
    S-->>P: New job data and current statuses
    P->>P: Queue job and create running-job record
    P->>E: Run task
    loop While task is active
        P->>S: Send status and log progress
        S-->>P: Acknowledge logs or request termination
    end
    E-->>P: Return terminal result
    P->>S: Send terminal status and result data
    S-->>P: Accept result or identify stale job

References: services/…/job_pool.go• L31-107 services/…/job_pool.go• L110-158 services/…/job_pool.go• L349-479 services/…/job_pool.go• L481-598 services/…/job_pool.go• L740-895 services/…/job_pool_test.go• L245-338 services/…/job_pool_test.go• L514-688

Executor factory

The executor factory turns runner configuration into a long-lived executor provider and turns each received job into one executor. The provider holds strategy-wide resources, while the executor represents one task.

The configured strategy can be:

  • Local execution on the runner host.
  • Kubernetes-backed execution.
  • Docker-backed execution.

An empty executor setting uses local execution. An unknown setting prevents the runner from accepting jobs until its configuration is corrected. Before the per-task executor is created, the runner wires the access keys sent with the task into the repository, host, inventory, and vault data.

The factory keeps the job pool independent of the execution strategy. A new strategy can provide the same task lifecycle without changing server dispatch or runner progress handling.

References: services/…/executor_factory.go• L14-110 services/…/executor.go• L8-83

Running-job tracking

Each accepted runner job has a running-job record between queue removal and server acknowledgement of its terminal status. The record is the shared point for execution status, task logs, commit information, host statistics, and workflow outputs.

The record supports concurrent task execution and progress reporting. It snapshots progress for a report, keeps unacknowledged log records for the next report, and sends captured result data with the matching final status. It removes the job from the pool after the server accepts a terminal status. If the server says that a job is already finished or no longer belongs to the runner, the runner kills the local execution and removes the record.

Workflow outputs travel with the success status. The server validates them before it persists success so downstream workflow steps do not see a successful task with missing or invalid output data. Host statistics travel with the final status when the executor provides them. Executors that do not capture workflow outputs report that limit in the task log.

References: services/…/running_job.go• L18-46 services/…/running_job.go• L127-203 services/…/job_pool.go• L161-187 services/…/job_pool.go• L546-597 services/…/types.go• L49-81 api/…/runners.go• L447-520

Remote task dispatch

The server represents runner-backed work as an asynchronous remote job. The task pool first selects a runner from project and global runners, then filters by active state, registration, tag or default scope, online status, and parallel capacity.

When a runner is selected, the server may send a start webhook. It then assigns the runner ID to the task and returns control to the task pool. The runner later asks for the task data. The server prepares the payload at that point, including decrypted task access keys and optional task JWT data, so the runner receives the complete execution context.

MCP server tokens are part of this task context. The server sends each token as a decrypted access key for that job instead of placing the token in the serialized inventory or in the shared access-key map for the whole poll response. This keeps a token paired with the task's saved MCP server URL, even when two queued tasks use the same access-key ID after a server edit. If the token is not decrypted, the server fails that dispatch rather than send a job with incomplete access.

The server does not wait for the remote process in the dispatch call. A progress request can arrive at any server node, which updates task state and starts finalization when the reported status is terminal. This lets task completion continue even if the node that made the assignment stops.

References: services/…/RemoteJob.go• L112-158 services/…/RemoteJob.go• L204-283 api/…/runners.go• L96-180 api/…/runners.go• L184-283 api/…/runners.go• L386-424 api/…/runners_mcp_test.go• L24-124

Remote task finalization

Remote finalization converts a runner result into the normal server-side task completion flow. It persists the terminal task state, records the end reason and actor, runs the finish webhook when available, starts autorun children, and releases task-pool state.

Several signals can request finalization:

  • A runner reports a terminal status.
  • A remote task reaches its server-side time limit.
  • A user force-stops the task.
  • Reconciliation decides that the runner is lost.

The state store allows only one finalizer for a task. In high-availability mode, the finalizer also checks the database before it performs completion work. These guards prevent duplicate finish actions when a timeout, runner report, and another server node race.

References: services/…/TaskPool.go• L539-603 services/…/TaskPool.go• L799-817 services/…/RemoteJob.go• L286-312 api/…/runners.go• L567-574

Runner reconciliation

Runner reconciliation repairs tasks when the server view of a runner no longer matches the runner's ability to execute work. It runs over the tasks owned by the current server node and uses runner heartbeat and process start data to choose a safe action.

ConditionServer action
A starting or waiting task has an offline or deleted runnerClear the assignment and return the task to the queue
A running task exceeds the runner recovery windowMark the task failed and finalize it as runner-lost
A running task belongs to a runner that restarted after the task startedFail it at once because the runner lost its in-memory job
A starting task belongs to a restarted runnerKeep it eligible for the runner to pull again
A task has no runner after a server restart and no live dispatch existsReturn it to the queue

Reconciliation does not treat a short loss of heartbeat as proof that a running process stopped. The separate offline and task-failure windows give a runner time to reconnect before the server fails work. In high-availability mode, a cluster-wide finalize lock and database checks make requeue and failure decisions idempotent.

References: services/…/runner_reconciler.go• L16-101 services/…/runner_reconciler.go• L104-189 services/…/runner_reconciler.go• L216-375 api/…/runners.go• L103-149

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the runner request and response contracts aligned across registration, job checks, progress reports, termination replies, and optional workflow outputs or host statistics.
  • Keep runner process start tracking aligned with reconciliation rules. A restart must fail running jobs but allow starting jobs to be pulled again.
  • Keep assignment clearing and queue insertion atomic from the task pool's point of view. A stale runner must not pull a task after it is reassigned.
  • Keep remote finalization safe for reports, timeouts, force stops, and high-availability races.
  • Keep executor providers consistent with access-key hydration so every execution strategy receives the same task context.
  • Keep per-job MCP token keys separate from the shared response keys. A task's token must stay paired with the URL snapshot in that task.
  • Keep auto-registration token sources and token persistence aligned with the runner wrapper and both runner start modes.
  • Keep request deadlines and shared-client behavior intact. A stuck server must not stop runner heartbeats or create a new connection on every poll.

Important failure modes

TriggerConsequenceGuard
Runner stops polling before a starting task beginsThe task can remain assigned to a runner that cannot pull itReconciliation requeues starting and waiting tasks after the offline window
Runner restarts while a task is runningThe runner's in-memory job record is goneThe changed process start time makes reconciliation fail the running task
A stale runner reports after reassignmentTwo runners could execute the same taskThe server rejects the old assignment and tells the old runner to stop
A terminal report races with timeout or force stopDuplicate finish actions or conflicting task stateFinalization locks and database re-checks allow one completion path
Executor configuration is invalidThe runner starts but cannot construct task executorsProvider creation fails at startup and dispatch rejects jobs cleanly
An MCP token is not decrypted before dispatchThe runner would lack the credential required by the task's MCP serverThe server fails dispatch instead of sending an incomplete job
MCP tokens from queued tasks share one response-level key mapOne task could receive another task's token for its saved MCP URLThe server keeps MCP tokens in each job's own access-key map
A runner reports invalid workflow outputsDownstream workflow steps could consume bad dataThe server validates outputs before accepting task success
The server accepts a connection but stops sending a responseThe poll loop can stop sending heartbeats and the runner can appear offlineTransport and response-body deadlines bound server waits
Registration returns no runner tokenThe runner cannot authenticate future requestsRegistration rejects the response before saving it

Key files

FileWhy to read it
services/…/RemoteJob.goShows runner selection, asynchronous dispatch, webhooks, and remote timeout behavior
api/…/runners.goShows the server-side runner authentication, poll, progress, task payload, access-key, and termination boundary
services/…/job_pool.goShows the runner-side queue, polling, progress, registration, and termination loop
services/…/executor_factory.goShows how runner configuration selects per-task execution strategies
services/…/executor.goDefines the provider and executor contracts shared by runner strategies
services/…/running_job.goShows the in-memory status, log, commit, host, and output record for one running job
services/…/runner_reconciler.goShows recovery rules for offline, restarted, deleted, and lost runners
services/…/TaskPool.goShows remote finalization and release of server-side task state
services/…/types.goDefines the job, progress, state, result, and registration data exchanged across the runner boundary
services/…/job_pool_test.goSummarizes the pool guarantees for concurrency, connection reuse, deadlines, outputs, and token persistence
services/…/running_job_test.goSummarizes running-job guarantees for status, logs, progress snapshots, outputs, and concurrent access
api/…/runners_mcp_test.goShows that MCP tokens stay private to each dispatched job and fail dispatch when missing
cli/…/runner_register.goShows the supported registration token sources and runner registration options
cli/…/runner_start.goShows startup and retry behavior for automatic registration
deployment/docker/runner/runner-wrapperShows how the Docker runner selects normal or auto-registration startup