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

Classes: Load, Save

Constant Summary collapse

PERSISTENT_KEY =
'cattr'

Class Method Summary collapse

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.



33
34
35
# File 'lib/wurk/middleware/current_attributes.rb', line 33

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.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
# 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.server_middleware.add(Load, classes)
end

.restore(klass, attrs) ⇒ Object



43
44
45
# File 'lib/wurk/middleware/current_attributes.rb', line 43

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.



39
40
41
# File 'lib/wurk/middleware/current_attributes.rb', line 39

def snapshot(klass)
  klass.attributes.to_h
end