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,17 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "json"
|
4
|
-
|
5
3
|
module SidekiqUniqueJobs
|
6
4
|
# Normalizes hashes by dumping them to json and loading them from json
|
7
5
|
#
|
8
6
|
# @author Mikael Henriksson <mikael@zoolutions.se>
|
9
7
|
module Normalizer
|
8
|
+
extend SidekiqUniqueJobs::JSON
|
9
|
+
|
10
10
|
# Changes hash to a json compatible hash
|
11
11
|
# @param [Hash] args
|
12
12
|
# @return [Hash] a json compatible hash
|
13
13
|
def self.jsonify(args)
|
14
|
-
|
14
|
+
load_json(dump_json(args))
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -15,17 +15,30 @@ module SidekiqUniqueJobs
|
|
15
15
|
# @author Mikael Henriksson <mikael@zoolutions.se>
|
16
16
|
#
|
17
17
|
module OnConflict
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
replace: OnConflict::Replace,
|
23
|
-
reschedule: OnConflict::Reschedule,
|
24
|
-
}.freeze
|
18
|
+
# A convenience method for using the configured strategies
|
19
|
+
def self.strategies
|
20
|
+
SidekiqUniqueJobs.strategies
|
21
|
+
end
|
25
22
|
|
26
|
-
#
|
23
|
+
#
|
24
|
+
# Find a strategy to use for conflicting locks
|
25
|
+
#
|
26
|
+
# @param [Symbol] strategy the key for the strategy
|
27
|
+
#
|
28
|
+
# @return [OnConflict::Strategy] when found
|
29
|
+
# @return [OnConflict::NullStrategy] when no other could be found
|
30
|
+
#
|
27
31
|
def self.find_strategy(strategy)
|
28
|
-
|
32
|
+
return OnConflict::NullStrategy unless strategy
|
33
|
+
|
34
|
+
strategies.fetch(strategy.to_sym) do
|
35
|
+
SidekiqUniqueJobs.logger.warn(
|
36
|
+
"No matching implementation for strategy: #{strategy}, returning OnConflict::NullStrategy." \
|
37
|
+
" Available strategies are (#{strategies.inspect})",
|
38
|
+
)
|
39
|
+
|
40
|
+
OnConflict::NullStrategy
|
41
|
+
end
|
29
42
|
end
|
30
43
|
end
|
31
44
|
end
|
@@ -8,12 +8,16 @@ module SidekiqUniqueJobs
|
|
8
8
|
class Log < OnConflict::Strategy
|
9
9
|
include SidekiqUniqueJobs::Logging
|
10
10
|
|
11
|
+
#
|
11
12
|
# Logs an informational message about that the job was not unique
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# @return [void]
|
16
|
+
#
|
12
17
|
def call
|
13
|
-
log_info(
|
14
|
-
|
15
|
-
|
16
|
-
)
|
18
|
+
log_info(<<~MESSAGE.chomp)
|
19
|
+
Skipping job with id (#{item[JID]}) because unique_digest: (#{item[UNIQUE_DIGEST]}) already exists
|
20
|
+
MESSAGE
|
17
21
|
end
|
18
22
|
end
|
19
23
|
end
|
@@ -6,14 +6,11 @@ module SidekiqUniqueJobs
|
|
6
6
|
#
|
7
7
|
# @author Mikael Henriksson <mikael@zoolutions.se>
|
8
8
|
class Reject < OnConflict::Strategy
|
9
|
+
include SidekiqUniqueJobs::Timing
|
10
|
+
|
9
11
|
# Send jobs to dead queue
|
10
12
|
def call
|
11
|
-
|
12
|
-
send_to_deadset
|
13
|
-
end
|
14
|
-
|
15
|
-
def send_to_deadset
|
16
|
-
log_info { "Adding dead #{item[CLASS_KEY]} job #{item[JID_KEY]}" }
|
13
|
+
log_info { "Adding dead #{item[CLASS]} job #{item[JID]}" }
|
17
14
|
|
18
15
|
if deadset_kill?
|
19
16
|
deadset_kill
|
@@ -22,10 +19,25 @@ module SidekiqUniqueJobs
|
|
22
19
|
end
|
23
20
|
end
|
24
21
|
|
22
|
+
#
|
23
|
+
# Sidekiq version compatibility check
|
24
|
+
# @api private
|
25
|
+
#
|
26
|
+
#
|
27
|
+
# @return [true, false] depending on if Sidekiq::Deadset responds to kill
|
28
|
+
#
|
25
29
|
def deadset_kill?
|
26
30
|
deadset.respond_to?(:kill)
|
27
31
|
end
|
28
32
|
|
33
|
+
#
|
34
|
+
# Use Sidekiqs built in Sidekiq::DeadSet#kill
|
35
|
+
# to get rid of the job
|
36
|
+
# @api private
|
37
|
+
#
|
38
|
+
#
|
39
|
+
# @return [void]
|
40
|
+
#
|
29
41
|
def deadset_kill
|
30
42
|
if kill_with_options?
|
31
43
|
kill_job_with_options
|
@@ -34,36 +46,70 @@ module SidekiqUniqueJobs
|
|
34
46
|
end
|
35
47
|
end
|
36
48
|
|
49
|
+
#
|
50
|
+
# Sidekiq version compatibility check
|
51
|
+
# @api private
|
52
|
+
#
|
53
|
+
#
|
54
|
+
# @return [true] when Sidekiq::Deadset#kill takes more than 1 argument
|
55
|
+
# @return [false] when Sidekiq::Deadset#kill does not take multiple arguments
|
56
|
+
#
|
37
57
|
def kill_with_options?
|
38
58
|
Sidekiq::DeadSet.instance_method(:kill).arity > 1
|
39
59
|
end
|
40
60
|
|
61
|
+
#
|
62
|
+
# Executes the kill instructions without arguments
|
63
|
+
# @api private
|
64
|
+
#
|
65
|
+
# @return [void]
|
66
|
+
#
|
41
67
|
def kill_job_without_options
|
42
68
|
deadset.kill(payload)
|
43
69
|
end
|
44
70
|
|
71
|
+
#
|
72
|
+
# Executes the kill instructions with arguments
|
73
|
+
# @api private
|
74
|
+
#
|
75
|
+
# @return [void]
|
76
|
+
#
|
45
77
|
def kill_job_with_options
|
46
78
|
deadset.kill(payload, notify_failure: false)
|
47
79
|
end
|
48
80
|
|
81
|
+
#
|
82
|
+
# An instance of Sidekiq::Deadset
|
83
|
+
# @api private
|
84
|
+
#
|
85
|
+
# @return [Sidekiq::Deadset]>
|
86
|
+
#
|
49
87
|
def deadset
|
50
88
|
@deadset ||= Sidekiq::DeadSet.new
|
51
89
|
end
|
52
90
|
|
91
|
+
#
|
92
|
+
# Used for compatibility with older Sidekiq versions
|
93
|
+
#
|
94
|
+
#
|
95
|
+
# @return [void]
|
96
|
+
#
|
53
97
|
def push_to_deadset
|
54
|
-
|
98
|
+
redis do |conn|
|
55
99
|
conn.multi do
|
56
|
-
conn.zadd("dead",
|
57
|
-
conn.zremrangebyscore("dead", "-inf",
|
100
|
+
conn.zadd("dead", now_f, payload)
|
101
|
+
conn.zremrangebyscore("dead", "-inf", now_f - Sidekiq::DeadSet.timeout)
|
58
102
|
conn.zremrangebyrank("dead", 0, -Sidekiq::DeadSet.max_jobs)
|
59
103
|
end
|
60
104
|
end
|
61
105
|
end
|
62
106
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
107
|
+
#
|
108
|
+
# The Sidekiq job hash as JSON
|
109
|
+
#
|
110
|
+
#
|
111
|
+
# @return [String] a JSON formatted string
|
112
|
+
#
|
67
113
|
def payload
|
68
114
|
@payload ||= Sidekiq.dump_json(item)
|
69
115
|
end
|
@@ -6,36 +6,65 @@ module SidekiqUniqueJobs
|
|
6
6
|
#
|
7
7
|
# @author Mikael Henriksson <mikael@zoolutions.se>
|
8
8
|
class Replace < OnConflict::Strategy
|
9
|
-
|
9
|
+
#
|
10
|
+
# @!attribute [r] queue
|
11
|
+
# @return [String] rthe sidekiq queue this job belongs to
|
12
|
+
attr_reader :queue
|
13
|
+
#
|
14
|
+
# @!attribute [r] unique_digest
|
15
|
+
# @return [String] the unique digest to use for locking
|
16
|
+
attr_reader :unique_digest
|
10
17
|
|
18
|
+
#
|
19
|
+
# Initialize a new Replace strategy
|
20
|
+
#
|
11
21
|
# @param [Hash] item sidekiq job hash
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@
|
22
|
+
#
|
23
|
+
def initialize(item, redis_pool = nil)
|
24
|
+
super(item, redis_pool)
|
25
|
+
@queue = item[QUEUE]
|
26
|
+
@unique_digest = item[UNIQUE_DIGEST]
|
16
27
|
end
|
17
28
|
|
29
|
+
#
|
18
30
|
# Replace the old job in the queue
|
31
|
+
#
|
32
|
+
#
|
33
|
+
# @return [void] <description>
|
34
|
+
#
|
19
35
|
# @yield to retry the lock after deleting the old one
|
36
|
+
#
|
20
37
|
def call(&block)
|
21
|
-
return unless delete_job_by_digest
|
38
|
+
return unless (deleted_job = delete_job_by_digest)
|
22
39
|
|
23
|
-
|
40
|
+
log_info("Deleting job: #{deleted_job}")
|
41
|
+
if (del_count = delete_lock)
|
42
|
+
log_info("Deleted `#{del_count}` keys for #{unique_digest}")
|
43
|
+
end
|
24
44
|
block&.call
|
25
45
|
end
|
26
46
|
|
47
|
+
#
|
27
48
|
# Delete the job from either schedule, retry or the queue
|
49
|
+
#
|
50
|
+
#
|
51
|
+
# @return [String] the deleted job hash
|
52
|
+
# @return [nil] when deleting nothing
|
53
|
+
#
|
28
54
|
def delete_job_by_digest
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
keys: ["#{QUEUE_KEY}:#{queue}", SCHEDULE_SET, RETRY_SET], argv: [unique_digest],
|
33
|
-
)
|
55
|
+
call_script(:delete_job_by_digest,
|
56
|
+
keys: ["#{QUEUE}:#{queue}", SCHEDULE, RETRY],
|
57
|
+
argv: [unique_digest])
|
34
58
|
end
|
35
59
|
|
60
|
+
#
|
36
61
|
# Delete the keys belonging to the job
|
62
|
+
#
|
63
|
+
#
|
64
|
+
# @return [Integer] the number of keys deleted
|
65
|
+
#
|
37
66
|
def delete_lock
|
38
|
-
|
67
|
+
call_script(:delete_by_digest, keys: [unique_digest, DIGESTS])
|
39
68
|
end
|
40
69
|
end
|
41
70
|
end
|
@@ -9,15 +9,15 @@ module SidekiqUniqueJobs
|
|
9
9
|
include SidekiqUniqueJobs::SidekiqWorkerMethods
|
10
10
|
|
11
11
|
# @param [Hash] item sidekiq job hash
|
12
|
-
def initialize(item)
|
13
|
-
super
|
14
|
-
@worker_class = item[
|
12
|
+
def initialize(item, redis_pool = nil)
|
13
|
+
super(item, redis_pool)
|
14
|
+
@worker_class = item[CLASS]
|
15
15
|
end
|
16
16
|
|
17
17
|
# Create a new job from the current one.
|
18
18
|
# This will mess up sidekiq stats because a new job is created
|
19
19
|
def call
|
20
|
-
worker_class&.perform_in(5, *item[
|
20
|
+
worker_class&.perform_in(5, *item[ARGS]) if sidekiq_worker_class?
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
@@ -7,15 +7,27 @@ module SidekiqUniqueJobs
|
|
7
7
|
# @abstract
|
8
8
|
# @author Mikael Henriksson <mikael@zoolutions.se>
|
9
9
|
class Strategy
|
10
|
+
include SidekiqUniqueJobs::JSON
|
10
11
|
include SidekiqUniqueJobs::Logging
|
12
|
+
include SidekiqUniqueJobs::Script::Caller
|
13
|
+
include SidekiqUniqueJobs::Timing
|
11
14
|
|
12
|
-
#
|
13
|
-
#
|
15
|
+
# @!attribute [r] item
|
16
|
+
# @return [Hash] sidekiq job hash
|
14
17
|
attr_reader :item
|
18
|
+
# @!attribute [r] redis_pool
|
19
|
+
# @return [Sidekiq::RedisConnection, ConnectionPool, NilClass] the redis connection
|
20
|
+
attr_reader :redis_pool
|
15
21
|
|
16
|
-
#
|
17
|
-
|
18
|
-
|
22
|
+
#
|
23
|
+
# Initialize a new Strategy
|
24
|
+
#
|
25
|
+
# @param [Hash] item sidekiq job hash
|
26
|
+
# @param [ConnectionPool] redis_pool the connection pool instance
|
27
|
+
#
|
28
|
+
def initialize(item, redis_pool = nil)
|
29
|
+
@item = item
|
30
|
+
@redis_pool = redis_pool
|
19
31
|
end
|
20
32
|
|
21
33
|
# Use strategy on conflict
|
@@ -24,6 +36,13 @@ module SidekiqUniqueJobs
|
|
24
36
|
raise NotImplementedError, "needs to be implemented in child class"
|
25
37
|
end
|
26
38
|
|
39
|
+
#
|
40
|
+
# Check if the strategy is kind of {Replace}
|
41
|
+
#
|
42
|
+
#
|
43
|
+
# @return [true] when the strategy is a {Replace}
|
44
|
+
# @return [false] when the strategy is not a {Replace}
|
45
|
+
#
|
27
46
|
def replace?
|
28
47
|
is_a?(Replace)
|
29
48
|
end
|
@@ -9,24 +9,19 @@ module SidekiqUniqueJobs
|
|
9
9
|
# 3. worker_class (required, can be anything)
|
10
10
|
# @author Mikael Henriksson <mikael@zoolutions.se>
|
11
11
|
module OptionsWithFallback
|
12
|
-
LOCKS = {
|
13
|
-
until_and_while_executing: SidekiqUniqueJobs::Lock::UntilAndWhileExecuting,
|
14
|
-
until_executed: SidekiqUniqueJobs::Lock::UntilExecuted,
|
15
|
-
until_executing: SidekiqUniqueJobs::Lock::UntilExecuting,
|
16
|
-
until_expired: SidekiqUniqueJobs::Lock::UntilExpired,
|
17
|
-
until_timeout: SidekiqUniqueJobs::Lock::UntilExpired,
|
18
|
-
while_executing: SidekiqUniqueJobs::Lock::WhileExecuting,
|
19
|
-
while_executing_reject: SidekiqUniqueJobs::Lock::WhileExecutingReject,
|
20
|
-
}.freeze
|
21
|
-
|
22
12
|
def self.included(base)
|
23
13
|
base.send(:include, SidekiqUniqueJobs::SidekiqWorkerMethods)
|
24
14
|
end
|
25
15
|
|
16
|
+
# A convenience method for using the configured locks
|
17
|
+
def locks
|
18
|
+
SidekiqUniqueJobs.locks
|
19
|
+
end
|
20
|
+
|
26
21
|
# Check if unique has been enabled
|
27
22
|
# @return [true, false] indicate if the gem has been enabled
|
28
23
|
def unique_enabled?
|
29
|
-
SidekiqUniqueJobs.
|
24
|
+
SidekiqUniqueJobs.enabled? && lock_type
|
30
25
|
end
|
31
26
|
|
32
27
|
# Check if unique has been disabled
|
@@ -34,36 +29,65 @@ module SidekiqUniqueJobs
|
|
34
29
|
!unique_enabled?
|
35
30
|
end
|
36
31
|
|
37
|
-
#
|
38
|
-
|
39
|
-
|
32
|
+
# Should duplicate payloads be logged?
|
33
|
+
#
|
34
|
+
#
|
35
|
+
# @return [true, false, nil]
|
36
|
+
#
|
37
|
+
def log_duplicate?
|
38
|
+
options[LOG_DUPLICATE] || item[LOG_DUPLICATE]
|
40
39
|
end
|
41
40
|
|
42
|
-
#
|
43
|
-
#
|
44
|
-
|
45
|
-
|
41
|
+
#
|
42
|
+
# A new lock for this Sidekiq Job
|
43
|
+
#
|
44
|
+
#
|
45
|
+
# @return [Lock::BaseLock] an instance of a lock implementation
|
46
|
+
#
|
47
|
+
def lock_instance
|
48
|
+
@lock_instance ||= lock_class.new(item, after_unlock_hook, @redis_pool)
|
46
49
|
end
|
47
50
|
|
48
|
-
#
|
49
|
-
#
|
51
|
+
#
|
52
|
+
# Returns the corresponding class for the lock_type
|
53
|
+
#
|
54
|
+
#
|
55
|
+
# @return [Class]
|
56
|
+
#
|
50
57
|
def lock_class
|
51
58
|
@lock_class ||= begin
|
52
|
-
|
59
|
+
locks.fetch(lock_type.to_sym) do
|
53
60
|
raise UnknownLock, "No implementation for `lock: :#{lock_type}`"
|
54
61
|
end
|
55
62
|
end
|
56
63
|
end
|
57
64
|
|
65
|
+
#
|
66
|
+
# The type of lock for this worker
|
67
|
+
#
|
68
|
+
#
|
58
69
|
# @return [Symbol]
|
70
|
+
#
|
59
71
|
def lock_type
|
60
|
-
@lock_type ||=
|
72
|
+
@lock_type ||= options[LOCK] || item[LOCK] || unique_type
|
61
73
|
end
|
62
74
|
|
75
|
+
#
|
76
|
+
# @deprecated in favour of {lock_type}
|
77
|
+
#
|
78
|
+
#
|
79
|
+
# @return [Symbol]
|
80
|
+
#
|
63
81
|
def unique_type
|
64
|
-
options[
|
82
|
+
options[UNIQUE] || item[UNIQUE]
|
65
83
|
end
|
66
84
|
|
85
|
+
#
|
86
|
+
# The default options with any matching keys overridden from worker options
|
87
|
+
#
|
88
|
+
#
|
89
|
+
# @return [Hash<String, Object>]
|
90
|
+
#
|
67
91
|
def options
|
68
92
|
@options ||= begin
|
69
93
|
opts = default_worker_options.dup
|