Module: Sidekiq::ThrottledWorker::Concurrency

Defined in:
lib/sidekiq/throttled_worker/concurrency.rb

Class Method Summary collapse

Class Method Details

.finalize!(worker_class, jid) ⇒ Object



20
21
22
23
24
# File 'lib/sidekiq/throttled_worker/concurrency.rb', line 20

def finalize!(worker_class, jid)
  Sidekiq::ThrottledWorker.redis.with do |conn|
    conn.zrem(concurrency_key(worker_class), jid)
  end
end

.limit?(worker_class, jid) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sidekiq/throttled_worker/concurrency.rb', line 5

def limit?(worker_class, jid)
  # only allow one worker check limit
  concurrency = worker_class.get_sidekiq_options["concurrency"]
  ttl = (worker_class.get_sidekiq_options["concurrency_ttl"] || 900) # assume worker should finish run in 15 minutes, and worker may be block completely max for 15 minutes in extreme case
  now = Time.now.to_f
  Sidekiq::ThrottledWorker.redis.with do |conn|
    conn.zremrangebyscore(concurrency_key(worker_class), "-inf", "(#{now}")
    return true if conn.zcard(concurrency_key(worker_class)) >= concurrency && conn.zscore(concurrency_key(worker_class), jid).nil?
    conn.zadd(concurrency_key(worker_class), (now + ttl).to_i, jid)
    conn.expire(concurrency_key(worker_class), (now + ttl).to_i)

    false
  end
end