class SimpleMutex::SidekiqSupport::Batch
Constants
- DEFAULT_EXPIRES_IN
Attributes
batch[RW]
expires_in[RW]
lock_key[RW]
Public Class Methods
new(lock_key:, expires_in: DEFAULT_EXPIRES_IN)
click to toggle source
# File lib/simple_mutex/sidekiq_support/batch.rb, line 18 def initialize(lock_key:, expires_in: DEFAULT_EXPIRES_IN) ::SimpleMutex.sidekiq_pro_check! self.lock_key = lock_key self.expires_in = expires_in self.batch = ::Sidekiq::Batch.new end
Public Instance Methods
jobs(&block)
click to toggle source
# File lib/simple_mutex/sidekiq_support/batch.rb, line 26 def jobs(&block) mutex.lock! set_callbacks(mutex.signature) begin batch.jobs(&block) rescue => error mutex.unlock! raise error end status = ::Sidekiq::Batch::Status.new(batch.bid) if status.total.zero? mutex.unlock! raise Error, "Batch should contain at least one job." end end
Private Instance Methods
generate_payload(batch)
click to toggle source
# File lib/simple_mutex/sidekiq_support/batch.rb, line 60 def generate_payload(batch) { "type" => "Batch", "started_at" => Time.now.to_s, "bid" => batch.bid } end
mutex()
click to toggle source
# File lib/simple_mutex/sidekiq_support/batch.rb, line 50 def mutex return @mutex if defined? @mutex @mutex = ::SimpleMutex::Mutex.new( lock_key, expires_in: expires_in, payload: generate_payload(batch), ) end
set_callbacks(signature)
click to toggle source
# File lib/simple_mutex/sidekiq_support/batch.rb, line 66 def set_callbacks(signature) %i[death success].each do |event| batch.on( event, ::SimpleMutex::SidekiqSupport::BatchCallbacks, "lock_key" => lock_key, "signature" => signature, ) end end