Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .quality/baseline.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"measuredOn": "main 08061db, 2026-11-21, with the commands in .quality/README.md; the full mutation run went one file at a time with .quality/mutant-tests.sh and 2 workers",
"coverage": { "lines": 87.88, "metric": "statement coverage of cmd/ and internal/ without tests, as go tool cover -func totals it" },
"coverage": { "lines": 89.19, "metric": "statement coverage of cmd/ and internal/ without tests, as go tool cover -func totals it" },
"functionsMeasured": 59,
"functionsOverCrap10": 10,
"functionsOverCrap10List": [
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ the major version is 0, a minor release may change configuration (each such chan
- The GitHub CLI (`gh` 2.102.0, pinned by version and sha256) in every image variant, so a job can open or comment on issues without a GitHub-hosted runner. Nothing is logged in: a job passes `GH_TOKEN`.

### Fixed
- A queued job whose run GitHub's `runs?status=queued` list leaves out gets a runner. GitHub left such a run out for 25 minutes on 2026-11-21 and its job never got a runner. Each cycle now also reads the 20 newest runs of each repository without the status filter, and serves the queued jobs of any run that is not completed and was not already listed. `/healthz` reports `unlisted_jobs`, the `cycle` log line carries `unlisted`, and each such job is logged at warn. If the extra read fails (a 5xx, or a 403 at the rate limit), the jobs from the filtered lists are still served: the failure is logged at warn with the repository and status, counted as `unlisted_read_failures` on `/healthz` and `unlisted_read_failed` on the `cycle` line, and does not fail the scope. On an idle repository the extra read is an unchanged `304`, which does not count against the rate limit.
- A scope in `RUNNER_SCOPES` that GitHub answers 404 for (a deleted repository, for example) no longer fails every cycle and turns `/healthz` 503: it is counted in `unreadable_scopes`, logged at warn once a cycle and left out of the failing alert, while the other scopes keep getting runners. After `ALERT_FAILED_CYCLES` cycles in a row such a scope raises one alert of its own, and one more when it reads again. When every scope answers 404 (the App uninstalled, for example), the cycle still fails. In an org scope, a repository or run that vanishes mid-cycle is skipped instead of hiding the whole org. Other GitHub errors still fail the cycle.
- `/healthz` answers at once while a cycle runs, with the previous cycle's status; it waited for the cycle to end (about a minute with many repositories) and health checks timed out.
- Jobs queued in a run that GitHub reports as `pending` (for example while it waits on a concurrency group) are served; the controller listed only `queued` and `in_progress` runs and left such jobs waiting forever.
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ RUNNER_LABEL=railway
MAX_RUNNERS=2
```

`/healthz` answers 200 once a cycle has completed, and 503 when the last cycle is older than three polls or `ALERT_FAILED_CYCLES` cycles in a row have failed. Its JSON: `last_cycle`, `last_success`, `consecutive_failures`, `unreadable_scopes`, `services`, `queued`, `error`. `/healthz` is public, so `unreadable_scopes` is only how many scopes the last cycle could not read; their names are in the controller's log lines and in the failing alert, which only the operator sees. A scope GitHub answers 404 for (a deleted repository, a typo in `RUNNER_SCOPES`, or an App no longer installed there) is counted in `unreadable_scopes` and logged at warn each cycle, but does not fail the cycle; after `ALERT_FAILED_CYCLES` cycles in a row it raises one alert of its own, and one more when it reads again. When every scope answers 404 (the App uninstalled, for example) the cycle fails, since nothing can be served. Any other read error fails the cycle too. `github_rate_limits` shows the requests GitHub last reported left per installation account (a warning is logged once per window below a tenth). The log line `controller up` shows the scopes,
`/healthz` answers 200 once a cycle has completed, and 503 when the last cycle is older than three polls or `ALERT_FAILED_CYCLES` cycles in a row have failed. Its JSON: `last_cycle`, `last_success`, `consecutive_failures`, `unreadable_scopes`, `services`, `queued`, `unlisted_jobs`, `unlisted_read_failures`, `error`. `unlisted_jobs` counts the queued jobs the last cycle found only through the unfiltered run list, because GitHub's `status=` filter left their run out; each one is also logged at warn with its repository and job id, and the `cycle` line carries `unlisted`. `unlisted_read_failures` counts the repositories whose unfiltered run list the last cycle could not read (a 5xx, or a 403 at the rate limit); those repositories are still served from the filtered lists, each failure is logged at warn with its repository and status, and the `cycle` line carries `unlisted_read_failed`. `/healthz` is public, so `unreadable_scopes` is only how many scopes the last cycle could not read; their names are in the controller's log lines and in the failing alert, which only the operator sees. A scope GitHub answers 404 for (a deleted repository, a typo in `RUNNER_SCOPES`, or an App no longer installed there) is counted in `unreadable_scopes` and logged at warn each cycle, but does not fail the cycle; after `ALERT_FAILED_CYCLES` cycles in a row it raises one alert of its own, and one more when it reads again. When every scope answers 404 (the App uninstalled, for example) the cycle fails, since nothing can be served. Any other read error fails the cycle too. `github_rate_limits` shows the requests GitHub last reported left per installation account (a warning is logged once per window below a tenth). The log line `controller up` shows the scopes,
label, prefix, image and limits it runs with.

### 4. Use it in a workflow
Expand Down Expand Up @@ -264,7 +264,9 @@ default images stay sudo-free.
roughly 230 jobs an hour at the default poll; two controllers sharing one token share that budget.
* **GitHub API:** 5,000 requests an hour per installation, shared by every scope of that installation
(one access token per installation). Run and job lists are read with `If-None-Match`, and an
unchanged list (`304`) does not count against the limit. Prefer `repo:` scopes for large organizations.
unchanged list (`304`) does not count against the limit. Prefer `repo:` scopes for large organizations:
the cache of unchanged lists holds 2,000 entries, four run lists per repository plus the jobs lists,
so past about 500 repositories every read counts.
* **Cold start** is the Railway deploy plus the image pull: about 20 s when the host has the image,
longer on a host's first pull of a large image (see `docs/spike.md`).

Expand Down Expand Up @@ -296,6 +298,7 @@ pin a version (or a digest, `image@sha256:...`) so a runner never changes under
| `/healthz` 503 | no cycle has completed yet, the last one is older than three polls, or cycles keep failing (`consecutive_failures`, `unreadable_scopes`); the `cycle failed` log lines say why and name the scopes |
| `/healthz` 200 but `unreadable_scopes` above 0 | GitHub answers 404 for a scope (the warn line below names it): the repository was deleted, the name in `RUNNER_SCOPES` is wrong, or the App is no longer installed there. See the `scope not found on GitHub; skipped` warn line and the `not found on GitHub for N cycles in a row` alert, then fix or remove the scope. If every scope answers 404, the cycles fail and `/healthz` goes 503: check the App is still installed. A renamed or transferred repository answers a redirect instead, which fails the cycle until `RUNNER_SCOPES` has the new name |
| `/healthz` `"error": true` | the last cycle failed; the `cycle failed` log line says why (often a GitHub or Railway API error, which the next cycle retries) |
| A job stays queued, and its run is missing from `GET /repos/{repo}/actions/runs?status=queued` | GitHub's status filter sometimes leaves a queued run out. Each cycle also reads the 20 newest runs without the filter and serves their queued jobs (`unlisted_jobs` on `/healthz`, a warn line per job). A run pushed out of the 20 newest is never found that way: cancel it and re-run it |
| A job stays queued | the job's labels must all be among `self-hosted`, `linux`, `x64` and your label; its repository must be in `RUNNER_SCOPES` and the App installed on it; `MAX_RUNNERS` may be reached |
| How often cycles run, and what they cost | every cycle logs one `cycle` line: `duration`, `github_calls`, `railway_calls`, `created`, `reaped`, `failed` |
| Runner services pile up | each should go about a minute after its job ends ("runner gone" in the log); check the Railway token can delete services |
Expand Down
10 changes: 8 additions & 2 deletions docs/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,14 @@ touched (tested in `internal/railway`). Still:
calls (create, configure, deploy, delete) and every cycle one list call; controllers that share a
token share its budget. At 1,000 an hour and the default poll that is roughly 230 jobs an hour.
* **GitHub API**: 5,000 requests an hour per installation (more above 20 repositories), shared by
every scope of that installation. A cycle reads, per repository, two run lists, the jobs of each
active run and the runner list; unchanged lists come back as `304` and do not count. `/healthz`
every scope of that installation. A cycle reads, per repository, four run lists (`queued`,
`pending`, `in_progress`, and the 20 newest runs unfiltered, since GitHub's status filter can leave
a queued run out), the jobs of each active run and the runner list; unchanged lists come back as
`304` and do not count. A repository with recent activity changes its unfiltered page, so that read
counts once a cycle. When that unfiltered read fails (a 5xx, or a 403 at the rate limit), the
repository is still served from its filtered lists for that cycle, and the failure is logged and
counted rather than failing the scope. The cache of unchanged lists holds 2,000 entries, so past about 500
repositories in one controller every read counts; prefer `repo:` scopes there. `/healthz`
shows `github_rate_limits`, and a warning is logged below a tenth.
* **Railway per-service limits** (Railway docs, plans, read 2026-10-17): up to 48 vCPU and 48 GB per
service on Hobby, 1,000 vCPU and 1 TB on Pro, 100 GB ephemeral storage, image size 100 GB on Hobby.
Expand Down
66 changes: 55 additions & 11 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ type Status struct {
ConsecutiveFailures int
LastSuccess time.Time
UnreadableScopes int // how many scopes the last cycle could not read; /healthz is public, so no names
UnlistedJobs int // queued jobs the last cycle found only because GitHub's status filter left their run out
UnlistedReadFailed int // repositories whose unfiltered run list the last cycle could not read; their listed jobs were still served
Failing bool // ConsecutiveFailures reached FailAlertAfter
}

Expand Down Expand Up @@ -213,16 +215,19 @@ func (c *Controller) Cycle(ctx context.Context) (err error) {
now := c.now()
began, ghBefore, rwBefore := time.Now(), calls(c.gh), calls(c.rw)
created, reaped := 0, 0
var gap unlistedGap // set once the scopes are read; the log line below reads it when the cycle ends
defer func() {
c.cfg.Log.Info("cycle", "duration", time.Since(began).Round(time.Millisecond).String(), "github_calls", calls(c.gh)-ghBefore,
"railway_calls", calls(c.rw)-rwBefore, "created", created, "reaped", reaped, "failed", err != nil)
"railway_calls", calls(c.rw)-rwBefore, "created", created, "reaped", reaped, "unlisted", gap.jobs,
"unlisted_read_failed", gap.readFailed, "failed", err != nil)
}()
rd := c.readScopes(ctx)
gap = unlistedGap{jobs: c.warnUnlisted(rd.jobs), readFailed: rd.unlistedReadFailed}
errs, queued, runners, jobsOf, readable := rd.errs, rd.queued, rd.runners, rd.jobs, rd.readable
all, err := c.rw.List(ctx)
if err != nil {
errs = append(errs, err)
c.record(now, errs, 0, 0)
c.record(now, errs, 0, 0, gap)
return errors.Join(errs...)
}
ok := map[string]bool{}
Expand Down Expand Up @@ -269,7 +274,7 @@ func (c *Controller) Cycle(ctx context.Context) (err error) {
for _, n := range queued {
total += n
}
c.record(now, errs, len(services)+len(plan.Create)-len(plan.Reap), total)
c.record(now, errs, len(services)+len(plan.Create)-len(plan.Reap), total, gap)
return errors.Join(errs...)
}

Expand All @@ -280,13 +285,48 @@ type scopeReads struct {
runners map[string][]reconcile.Runner
jobs map[string][]ghapp.Job
errs []error // the errors that fail the cycle

unlistedReadFailed int // repositories whose fallback read failed; it does not fail the cycle
}

// unlistedGap is how the last cycle saw GitHub's status-filter gap: the jobs found only through
// the unfiltered run list, and the repositories whose unfiltered list could not be read.
type unlistedGap struct{ jobs, readFailed int }

// warnUnlisted logs each queued job found only because GitHub's status filter left its run out, so
// the gap on GitHub's side stays visible, and returns how many there were.
func (c *Controller) warnUnlisted(jobs map[string][]ghapp.Job) int {
n := 0
for _, js := range jobs {
for _, j := range js {
if j.Unlisted {
n++
c.cfg.Log.Warn("queued job missing from GitHub's status-filtered run list", "job", j.ID, "repo", j.Repo)
}
}
}
return n
}

// warnUnlistedRead logs each repository whose fallback read failed (repository and status only) and
// returns how many there were. The scope's listed jobs are still served: the fallback only adds jobs.
func (c *Controller) warnUnlistedRead(e *ghapp.UnlistedReadError) int {
for _, f := range e.Failed {
c.cfg.Log.Warn("unfiltered run list unreadable; serving the status-filtered jobs only", "repo", f.Repo, "status", f.Status)
}
return len(e.Failed)
}

// readScopes reads every scope, records how each went, and raises or clears the missing-scope alerts.
func (c *Controller) readScopes(ctx context.Context) scopeReads {
rd := scopeReads{queued: map[string]int{}, runners: map[string][]reconcile.Runner{}, jobs: map[string][]ghapp.Job{}}
for _, s := range c.cfg.Scopes {
jobs, rs, err := c.readScope(ctx, s)
var ue *ghapp.UnlistedReadError
if errors.As(err, &ue) {
rd.unlistedReadFailed += c.warnUnlistedRead(ue)
err = nil
}
if fail := c.noteRead(s.Key(), err); fail != nil {
rd.errs = append(rd.errs, fmt.Errorf("%s: %w", s.Key(), fail))
}
Expand Down Expand Up @@ -363,24 +403,26 @@ func (c *Controller) noteRead(key string, err error) error {
}
}

// readScope reads the scope's queued jobs and its runners.
// readScope reads the scope's queued jobs and its runners. When only the fallback read failed
// (an *ghapp.UnlistedReadError), the jobs and runners come with that error.
func (c *Controller) readScope(ctx context.Context, s reconcile.Scope) ([]ghapp.Job, []reconcile.Runner, error) {
jobs, err := c.gh.QueuedJobs(ctx, s)
if err != nil {
return nil, nil, err
jobs, jobsErr := c.gh.QueuedJobs(ctx, s)
var ue *ghapp.UnlistedReadError
if jobsErr != nil && !errors.As(jobsErr, &ue) {
return nil, nil, jobsErr
}
rs, err := c.gh.Runners(ctx, s)
if err != nil {
return nil, nil, err
}
return jobs, rs, nil
return jobs, rs, jobsErr
}

// record keeps the cycle's outcome for /healthz and raises the failing and recovered alerts: one
// alert when FailAlertAfter cycles in a row have failed, naming the scopes unreadable for that long,
// and one message when a cycle succeeds again. A failed cycle still stamps LastCycle (the loop is
// alive); ConsecutiveFailures is what tells a failing controller from a working one.
func (c *Controller) record(now time.Time, errs []error, services, queued int) {
func (c *Controller) record(now time.Time, errs []error, services, queued int, gap unlistedGap) {
if len(errs) > 0 {
c.failures++
} else {
Expand All @@ -389,7 +431,8 @@ func (c *Controller) record(now time.Time, errs []error, services, queued int) {
}
unreadable, stuck := c.unreadableScopes()
st := &Status{LastCycle: now, Services: services, Queued: queued, ConsecutiveFailures: c.failures,
LastSuccess: c.lastSuccess, UnreadableScopes: unreadable, Failing: c.failures >= c.cfg.FailAlertAfter}
LastSuccess: c.lastSuccess, UnreadableScopes: unreadable, UnlistedJobs: gap.jobs,
UnlistedReadFailed: gap.readFailed, Failing: c.failures >= c.cfg.FailAlertAfter}
if len(errs) > 0 {
st.LastError = errors.Join(errs...).Error()
}
Expand Down Expand Up @@ -520,7 +563,8 @@ func (c *Controller) HealthHandler() http.Handler {
}
body := map[string]any{"last_cycle": st.LastCycle, "services": st.Services, "queued": st.Queued,
"error": st.LastError != "", "consecutive_failures": st.ConsecutiveFailures, "last_success": lastSuccess,
"unreadable_scopes": st.UnreadableScopes}
"unreadable_scopes": st.UnreadableScopes, "unlisted_jobs": st.UnlistedJobs,
"unlisted_read_failures": st.UnlistedReadFailed}
if rl, ok := c.gh.(RateLimiter); ok {
body["github_rate_limits"] = rl.RateLimits()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *countedRW) Calls() int64 { return r.n }
// Every cycle logs one summary line: how long it took, the API calls it made to GitHub and
// Railway, and what it created and reaped, so the cycle rate and its cost can be read from logs.
func TestEveryCycleLogsOneSummaryLine(t *testing.T) {
gh := &countedGH{fakeGH: fakeGH{jobs: map[string][]ghapp.Job{repo.Key(): {{ID: 1, Repo: "acme/app", CreatedAt: now}}}}}
gh := &countedGH{fakeGH: fakeGH{jobs: map[string][]ghapp.Job{repo.Key(): {{ID: 1, Repo: "acme/app", CreatedAt: now}}}}, n: 100}
rw := &countedRW{n: 10}
var buf bytes.Buffer
c := newC(&gh.fakeGH, &rw.fakeRW)
Expand Down
Loading
Loading