Module: Wurk::Middleware::CurrentAttributes
- Defined in:
- lib/wurk/middleware/current_attributes.rb
Overview
Propagates ActiveSupport::CurrentAttributes from the enqueueing process
into the worker. Off by default — host opts in by calling
Wurk::Middleware::CurrentAttributes.persist(klass_or_array).
One registered class → job hash key "cattr".
Multiple → "cattr", "cattr_1", "cattr_2", … (keys mirror Sidekiq's
naming exactly: wire-compat sacred).
Spec: docs/target/sidekiq-free.md §10.3 and §2.2.
Defined Under Namespace
Constant Summary collapse
- PERSISTENT_KEY =
'cattr'
Class Method Summary collapse
-
.key_for(index) ⇒ Object
Composes the wire key for the Nth registered class.
-
.persist(klass_or_array, config = Wurk.configuration) ⇒ Object
Register one or more CurrentAttributes classes.
- .restore(klass, attrs) ⇒ Object
-
.snapshot(klass) ⇒ Object
AS::CurrentAttributes#attributes returns a HashWithIndifferentAccess; we coerce to a plain Hash so JSON encoding is predictable.
Class Method Details
.key_for(index) ⇒ Object
Composes the wire key for the Nth registered class. Sidekiq numbers from 1 ("cattr_1"); index 0 keeps the bare "cattr" key.
34 35 36 |
# File 'lib/wurk/middleware/current_attributes.rb', line 34 def key_for(index) index.zero? ? PERSISTENT_KEY : "#{PERSISTENT_KEY}_#{index}" end |
.persist(klass_or_array, config = Wurk.configuration) ⇒ Object
Register one or more CurrentAttributes classes. Re-registering is a
no-op: add already dedupes by klass, so calling persist twice
with the same set replaces the old entry with the new args.
23 24 25 26 27 28 29 30 |
# File 'lib/wurk/middleware/current_attributes.rb', line 23 def persist(klass_or_array, config = Wurk.configuration) classes = Array(klass_or_array) raise ArgumentError, 'persist requires at least one CurrentAttributes class' if classes.empty? config.client_middleware.add(Save, classes) config.client_middleware.add(Load, classes) config.server_middleware.add(Load, classes) end |
.restore(klass, attrs) ⇒ Object
47 48 49 |
# File 'lib/wurk/middleware/current_attributes.rb', line 47 def restore(klass, attrs) attrs&.each { |name, value| klass.public_send("#{name}=", value) } end |
.snapshot(klass) ⇒ Object
AS::CurrentAttributes#attributes returns a HashWithIndifferentAccess;
we coerce to a plain Hash so JSON encoding is predictable. .dup so
the snapshot is a detached copy — Load holds onto the pre-job state
and must not see it mutated when it restores the job's attributes,
and Save must not alias live attributes into the persisted job hash.
43 44 45 |
# File 'lib/wurk/middleware/current_attributes.rb', line 43 def snapshot(klass) klass.attributes.to_h.dup end |