class Sidekiq::Bunch

Constants

TimeoutError
VERSION

Attributes

bunch_id[R]

Public Class Methods

generate_bunch_id() click to toggle source
# File lib/sidekiq/bunch.rb, line 13
def self.generate_bunch_id
  SecureRandom.hex(12)
end
new(bunch_id = self.class.generate_bunch_id) click to toggle source
# File lib/sidekiq/bunch.rb, line 17
def initialize(bunch_id = self.class.generate_bunch_id)
  @bunch_id = bunch_id
  @jids_key = build_jids_key
end

Public Instance Methods

add(jids) click to toggle source
# File lib/sidekiq/bunch.rb, line 45
def add(jids)
  jids = Array(jids)

  redis do |r|
    r.sadd(@jids_key, jids)
  end
end
empty?() click to toggle source
# File lib/sidekiq/bunch.rb, line 65
def empty?
  0 == size
end
hook(ignore: nil) { || ... } click to toggle source
# File lib/sidekiq/bunch.rb, line 22
def hook(ignore: nil)
  Thread.current[:sidekiq_bunch] = self
  Sidekiq::Postpone.wrap(join_parent: false, flush: false) do |postpone|
    yield
    jids =
      if ignore
        postpone.all_jobs.reject(&ignore).map { |j| j['jid'] }
      else
        postpone.jids
      end
    multi do
      add(jids)
      postpone.flush!
    end
  end
ensure
  Thread.current[:sidekiq_bunch] = nil
end
on_success(job) click to toggle source
# File lib/sidekiq/bunch.rb, line 41
def on_success(job)
  rem(job['jid'])
end
rem(jids) click to toggle source
# File lib/sidekiq/bunch.rb, line 53
def rem(jids)
  jids = Array(jids)

  redis do |r|
    r.srem(@jids_key, jids)
  end
end
size() click to toggle source
# File lib/sidekiq/bunch.rb, line 61
def size
  redis { |r| r.scard(@jids_key) }
end
wait_for_empty(poll_interval: 1.0, timeout: nil) click to toggle source
# File lib/sidekiq/bunch.rb, line 69
def wait_for_empty(poll_interval: 1.0, timeout: nil)
  start_time = Time.now if timeout
  loop do
    break if empty?
    raise TimeoutError if timeout && (Time.now - start_time) >= timeout
    sleep(poll_interval)
  end
end

Private Instance Methods

build_jids_key() click to toggle source
# File lib/sidekiq/bunch.rb, line 82
def build_jids_key
  "bunches:#{@bunch_id}-jids"
end
multi() { |m| ... } click to toggle source
# File lib/sidekiq/bunch.rb, line 90
def multi
  redis { |r| r.multi { |m| yield m } }
end
redis() { |r| ... } click to toggle source
# File lib/sidekiq/bunch.rb, line 86
def redis
  Sidekiq.redis { |r| yield r }
end