Class: SimpleMutex::SidekiqSupport::Batch

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/simple_mutex/sidekiq_support/batch.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

DEFAULT_EXPIRES_IN =
6 * 60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lock_key:, expires_in: DEFAULT_EXPIRES_IN) ⇒ Batch

Returns a new instance of Batch.



18
19
20
21
22
23
24
# 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

Instance Attribute Details

#batchObject

Returns the value of attribute batch.



14
15
16
# File 'lib/simple_mutex/sidekiq_support/batch.rb', line 14

def batch
  @batch
end

#expires_inObject

Returns the value of attribute expires_in.



14
15
16
# File 'lib/simple_mutex/sidekiq_support/batch.rb', line 14

def expires_in
  @expires_in
end

#lock_keyObject

Returns the value of attribute lock_key.



14
15
16
# File 'lib/simple_mutex/sidekiq_support/batch.rb', line 14

def lock_key
  @lock_key
end

Instance Method Details

#jobs(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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