sidekiq-unique-jobs 6.0.25 → 7.1.5
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 +840 -41
- data/README.md +814 -284
- 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 +275 -0
- data/lib/sidekiq_unique_jobs/connection.rb +6 -5
- data/lib/sidekiq_unique_jobs/constants.rb +45 -25
- data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
- data/lib/sidekiq_unique_jobs/deprecation.rb +35 -0
- data/lib/sidekiq_unique_jobs/digests.rb +71 -100
- data/lib/sidekiq_unique_jobs/exceptions.rb +87 -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/base_lock.rb +100 -79
- 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 +34 -15
- data/lib/sidekiq_unique_jobs/lock/until_executed.rb +19 -7
- data/lib/sidekiq_unique_jobs/lock/until_executing.rb +16 -2
- data/lib/sidekiq_unique_jobs/lock/until_expired.rb +20 -16
- data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +19 -10
- data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -3
- data/lib/sidekiq_unique_jobs/lock.rb +325 -0
- data/lib/sidekiq_unique_jobs/lock_args.rb +123 -0
- data/lib/sidekiq_unique_jobs/lock_config.rb +126 -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 +275 -102
- data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
- data/lib/sidekiq_unique_jobs/logging.rb +179 -33
- 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/client.rb +40 -0
- data/lib/sidekiq_unique_jobs/middleware/server.rb +29 -0
- data/lib/sidekiq_unique_jobs/middleware.rb +29 -43
- data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
- 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 +16 -5
- data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +25 -6
- data/lib/sidekiq_unique_jobs/on_conflict.rb +23 -10
- data/lib/sidekiq_unique_jobs/options_with_fallback.rb +34 -29
- data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
- data/lib/sidekiq_unique_jobs/orphans/manager.rb +213 -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/reaper_resurrector.rb +170 -0
- data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +213 -0
- data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -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/redis.rb +11 -0
- data/lib/sidekiq_unique_jobs/reflectable.rb +17 -0
- data/lib/sidekiq_unique_jobs/reflections.rb +68 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
- data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
- data/lib/sidekiq_unique_jobs/script.rb +15 -0
- data/lib/sidekiq_unique_jobs/server.rb +49 -0
- data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +92 -65
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +241 -35
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +11 -15
- 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/helpers.rb +128 -13
- data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
- 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 +54 -0
- data/lib/sidekiq_unique_jobs/web.rb +57 -27
- data/lib/sidekiq_unique_jobs.rb +52 -7
- data/lib/tasks/changelog.rake +5 -5
- metadata +121 -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/calculator.rb +0 -63
- data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
- 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,33 @@ 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
|
+
extend Forwardable
|
11
|
+
|
12
|
+
# includes "SidekiqUniqueJobs::Logging"
|
13
|
+
# @!parse include SidekiqUniqueJobs::Logging
|
10
14
|
include SidekiqUniqueJobs::Logging
|
11
15
|
|
16
|
+
# includes "SidekiqUniqueJobs::Reflectable"
|
17
|
+
# @!parse include SidekiqUniqueJobs::Reflectable
|
18
|
+
include SidekiqUniqueJobs::Reflectable
|
19
|
+
|
20
|
+
#
|
21
|
+
# Validates that the sidekiq_options for the worker is valid
|
22
|
+
#
|
23
|
+
# @param [Hash] options the sidekiq_options given to the worker
|
24
|
+
#
|
25
|
+
# @return [void]
|
26
|
+
#
|
27
|
+
def self.validate_options(options = {})
|
28
|
+
Validator.validate(options)
|
29
|
+
end
|
30
|
+
|
31
|
+
# NOTE: Mainly used for a clean testing API
|
32
|
+
#
|
33
|
+
def_delegators :locksmith, :locked?
|
34
|
+
|
12
35
|
# @param [Hash] item the Sidekiq job hash
|
13
36
|
# @param [Proc] callback the callback to use after unlock
|
14
37
|
# @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
|
@@ -16,21 +39,22 @@ module SidekiqUniqueJobs
|
|
16
39
|
@item = item
|
17
40
|
@callback = callback
|
18
41
|
@redis_pool = redis_pool
|
19
|
-
|
42
|
+
@attempt = 0
|
43
|
+
prepare_item # Used to ease testing
|
44
|
+
@lock_config = LockConfig.new(item)
|
20
45
|
end
|
21
46
|
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
47
|
+
#
|
48
|
+
# Locks a sidekiq job
|
49
|
+
#
|
50
|
+
# @note Will call a conflict strategy if lock can't be achieved.
|
51
|
+
#
|
52
|
+
# @return [String, nil] the locked jid when properly locked, else nil.
|
53
|
+
#
|
54
|
+
# @yield to the caller when given a block
|
55
|
+
#
|
25
56
|
def lock
|
26
|
-
|
27
|
-
return item[JID_KEY] if locked?
|
28
|
-
|
29
|
-
if (token = locksmith.lock(item[LOCK_TIMEOUT_KEY]))
|
30
|
-
token
|
31
|
-
else
|
32
|
-
call_strategy
|
33
|
-
end
|
57
|
+
raise NotImplementedError, "##{__method__} needs to be implemented in #{self.class}"
|
34
58
|
end
|
35
59
|
|
36
60
|
# Execute the job in the Sidekiq server processor
|
@@ -39,97 +63,94 @@ module SidekiqUniqueJobs
|
|
39
63
|
raise NotImplementedError, "##{__method__} needs to be implemented in #{self.class}"
|
40
64
|
end
|
41
65
|
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
def delete
|
51
|
-
locksmith.delete # Soft delete (don't forcefully remove when expiration is set)
|
52
|
-
end
|
53
|
-
|
54
|
-
# Forcefully deletes the job from redis.
|
55
|
-
# This is good for jobs when a previous lock was not unlocked
|
56
|
-
def delete!
|
57
|
-
locksmith.delete! # Force delete the lock
|
58
|
-
end
|
59
|
-
|
60
|
-
# Checks if the item has achieved a lock
|
61
|
-
# @return [true] when this jid has locked the job
|
62
|
-
# @return [false] when this jid has not locked the job
|
63
|
-
def locked?
|
64
|
-
locksmith.locked?(item[JID_KEY])
|
66
|
+
#
|
67
|
+
# The lock manager/client
|
68
|
+
#
|
69
|
+
# @api private
|
70
|
+
# @return [SidekiqUniqueJobs::Locksmith] the locksmith for this sidekiq job
|
71
|
+
#
|
72
|
+
def locksmith
|
73
|
+
@locksmith ||= SidekiqUniqueJobs::Locksmith.new(item, redis_pool)
|
65
74
|
end
|
66
75
|
|
67
76
|
private
|
68
77
|
|
69
|
-
|
70
|
-
|
78
|
+
# @!attribute [r] item
|
79
|
+
# @return [Hash<String, Object>] the Sidekiq job hash
|
80
|
+
attr_reader :item
|
81
|
+
# @!attribute [r] lock_config
|
82
|
+
# @return [LockConfig] a lock configuration
|
83
|
+
attr_reader :lock_config
|
84
|
+
# @!attribute [r] redis_pool
|
85
|
+
# @return [Sidekiq::RedisConnection, ConnectionPool, NilClass] the redis connection
|
86
|
+
attr_reader :redis_pool
|
87
|
+
# @!attribute [r] callback
|
88
|
+
# @return [Proc] the block to call after unlock
|
89
|
+
attr_reader :callback
|
90
|
+
# @!attribute [r] attempt
|
91
|
+
# @return [Integer] the current locking attempt
|
92
|
+
attr_reader :attempt
|
93
|
+
|
94
|
+
def prepare_item
|
95
|
+
return if item.key?(LOCK_DIGEST)
|
71
96
|
|
72
97
|
# The below should only be done to ease testing
|
73
98
|
# in production this will be done by the middleware
|
74
|
-
SidekiqUniqueJobs::Job.
|
99
|
+
SidekiqUniqueJobs::Job.prepare(item)
|
75
100
|
end
|
76
101
|
|
77
|
-
|
78
|
-
|
79
|
-
|
102
|
+
#
|
103
|
+
# Handle when lock failed
|
104
|
+
#
|
105
|
+
# @param [Symbol] location: :client or :server
|
106
|
+
#
|
107
|
+
# @return [void]
|
108
|
+
#
|
109
|
+
def lock_failed(origin: :client)
|
110
|
+
reflect(:lock_failed, item)
|
111
|
+
call_strategy(origin: origin)
|
80
112
|
end
|
81
113
|
|
82
|
-
def
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
# The sidekiq job hash
|
87
|
-
# @return [Hash] the Sidekiq job hash
|
88
|
-
attr_reader :item
|
89
|
-
|
90
|
-
# The sidekiq redis pool
|
91
|
-
# @return [Sidekiq::RedisConnection, ConnectionPool, NilClass] the redis connection
|
92
|
-
attr_reader :redis_pool
|
93
|
-
|
94
|
-
# The sidekiq job hash
|
95
|
-
# @return [Proc] the callback to use after unlock
|
96
|
-
attr_reader :callback
|
97
|
-
|
98
|
-
# The current attempt to lock the job
|
99
|
-
# @return [Integer] the numerical value of the attempt
|
100
|
-
attr_reader :attempt
|
114
|
+
def call_strategy(origin:)
|
115
|
+
@attempt += 1
|
101
116
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
117
|
+
case origin
|
118
|
+
when :client
|
119
|
+
client_strategy.call { lock if replace? }
|
120
|
+
when :server
|
121
|
+
server_strategy.call { lock if replace? }
|
122
|
+
else
|
123
|
+
raise SidekiqUniqueJobs::InvalidArgument,
|
124
|
+
"either `for: :server` or `for: :client` needs to be specified"
|
125
|
+
end
|
106
126
|
end
|
107
127
|
|
108
|
-
def
|
109
|
-
|
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
|
128
|
+
def replace?
|
129
|
+
client_strategy.replace? && attempt < 2
|
115
130
|
end
|
116
131
|
|
117
|
-
def
|
118
|
-
return
|
132
|
+
def unlock_and_callback
|
133
|
+
return callback_safely if locksmith.unlock
|
119
134
|
|
120
|
-
|
135
|
+
reflect(:unlock_failed, item)
|
121
136
|
end
|
122
137
|
|
123
138
|
def callback_safely
|
124
139
|
callback&.call
|
125
|
-
item[
|
140
|
+
item[JID]
|
126
141
|
rescue StandardError
|
127
|
-
|
142
|
+
reflect(:after_unlock_callback_failed, item)
|
128
143
|
raise
|
129
144
|
end
|
130
145
|
|
131
|
-
def
|
132
|
-
@
|
146
|
+
def client_strategy
|
147
|
+
@client_strategy ||=
|
148
|
+
OnConflict.find_strategy(lock_config.on_client_conflict).new(item, redis_pool)
|
149
|
+
end
|
150
|
+
|
151
|
+
def server_strategy
|
152
|
+
@server_strategy ||=
|
153
|
+
OnConflict.find_strategy(lock_config.on_server_conflict).new(item, redis_pool)
|
133
154
|
end
|
134
155
|
end
|
135
156
|
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,36 +11,55 @@ 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
|
+
#
|
17
|
+
# Locks a sidekiq job
|
18
|
+
#
|
19
|
+
# @note Will call a conflict strategy if lock can't be achieved.
|
20
|
+
#
|
21
|
+
# @return [String, nil] the locked jid when properly locked, else nil.
|
22
|
+
#
|
23
|
+
# @yield to the caller when given a block
|
24
|
+
#
|
25
|
+
def lock(origin: :client)
|
26
|
+
return lock_failed(origin: origin) unless (token = locksmith.lock)
|
27
|
+
return yield token if block_given?
|
28
|
+
|
29
|
+
token
|
30
|
+
end
|
31
|
+
|
16
32
|
# Executes in the Sidekiq server process
|
17
33
|
# @yield to the worker class perform method
|
18
34
|
def execute
|
19
|
-
if unlock
|
20
|
-
|
21
|
-
|
22
|
-
end
|
35
|
+
if locksmith.unlock
|
36
|
+
# ensure_relocked do
|
37
|
+
runtime_lock.execute { return yield }
|
38
|
+
# end
|
23
39
|
else
|
24
|
-
|
40
|
+
reflect(:unlock_failed, item)
|
25
41
|
end
|
26
|
-
|
27
|
-
|
28
|
-
|
42
|
+
rescue Exception # rubocop:disable Lint/RescueException
|
43
|
+
reflect(:execution_failed, item)
|
44
|
+
locksmith.lock(wait: 2)
|
29
45
|
|
30
|
-
|
31
|
-
@runtime_lock ||= SidekiqUniqueJobs::Lock::WhileExecuting.new(item, callback, redis_pool)
|
46
|
+
raise
|
32
47
|
end
|
33
48
|
|
34
49
|
private
|
35
50
|
|
36
|
-
def
|
51
|
+
def ensure_relocked
|
37
52
|
yield
|
38
|
-
runtime_lock.delete!
|
39
53
|
rescue Exception # rubocop:disable Lint/RescueException
|
40
|
-
|
41
|
-
lock
|
54
|
+
reflect(:execution_failed, item)
|
55
|
+
locksmith.lock
|
56
|
+
|
42
57
|
raise
|
43
58
|
end
|
59
|
+
|
60
|
+
def runtime_lock
|
61
|
+
@runtime_lock ||= SidekiqUniqueJobs::Lock::WhileExecuting.new(item.dup, callback, redis_pool)
|
62
|
+
end
|
44
63
|
end
|
45
64
|
end
|
46
65
|
end
|
@@ -6,18 +6,30 @@ 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
|
+
#
|
12
|
+
# Locks a sidekiq job
|
13
|
+
#
|
14
|
+
# @note Will call a conflict strategy if lock can't be achieved.
|
15
|
+
#
|
16
|
+
# @return [String, nil] the locked jid when properly locked, else nil.
|
17
|
+
#
|
18
|
+
# @yield to the caller when given a block
|
19
|
+
#
|
20
|
+
def lock
|
21
|
+
return lock_failed(origin: :client) unless (token = locksmith.lock)
|
22
|
+
return yield token if block_given?
|
23
|
+
|
24
|
+
token
|
25
|
+
end
|
12
26
|
|
13
27
|
# Executes in the Sidekiq server process
|
14
28
|
# @yield to the worker class perform method
|
15
29
|
def execute
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
log_warn "the unique_key: #{item[UNIQUE_DIGEST_KEY]} is not locked, allowing job to silently complete"
|
20
|
-
nil
|
30
|
+
locksmith.execute do
|
31
|
+
yield
|
32
|
+
unlock_and_callback
|
21
33
|
end
|
22
34
|
end
|
23
35
|
end
|
@@ -6,12 +6,26 @@ 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
|
+
#
|
12
|
+
# Locks a sidekiq job
|
13
|
+
#
|
14
|
+
# @note Will call a conflict strategy if lock can't be achieved.
|
15
|
+
#
|
16
|
+
# @return [String, nil] the locked jid when properly locked, else nil.
|
17
|
+
#
|
18
|
+
def lock
|
19
|
+
return lock_failed unless (job_id = locksmith.lock)
|
20
|
+
return yield job_id if block_given?
|
21
|
+
|
22
|
+
job_id
|
23
|
+
end
|
24
|
+
|
11
25
|
# Executes in the Sidekiq server process
|
12
26
|
# @yield to the worker class perform method
|
13
27
|
def execute
|
14
|
-
|
28
|
+
callback_safely if locksmith.unlock
|
15
29
|
yield
|
16
30
|
end
|
17
31
|
end
|
@@ -2,28 +2,32 @@
|
|
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
|
-
#
|
16
|
-
|
17
|
-
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class UntilExpired < UntilExecuted
|
11
|
+
#
|
12
|
+
# Locks a sidekiq job
|
13
|
+
#
|
14
|
+
# @note Will call a conflict strategy if lock can't be achieved.
|
15
|
+
#
|
16
|
+
# @return [String, nil] the locked jid when properly locked, else nil.
|
17
|
+
#
|
18
|
+
# @yield to the caller when given a block
|
19
|
+
#
|
20
|
+
def lock
|
21
|
+
return lock_failed unless (job_id = locksmith.lock)
|
22
|
+
return yield job_id if block_given?
|
23
|
+
|
24
|
+
job_id
|
18
25
|
end
|
19
26
|
|
20
27
|
# Executes in the Sidekiq server process
|
21
28
|
# @yield to the worker class perform method
|
22
|
-
def execute
|
23
|
-
|
24
|
-
|
25
|
-
yield
|
26
|
-
# this lock does not handle after_unlock since we don't know when that would happen
|
29
|
+
def execute(&block)
|
30
|
+
locksmith.execute(&block)
|
27
31
|
end
|
28
32
|
end
|
29
33
|
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
|
@@ -9,10 +9,13 @@ module SidekiqUniqueJobs
|
|
9
9
|
# See {#lock} for more information about the client.
|
10
10
|
# See {#execute} for more information about the server
|
11
11
|
#
|
12
|
-
# @author Mikael Henriksson <mikael@
|
12
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
13
13
|
class WhileExecuting < BaseLock
|
14
14
|
RUN_SUFFIX ||= ":RUN"
|
15
15
|
|
16
|
+
include SidekiqUniqueJobs::OptionsWithFallback
|
17
|
+
include SidekiqUniqueJobs::Logging::Middleware
|
18
|
+
|
16
19
|
# @param [Hash] item the Sidekiq job hash
|
17
20
|
# @param [Proc] callback callback to call after unlock
|
18
21
|
# @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
|
@@ -26,20 +29,24 @@ module SidekiqUniqueJobs
|
|
26
29
|
# These locks should only ever be created in the server process.
|
27
30
|
# @return [true] always returns true
|
28
31
|
def lock
|
29
|
-
|
32
|
+
job_id = item[JID]
|
33
|
+
yield job_id if block_given?
|
34
|
+
|
35
|
+
job_id
|
30
36
|
end
|
31
37
|
|
32
38
|
# Executes in the Sidekiq server process.
|
33
39
|
# These jobs are locked in the server process not from the client
|
34
40
|
# @yield to the worker class perform method
|
35
41
|
def execute
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
42
|
+
with_logging_context do
|
43
|
+
call_strategy(origin: :server) unless locksmith.execute do
|
44
|
+
yield
|
45
|
+
callback_safely if locksmith.unlock
|
46
|
+
ensure
|
47
|
+
locksmith.unlock
|
48
|
+
end
|
49
|
+
end
|
43
50
|
end
|
44
51
|
|
45
52
|
private
|
@@ -47,7 +54,9 @@ module SidekiqUniqueJobs
|
|
47
54
|
# This is safe as the base_lock always creates a new digest
|
48
55
|
# The append there for needs to be done every time
|
49
56
|
def append_unique_key_suffix
|
50
|
-
|
57
|
+
return if (lock_digest = item[LOCK_DIGEST]).end_with?(RUN_SUFFIX)
|
58
|
+
|
59
|
+
item[LOCK_DIGEST] = lock_digest + RUN_SUFFIX
|
51
60
|
end
|
52
61
|
end
|
53
62
|
end
|
@@ -9,12 +9,12 @@ module SidekiqUniqueJobs
|
|
9
9
|
# See {#lock} for more information about the client.
|
10
10
|
# See {#execute} for more information about the server
|
11
11
|
#
|
12
|
-
# @author Mikael Henriksson <mikael@
|
12
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
13
13
|
class WhileExecutingReject < WhileExecuting
|
14
14
|
# Overridden with a forced {OnConflict::Reject} strategy
|
15
15
|
# @return [OnConflict::Reject] a reject strategy
|
16
|
-
def
|
17
|
-
@
|
16
|
+
def server_strategy
|
17
|
+
@server_strategy ||= OnConflict.find_strategy(:reject).new(item, redis_pool)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|