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, with one deliberate difference in cleanup: Sidekiq only touches the classes carried by a job and leaves the general reset to the Rails executor it runs jobs inside. Shoryuken does not run jobs inside that executor by default, so the loader resets every registered class after each job itself - see the note on Loading#perform.
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.
55 56 57 |
# File 'lib/shoryuken/active_job/current_attributes.rb', line 55 def cattrs @cattrs end |
Class Method Details
.persist(*klasses) ⇒ Object
Register CurrentAttributes classes to persist across job execution.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/shoryuken/active_job/current_attributes.rb', line 63 def persist(*klasses) @cattrs ||= {} klasses.flatten.each do |klass| # Key off the running registry size, not the per-call index, so that # registering classes across separate persist calls still produces # distinct keys (a per-call index restarts at 0 each call and would # overwrite earlier registrations). key = @cattrs.empty? ? 'cattr' : "cattr_#{@cattrs.size}" @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 |