Module: Shoryuken::ActiveJob::CurrentAttributes
- Defined in:
- lib/shoryuken/active_job/current_attributes.rb
Overview
Middleware to persist Rails CurrentAttributes across job execution.
This ensures that request-scoped context (like current user, tenant, locale) automatically flows from the code that enqueues a job to the job’s execution.
Based on Sidekiq’s approach to persisting current attributes.
Defined Under Namespace
Modules: Loading, Persistence, Serializer
Class Attribute Summary collapse
-
.cattrs ⇒ Hash{String => String}
readonly
Serialization keys mapped to CurrentAttributes class names.
Class Method Summary collapse
-
.persist(*klasses) ⇒ Object
Register CurrentAttributes classes to persist across job execution.
Class Attribute Details
.cattrs ⇒ Hash{String => String} (readonly)
Returns serialization keys mapped to CurrentAttributes class names.
50 51 52 |
# File 'lib/shoryuken/active_job/current_attributes.rb', line 50 def cattrs @cattrs end |
Class Method Details
.persist(*klasses) ⇒ Object
Register CurrentAttributes classes to persist across job execution.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/shoryuken/active_job/current_attributes.rb', line 58 def persist(*klasses) @cattrs ||= {} klasses.flatten.each_with_index do |klass, idx| key = @cattrs.empty? ? 'cattr' : "cattr_#{idx}" @cattrs[key] = klass.to_s end # Prepend the persistence module to the adapter for serialization unless ::ActiveJob::QueueAdapters::ShoryukenAdapter.ancestors.include?(Persistence) ::ActiveJob::QueueAdapters::ShoryukenAdapter.prepend(Persistence) end # Prepend the loading module to JobWrapper for deserialization unless Shoryuken::ActiveJob::JobWrapper.ancestors.include?(Loading) Shoryuken::ActiveJob::JobWrapper.prepend(Loading) end end |