Class: Wurk::Middleware::CurrentAttributes::Load

Inherits:
Object
  • Object
show all
Includes:
ServerMiddleware
Defined in:
lib/wurk/middleware/current_attributes.rb

Overview

Restores each registered CurrentAttributes class for the duration of the inner block, then puts back whatever was there before — not a blanket reset. On a server thread the prior state is empty, so this is equivalent to resetting; but Load also runs on the CLIENT chain (persist registers it on both), where an enqueue happens mid-request with request-scoped attributes already set. Resetting there would wipe the caller’s state right after ‘perform_async`. Save/restore is what Sidekiq does and is correct on both chains. Restore runs in `ensure` to survive raises and Skip.

Registered on BOTH chains, so ‘call` takes an optional 4th arg: the client chain passes a `redis_pool`, the server chain stops at `queue`.

Instance Attribute Summary

Attributes included from ServerMiddleware

#config

Instance Method Summary collapse

Methods included from ServerMiddleware

#logger, #redis, #redis_pool

Constructor Details

#initialize(classes) ⇒ Load

Returns a new instance of Load.



85
86
87
# File 'lib/wurk/middleware/current_attributes.rb', line 85

def initialize(classes)
  @classes = classes
end

Instance Method Details

#call(_job_or_class, job, _queue, _redis_pool = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/wurk/middleware/current_attributes.rb', line 89

def call(_job_or_class, job, _queue, _redis_pool = nil)
  previous = @classes.map { |klass| CurrentAttributes.snapshot(klass) }
  @classes.each_with_index do |klass, idx|
    CurrentAttributes.restore(klass, job[CurrentAttributes.key_for(idx)])
  end
  yield
ensure
  previous&.each_with_index do |attrs, idx|
    @classes[idx].reset
    CurrentAttributes.restore(@classes[idx], attrs)
  end
end