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
@@ -0,0 +1,24 @@
|
|
1
|
+
local function find_digest_in_sorted_set(name, digest)
|
2
|
+
local cursor = 0
|
3
|
+
local count = 5
|
4
|
+
local pattern = "*" .. digest .. "*"
|
5
|
+
local found = false
|
6
|
+
|
7
|
+
log_debug("searching in:", name,
|
8
|
+
"for digest:", digest,
|
9
|
+
"cursor:", cursor)
|
10
|
+
repeat
|
11
|
+
local pagination = redis.call("ZSCAN", name, cursor, "MATCH", pattern, "COUNT", count)
|
12
|
+
local next_cursor = pagination[1]
|
13
|
+
local items = pagination[2]
|
14
|
+
|
15
|
+
if #items > 0 then
|
16
|
+
log_debug("Found digest", digest, "in sorted set:", name)
|
17
|
+
found = true
|
18
|
+
end
|
19
|
+
|
20
|
+
cursor = next_cursor
|
21
|
+
until found == true or cursor == "0"
|
22
|
+
|
23
|
+
return found
|
24
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
-------- BEGIN keys ---------
|
2
|
+
local digest = KEYS[1]
|
3
|
+
local queued = KEYS[2]
|
4
|
+
local primed = KEYS[3]
|
5
|
+
local locked = KEYS[4]
|
6
|
+
local info = KEYS[5]
|
7
|
+
local changelog = KEYS[6]
|
8
|
+
local digests = KEYS[7]
|
9
|
+
-------- END keys ---------
|
10
|
+
|
11
|
+
-------- BEGIN lock arguments ---------
|
12
|
+
local job_id = ARGV[1]
|
13
|
+
local pttl = tonumber(ARGV[2])
|
14
|
+
local type = ARGV[3]
|
15
|
+
local limit = tonumber(ARGV[4])
|
16
|
+
-------- END lock arguments -----------
|
17
|
+
|
18
|
+
-------- BEGIN injected arguments --------
|
19
|
+
local current_time = tonumber(ARGV[5])
|
20
|
+
local debug_lua = ARGV[6] == "true"
|
21
|
+
local max_history = tonumber(ARGV[7])
|
22
|
+
local script_name = tostring(ARGV[8]) .. ".lua"
|
23
|
+
local redisversion = ARGV[9]
|
24
|
+
--------- END injected arguments ---------
|
25
|
+
|
26
|
+
-------- BEGIN Variables --------
|
27
|
+
local queued_count = redis.call("LLEN", queued)
|
28
|
+
local primed_count = redis.call("LLEN", primed)
|
29
|
+
local locked_count = redis.call("HLEN", locked)
|
30
|
+
--------- END Variables ---------
|
31
|
+
|
32
|
+
-------- BEGIN local functions --------
|
33
|
+
<%= include_partial "shared/_common.lua" %>
|
34
|
+
---------- END local functions ----------
|
35
|
+
|
36
|
+
|
37
|
+
--------- Begin unlock.lua ---------
|
38
|
+
log_debug("BEGIN unlock digest:", digest, "(job_id: " .. job_id ..")")
|
39
|
+
|
40
|
+
log_debug("HEXISTS", locked, job_id)
|
41
|
+
if redis.call("HEXISTS", locked, job_id) == 0 then
|
42
|
+
-- TODO: Improve orphaned lock detection
|
43
|
+
if queued_count == 0 and primed_count == 0 and locked_count == 0 then
|
44
|
+
log_debug("Orphaned lock")
|
45
|
+
else
|
46
|
+
local result = ""
|
47
|
+
for i,v in ipairs(redis.call("HKEYS", locked)) do
|
48
|
+
result = result .. v .. ","
|
49
|
+
end
|
50
|
+
result = locked .. " (" .. result .. ")"
|
51
|
+
log("Yielding to: " .. result)
|
52
|
+
log_debug("Yielding to", result, locked, "by job", job_id)
|
53
|
+
return nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
-- Just in case something went wrong
|
58
|
+
log_debug("LREM", queued, -1, job_id)
|
59
|
+
redis.call("LREM", queued, -1, job_id)
|
60
|
+
|
61
|
+
log_debug("LREM", primed, -1, job_id)
|
62
|
+
redis.call("LREM", primed, -1, job_id)
|
63
|
+
|
64
|
+
-- TODO: Check if there are other locks before removing
|
65
|
+
log_debug("ZREM", digests, digest)
|
66
|
+
redis.call("ZREM", digests, digest)
|
67
|
+
|
68
|
+
if pttl and pttl > 0 then
|
69
|
+
log_debug("PEXPIRE", digest, pttl)
|
70
|
+
redis.call("PEXPIRE", digest, pttl)
|
71
|
+
|
72
|
+
log_debug("PEXPIRE", locked, pttl)
|
73
|
+
redis.call("PEXPIRE", locked, pttl)
|
74
|
+
|
75
|
+
log_debug("PEXPIRE", info, pttl)
|
76
|
+
redis.call("PEXPIRE", info, pttl)
|
77
|
+
else
|
78
|
+
local redis_version = toversion(redisversion)
|
79
|
+
local del_cmd = "DEL"
|
80
|
+
|
81
|
+
if tonumber(redis_version["major"]) >= 4 then del_cmd = "UNLINK"; end
|
82
|
+
|
83
|
+
log_debug(del_cmd, digest, info)
|
84
|
+
redis.call(del_cmd, digest, info)
|
85
|
+
|
86
|
+
log_debug("HDEL", locked, job_id)
|
87
|
+
redis.call("HDEL", locked, job_id)
|
88
|
+
end
|
89
|
+
|
90
|
+
log_debug("LPUSH", queued, "1")
|
91
|
+
redis.call("LPUSH", queued, "1")
|
92
|
+
|
93
|
+
log_debug("PEXPIRE", queued, 500)
|
94
|
+
redis.call("PEXPIRE", queued, 500)
|
95
|
+
|
96
|
+
log("Unlocked")
|
97
|
+
log_debug("END unlock digest:", digest, "(job_id: " .. job_id ..")")
|
98
|
+
return job_id
|
99
|
+
--------- END unlock.lua ---------
|
@@ -0,0 +1,40 @@
|
|
1
|
+
-------- BEGIN keys ---------
|
2
|
+
local live_key = KEYS[1]
|
3
|
+
local dead_key = KEYS[2]
|
4
|
+
-------- END keys ---------
|
5
|
+
|
6
|
+
-------- BEGIN lock arguments ---------
|
7
|
+
local version = ARGV[1]
|
8
|
+
-------- END lock arguments -----------
|
9
|
+
|
10
|
+
-------- BEGIN injected arguments --------
|
11
|
+
local current_time = tonumber(ARGV[2])
|
12
|
+
local debug_lua = ARGV[3] == "true"
|
13
|
+
local max_history = tonumber(ARGV[4])
|
14
|
+
local script_name = tostring(ARGV[5]) .. ".lua"
|
15
|
+
--------- END injected arguments ---------
|
16
|
+
|
17
|
+
-------- BEGIN local functions --------
|
18
|
+
<%= include_partial "shared/_common.lua" %>
|
19
|
+
---------- END local functions ----------
|
20
|
+
|
21
|
+
|
22
|
+
-------- BEGIN set_version.lua --------
|
23
|
+
log_debug("BEGIN setting version:", version)
|
24
|
+
|
25
|
+
local updated = false
|
26
|
+
local old_version = redis.call("GETSET", live_key, version)
|
27
|
+
|
28
|
+
if not old_version then
|
29
|
+
log_debug("No previous version found")
|
30
|
+
updated = true
|
31
|
+
elseif old_version ~= version then
|
32
|
+
log_debug("Old version:", old_version, "differs from:", version)
|
33
|
+
redis.call("SET", dead_key, old_version)
|
34
|
+
updated = true
|
35
|
+
end
|
36
|
+
|
37
|
+
return updated
|
38
|
+
-------- END delete.lua --------
|
39
|
+
|
40
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
redis.replicate_commands()
|
2
|
+
-------- BEGIN keys ---------
|
3
|
+
local live_version = KEYS[1]
|
4
|
+
local dead_version = KEYS[2]
|
5
|
+
-------- END keys ---------
|
6
|
+
|
7
|
+
-------- BEGIN injected arguments --------
|
8
|
+
local current_time = tonumber(ARGV[5])
|
9
|
+
local debug_lua = ARGV[6] == "true"
|
10
|
+
local max_history = tonumber(ARGV[7])
|
11
|
+
local script_name = tostring(ARGV[8]) .. ".lua"
|
12
|
+
local redisversion = ARGV[9]
|
13
|
+
--------- END injected arguments ---------
|
14
|
+
|
15
|
+
-------- BEGIN local functions --------
|
16
|
+
<%= include_partial "shared/_common.lua" %>
|
17
|
+
<%= include_partial "shared/_upgrades.lua" %>
|
18
|
+
---------- END local functions ----------
|
19
|
+
|
20
|
+
|
21
|
+
local new_version = redis.call("GET", live_version)
|
22
|
+
local old_version = redis.call("GET", dead_version)
|
23
|
+
local redis_version = toversion(redisversion)
|
24
|
+
local upgraded = 0
|
25
|
+
local del_cmd = "DEL"
|
26
|
+
|
27
|
+
if redis_version["major"] >= 4 then del_cmd = "UNLINK"; end
|
28
|
+
-------- BEGIN delete.lua --------
|
29
|
+
|
30
|
+
log_debug("BEGIN upgrading from: ", old_version, "to:", new_version)
|
31
|
+
|
32
|
+
-- 1. Loop through all uniquejobs:jajshdahdas:GRABBED
|
33
|
+
local cursor = "0"
|
34
|
+
local per = 50
|
35
|
+
repeat
|
36
|
+
local pagination = redis.call("SCAN", cursor, "MATCH", "*:GRABBED", "COUNT", per)
|
37
|
+
local next_cursor = pagination[1]
|
38
|
+
local grabbed_keys = pagination[2]
|
39
|
+
|
40
|
+
for _, grabbed in ipairs(grabbed_keys) do
|
41
|
+
local locked_key = grabbed.gsub(":GRABBED", ":LOCKED")
|
42
|
+
local locks = redis.call("HGETALL", grabbed)
|
43
|
+
|
44
|
+
if #locks == 0 then
|
45
|
+
log_debug("No entries in:", grabbed)
|
46
|
+
else
|
47
|
+
log_debug("HMSET", locked_key, unpack(locks))
|
48
|
+
redis.call("HMSET", locked_key, unpack(locks))
|
49
|
+
end
|
50
|
+
|
51
|
+
log_debug("DEL", grabbed)
|
52
|
+
redis.call("DEL", grabbed)
|
53
|
+
|
54
|
+
upgraded = upgraded + 1
|
55
|
+
end
|
56
|
+
|
57
|
+
cursor = next_cursor
|
58
|
+
if cursor == "0" then
|
59
|
+
log_debug("Looped through all grabbed keys, stopping iteration")
|
60
|
+
end
|
61
|
+
until cursor == "0"
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
log_debug("END upgrading from: ", old_version, "to:", new_version)
|
66
|
+
|
67
|
+
return
|
68
|
+
-------- END delete.lua --------
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "sidekiq"
|
4
|
-
|
5
3
|
module SidekiqUniqueJobs
|
6
4
|
#
|
7
5
|
# Provides the sidekiq middleware that makes the gem work
|
@@ -9,51 +7,84 @@ module SidekiqUniqueJobs
|
|
9
7
|
# @author Mikael Henriksson <mikael@zoolutions.se>
|
10
8
|
#
|
11
9
|
module Middleware
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|
10
|
+
include SidekiqUniqueJobs::Logging::Middleware
|
11
|
+
include SidekiqUniqueJobs::OptionsWithFallback
|
12
|
+
include SidekiqUniqueJobs::JSON
|
17
13
|
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
#
|
15
|
+
# Configure both server and client
|
16
|
+
#
|
17
|
+
def self.configure
|
18
|
+
configure_server
|
19
|
+
configure_client
|
21
20
|
end
|
22
21
|
|
23
|
-
|
22
|
+
#
|
23
|
+
# Configures the Sidekiq server
|
24
|
+
#
|
25
|
+
def self.configure_server # rubocop:disable Metrics/MethodLength
|
24
26
|
Sidekiq.configure_server do |config|
|
25
27
|
config.client_middleware do |chain|
|
26
|
-
|
27
|
-
if defined?(Apartment::Sidekiq::Middleware::Client)
|
28
|
-
chain.insert_after Apartment::Sidekiq::Middleware::Client, SidekiqUniqueJobs::Client::Middleware
|
29
|
-
else
|
30
|
-
chain.add SidekiqUniqueJobs::Client::Middleware
|
31
|
-
end
|
28
|
+
chain.add SidekiqUniqueJobs::Middleware::Client
|
32
29
|
end
|
33
30
|
|
34
31
|
config.server_middleware do |chain|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
32
|
+
chain.add SidekiqUniqueJobs::Middleware::Server
|
33
|
+
end
|
34
|
+
|
35
|
+
config.on(:startup) do
|
36
|
+
SidekiqUniqueJobs::UpdateVersion.call
|
37
|
+
SidekiqUniqueJobs::UpgradeLocks.call
|
38
|
+
|
39
|
+
SidekiqUniqueJobs::Orphans::Manager.start
|
40
|
+
end
|
41
|
+
|
42
|
+
config.on(:shutdown) do
|
43
|
+
SidekiqUniqueJobs::Orphans::Manager.stop
|
41
44
|
end
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
45
|
-
|
48
|
+
#
|
49
|
+
# Configures the Sidekiq client
|
50
|
+
#
|
51
|
+
def self.configure_client
|
46
52
|
Sidekiq.configure_client do |config|
|
47
53
|
config.client_middleware do |chain|
|
48
|
-
|
49
|
-
if defined?(Apartment::Sidekiq::Middleware::Client)
|
50
|
-
chain.insert_after Apartment::Sidekiq::Middleware::Client, SidekiqUniqueJobs::Client::Middleware
|
51
|
-
else
|
52
|
-
chain.add SidekiqUniqueJobs::Client::Middleware
|
53
|
-
end
|
54
|
+
chain.add SidekiqUniqueJobs::Middleware::Client
|
54
55
|
end
|
55
56
|
end
|
56
57
|
end
|
58
|
+
|
59
|
+
# The sidekiq job hash
|
60
|
+
# @return [Hash] the Sidekiq job hash
|
61
|
+
attr_reader :item
|
62
|
+
|
63
|
+
#
|
64
|
+
# This method runs before (prepended) the actual middleware implementation.
|
65
|
+
# This is done to reduce duplication
|
66
|
+
#
|
67
|
+
# @param [Sidekiq::Worker] worker_class
|
68
|
+
# @param [Hash] item a sidekiq job hash
|
69
|
+
# @param [String] queue name of the queue
|
70
|
+
# @param [ConnectionPool] redis_pool only used for compatility reasons
|
71
|
+
#
|
72
|
+
# @return [yield<super>] <description>
|
73
|
+
#
|
74
|
+
# @yieldparam [<type>] if <description>
|
75
|
+
# @yieldreturn [<type>] <describe what yield should return>
|
76
|
+
def call(worker_class, item, queue, redis_pool = nil)
|
77
|
+
@worker_class = worker_class
|
78
|
+
@item = item
|
79
|
+
@queue = queue
|
80
|
+
@redis_pool = redis_pool
|
81
|
+
return yield if unique_disabled?
|
82
|
+
|
83
|
+
SidekiqUniqueJobs::Job.add_uniqueness(item)
|
84
|
+
|
85
|
+
with_logging_context do
|
86
|
+
super
|
87
|
+
end
|
88
|
+
end
|
57
89
|
end
|
58
90
|
end
|
59
|
-
SidekiqUniqueJobs.extend SidekiqUniqueJobs::Middleware
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Middleware
|
5
|
+
# The unique sidekiq middleware for the client push
|
6
|
+
#
|
7
|
+
# @author Mikael Henriksson <mikael@zoolutions.se>
|
8
|
+
class Client
|
9
|
+
prepend SidekiqUniqueJobs::Middleware
|
10
|
+
|
11
|
+
# Calls this client middleware
|
12
|
+
# Used from Sidekiq.process_single
|
13
|
+
#
|
14
|
+
# @see SidekiqUniqueJobs::Middleware#call
|
15
|
+
#
|
16
|
+
# @see https://github.com/mperham/sidekiq/wiki/Job-Format
|
17
|
+
# @see https://github.com/mperham/sidekiq/wiki/Middleware
|
18
|
+
#
|
19
|
+
# @yield when uniqueness is disable
|
20
|
+
# @yield when the lock is successful
|
21
|
+
def call(*)
|
22
|
+
lock { yield }
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def lock
|
28
|
+
if (token = lock_instance.lock)
|
29
|
+
yield token
|
30
|
+
else
|
31
|
+
warn_about_duplicate
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def warn_about_duplicate
|
36
|
+
return unless log_duplicate?
|
37
|
+
|
38
|
+
log_warn "Already locked with another job_id (#{dump_json(item)})"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Middleware
|
5
|
+
# The unique sidekiq middleware for the server processor
|
6
|
+
#
|
7
|
+
# @author Mikael Henriksson <mikael@zoolutions.se>
|
8
|
+
class Server
|
9
|
+
prepend SidekiqUniqueJobs::Middleware
|
10
|
+
|
11
|
+
#
|
12
|
+
#
|
13
|
+
# Runs the server middleware (used from Sidekiq::Processor#process)
|
14
|
+
#
|
15
|
+
# @see SidekiqUniqueJobs::Middleware#call
|
16
|
+
#
|
17
|
+
# @see https://github.com/mperham/sidekiq/wiki/Job-Format
|
18
|
+
# @see https://github.com/mperham/sidekiq/wiki/Middleware
|
19
|
+
#
|
20
|
+
# @yield when uniqueness is disabled
|
21
|
+
# @yield when owning the lock
|
22
|
+
def call(*)
|
23
|
+
lock_instance.execute { yield }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|