Sub topic 3
Waiting for a runner versus running
This chapter assumes you can read a workflow run page in the Actions UI and run gh commands in a shell. You should also know that each job in a workflow runs on a separate runner, which is the machine that executes it.
Two clocks for every job
A job's wall-clock time has two parts:
- Queue time is how long the job waits for a runner to pick it up.
- Execution time is how long the job runs on that runner, from its first step to its last.
Queue time is idle waiting, and none of your code runs during it. Execution time is the work your steps do. The two have different causes and different fixes, so measure them separately before changing anything.
Spotting queue time in the UI
Open a run and click a job. While the job is waiting, the log pane says it is waiting for a runner to pick up the job. After the job starts, the duration shown next to it covers only execution. Two consequences follow:
- A job that shows "12s" may still have waited several minutes before it started.
- The total run duration can be much longer than the sum of the job durations on its longest chain, and queue time is often the gap.
If a run feels slower than its job durations suggest, look for queue time.
Measuring it with the REST API
The UI does not show the wait as a number. The REST API does, because each job records when it was created and when it started.
gh api repos/{owner}/{repo}/actions/runs/1234567890/jobs --jq '.jobs[] | {name, queued_seconds: ((.started_at | fromdateiso8601) - (.created_at | fromdateiso8601))}'Replace 1234567890 with a run ID. The command prints one object per job. queued_seconds is the difference between created_at and started_at. Inside a gh repository checkout, {owner} and {repo} are filled in for you.
Compare this with the execution time you get from the gh run view command in the earlier chapter. A job with queued_seconds of 2 and an execution time of 240 seconds is slow because of its steps. A job with queued_seconds of 180 and an execution time of 20 seconds is slow because of the wait.
Why jobs wait
Common causes of long queues:
- Not enough self-hosted runners. If your jobs target
self-hostedrunners and all of them are busy, new jobs wait until one frees up. - Concurrency limits. GitHub-hosted runners have a cap on simultaneous jobs for your plan. Jobs beyond the cap queue.
- Large fan-out. A matrix with 20 entries creates 20 jobs at once. They can compete with each other and with other workflows in the same repository or organization.
- Scarce runner labels. A job that needs a rare label, such as a specific larger runner, can wait longer than one on
ubuntu-latest. - Peak hours. Queue time can vary with the time of day even when your workflow has not changed.
Fixes that target queue time
Match the fix to the cause:
| Cause | Possible fix |
|---|---|
| Too few self-hosted runners | Add runners or autoscale them |
| Plan concurrency cap | Reduce simultaneous jobs, or move to a plan or larger runner pool with more capacity |
| Large matrix | Use fewer, longer shards |
| Rare label | Use a more common runner label, if the job allows it |
Notice the trade-off with the sharding chapter that follows. Splitting tests into more parallel jobs shortens execution time, but each extra job adds a possible wait. Past a certain point, more shards make the run slower.
Fixes that do not help
Caching, faster tests, and a leaner npm ci all reduce execution time. They do nothing for a job that sits in a queue. If queued_seconds is the large number, optimizing steps wastes effort.
A quick triage routine
- Pick a slow run and note its total duration.
- Run the REST command above and list each job's
queued_seconds. - Run the
gh run viewcommand for execution seconds. - For each job, decide which number is larger.
- Send step-bound jobs to the caching and parallelism chapters. Send wait-bound jobs to runner choice.
Exercise
A job's page in the Actions UI shows a duration of 30 seconds, but the run took 5 minutes to reach that job's completion, and the job has no dependencies. What do you check next, and which command gives you the number?
Show answer
Check queue time. The UI duration covers only execution, so most of the 5 minutes was probably spent waiting for a runner. Run gh api repos/{owner}/{repo}/actions/runs/<run-id>/jobs with a --jq filter that subtracts created_at from started_at. A large queued_seconds confirms it, and the fix is on the runner side (more capacity, fewer simultaneous jobs, or a different runner label) rather than in the job's steps.