Module: SidekiqUniqueJobs::ScriptMock

Extended by:
SingleForwardable
Defined in:
lib/sidekiq_unique_jobs/script_mock.rb

Class Method Summary collapse

Class Method Details

.acquire_lock(redis_pool, options = {}) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sidekiq_unique_jobs/script_mock.rb', line 17

def acquire_lock(redis_pool, options = {})
  connection(redis_pool) do |conn|
    unique_key = options[:keys][0]
    job_id     = options[:argv][0]
    expires    = options[:argv][1].to_i
    stored_jid = conn.get(unique_key)

    return (stored_jid == job_id) ? 1 : 0 if stored_jid
    return 0 unless conn.set(unique_key, job_id, nx: true, ex: expires)

    conn.hsetnx(SidekiqUniqueJobs::HASH_KEY, job_id, unique_key)

    return 1
  end
end

.call(file_name, redis_pool, options = {}) ⇒ Object


13
14
15
# File 'lib/sidekiq_unique_jobs/script_mock.rb', line 13

def call(file_name, redis_pool, options = {})
  send(file_name, redis_pool, options)
end

.release_lock(redis_pool, options = {}) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sidekiq_unique_jobs/script_mock.rb', line 33

def release_lock(redis_pool, options = {})
  connection(redis_pool) do |conn|
    unique_key = options[:keys][0]
    job_id     = options[:argv][0]
    stored_jid = conn.get(unique_key)

    return -1 unless stored_jid
    return 0 unless stored_jid == job_id || stored_jid == '2'

    conn.del(unique_key)
    conn.hdel(SidekiqUniqueJobs::HASH_KEY, job_id)

    return 1
  end
end

.synchronize(redis_pool, options = {}) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sidekiq_unique_jobs/script_mock.rb', line 49

def synchronize(redis_pool, options = {})
  connection(redis_pool) do |conn|
    unique_key = options[:keys][0]
    time       = options[:argv][0].to_i
    expires    = options[:argv][1].to_f

    return 1 if conn.set(unique_key, time + expires, nx: true, ex: expires)

    stored_time = conn.get(unique_key)
    if stored_time && stored_time < time
      return 1 if conn.set(unique_key, time + expires, xx: true, ex: expires)
    end

    return 0
  end
end