sidekiq-unique-jobs 6.0.25 → 7.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sidekiq-unique-jobs might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +155 -20
- data/README.md +349 -112
- data/lib/sidekiq-unique-jobs.rb +2 -0
- data/lib/sidekiq_unique_jobs.rb +43 -6
- data/lib/sidekiq_unique_jobs/batch_delete.rb +121 -0
- data/lib/sidekiq_unique_jobs/changelog.rb +71 -0
- data/lib/sidekiq_unique_jobs/cli.rb +20 -29
- data/lib/sidekiq_unique_jobs/config.rb +193 -0
- data/lib/sidekiq_unique_jobs/connection.rb +5 -4
- data/lib/sidekiq_unique_jobs/constants.rb +36 -24
- data/lib/sidekiq_unique_jobs/core_ext.rb +38 -0
- data/lib/sidekiq_unique_jobs/digests.rb +78 -93
- data/lib/sidekiq_unique_jobs/exceptions.rb +152 -8
- data/lib/sidekiq_unique_jobs/job.rb +3 -3
- data/lib/sidekiq_unique_jobs/json.rb +34 -0
- data/lib/sidekiq_unique_jobs/key.rb +93 -0
- data/lib/sidekiq_unique_jobs/lock.rb +295 -0
- data/lib/sidekiq_unique_jobs/lock/base_lock.rb +49 -43
- data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
- data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
- data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +8 -17
- data/lib/sidekiq_unique_jobs/lock/until_executed.rb +5 -5
- data/lib/sidekiq_unique_jobs/lock/until_expired.rb +1 -23
- data/lib/sidekiq_unique_jobs/lock/validator.rb +65 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +12 -8
- data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +1 -1
- data/lib/sidekiq_unique_jobs/lock_config.rb +95 -0
- data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
- data/lib/sidekiq_unique_jobs/locksmith.rb +255 -99
- data/lib/sidekiq_unique_jobs/logging.rb +148 -22
- data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
- data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
- data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +46 -0
- data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
- data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
- data/lib/sidekiq_unique_jobs/lua/find_digest_in_sorted_set.lua +24 -0
- data/lib/sidekiq_unique_jobs/lua/lock.lua +91 -0
- data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
- data/lib/sidekiq_unique_jobs/lua/queue.lua +83 -0
- data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +86 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +19 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +46 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
- data/lib/sidekiq_unique_jobs/lua/shared/find_digest_in_sorted_set.lua +24 -0
- data/lib/sidekiq_unique_jobs/lua/unlock.lua +99 -0
- data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
- data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
- data/lib/sidekiq_unique_jobs/middleware.rb +62 -31
- data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
- data/lib/sidekiq_unique_jobs/middleware/server.rb +27 -0
- data/lib/sidekiq_unique_jobs/normalizer.rb +3 -3
- data/lib/sidekiq_unique_jobs/on_conflict.rb +22 -9
- data/lib/sidekiq_unique_jobs/on_conflict/log.rb +8 -4
- data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +59 -13
- data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +42 -13
- data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +4 -4
- data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +24 -5
- data/lib/sidekiq_unique_jobs/options_with_fallback.rb +47 -23
- data/lib/sidekiq_unique_jobs/orphans/manager.rb +100 -0
- data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
- data/lib/sidekiq_unique_jobs/orphans/reaper.rb +201 -0
- data/lib/sidekiq_unique_jobs/profiler.rb +51 -0
- data/lib/sidekiq_unique_jobs/redis.rb +11 -0
- data/lib/sidekiq_unique_jobs/redis/entity.rb +94 -0
- data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
- data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
- data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
- data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +59 -0
- data/lib/sidekiq_unique_jobs/redis/string.rb +49 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers.rb +19 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +43 -0
- data/lib/sidekiq_unique_jobs/{scripts.rb → script.rb} +43 -29
- data/lib/sidekiq_unique_jobs/script/caller.rb +125 -0
- data/lib/sidekiq_unique_jobs/script/template.rb +41 -0
- data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +92 -65
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +166 -28
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +10 -11
- data/lib/sidekiq_unique_jobs/testing.rb +47 -15
- data/lib/sidekiq_unique_jobs/time_calculator.rb +103 -0
- data/lib/sidekiq_unique_jobs/timing.rb +58 -0
- data/lib/sidekiq_unique_jobs/unique_args.rb +19 -21
- data/lib/sidekiq_unique_jobs/unlockable.rb +11 -2
- data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
- data/lib/sidekiq_unique_jobs/upgrade_locks.rb +151 -0
- data/lib/sidekiq_unique_jobs/version.rb +3 -1
- data/lib/sidekiq_unique_jobs/version_check.rb +1 -1
- data/lib/sidekiq_unique_jobs/web.rb +25 -19
- data/lib/sidekiq_unique_jobs/web/helpers.rb +98 -6
- data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
- data/lib/sidekiq_unique_jobs/web/views/locks.erb +52 -0
- data/lib/tasks/changelog.rake +4 -3
- metadata +70 -35
- data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
- data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
- data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
- data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
- data/lib/sidekiq_unique_jobs/util.rb +0 -103
- data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
- data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
- data/redis/acquire_lock.lua +0 -21
- data/redis/convert_legacy_lock.lua +0 -13
- data/redis/delete.lua +0 -14
- data/redis/delete_by_digest.lua +0 -23
- data/redis/delete_job_by_digest.lua +0 -60
- data/redis/lock.lua +0 -62
- data/redis/release_stale_locks.lua +0 -90
- data/redis/unlock.lua +0 -35
@@ -1,28 +0,0 @@
|
|
1
|
-
<header class="row">
|
2
|
-
<div class="col-sm-5">
|
3
|
-
<h3><%= t('Unique Digest') %> - <a class="btn btn-default btn-xs" href="<%= root_path %>unique_digests"><%= t('GoBack') %></a></h3>
|
4
|
-
</div>
|
5
|
-
</header>
|
6
|
-
|
7
|
-
<% if @unique_keys.size.positive? %>
|
8
|
-
<div class="table_container">
|
9
|
-
<table class="table table-striped table-bordered table-hover">
|
10
|
-
<thead>
|
11
|
-
<tr>
|
12
|
-
<th>
|
13
|
-
<%= t('Keys') %>
|
14
|
-
<span class="small text-muted">for (<%= @digest %>)</span>
|
15
|
-
</th>
|
16
|
-
</tr>
|
17
|
-
</thead>
|
18
|
-
<% @unique_keys.each do |key| %>
|
19
|
-
<tr><td colspan="2"><%= key %></td></tr>
|
20
|
-
<% end %>
|
21
|
-
</table>
|
22
|
-
</div>
|
23
|
-
<form action="<%= root_path %>unique_digests/<%= @digest %>/delete" method="get">
|
24
|
-
<%= csrf_tag %>
|
25
|
-
<a class="btn btn-default btn-xs" href="<%= root_path %>unique_digests"><%= t('GoBack') %></a>
|
26
|
-
<input class="btn btn-danger btn-xs flip" type="submit" name="delete" value="<%= t('Delete') %>" data-confirm="<%= t('AreYouSure') %>" />
|
27
|
-
</form>
|
28
|
-
<% end %>
|
@@ -1,46 +0,0 @@
|
|
1
|
-
<header class="row">
|
2
|
-
<div class="col-sm-5">
|
3
|
-
<h3><%= t('Unique Digests') %></h3>
|
4
|
-
</div>
|
5
|
-
<form action="<%= root_path %>unique_digests" class="form form-inline" method="get">
|
6
|
-
<%= csrf_tag %>
|
7
|
-
<input name="filter" class="form-control" type="text" value="<%= @filter %>" />
|
8
|
-
<button class="btn btn-default" type="submit"><%= t('Filter') %></button>
|
9
|
-
</form>
|
10
|
-
<% if @unique_digests.size > 0 && @total_size > @count.to_i %>
|
11
|
-
<div class="col-sm-4">
|
12
|
-
<%= erb unique_template(:_paging), locals: { url: "#{root_path}unique_digests" } %>
|
13
|
-
</div>
|
14
|
-
<% end %>
|
15
|
-
</header>
|
16
|
-
|
17
|
-
<% if @unique_digests.size.positive? %>
|
18
|
-
<div class="table_container">
|
19
|
-
<table class="table table-striped table-bordered table-hover">
|
20
|
-
<thead>
|
21
|
-
<tr>
|
22
|
-
<th><%= t('Delete') %></th>
|
23
|
-
<th><%= t('Digest') %></th>
|
24
|
-
</tr>
|
25
|
-
</thead>
|
26
|
-
<% @unique_digests.each do |digest| %>
|
27
|
-
<tr>
|
28
|
-
<td>
|
29
|
-
<form action="<%= root_path %>unique_digests/<%= digest %>/delete" method="get">
|
30
|
-
<%= csrf_tag %>
|
31
|
-
<input name="digest" value="<%= h digest %>" type="hidden" />
|
32
|
-
<input class="btn btn-danger btn-xs" type="submit" name="delete" value="<%= t('Delete') %>" data-confirm="<%= t('AreYouSure') %>" />
|
33
|
-
</form>
|
34
|
-
</td>
|
35
|
-
<td>
|
36
|
-
<a href="<%= root_path %>unique_digests/<%= digest %>"><%= digest %></a>
|
37
|
-
</td>
|
38
|
-
</tr>
|
39
|
-
<% end %>
|
40
|
-
</table>
|
41
|
-
|
42
|
-
<form action="<%= root_path %>unique_digests/delete_all" method="get">
|
43
|
-
<input class="btn btn-danger btn-xs" type="submit" name="delete_all" value="<%= t('DeleteAll') %>" data-confirm="<%= t('AreYouSure') %>" />
|
44
|
-
</form>
|
45
|
-
</div>
|
46
|
-
<% end %>
|
data/redis/acquire_lock.lua
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
local unique_key = KEYS[1]
|
2
|
-
local job_id = ARGV[1]
|
3
|
-
local expires = tonumber(ARGV[2])
|
4
|
-
local stored_jid = redis.pcall('get', unique_key)
|
5
|
-
|
6
|
-
if stored_jid then
|
7
|
-
if stored_jid == job_id then
|
8
|
-
return 1
|
9
|
-
else
|
10
|
-
return 0
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
if redis.call('SET', unique_key, job_id, 'nx') then
|
15
|
-
if expires then
|
16
|
-
redis.call('EXPIRE', unique_key, expires)
|
17
|
-
end
|
18
|
-
return 1
|
19
|
-
else
|
20
|
-
return 0
|
21
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
local grabbed_key = KEYS[1]
|
2
|
-
local unique_digest = KEYS[2]
|
3
|
-
|
4
|
-
local job_id = ARGV[1]
|
5
|
-
local current_time = tonumber(ARGV[2])
|
6
|
-
|
7
|
-
local old_token = redis.call('GET', unique_digest)
|
8
|
-
if old_token then
|
9
|
-
if old_token == job_id or old_token == '2' then
|
10
|
-
redis.call('DEL', unique_digest)
|
11
|
-
redis.call('HSET', grabbed_key, job_id, current_time)
|
12
|
-
end
|
13
|
-
end
|
data/redis/delete.lua
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
local exists_key = KEYS[1]
|
2
|
-
local grabbed_key = KEYS[2]
|
3
|
-
local available_key = KEYS[3]
|
4
|
-
local version_key = KEYS[4]
|
5
|
-
local unique_keys = KEYS[5]
|
6
|
-
local unique_digest = KEYS[6] -- TODO: Legacy support (Remove in v6.1)
|
7
|
-
|
8
|
-
redis.call('DEL', exists_key)
|
9
|
-
redis.call('SREM', unique_keys, unique_digest)
|
10
|
-
redis.call('DEL', grabbed_key)
|
11
|
-
redis.call('DEL', available_key)
|
12
|
-
redis.call('DEL', version_key)
|
13
|
-
redis.call('DEL', 'uniquejobs') -- TODO: Old job hash, just drop the darn thing
|
14
|
-
redis.call('DEL', unique_digest) -- TODO: Legacy support (Remove in v6.1)
|
data/redis/delete_by_digest.lua
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
-- redis.replicate_commands();
|
2
|
-
local unique_keys = KEYS[1]
|
3
|
-
local unique_digest = KEYS[2]
|
4
|
-
local exists_key = KEYS[3]
|
5
|
-
local grabbed_key = KEYS[4]
|
6
|
-
local available_key = KEYS[5]
|
7
|
-
local version_key = KEYS[6]
|
8
|
-
local run_exists_key = KEYS[7]
|
9
|
-
local run_grabbed_key = KEYS[8]
|
10
|
-
local run_available_key = KEYS[9]
|
11
|
-
local run_version_key = KEYS[10]
|
12
|
-
|
13
|
-
local count = redis.call('SREM', unique_keys, unique_digest)
|
14
|
-
redis.call('DEL', exists_key)
|
15
|
-
redis.call('DEL', grabbed_key)
|
16
|
-
redis.call('DEL', available_key)
|
17
|
-
redis.call('DEL', version_key)
|
18
|
-
redis.call('DEL', run_exists_key)
|
19
|
-
redis.call('DEL', run_grabbed_key)
|
20
|
-
redis.call('DEL', run_available_key)
|
21
|
-
redis.call('DEL', run_version_key)
|
22
|
-
|
23
|
-
return count
|
@@ -1,60 +0,0 @@
|
|
1
|
-
local queue = KEYS[1]
|
2
|
-
local schedule_set = KEYS[2]
|
3
|
-
local retry_set = KEYS[3]
|
4
|
-
local unique_digest = ARGV[1]
|
5
|
-
|
6
|
-
local function delete_from_sorted_set(name, digest)
|
7
|
-
local per = 50
|
8
|
-
local total = redis.call('zcard', name)
|
9
|
-
local index = 0
|
10
|
-
local result
|
11
|
-
-- redis.log(redis.LOG_DEBUG, "delete_from_sorted_set("..name..","..digest..")")
|
12
|
-
while (index < total) do
|
13
|
-
-- redis.log(redis.LOG_DEBUG, "delete_from_sorted_set("..name..","..digest..") - "..index.."-"..per)
|
14
|
-
local items = redis.call('ZRANGE', name, index, index + per -1)
|
15
|
-
for _, item in pairs(items) do
|
16
|
-
-- redis.log(redis.LOG_DEBUG, "delete_from_sorted_set("..name..","..digest..") - current item: " .. item)
|
17
|
-
if string.find(item, digest) then
|
18
|
-
-- redis.log(redis.LOG_DEBUG, "delete_from_sorted_set("..name..","..digest..") - deleting item")
|
19
|
-
redis.call('ZREM', name, item)
|
20
|
-
result = item
|
21
|
-
break
|
22
|
-
end
|
23
|
-
end
|
24
|
-
index = index + per
|
25
|
-
end
|
26
|
-
return result
|
27
|
-
end
|
28
|
-
|
29
|
-
local per = 50
|
30
|
-
local total = redis.call('LLEN', queue)
|
31
|
-
local index = 0
|
32
|
-
local result = nil
|
33
|
-
|
34
|
-
-- redis.log(redis.LOG_DEBUG, "delete_job_by_digest.lua - looping through: " .. queue)
|
35
|
-
while (index < total) do
|
36
|
-
-- redis.log(redis.LOG_DEBUG, "delete_job_by_digest.lua - " .. index .. "-" .. per)
|
37
|
-
local items = redis.call('LRANGE', queue, index, index + per -1)
|
38
|
-
for _, item in pairs(items) do
|
39
|
-
-- redis.log(redis.LOG_DEBUG, "delete_job_by_digest.lua - item: " .. item)
|
40
|
-
if string.find(item, unique_digest) then
|
41
|
-
-- redis.log(redis.LOG_DEBUG, "delete_job_by_digest.lua - found item with digest: " .. unique_digest .. " in: " ..queue)
|
42
|
-
redis.call('LREM', queue, 1, item)
|
43
|
-
result = item
|
44
|
-
break
|
45
|
-
end
|
46
|
-
end
|
47
|
-
index = index + per
|
48
|
-
end
|
49
|
-
|
50
|
-
if result then
|
51
|
-
return result
|
52
|
-
end
|
53
|
-
|
54
|
-
result = delete_from_sorted_set(schedule_set, unique_digest)
|
55
|
-
if result then
|
56
|
-
return result
|
57
|
-
end
|
58
|
-
|
59
|
-
result = delete_from_sorted_set(retry_set, unique_digest)
|
60
|
-
return result
|
data/redis/lock.lua
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
-- redis.replicate_commands();
|
2
|
-
|
3
|
-
local exists_key = KEYS[1]
|
4
|
-
local grabbed_key = KEYS[2]
|
5
|
-
local available_key = KEYS[3]
|
6
|
-
local unique_keys = KEYS[4]
|
7
|
-
local unique_digest = KEYS[5]
|
8
|
-
|
9
|
-
local job_id = ARGV[1]
|
10
|
-
local ttl = tonumber(ARGV[2])
|
11
|
-
local lock = ARGV[3]
|
12
|
-
|
13
|
-
local function current_time()
|
14
|
-
local time = redis.call('time')
|
15
|
-
local s = time[1]
|
16
|
-
local ms = time[2]
|
17
|
-
local number = tonumber((s .. '.' .. ms))
|
18
|
-
|
19
|
-
return number
|
20
|
-
end
|
21
|
-
|
22
|
-
local stored_token = redis.call('GET', exists_key)
|
23
|
-
if stored_token and stored_token ~= job_id then
|
24
|
-
return stored_token
|
25
|
-
end
|
26
|
-
|
27
|
-
redis.call('SET', exists_key, job_id)
|
28
|
-
|
29
|
-
----------------------------------------------------------------
|
30
|
-
-- TODO: Legacy support (Remove in v6.1)
|
31
|
-
local old_token = redis.call('GET', unique_digest)
|
32
|
-
if old_token then
|
33
|
-
if old_token == job_id or old_token == '2' then
|
34
|
-
-- No need to return, we just delete the old key
|
35
|
-
redis.call('DEL', unique_digest)
|
36
|
-
else
|
37
|
-
return old_token
|
38
|
-
end
|
39
|
-
end
|
40
|
-
----------------------------------------------------------------
|
41
|
-
|
42
|
-
redis.call('SADD', unique_keys, unique_digest)
|
43
|
-
redis.call('DEL', grabbed_key)
|
44
|
-
-- TODO: Move this to LUA when redis 3.2 is the least supported
|
45
|
-
-- redis.call('HSET', grabbed_key, job_id, current_time())
|
46
|
-
---------------------------------------------------------------
|
47
|
-
redis.call('DEL', available_key)
|
48
|
-
redis.call('RPUSH', available_key, job_id)
|
49
|
-
|
50
|
-
-- The client should only set ttl for until_expired
|
51
|
-
-- The server should set ttl for all other jobs
|
52
|
-
-- redis.log(redis.LOG_WARNING, "lock: " .. lock .. ", ttl: " .. tostring(ttl))
|
53
|
-
if lock == "until_expired" and ttl and ttl > 0 then
|
54
|
-
-- We can't keep the key here because it will otherwise never be deleted
|
55
|
-
redis.call('SREM', unique_keys, unique_digest)
|
56
|
-
|
57
|
-
redis.call('EXPIRE', available_key, ttl)
|
58
|
-
redis.call('EXPIRE', exists_key, ttl)
|
59
|
-
redis.call('EXPIRE', unique_digest, ttl)
|
60
|
-
end
|
61
|
-
|
62
|
-
return job_id
|
@@ -1,90 +0,0 @@
|
|
1
|
-
redis.replicate_commands();
|
2
|
-
|
3
|
-
local exists_key = KEYS[1]
|
4
|
-
local grabbed_key = KEYS[2]
|
5
|
-
local available_key = KEYS[3]
|
6
|
-
local release_key = KEYS[4]
|
7
|
-
local version_key = KEYS[5]
|
8
|
-
|
9
|
-
local expires_in = tonumber(ARGV[1])
|
10
|
-
local stale_client_timeout = tonumber(ARGV[2])
|
11
|
-
local expiration = tonumber(ARGV[3])
|
12
|
-
|
13
|
-
local function current_time()
|
14
|
-
local time = redis.call('time')
|
15
|
-
local s = time[1]
|
16
|
-
local ms = time[2]
|
17
|
-
local number = tonumber((s .. '.' .. ms))
|
18
|
-
|
19
|
-
return number
|
20
|
-
end
|
21
|
-
|
22
|
-
local hgetall = function (key)
|
23
|
-
local bulk = redis.call('HGETALL', key)
|
24
|
-
local result = {}
|
25
|
-
local nextkey
|
26
|
-
for i, v in ipairs(bulk) do
|
27
|
-
if i % 2 == 1 then
|
28
|
-
nextkey = v
|
29
|
-
else
|
30
|
-
result[nextkey] = v
|
31
|
-
end
|
32
|
-
end
|
33
|
-
return result
|
34
|
-
end
|
35
|
-
|
36
|
-
local cached_current_time = current_time()
|
37
|
-
redis.log(redis.LOG_DEBUG, "release_stale_locks.lua - started at : " .. cached_current_time)
|
38
|
-
|
39
|
-
local my_lock_expires_at = cached_current_time + expires_in + 1
|
40
|
-
redis.log(redis.LOG_DEBUG, "release_stale_locks.lua - my_lock_expires_at: " .. my_lock_expires_at)
|
41
|
-
|
42
|
-
if not redis.call('SETNX', release_key, my_lock_expires_at) then
|
43
|
-
-- Check if expired
|
44
|
-
local other_lock_expires_at = tonumber(redis.call('GET', release_key))
|
45
|
-
redis.log(redis.LOG_DEBUG, "release_stale_locks.lua - other_lock_expires_at: " .. other_lock_expires_at)
|
46
|
-
|
47
|
-
if other_lock_expires_at < cached_current_time then
|
48
|
-
local old_expires_at = tonumber(redis.call('GETSET', release_key, my_lock_expires_at))
|
49
|
-
redis.log(redis.LOG_DEBUG, "release_stale_locks.lua - old_expires_at: " .. old_expires_at)
|
50
|
-
|
51
|
-
-- Check if another client started cleanup yet. If not,
|
52
|
-
-- then we now have the lock.
|
53
|
-
if not old_expires_at == other_lock_expires_at then
|
54
|
-
redis.log(redis.LOG_DEBUG, "release_stale_locks.lua -could not retrieve lock: exiting 0")
|
55
|
-
return 0
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
local keys = hgetall(grabbed_key)
|
61
|
-
for key, locked_at in pairs(keys) do
|
62
|
-
local timed_out_at = tonumber(locked_at) + stale_client_timeout
|
63
|
-
redis.log(redis.LOG_DEBUG,
|
64
|
-
"release_stale_locks.lua - processing: " .. grabbed_key .. " key: " .. key .. " locked_at: " .. locked_at
|
65
|
-
)
|
66
|
-
|
67
|
-
if timed_out_at < current_time() then
|
68
|
-
redis.log(redis.LOG_DEBUG, "HDEL " .. grabbed_key .. ":" .. key)
|
69
|
-
redis.call('HDEL', grabbed_key, key)
|
70
|
-
redis.log(redis.LOG_DEBUG, "LPUSH " .. available_key .. ":" .. key)
|
71
|
-
redis.call('LPUSH', available_key, key)
|
72
|
-
|
73
|
-
if expiration then
|
74
|
-
redis.log(redis.LOG_DEBUG, "release_stale_locks.lua - EXPIRE " .. available_key .. " with " .. expiration)
|
75
|
-
redis.call('EXPIRE', available_key, expiration)
|
76
|
-
redis.log(redis.LOG_DEBUG, "release_stale_locks.lua - EXPIRE " .. exists_key .. " with " .. expiration)
|
77
|
-
redis.call('EXPIRE', exists_key, expiration)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
-- Make sure not to delete the lock in case someone else already expired
|
83
|
-
-- our lock, with one second in between to account for some lag.
|
84
|
-
if my_lock_expires_at > (current_time() - 1) then
|
85
|
-
redis.log(redis.LOG_DEBUG, "release_stale_locks.lua - DEL " .. release_key)
|
86
|
-
redis.call('DEL', release_key)
|
87
|
-
end
|
88
|
-
|
89
|
-
redis.log(redis.LOG_DEBUG, "release_stale_locks.lua - comleted at : " .. current_time())
|
90
|
-
return 1
|
data/redis/unlock.lua
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
local exists_key = KEYS[1]
|
2
|
-
local grabbed_key = KEYS[2]
|
3
|
-
local available_key = KEYS[3]
|
4
|
-
local version_key = KEYS[4]
|
5
|
-
local unique_keys = KEYS[5]
|
6
|
-
local unique_digest = KEYS[6] -- TODO: Legacy support (Remove in v6.1)
|
7
|
-
|
8
|
-
local token = ARGV[1]
|
9
|
-
local ttl = tonumber(ARGV[2])
|
10
|
-
local lock = ARGV[3]
|
11
|
-
|
12
|
-
redis.call('SREM', unique_keys, unique_digest)
|
13
|
-
|
14
|
-
if ttl then
|
15
|
-
redis.call('SREM', unique_keys, unique_digest)
|
16
|
-
redis.call('EXPIRE', exists_key, ttl)
|
17
|
-
redis.call('EXPIRE', grabbed_key, ttl)
|
18
|
-
redis.call('EXPIRE', available_key, ttl)
|
19
|
-
redis.call('EXPIRE', version_key, ttl) -- TODO: Legacy support (Remove in v6.1)
|
20
|
-
redis.call('EXPIRE', unique_digest, ttl) -- TODO: Legacy support (Remove in v6.1)
|
21
|
-
else
|
22
|
-
redis.call('DEL', exists_key)
|
23
|
-
redis.call('SREM', unique_keys, unique_digest)
|
24
|
-
redis.call('DEL', grabbed_key)
|
25
|
-
redis.call('DEL', available_key)
|
26
|
-
redis.call('DEL', version_key) -- TODO: Legacy support (Remove in v6.1)
|
27
|
-
redis.call('DEL', 'uniquejobs') -- TODO: Old job hash, just drop the darn thing (Remove in v6.1)
|
28
|
-
redis.call('DEL', unique_digest) -- TODO: Legacy support (Remove in v6.1)
|
29
|
-
end
|
30
|
-
|
31
|
-
redis.call('HDEL', grabbed_key, token)
|
32
|
-
local count = redis.call('LPUSH', available_key, token)
|
33
|
-
redis.call('EXPIRE', available_key, 5)
|
34
|
-
return count
|
35
|
-
|