Skip to content

fix(active_job): perform_all_later bypasses batch and concurrency metadata #413

Closed

Description

@mhenrixon

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:

  1. Jobs bulk-enqueued inside a Pgbus::Batch#enqueue block are not tagged with pgbus_batch_id and not counted into total_jobs — the batch can finish and fire on_finish/on_success before those jobs ever run.
  2. Jobs whose class declares pgbus_concurrency bypass semaphore acquisition entirely when enqueued via perform_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 increments Thread.current[:pgbus_batch_job_count].
  • lib/pgbus/batch.rb — METADATA_KEY, #count_jobs (L66-79) reads the thread-local counter after the block to set total_jobs.
  • lib/pgbus/active_job/concurrency.rb — inject_metadata, semaphore acquisition (invoked from enqueue_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:

  1. Concurrency jobs leave the bulk path: in enqueue_all, partition jobs whose class responds to a truthy pgbus_concurrency into the individual enqueue/enqueue_at path, exactly like uniqueness jobs today. Bulk send cannot acquire per-job semaphores, so routing is the correct fix, not injecting metadata into bulk payloads.
  2. Batch metadata joins the bulk path: in enqueue_immediate, map each serialized payload through inject_batch_metadata so bulk jobs get tagged and counted. (Scheduled bulk jobs already go through enqueue_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:

Implementation steps (TDD: specs first)

  1. RED — spec/pgbus/active_job/adapter_spec.rb:
    • enqueue_all inside a batch context (Thread.current[:pgbus_batch_id] set) passes payloads containing Pgbus::Batch::METADATA_KEY to client.send_batch, and increments the thread-local job count by the number of bulk jobs.
    • enqueue_all with a job class declaring pgbus_concurrency never reaches send_batch for that job; it goes through the individual path (semaphore acquired / conflict handled).
    • Mixed input (plain + unique + concurrency-limited) routes each group correctly.
  2. RED — spec/integration/batch_flow_spec.rb: a batch whose block uses perform_all_later has total_jobs equal to the bulk job count, stays processing until they complete, then fires callbacks.
  3. GREEN — implement both moves in lib/pgbus/active_job/adapter.rb.
  4. 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 — green
  • PGBUS_DATABASE_URL=postgres://mhenrixon@localhost:5432/pgbus_test?gssencmode=disable bundle exec rspec spec/integration/batch_flow_spec.rb — green
  • bundle exec rspec — full suite green
  • bundle exec rubocop — no offenses

Out of scope

Execution

Execute with /lfg <this issue number>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions