ActiveJob adapter
Set the adapter to :pgbus and your existing jobs run on PGMQ, unchanged.
Set the adapter#
pgbus is a standard ActiveJob queue adapter. Point Rails at it and every job in your app enqueues through PGMQ:
config.active_job.queue_adapter = :pgbusqueue_as, retry_on, and discard_on all work as usual.Enqueue and schedule#
Your existing jobs work with no edits:
class OrderConfirmationJob < ApplicationJob
queue_as :mailers
def perform(order)
OrderMailer.confirmation(order).deliver_now
end
end
OrderConfirmationJob.perform_later(order) # enqueue now
OrderConfirmationJob.set(wait: 5.minutes).perform_later(order) # scheduledA scheduled job is sent with a PGMQ delay, so it stays invisible until its time arrives — no separate scheduler poll table.
Coalescing enqueues#
One send for a unit of work's jobs.
Every perform_later is one send, so one database round trip. Inside Pgbus.coalesce the adapter holds the jobs it can send in bulk and sends them when the block ends: one send_batch per queue, priority and delay (per chunk of config.coalesce_chunk_size).
Pgbus.coalesce do # on_failure: :raise (default) | :report | ->(jobs, error) { }
EventJob.perform_later("a")
EventJob.perform_later("b")
end # one send hereFor every request, an opt-in Rack middleware runs the request inside the scope and sends before it returns the response, never after it:
config.pgbus.coalesce_requests = true # inserted after ActionDispatch::Executor
config.pgbus.coalesce_requests_on_failure = :report # default :raise- Sent at once, not held: jobs with uniqueness or a concurrency limit,
retry_onre-enqueues, deploy task runs, batch members outside their own built batch's block, workflow steps, jobs from another thread or fiber, and jobs enqueued once the flush has started. On Rails 7.2+ a job that Rails defers to after commit (enqueue_after_transaction_commit) reaches the scope at commit, so one from a rolled-back transaction never does; a job that does not defer is held like any other. - Bulk-eligible future jobs (
set(wait:)) are held too and sent with the delay left at the flush, onesend_batchper queue, priority and delay. - Serialized at
perform_later: an argument that cannot be serialized raises there, and a later change to the job is not sent. - The return value:
perform_laterreturns the job withsuccessfully_enqueued?true andprovider_job_idnil until the flush, as Rails does for a job deferred to after commit. - The flush: one
send_batchper queue, priority and delay (per chunk), then each job left unsent is retried on its own; what still fails goes toon_failure.:raiseraisesPgbus::EnqueueErrornaming each job. When the block raises, the buffer is still sent and the block's error propagates. - Chunks: a scope sends what it holds every
config.coalesce_chunk_sizejobs (default 1 000) and at the end. A built batch's scope keeps holding while a transaction is open on the batch's connection;on_failureruns once, at the end, over every chunk. - Nesting and batches: a nested scope joins the outer one. A
Pgbus.batchbuilt in a pgbus process opens a scope of its own that holds only its members, sends them by the batch block's end and never joins an outer scope (see Batches).
An endpoint that must not answer 500 after committing its writes should use on_failure: :report or a callable that does not raise.
Jobs whose caller reads the enqueue outcome. A caller that checks successfully_enqueued? or rescues around perform_later to undo its own state needs the job sent at the call. Declare pgbus_coalesce false on its class (subclasses inherit it; pgbus_coalesce true turns it back on). When most jobs are like that, flip the default and hold only the jobs the scope is for: with config.coalesce_default = false only classes that declare pgbus_coalesce true are held, in a batch's own scope too.
This bypasses pgbus's scope only: a job Rails defers to after commit (enqueue_after_transaction_commit, Rails 7.2+) still reaches the adapter at commit. A class that defines pgbus_coalesce? itself (the hook before this setting) answers for itself, whatever the macro or the default say; remove it when adopting them.
class ProcessItemJob < ApplicationJob
pgbus_coalesce false # sent at its perform_later, inside a scope too
endOr hold only the classes you name:
Pgbus.configure { |c| c.coalesce_default = false }class ClickEventJob < ApplicationJob
pgbus_coalesce true
endWhat happens to a message#
Enqueue, read, execute, archive — or dead-letter.
Each job becomes one PGMQ message. The adapter serializes it to JSON and sends it to the queue; a worker claims it under a visibility timeout, runs it inside the Rails executor, and archives it on success. On failure the visibility timeout expires and the message is retried — until read_ct crosses max_retries, when it routes to the dead-letter queue.
The retry backoff and dead-letter details are on Retries & dead letters.
Current attributes#
Request context travels with the job.
ActiveSupport::CurrentAttributes (Current.tenant, Current.user, Current.request_id) is reset around every job, so inside perform it is empty — unless you ask pgbus to carry it. One switch:
Pgbus.configure do |config|
config.current_attributes = :auto # every ActiveSupport::CurrentAttributes subclass
# config.current_attributes = [Current, "Admin::Current"] # or an explicit list
# config.current_attributes = { Current => { except: [:request] } } # or per-class only:/except:
endAt perform_later the assigned attributes of each persisted class are serialized with ActiveJob::Arguments (a record becomes a GlobalID, a Symbol stays a Symbol) into the job payload under pgbus_current. When the job runs, pgbus wraps the whole perform_now in Current.set(...) — so before_perform, perform, rescue_from, retry_on / discard_on blocks and a job enqueued from inside perform all see the context, and the previous values come back afterwards. Because it lives in the job hash (not in pgbus metadata), it behaves the same under Rails' :test and :inline adapters and a bare job.perform_now.
| Path | What the job sees |
|---|---|
retry_on | The context captured at the original enqueue — a retry never picks up whatever Current happened to be during the failed attempt. |
limits_concurrency on_conflict: :block | Promotion re-sends the stored payload; context preserved. |
| Dead-letter / dashboard retry | Same payload, same context. |
perform_all_later | Every job tagged. |
| Batch callbacks | Captured from the job that finished the batch (they are enqueued from its executor). |
| Recurring tasks | Nothing persisted — the scheduler has no request context. |
Safety. GlobalIDs inside the context are gated by the same allowed_global_id_models allowlist as job arguments. An attribute that cannot be serialized (Current.request holding an ActionDispatch::Request, say) raises Pgbus::CurrentAttributesError at perform_later naming the class, the attribute and the except: fix — nothing is dropped silently. One deliberate exception: an unpersisted record (persisted? falsey — a dev-mode fallback record, a model captured before save, a destroyed record) is skipped with a debug log instead of raising, because with no id it could never be restored anyway and its momentary state should not abort the enqueue. Per job class: self.pgbus_persist_current_attributes = false (never persist) or a list/hash in the config shapes (replace the list for this class). Under execution_mode: :async remember CurrentAttributes is per isolation unit — set config.active_support.isolation_level = :fiber (see Running workers).
The failed-job and dead-letter pages in the dashboard show the persisted context in a Context card (through the same parameter filter as the payload). Pairs naturally with fair share: config.fair_share = ->(job) { Current.tenant_id } now also sees the restored tenant on a retry re-enqueue.
Serialization and safety#
Payloads are JSON only — pgbus never uses Marshal, so a malicious or corrupt payload can't deserialize into arbitrary Ruby. GlobalID arguments (an Active Record record) resolve through the same config.allowed_global_id_models allowlist that EventBus payloads use: when the allowlist is set, a crafted _aj_globalid job argument whose model is not listed raises Pgbus::SerializationError before Rails' unrestricted GlobalID::Locator runs. Leave the config nil (default) for allow-all; set [] to deny all. Apps that enqueue ActiveStorage analyze/purge/transform jobs need ActiveStorage::Blob (and related models) on the allowlist.
ensures_uniqueness and limits_concurrency layer straight onto a job class.