Repository navigation
Description
Problem / Goal
ActiveJob.perform_all_later silently escapes batch tracking — and concurrency limits. Pgbus::ActiveJob::Adapter#enqueue_all routes non-unique jobs through enqueue_immediate (lib/pgbus/active_job/adapter.rb:158-172), which serializes payloads directly and calls Pgbus.client.send_batch without running inject_batch_metadata or Concurrency.inject_metadata. Consequences:
- Jobs bulk-enqueued inside a
Pgbus::Batch#enqueueblock are not tagged withpgbus_batch_idand not counted intototal_jobs— the batch can finish and fireon_finish/on_successbefore those jobs ever run. - Jobs whose class declares
pgbus_concurrencybypass semaphore acquisition entirely when enqueued viaperform_all_later— concurrency limits are silently ignored.
Done looks like: bulk-enqueued jobs join the active batch exactly like individually enqueued ones, and concurrency-configured jobs are routed through the individual enqueue path (mirroring how uniqueness-configured jobs already are at adapter.rb:35).
Context (read these first)
lib/pgbus/active_job/adapter.rb—enqueue_all(L33-51) partitions only uniqueness jobs out of the bulk path;enqueue_immediate(L158-172) is the escape hatch;inject_batch_metadata(L150-156) tags payloads and incrementsThread.current[:pgbus_batch_job_count].lib/pgbus/batch.rb—METADATA_KEY,#count_jobs(L66-79) reads the thread-local counter after the block to settotal_jobs.lib/pgbus/active_job/concurrency.rb—inject_metadata, semaphore acquisition (invoked fromenqueue_with_concurrency, adapter L55-88).spec/pgbus/active_job/adapter_spec.rb— existing adapter specs (mocked client per testing rules).spec/integration/batch_flow_spec.rb— end-to-end batch specs against a real database.
Decision
Fix inside the adapter, in two moves:
- Concurrency jobs leave the bulk path: in
enqueue_all, partition jobs whose class responds to a truthypgbus_concurrencyinto the individualenqueue/enqueue_atpath, exactly like uniqueness jobs today. Bulk send cannot acquire per-job semaphores, so routing is the correct fix, not injecting metadata into bulk payloads. - Batch metadata joins the bulk path: in
enqueue_immediate, map each serialized payload throughinject_batch_metadataso bulk jobs get tagged and counted. (Scheduled bulk jobs already go throughenqueue_at, which injects.)
Alternative rejected: de-bulking everything inside a batch block (calling enqueue per job). Loses send_batch throughput for the common case; tagging payloads is sufficient because batch tracking is payload metadata, not a per-job lock.
Settled in interview:
- This fix ships first, as its own PR, independent of the batches-v2 work (Batches v2: adopt solid_queue's released batch design (tracking) #416).
Implementation steps (TDD: specs first)
- RED —
spec/pgbus/active_job/adapter_spec.rb:enqueue_allinside a batch context (Thread.current[:pgbus_batch_id]set) passes payloads containingPgbus::Batch::METADATA_KEYtoclient.send_batch, and increments the thread-local job count by the number of bulk jobs.enqueue_allwith a job class declaringpgbus_concurrencynever reachessend_batchfor that job; it goes through the individual path (semaphore acquired / conflict handled).- Mixed input (plain + unique + concurrency-limited) routes each group correctly.
- RED —
spec/integration/batch_flow_spec.rb: a batch whose block usesperform_all_laterhastotal_jobsequal to the bulk job count, staysprocessinguntil they complete, then fires callbacks. - GREEN — implement both moves in
lib/pgbus/active_job/adapter.rb. - CHANGELOG:
fix(active_job)entry under[Unreleased]covering both the batch escape and the concurrency bypass.
Verification gates
bundle exec rspec spec/pgbus/active_job/adapter_spec.rb spec/pgbus/batch_spec.rb— greenPGBUS_DATABASE_URL=postgres://mhenrixon@localhost:5432/pgbus_test?gssencmode=disable bundle exec rspec spec/integration/batch_flow_spec.rb— greenbundle exec rspec— full suite greenbundle exec rubocop— no offenses
Out of scope
- Any batches-v2 schema or lifecycle change (see Batches v2: adopt solid_queue's released batch design (tracking) #416 and its other sub-issues).
- Making bulk enqueue acquire semaphores in bulk — routing concurrency jobs individually is the fix.
- Uniqueness behavior in
enqueue_all(already correct).
Execution
Execute with /lfg <this issue number>.
Activity
fix(active_job): release uniqueness lock on concurrency :discard
fix(active_job): perform_all_later no longer escapes batch tracking o…
Merge origin/main into fix/uniqueness-pending-orphan-reaper
feat(web): surface concurrency keys on the Locks page, in metrics and…