Module: SidekiqUniqueJobs::ScriptMock
- Extended by:
- SingleForwardable
- Defined in:
- lib/sidekiq_unique_jobs/script_mock.rb
Class Method Summary collapse
- .acquire_lock(redis_pool, options = {}) ⇒ Object
- .call(file_name, redis_pool, options = {}) ⇒ Object
- .release_lock(redis_pool, options = {}) ⇒ Object
- .synchronize(redis_pool, options = {}) ⇒ Object
Class Method Details
.acquire_lock(redis_pool, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sidekiq_unique_jobs/script_mock.rb', line 15 def acquire_lock(redis_pool, = {}) connection(redis_pool) do |redis| unique_key = [:keys][0] job_id = [:argv][0] expires = [:argv][1].to_i stored_jid = redis.get(unique_key) return stored_jid == job_id ? 1 : 0 if stored_jid return 0 unless redis.set(unique_key, job_id, nx: true, ex: expires) redis.hsetnx(SidekiqUniqueJobs::HASH_KEY, job_id, unique_key) return 1 end end |
.call(file_name, redis_pool, options = {}) ⇒ Object
11 12 13 |
# File 'lib/sidekiq_unique_jobs/script_mock.rb', line 11 def call(file_name, redis_pool, = {}) send(file_name, redis_pool, ) end |
.release_lock(redis_pool, options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sidekiq_unique_jobs/script_mock.rb', line 30 def release_lock(redis_pool, = {}) connection(redis_pool) do |redis| unique_key = [:keys][0] job_id = [:argv][0] stored_jid = redis.get(unique_key) return -1 unless stored_jid return 0 unless stored_jid == job_id || stored_jid == '2' redis.del(unique_key) redis.hdel(SidekiqUniqueJobs::HASH_KEY, job_id) return 1 end end |
.synchronize(redis_pool, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sidekiq_unique_jobs/script_mock.rb', line 45 def synchronize(redis_pool, = {}) connection(redis_pool) do |redis| unique_key = [:keys][0] time = [:argv][0].to_i expires = [:argv][1].to_f return 1 if redis.set(unique_key, time + expires, nx: true, ex: expires) stored_time = redis.get(unique_key) if stored_time && stored_time < time if redis.set(unique_key, time + expires, xx: true, ex: expires) return 1 end end return 0 end end |