Class: SidekiqCurrentModelMiddleware::Save

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::ClientMiddleware
Defined in:
lib/sidekiq_current_model_middleware.rb

Overview

Save class

Client middleware that saves CurrentAttributes to the Sidekiq job payload. It handles serialization of ActiveRecord models using GlobalID.

Instance Method Summary collapse

Constructor Details

#initialize(cattrs) ⇒ Save

Initialize the Save middleware

Parameters:

  • cattrs (Hash)

    A hash of CurrentAttributes classes to persist



65
66
67
# File 'lib/sidekiq_current_model_middleware.rb', line 65

def initialize(cattrs)
  @cattrs = cattrs
end

Instance Method Details

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sidekiq_current_model_middleware.rb', line 69

def call(_, job, _, _)
  @cattrs.each do |(key, strklass)|
    next if job.key?(key)

    # Add the global id if the attribute is an ActiveRecord model
    attrs = strklass.constantize.attributes.transform_values do |attr|
      attr.class <= ActiveRecord::Base ? attr.to_global_id : attr
    end
    # Retries can push the job N times, we don't
    # want retries to reset cattr. #5692, #5090 (from orig Sidekiq repo)
    job[key] = attrs if attrs.any?
  end
  yield
end