Skip to content

Run a node as several worker processes, each its own runner - #92

Open
benjaminwood wants to merge 1 commit into
mainfrom
feat/workers
Open

benjaminwood wants to merge 1 commit into
mainfrom
feat/workers

Conversation

@benjaminwood

@benjaminwood benjaminwood commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Runs a CI node as several worker processes, each connecting to Selective as its own runner (<runner id>-w<index>).

How it works

  • Count: --workers N or SELECTIVE_WORKERS (auto = one per CPU). Without either, the framework adapter's setting applies (Rails parallelize, minitest-parallel_fork). SELECTIVE_WORKERS=1 turns it off.
  • Fresh processes, not forks. The command's own process is worker 0. Once its suite has loaded, it spawns the others with Process.spawn, using the environment it captured before the app booted. Each boots the app itself with parallel_tests' TEST_ENV_NUMBER ("", "2", …; PARALLEL_TEST_FIRST_IS_1 honored) and PARALLEL_TEST_GROUPS already set. Config that reads them at boot (ports, Redis dbs, cache dirs) works as under parallel_tests.
  • Rails databases. A database.yml that names databases by TEST_ENV_NUMBER is used as is, and worker 0 creates the others' before they boot. Otherwise each worker gets <name>_<index>, as with parallelize. Both load from the schema. parallelize_setup/teardown run in each worker, RSpec included. When Minitest loads rails/test_help, Rails' own hook does this.
  • Waiting and exit. Worker 0 waits for the others before its own teardown and after_run, so SimpleCov merges every worker under a shared SIMPLECOV_RUN_ID. The node exits with the worst status and names any worker that failed, was killed, or was lost.
  • Signals. INT/TERM are forwarded. A worker whose worker 0 dies stops itself.
  • Outer tools. parallel_tests/turbo_tests processes get -p<TEST_ENV_NUMBER> runner ids. A process handed a slice of the suite exits 1 and points at --workers.

Worker 0 boots before starting the others because the parallelize count is only known after load, per-worker ERB databases must exist before the others boot, and it avoids racing maintain_test_schema! on a shared database. The cost is about one app boot.

Companion PRs

The adapters guard with respond_to?, so each can ship before or after this one. This should be released as 0.4.0.

Testing

  • bundle exec rspec: 143 examples, 0 failures. Workers specs spawn a stand-in script to check env, hooks, worst status, killed workers and signal forwarding.
  • Lab apps against a local server:
    • Rails 8.1/7.2 on Minitest 5 and 6, plus RSpec, on Postgres (plain and ERB database.yml) and SQLite.
    • minitest-parallel_fork and plain Minitest.
    • SimpleCov merges N/N.
    • A load error in one worker.
    • Chaos: killing a worker reassigns its tests. Killing worker 0 (KILL/TERM/INT) stops the rest, leaves no orphans, and records the run as cancelled. A transport kill still completes. 2 nodes × 3 workers completes.
  • ruby-demo load and chaos with SELECTIVE_WORKERS=3: 200/200, all 12 runners took work.

Also fixes a race in stdio_binary_spec: the transport can exit, and its /proc/<pid>/cmdline disappear, between the existence check and the read. It failed every Ruby 4.0.7 run of this PR.

Not tested: JRuby and Windows (Process.spawn should work, but unverified).

🤖 Generated with Claude Code

https://claude.ai/code/session_01LGDurRE6Gen6LaFLhpq7Rf

--workers N, SELECTIVE_WORKERS, or the suite's own parallelism (Rails
parallelize, minitest-parallel_fork) runs N workers on the node. Each is
a fresh selective process that boots the app with TEST_ENV_NUMBER and
PARALLEL_TEST_GROUPS set as parallel_tests sets them, and connects as
<runner id>-w<index>.

The command's own process is worker 0: once its suite has loaded it
starts the others from an environment snapshot taken before boot,
forwards INT/TERM, waits for them before its own teardown and report so
coverage merging sees every worker, and exits with the worst status.
Workers stop if worker 0 dies.

Rails apps get a database per worker: a database.yml that names them by
TEST_ENV_NUMBER is used as is, with worker 0 creating the others' first;
otherwise <name>_<index>, as Rails parallelize does. parallelize hooks
run in each worker.

parallel_tests/turbo_tests processes get distinct runner ids, and a
process handed a slice of the suite is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LGDurRE6Gen6LaFLhpq7Rf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant