Skip to content
13 min read

A shared runner pool for many forges

Runner autoscaling describes the pool that serves one forge. This page is about the harder shape: one pool of short-lived machines that serves many customers’ forges, where a mistake is not only expensive but can put one customer’s job next to another’s. Fjord’s scheduler for that shape is called flux. The rules here apply to any shared pool, whatever you build it with.

It is a design and operations page, not a plan page. Which plans include shared CI, and how much, is on Runners, and it can change without this page changing.

Note

A shared pool is a different promise from a dedicated one. If you run one forge, you do not need most of this, and the dedicated pattern is simpler and safer. Reach for a shared pool only when you are serving tenants who should not know each other exist.

The loop, end to end

Every job goes through the same stages, and each stage is a place to stop:

sense -> admit -> reserve -> create -> register -> run -> complete -> destroy
StageWhat happensWhat it must never do
SenseFind queued jobs on each forge.Act on a job it saw twice as two jobs.
AdmitDecide whether this job may use the pool at all (policy, quota, queue limit).Leave a refused job silently queued.
ReserveAsk the billing control plane for a reservation before any machine exists.Spend first and account later.
CreateBoot one machine for that one job, with its network restrictions attached.Create a machine it cannot later find.
RegisterThe machine registers an ephemeral runner into the customer’s forge.Hand a standing credential to the machine.
Run, completeThe runner takes the pinned job and exits.Take any other job.
DestroyThe machine is removed, and the reservation is committed or cancelled exactly once.Bill twice, or leave a machine billing.

The pool rests at zero. A quiet pool has no machines, and any machine older than the job time limit plus the reap interval is a defect, never a buffer.

Sensing: poll, because there is nothing to push

Forgejo emits Actions webhooks only when a run has finished (the action_run_* events, from Forgejo 12). There is no “job queued” event to subscribe to, unlike some other forges. So the pool polls each forge’s waiting-jobs API on a short, bounded interval, and treats that poll as the trigger, not as a backup.

  • The interval is the latency knob. Queue-to-start is the poll interval plus boot time. Shorter is faster and costs more API load on every customer’s forge; add jitter and a per-forge lease so thousands of forges do not get polled in lockstep.
  • Webhooks are an optimisation. Use a finished-run event to notice completion and tear down sooner, but never let it gate admission. The pool must stay correct with every webhook lost.

One job attempt, one identity

The same job is observed more than once, out of order, by more than one worker. The defence is a stable identity for an attempt: the forge, repository, run, job and run attempt together. The attempt number is not optional, because a re-run reuses every other id.

Everything else follows from that key:

  • Re-runs are distinct attempts, not duplicates of the first.
  • A duplicate observation is a no-op, and a late one cannot move a job backwards.
  • Committing a reservation twice, or destroying a machine twice, changes nothing the second time. That is what makes it safe to run more than one scheduler.

Coordinating several workers with advisory locks only works if the lock holder keeps one database session. See Leader-only background workers for the pooler trap that silently gives you two active schedulers.

Reserve before you spend

The scheduler never creates a machine until the billing control plane has granted a reservation for that job. Then:

  • A refused or unreachable control plane means no machine. Default-deny.
  • A reservation that is open too long, or close to expiry, is a stranded job, and is an alert on its own.
  • The reservation is the join key between the machine, the job and the bill.

Refusals carry a closed set of reasons, such as quota exhausted, policy blocked or queue full, and each is reported as an explicit status. A job that is refused but shown as “waiting” is the worst outcome, because the customer cannot tell a limit from an outage.

Targeting: a label is a hint, not a boundary

Each machine must run exactly the job it was created for. Pinning by runner label alone is not enough, because a label is a routing hint and a tenant may influence or guess it. The pool layers its controls and trusts none of them alone:

  • The label is generated by the scheduler, unguessable, specific to one reservation, and never built from anything in the workflow.
  • The registration token is scoped to that reservation, single use, and expires within minutes.
  • The runner is ephemeral and accepts only the pinned label.
  • A machine that claims anything else is a hard failure: destroy it, cancel the reservation, alert. The path is exercised in a drill, not just written down.

