Class: Sidekiq::SerializedCurrentAttributes::Save

Inherits:
Object
  • Object
show all
Includes:
ClientMiddleware
Defined in:
lib/sidekiq/serialized_current_attributes.rb

Instance Method Summary collapse

Constructor Details

#initialize(cattrs) ⇒ Save

Returns a new instance of Save.



18
19
20
# File 'lib/sidekiq/serialized_current_attributes.rb', line 18

def initialize(cattrs)
  @cattrs = cattrs
end

Instance Method Details

#call(_, job, _, _) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sidekiq/serialized_current_attributes.rb', line 22

def call(_, job, _, _)
  @cattrs.each do |(key, strklass)|
    if !job.has_key?(key)
      attrs = serialize(strklass.constantize.attributes)
      # Retries can push the job N times, we don't
      # want retries to reset cattr. #5692, #5090
      job[key] = attrs if attrs.any?
    end
  end
  yield
end

#serialize(attributes) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/sidekiq/serialized_current_attributes.rb', line 34

def serialize(attributes)
  if Sidekiq::SerializedCurrentAttributes.discard_destroyed
    attributes = attributes.reject { |_, value| value.is_a?(GlobalID::Identification) && value.respond_to?(:destroyed?) && value.destroyed? }
  end

  serialized_values = ActiveJob::Arguments.serialize(attributes.values)
  attributes.keys.zip(serialized_values).to_h
end