sidekiq-unique-jobs 6.0.24 → 7.0.4
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 +707 -25
- data/README.md +516 -105
- data/lib/sidekiq_unique_jobs.rb +48 -7
- data/lib/sidekiq_unique_jobs/batch_delete.rb +123 -0
- data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
- data/lib/sidekiq_unique_jobs/cli.rb +34 -31
- data/lib/sidekiq_unique_jobs/config.rb +263 -0
- data/lib/sidekiq_unique_jobs/connection.rb +6 -5
- data/lib/sidekiq_unique_jobs/constants.rb +46 -24
- data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
- data/lib/sidekiq_unique_jobs/digests.rb +71 -100
- data/lib/sidekiq_unique_jobs/exceptions.rb +78 -12
- data/lib/sidekiq_unique_jobs/job.rb +41 -12
- data/lib/sidekiq_unique_jobs/json.rb +40 -0
- data/lib/sidekiq_unique_jobs/key.rb +93 -0
- data/lib/sidekiq_unique_jobs/lock.rb +325 -0
- data/lib/sidekiq_unique_jobs/lock/base_lock.rb +66 -50
- 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 +7 -10
- data/lib/sidekiq_unique_jobs/lock/until_executed.rb +6 -6
- data/lib/sidekiq_unique_jobs/lock/until_executing.rb +1 -1
- data/lib/sidekiq_unique_jobs/lock/until_expired.rb +4 -21
- data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +13 -9
- data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -3
- data/lib/sidekiq_unique_jobs/lock_args.rb +123 -0
- data/lib/sidekiq_unique_jobs/lock_config.rb +122 -0
- data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
- data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
- data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
- data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
- data/lib/sidekiq_unique_jobs/locksmith.rb +261 -101
- data/lib/sidekiq_unique_jobs/logging.rb +149 -23
- 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 +42 -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/lock.lua +93 -0
- data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
- data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
- data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +94 -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 +22 -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_process_set.lua +53 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -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/unlock.lua +95 -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 +29 -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 +4 -4
- data/lib/sidekiq_unique_jobs/on_conflict.rb +23 -10
- data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
- data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
- data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +1 -1
- data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +61 -15
- data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
- data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +12 -5
- data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +25 -6
- data/lib/sidekiq_unique_jobs/options_with_fallback.rb +41 -27
- data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
- data/lib/sidekiq_unique_jobs/orphans/manager.rb +212 -0
- data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
- data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
- data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
- data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +201 -0
- data/lib/sidekiq_unique_jobs/redis.rb +11 -0
- data/lib/sidekiq_unique_jobs/redis/entity.rb +106 -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 +86 -0
- data/lib/sidekiq_unique_jobs/redis/string.rb +49 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
- data/lib/sidekiq_unique_jobs/script.rb +15 -0
- data/lib/sidekiq_unique_jobs/script/caller.rb +125 -0
- data/lib/sidekiq_unique_jobs/server.rb +48 -0
- data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +92 -65
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +185 -34
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +11 -5
- data/lib/sidekiq_unique_jobs/testing.rb +62 -21
- data/lib/sidekiq_unique_jobs/timer_task.rb +78 -0
- data/lib/sidekiq_unique_jobs/timing.rb +58 -0
- data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
- data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
- data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
- data/lib/sidekiq_unique_jobs/version.rb +3 -1
- data/lib/sidekiq_unique_jobs/version_check.rb +23 -4
- data/lib/sidekiq_unique_jobs/web.rb +50 -27
- data/lib/sidekiq_unique_jobs/web/helpers.rb +125 -10
- data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
- 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 +5 -5
- metadata +117 -177
- data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
- data/lib/sidekiq_unique_jobs/scripts.rb +0 -118
- 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/unique_args.rb +0 -150
- 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
@@ -5,10 +5,21 @@ module SidekiqUniqueJobs
|
|
5
5
|
# Abstract base class for locks
|
6
6
|
#
|
7
7
|
# @abstract
|
8
|
-
# @author Mikael Henriksson <mikael@
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
9
|
class BaseLock
|
10
10
|
include SidekiqUniqueJobs::Logging
|
11
11
|
|
12
|
+
#
|
13
|
+
# Validates that the sidekiq_options for the worker is valid
|
14
|
+
#
|
15
|
+
# @param [Hash] options the sidekiq_options given to the worker
|
16
|
+
#
|
17
|
+
# @return [void]
|
18
|
+
#
|
19
|
+
def self.validate_options(options = {})
|
20
|
+
Validator.validate(options)
|
21
|
+
end
|
22
|
+
|
12
23
|
# @param [Hash] item the Sidekiq job hash
|
13
24
|
# @param [Proc] callback the callback to use after unlock
|
14
25
|
# @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
|
@@ -16,21 +27,24 @@ module SidekiqUniqueJobs
|
|
16
27
|
@item = item
|
17
28
|
@callback = callback
|
18
29
|
@redis_pool = redis_pool
|
19
|
-
|
30
|
+
@attempt = 0
|
31
|
+
prepare_item # Used to ease testing
|
32
|
+
@lock_config = LockConfig.new(item)
|
20
33
|
end
|
21
34
|
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
35
|
+
#
|
36
|
+
# Locks a sidekiq job
|
37
|
+
#
|
38
|
+
# @note Will call a conflict strategy if lock can't be achieved.
|
39
|
+
#
|
40
|
+
# @return [String, nil] the locked jid when properly locked, else nil.
|
41
|
+
#
|
42
|
+
# @yield to the caller when given a block
|
43
|
+
#
|
44
|
+
def lock(&block)
|
45
|
+
return call_strategy unless (locked_token = locksmith.lock(&block))
|
46
|
+
|
47
|
+
locked_token
|
34
48
|
end
|
35
49
|
|
36
50
|
# Execute the job in the Sidekiq server processor
|
@@ -43,7 +57,7 @@ module SidekiqUniqueJobs
|
|
43
57
|
# @return [String] sidekiq job id when successful
|
44
58
|
# @return [false] when unsuccessful
|
45
59
|
def unlock
|
46
|
-
locksmith.unlock
|
60
|
+
locksmith.unlock # Only signal to release the lock
|
47
61
|
end
|
48
62
|
|
49
63
|
# Deletes the job from redis if it is locked.
|
@@ -61,75 +75,77 @@ module SidekiqUniqueJobs
|
|
61
75
|
# @return [true] when this jid has locked the job
|
62
76
|
# @return [false] when this jid has not locked the job
|
63
77
|
def locked?
|
64
|
-
locksmith.locked?
|
78
|
+
locksmith.locked?
|
79
|
+
end
|
80
|
+
|
81
|
+
#
|
82
|
+
# The lock manager/client
|
83
|
+
#
|
84
|
+
# @api private
|
85
|
+
# @return [SidekiqUniqueJobs::Locksmith] the locksmith for this sidekiq job
|
86
|
+
#
|
87
|
+
def locksmith
|
88
|
+
@locksmith ||= SidekiqUniqueJobs::Locksmith.new(item, redis_pool)
|
65
89
|
end
|
66
90
|
|
67
91
|
private
|
68
92
|
|
69
|
-
def
|
70
|
-
return if item.key?(
|
93
|
+
def prepare_item
|
94
|
+
return if item.key?(LOCK_DIGEST)
|
71
95
|
|
72
96
|
# The below should only be done to ease testing
|
73
97
|
# in production this will be done by the middleware
|
74
|
-
SidekiqUniqueJobs::Job.
|
98
|
+
SidekiqUniqueJobs::Job.prepare(item)
|
75
99
|
end
|
76
100
|
|
77
101
|
def call_strategy
|
78
102
|
@attempt += 1
|
79
|
-
|
103
|
+
client_strategy.call { lock if replace? }
|
80
104
|
end
|
81
105
|
|
82
106
|
def replace?
|
83
|
-
|
107
|
+
client_strategy.replace? && attempt < 2
|
84
108
|
end
|
85
109
|
|
86
|
-
#
|
87
|
-
#
|
110
|
+
# @!attribute [r] item
|
111
|
+
# @return [Hash<String, Object>] the Sidekiq job hash
|
88
112
|
attr_reader :item
|
89
|
-
|
90
|
-
#
|
91
|
-
|
113
|
+
# @!attribute [r] lock_config
|
114
|
+
# @return [LockConfig] a lock configuration
|
115
|
+
attr_reader :lock_config
|
116
|
+
# @!attribute [r] redis_pool
|
117
|
+
# @return [Sidekiq::RedisConnection, ConnectionPool, NilClass] the redis connection
|
92
118
|
attr_reader :redis_pool
|
93
|
-
|
94
|
-
#
|
95
|
-
# @return [Proc] the callback to use after unlock
|
119
|
+
# @!attribute [r] callback
|
120
|
+
# @return [Proc] the block to call after unlock
|
96
121
|
attr_reader :callback
|
97
|
-
|
98
|
-
#
|
99
|
-
# @return [Integer] the numerical value of the attempt
|
122
|
+
# @!attribute [r] attempt
|
123
|
+
# @return [Integer] the current locking attempt
|
100
124
|
attr_reader :attempt
|
101
125
|
|
102
|
-
# The interface to the locking mechanism
|
103
|
-
# @return [SidekiqUniqueJobs::Locksmith]
|
104
|
-
def locksmith
|
105
|
-
@locksmith ||= SidekiqUniqueJobs::Locksmith.new(item, redis_pool)
|
106
|
-
end
|
107
|
-
|
108
|
-
def with_cleanup
|
109
|
-
yield
|
110
|
-
rescue Sidekiq::Shutdown
|
111
|
-
log_info("Sidekiq is shutting down, the job `should` be put back on the queue. Keeping the lock!")
|
112
|
-
raise
|
113
|
-
else
|
114
|
-
unlock_with_callback
|
115
|
-
end
|
116
|
-
|
117
126
|
def unlock_with_callback
|
118
127
|
return log_warn("might need to be unlocked manually") unless unlock
|
119
128
|
|
120
129
|
callback_safely
|
130
|
+
item[JID]
|
121
131
|
end
|
122
132
|
|
123
133
|
def callback_safely
|
124
134
|
callback&.call
|
125
|
-
item[
|
135
|
+
item[JID]
|
126
136
|
rescue StandardError
|
127
137
|
log_warn("unlocked successfully but the #after_unlock callback failed!")
|
128
138
|
raise
|
129
139
|
end
|
130
140
|
|
131
|
-
def
|
132
|
-
@
|
141
|
+
def client_strategy
|
142
|
+
@client_strategy ||=
|
143
|
+
OnConflict.find_strategy(lock_config.on_client_conflict).new(item, redis_pool)
|
144
|
+
end
|
145
|
+
|
146
|
+
def server_strategy
|
147
|
+
@server_strategy ||=
|
148
|
+
OnConflict.find_strategy(lock_config.on_server_conflict).new(item, redis_pool)
|
133
149
|
end
|
134
150
|
end
|
135
151
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
class Lock
|
5
|
+
#
|
6
|
+
# Validates the sidekiq options for the Sidekiq client process
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class ClientValidator
|
11
|
+
#
|
12
|
+
# @return [Array<Symbol>] a collection of invalid conflict resolutions
|
13
|
+
INVALID_ON_CONFLICTS = [:raise, :reject, :reschedule].freeze
|
14
|
+
|
15
|
+
#
|
16
|
+
# Validates the sidekiq options for the Sidekiq client process
|
17
|
+
#
|
18
|
+
#
|
19
|
+
def self.validate(lock_config)
|
20
|
+
on_conflict = lock_config.on_client_conflict
|
21
|
+
return lock_config unless INVALID_ON_CONFLICTS.include?(on_conflict)
|
22
|
+
|
23
|
+
lock_config.errors[:on_client_conflict] = "#{on_conflict} is incompatible with the client process"
|
24
|
+
lock_config
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
class Lock
|
5
|
+
#
|
6
|
+
# Validates the sidekiq options for the Sidekiq server process
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class ServerValidator
|
11
|
+
#
|
12
|
+
# @return [Array<Symbol>] a collection of invalid conflict resolutions
|
13
|
+
INVALID_ON_CONFLICTS = [:replace].freeze
|
14
|
+
|
15
|
+
#
|
16
|
+
# Validates the sidekiq options for the Sidekiq server process
|
17
|
+
#
|
18
|
+
#
|
19
|
+
def self.validate(lock_config)
|
20
|
+
on_conflict = lock_config.on_server_conflict
|
21
|
+
return lock_config unless INVALID_ON_CONFLICTS.include?(on_conflict)
|
22
|
+
|
23
|
+
lock_config.errors[:on_server_conflict] = "#{on_conflict} is incompatible with the server process"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -11,7 +11,7 @@ module SidekiqUniqueJobs
|
|
11
11
|
# See {#lock} for more information about the client.
|
12
12
|
# See {#execute} for more information about the server
|
13
13
|
#
|
14
|
-
# @author Mikael Henriksson <mikael@
|
14
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
15
15
|
class UntilAndWhileExecuting < BaseLock
|
16
16
|
# Executes in the Sidekiq server process
|
17
17
|
# @yield to the worker class perform method
|
@@ -21,26 +21,23 @@ module SidekiqUniqueJobs
|
|
21
21
|
runtime_lock.execute { return yield }
|
22
22
|
end
|
23
23
|
else
|
24
|
-
log_warn "couldn't unlock digest: #{item[
|
24
|
+
log_warn "couldn't unlock digest: #{item[LOCK_DIGEST]} #{item[JID]}"
|
25
25
|
end
|
26
|
-
ensure
|
27
|
-
runtime_lock.delete!
|
28
|
-
end
|
29
|
-
|
30
|
-
def runtime_lock
|
31
|
-
@runtime_lock ||= SidekiqUniqueJobs::Lock::WhileExecuting.new(item, callback, redis_pool)
|
32
26
|
end
|
33
27
|
|
34
28
|
private
|
35
29
|
|
36
30
|
def lock_on_failure
|
37
31
|
yield
|
38
|
-
runtime_lock.delete!
|
39
32
|
rescue Exception # rubocop:disable Lint/RescueException
|
40
|
-
log_error("
|
33
|
+
log_error("Runtime lock failed to execute job, restoring server lock")
|
41
34
|
lock
|
42
35
|
raise
|
43
36
|
end
|
37
|
+
|
38
|
+
def runtime_lock
|
39
|
+
@runtime_lock ||= SidekiqUniqueJobs::Lock::WhileExecuting.new(item, callback, redis_pool)
|
40
|
+
end
|
44
41
|
end
|
45
42
|
end
|
46
43
|
end
|
@@ -6,18 +6,18 @@ module SidekiqUniqueJobs
|
|
6
6
|
# - Locks on perform_in or perform_async
|
7
7
|
# - Unlocks after yielding to the worker's perform method
|
8
8
|
#
|
9
|
-
# @author Mikael Henriksson <mikael@
|
9
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
10
10
|
class UntilExecuted < BaseLock
|
11
11
|
OK ||= "OK"
|
12
12
|
|
13
13
|
# Executes in the Sidekiq server process
|
14
14
|
# @yield to the worker class perform method
|
15
15
|
def execute
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
lock do
|
17
|
+
yield
|
18
|
+
unlock_with_callback
|
19
|
+
callback_safely
|
20
|
+
item[JID]
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
@@ -6,7 +6,7 @@ module SidekiqUniqueJobs
|
|
6
6
|
# - Locks on perform_in or perform_async
|
7
7
|
# - Unlocks before yielding to the worker's perform method
|
8
8
|
#
|
9
|
-
# @author Mikael Henriksson <mikael@
|
9
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
10
10
|
class UntilExecuting < BaseLock
|
11
11
|
# Executes in the Sidekiq server process
|
12
12
|
# @yield to the worker class perform method
|
@@ -2,29 +2,12 @@
|
|
2
2
|
|
3
3
|
module SidekiqUniqueJobs
|
4
4
|
class Lock
|
5
|
-
# Locks jobs until the lock has expired
|
6
|
-
# - Locks on perform_in or perform_async
|
7
|
-
# - Unlocks when the expiration is hit
|
8
5
|
#
|
9
|
-
#
|
10
|
-
# See {#execute} for more information about the server
|
6
|
+
# UntilExpired locks until the job expires
|
11
7
|
#
|
12
|
-
# @author Mikael Henriksson <mikael@
|
13
|
-
|
14
|
-
|
15
|
-
# @return [true] always returns true
|
16
|
-
def unlock
|
17
|
-
true
|
18
|
-
end
|
19
|
-
|
20
|
-
# Executes in the Sidekiq server process
|
21
|
-
# @yield to the worker class perform method
|
22
|
-
def execute
|
23
|
-
return unless locked?
|
24
|
-
|
25
|
-
yield
|
26
|
-
# this lock does not handle after_unlock since we don't know when that would happen
|
27
|
-
end
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class UntilExpired < UntilExecuted
|
28
11
|
end
|
29
12
|
end
|
30
13
|
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
class Lock
|
5
|
+
#
|
6
|
+
# Validator base class to avoid some duplication
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class Validator
|
11
|
+
#
|
12
|
+
# @return [Hash] a hash mapping of deprecated keys and their new value
|
13
|
+
DEPRECATED_KEYS = {
|
14
|
+
UNIQUE.to_sym => LOCK.to_sym,
|
15
|
+
UNIQUE_ARGS.to_sym => LOCK_ARGS_METHOD.to_sym,
|
16
|
+
LOCK_ARGS.to_sym => LOCK_ARGS_METHOD.to_sym,
|
17
|
+
UNIQUE_PREFIX.to_sym => LOCK_PREFIX.to_sym,
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
#
|
21
|
+
# Shorthand for `new(options).validate`
|
22
|
+
#
|
23
|
+
# @param [Hash] options the sidekiq_options for the worker being validated
|
24
|
+
#
|
25
|
+
# @return [LockConfig] the lock configuration with errors if any
|
26
|
+
#
|
27
|
+
def self.validate(options)
|
28
|
+
new(options).validate
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# @!attribute [r] lock_config
|
33
|
+
# @return [LockConfig] the lock configuration for this worker
|
34
|
+
attr_reader :lock_config
|
35
|
+
|
36
|
+
#
|
37
|
+
# Initialize a new validator
|
38
|
+
#
|
39
|
+
# @param [Hash] options the sidekiq_options for the worker being validated
|
40
|
+
#
|
41
|
+
def initialize(options)
|
42
|
+
@options = options.transform_keys(&:to_sym)
|
43
|
+
@lock_config = LockConfig.new(options)
|
44
|
+
handle_deprecations
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# Validate the workers lock configuration
|
49
|
+
#
|
50
|
+
#
|
51
|
+
# @return [LockConfig] the lock configuration with errors if any
|
52
|
+
#
|
53
|
+
def validate
|
54
|
+
case lock_config.type
|
55
|
+
when :while_executing
|
56
|
+
validate_server
|
57
|
+
when :until_executing
|
58
|
+
validate_client
|
59
|
+
else
|
60
|
+
validate_client
|
61
|
+
validate_server
|
62
|
+
end
|
63
|
+
|
64
|
+
lock_config
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# Validate deprecated keys
|
69
|
+
# adds useful information about how to proceed with fixing handle_deprecations
|
70
|
+
#
|
71
|
+
# @return [void]
|
72
|
+
#
|
73
|
+
def handle_deprecations
|
74
|
+
DEPRECATED_KEYS.each do |old, new|
|
75
|
+
next unless @options.key?(old)
|
76
|
+
|
77
|
+
lock_config.errors[old] = "is deprecated, use `#{new}: #{@options[old]}` instead."
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
#
|
82
|
+
# Validates the client configuration
|
83
|
+
#
|
84
|
+
def validate_client
|
85
|
+
ClientValidator.validate(lock_config)
|
86
|
+
end
|
87
|
+
|
88
|
+
#
|
89
|
+
# Validates the server configuration
|
90
|
+
#
|
91
|
+
def validate_server
|
92
|
+
ServerValidator.validate(lock_config)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|