Module: Pgbus::ActiveJob::CurrentAttributes

Extended by:
ActiveSupport::Concern
Defined in:
lib/pgbus/active_job/current_attributes.rb

Overview

Carries ActiveSupport::CurrentAttributes across enqueue → perform (issue #430). Included on ActiveJob::Base by the engine next to BatchId.

  • serialize snapshots the persisted Current classes (Pgbus::CurrentAttributes.capture) under pgbus_current. A job that was itself deserialized re-serializes the context it was enqueued with — so a retry_on re-enqueue keeps the original context even if Current changed during the attempt. Nothing is added when the feature is off or no attribute is assigned: the payload is byte-for-byte what it was before this mixin existed.
  • perform_now is wrapped (not an around_perform) so the restored context also covers rescue_from / retry_on / discard_on blocks, which run in +perform_now+'s rescue outside the perform callbacks — and it works identically under the pgbus worker, Rails' :test and :inline adapters, and a bare job.perform_now.

Per-class control: self.pgbus_persist_current_attributes = false (never persist for this job class) or a spec in the same shapes as config.current_attributes (an Array / Hash) to replace the config's list for this class. nil (default) follows the config.

Instance Method Summary collapse

Instance Method Details

#deserialize(job_data) ⇒ Object



42
43
44
45
# File 'lib/pgbus/active_job/current_attributes.rb', line 42

def deserialize(job_data)
  super
  self.pgbus_current_attributes = job_data[Pgbus::CurrentAttributes::METADATA_KEY]
end

#perform_nowObject



47
48
49
# File 'lib/pgbus/active_job/current_attributes.rb', line 47

def perform_now
  Pgbus::CurrentAttributes.restore(pgbus_current_attributes) { super }
end

#serializeObject



34
35
36
37
38
39
40
# File 'lib/pgbus/active_job/current_attributes.rb', line 34

def serialize
  data = super
  captured = pgbus_current_attributes ||
             Pgbus::CurrentAttributes.capture(override: self.class.pgbus_persist_current_attributes)
  data[Pgbus::CurrentAttributes::METADATA_KEY] = captured if captured
  data
end