Be precise about what this does and does not do. The forge, not the pool, decides which queued job a registered runner receives, and it does so by matching the labels the job asked for. The runner therefore has to be registered with the labels the sensed job requested, together with the pinned label, or the job is never matched and the machine sits until its registration or time limit. The pinning does not stop a different job from the same forge that asks for the same labels from being matched first. What contains that case is the rest of the stack: the machine lives for one job, and comparing the attempt the runner actually claimed with the attempt that was reserved turns a wrong claim into a failure instead of a silent mix-up. The check is detective, and it should run on every claim, including retries.

The scheduler holds no forge credential

The forge belongs to the customer, who can delete an account or revoke a token at any time. A scheduler that depends on a forge admin token has a dependency it does not control, and a compromise of it reaches every tenant.

So the scheduler holds none. It confirms registration by a callback from the machine: after the runner registers, the machine posts the unguessable label to a receiver the scheduler owns. Teardown is destroying the machine, because the ephemeral runner removes itself after its job.

Know what you gave up. A callback is a claim, not an observation: the machine says it registered, and the scheduler believes the label. The assurance is weaker than reading the forge’s runner list, and it is accepted because the blast radius is one bit for one run. Bind the receiver before creating the machine, so a fast registration is queued rather than lost, and treat a confirmation that never arrives as a failure: tear the machine down.

The safety envelope comes before the first live machine

Bound spend and blast radius in code, before anything is allowed to cost money:

ControlBehaviour
Hard budget ceilingAn absolute spend rate. At the ceiling, creates stop.
Circuit breakerA threshold below the ceiling that opens the breaker and needs a person to re-enable spend.
Live-machine cap, create rate, per-forge concurrencyLayered limits so one tenant or one bad signal cannot take the pool.
Job time limit and reaperAnything past the limit is destroyed, not billed forever.
Flavour allow-list, fail closedOnly the machine types you enabled can start. A new type has zero capacity until someone gives it some.
Network restrictionsA machine without its egress policy verified is not created.

Meter spend in the unit you bill, not the unit your provider invoices, so the guards are sized against what a full fleet should cost. A breaker that fires with a believable fleet means an unexpected machine type or a machine nobody is tracking. Treat that as a finding, not noise.

Keep a brake and drill it. A kill switch is useless if you find it broken during the incident. Make it a ladder of reversible steps: stop admitting work, stop creating machines, drain idle machines, destroy machines running untrusted work. Exercise each step against the running service, and prove the failure modes too: no credential gives 401, an unknown mode gives 400, and a 404 means the endpoint was never wired.

How it fails

None of these should fire in steady state. Each is something that costs money or strands CI.

SignalPage or ticketWhat it meansFirst response
Leaked machinePageA machine carrying your ownership label, not tracked as live, older than the reap window. It bills until removed.Confirm it is yours and not live, let the orphan sweep reclaim it, and if it survives the next sweep, remove it by id and open an incident.
Breaker openPageCreates are refused because spend tripped the threshold.Compare the live spend estimate with the ceiling. A real spike: keep the breaker open, so creates stay refused, find the driver, and close it only once the driver is controlled and spend is verified. A stuck breaker with normal spend: close it.
Unknown forgePageA job for a forge that is not in the active pool, while the pool is not empty.Check whether inventory dropped the forge while its jobs were in flight. The jobs will run to the reaper limit, so drain them on purpose. It is silent from the customer’s side: CI just looks slow.
Stranded reservationTicketA reservation open too long or near expiry.Check whether the job is progressing. With no machine, let it expire. With a machine, treat it as a leak candidate.
Destroy backlogTicket, page if risingMachines are not being removed.Check the provider API. If it is healthy and the backlog persists, restart the reconciler.
Register failuresTicket, page if forge-wideRepeated registration failures for one repository or forge.Check the registration endpoint and token validity without printing the token. A forge-wide failure is a forge or credential outage.

Group repeated firings behind a cooldown of at least one scrape interval, so a condition that persists across many ticks is one alert, not one per tick. Keep alert payloads free of secrets and boot output.

Known money gaps

Name them, bound them, and decide on purpose whether to tolerate them. Each of these under-bills the pool and cannot over-bill a customer:

  • A run cancelled after its machine already ran may bill nothing.
  • If the reaper removes a machine before the completion is committed, the engine may fall back to an advisory zero-minute bill.
  • A crash between creating a machine and recording it durably can create a second machine on resume. The first leaks until the reaper takes it.

At small scale these are cents. If any of them costs more than that, stop and reassess the model, not just the caps.

Ownership is a label, never a name

When state is lost and you have to find the fleet, a machine’s name proves nothing, because a name can be copied. Only a positive ownership label written at creation counts. A machine without it is left alone even if it looks like yours.

Build the recovery tool as a dry run first: list labelled machines, join them against the live job records, and print a plan. It deletes nothing and changes no records. If the live set cannot be read, every machine is inspection only, because “unknown” must never be read as “none are live”. Report orphans with an unparseable or future creation time for a person to decide, since the automatic reapers defer on a timestamp they cannot trust.

When a customer says “my job never starts”

What the customer seesUsual causeHow to tell
A quota or allowance statusThe tenant used its allowance.The job was stopped with an explicit reason. Not stuck.
A policy statusThe trigger or repository is not eligible for the shared pool.The reason names the rule. A supported trigger refused as policy is an escalation.
Jobs wait, while the same forge has jobs runningThe per-forge concurrency limit.Waiting stops as running jobs finish.
Jobs wait, nothing running for that forge, pool looks greenSensing, admission or reservation is stuck.Look for a stranded reservation, and whether the forge is in the active pool.
Many forges slow at oncePool-wide: breaker open, provider API trouble, or a failed boot image.Breaker state, destroy backlog, register failures across forges.
A job starts and the runner never claims itRegistration confirmed late or never.Register failures; the machine is torn down on timeout.

Collect the same evidence every time: forge, repository, workflow, run id, job id, run attempt, trigger type, time and the status text shown. Support explains and gathers; operators own routing, the kill switch, budgets and rollback. Support never creates machines or changes limits.

Verify it is healthy

  • Every completed run bills exactly once. Compare reservation commits with completed jobs, with the reservation id as the join key.
  • The pool returns to zero after every quiet period, and no machine outlives the job limit plus the reap interval.
  • The live spend estimate never exceeds what a full fleet of your enabled machine types should cost.
  • The job table trends to terminal states. A growing pile of non-terminal jobs is a stall.
  • No stray machines carry your label without a live job behind them.
  • You know what is deployed, not only what is merged. A build stamp such as 0.0.0 cannot tell you which code is running. Read the source location of a fresh error event to see whether a fix is live. Merging a fix does not publish or deploy an image. The same rule as in Forgejo patch upgrades you can prove applies: intent is not effect.

Turning it on

Enable the loops in order, one gate at a time, against a single forge you control:

  1. The health surface only, with every loop off. Confirm the safety profile the service logged is the one you intended; an unknown profile name should refuse to start.
  2. Sensing. Waiting jobs appear as observed. No reservations, no machines.
  3. Scheduling. Jobs reach reserved, refusals carry reasons, and spend estimate stays at zero.
  4. The reconciler. This is the first real spend, so it needs an explicit decision on the specific caps. Watch one full job: machine created with its restrictions, runner claims the exact label, job runs, completes, bills once, machine destroyed.

Soak under conservative caps for days, not hours, before widening them. Abort in the order of the ladder above, then verify scale to zero and reconcile every destroyed machine’s reservation as committed once or cancelled.

What this page does not prove

This is a design and operating guide drawn from building and soaking the scheduler, not a certification. The tenant isolation of the machines themselves, including what a fork’s pull request may touch and what a machine can reach on the network, has its own review and is not repeated here. So is the forge-side matching of jobs to runners described above, which this page treats as a property of the forge to verify, not one the pool can guarantee. Treat any number you read elsewhere as deployment specific, and measure your own.

Contributors
  • Stephen Way