sidekiq-unique-jobs 6.0.25 → 7.1.33
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1157 -41
- data/README.md +825 -291
- data/lib/sidekiq_unique_jobs/batch_delete.rb +124 -0
- data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
- data/lib/sidekiq_unique_jobs/cli.rb +57 -29
- data/lib/sidekiq_unique_jobs/config.rb +319 -0
- data/lib/sidekiq_unique_jobs/connection.rb +6 -5
- data/lib/sidekiq_unique_jobs/constants.rb +46 -25
- data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
- data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
- data/lib/sidekiq_unique_jobs/digests.rb +70 -102
- data/lib/sidekiq_unique_jobs/exceptions.rb +88 -12
- data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
- data/lib/sidekiq_unique_jobs/job.rb +46 -12
- data/lib/sidekiq_unique_jobs/json.rb +47 -0
- data/lib/sidekiq_unique_jobs/key.rb +98 -0
- data/lib/sidekiq_unique_jobs/lock/base_lock.rb +111 -82
- 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 +40 -15
- data/lib/sidekiq_unique_jobs/lock/until_executed.rb +30 -7
- data/lib/sidekiq_unique_jobs/lock/until_executing.rb +26 -2
- data/lib/sidekiq_unique_jobs/lock/until_expired.rb +27 -15
- data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +26 -12
- data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -3
- data/lib/sidekiq_unique_jobs/lock.rb +342 -0
- data/lib/sidekiq_unique_jobs/lock_args.rb +127 -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/lock_type.rb +37 -0
- data/lib/sidekiq_unique_jobs/locksmith.rb +305 -101
- data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
- data/lib/sidekiq_unique_jobs/logging.rb +202 -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 +99 -0
- data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -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 +122 -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 +107 -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 +42 -0
- data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -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 +63 -17
- 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 +39 -36
- data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
- data/lib/sidekiq_unique_jobs/orphans/manager.rb +241 -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 +298 -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 +51 -0
- data/lib/sidekiq_unique_jobs/redis.rb +11 -0
- data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
- data/lib/sidekiq_unique_jobs/reflections.rb +79 -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 +61 -0
- data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +114 -65
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +252 -36
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +47 -32
- data/lib/sidekiq_unique_jobs/testing.rb +102 -29
- data/lib/sidekiq_unique_jobs/timer_task.rb +299 -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 +138 -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 +110 -0
- data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
- data/lib/sidekiq_unique_jobs/web.rb +82 -32
- data/lib/sidekiq_unique_jobs.rb +54 -7
- data/lib/tasks/changelog.rake +16 -16
- metadata +134 -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
@@ -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,9 +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 WhileExecuting < BaseLock
|
14
|
-
RUN_SUFFIX
|
14
|
+
RUN_SUFFIX = ":RUN"
|
15
|
+
|
16
|
+
include SidekiqUniqueJobs::OptionsWithFallback
|
17
|
+
include SidekiqUniqueJobs::Logging::Middleware
|
15
18
|
|
16
19
|
# @param [Hash] item the Sidekiq job hash
|
17
20
|
# @param [Proc] callback callback to call after unlock
|
@@ -26,20 +29,29 @@ 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 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
|
-
def execute
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
def execute(&block)
|
42
|
+
with_logging_context do
|
43
|
+
executed = locksmith.execute do
|
44
|
+
yield
|
45
|
+
item[JID]
|
46
|
+
ensure
|
47
|
+
unlock_and_callback
|
48
|
+
end
|
49
|
+
|
50
|
+
unless executed
|
51
|
+
reflect(:execution_failed, item)
|
52
|
+
call_strategy(origin: :server, &block)
|
53
|
+
end
|
54
|
+
end
|
43
55
|
end
|
44
56
|
|
45
57
|
private
|
@@ -47,7 +59,9 @@ module SidekiqUniqueJobs
|
|
47
59
|
# This is safe as the base_lock always creates a new digest
|
48
60
|
# The append there for needs to be done every time
|
49
61
|
def append_unique_key_suffix
|
50
|
-
|
62
|
+
return if (lock_digest = item[LOCK_DIGEST]).end_with?(RUN_SUFFIX)
|
63
|
+
|
64
|
+
item[LOCK_DIGEST] = lock_digest + RUN_SUFFIX
|
51
65
|
end
|
52
66
|
end
|
53
67
|
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
|
@@ -0,0 +1,342 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
#
|
5
|
+
# Class Lock provides access to information about a lock
|
6
|
+
#
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
|
+
#
|
9
|
+
class Lock # rubocop:disable Metrics/ClassLength
|
10
|
+
# includes "SidekiqUniqueJobs::Connection"
|
11
|
+
# @!parse include SidekiqUniqueJobs::Connection
|
12
|
+
include SidekiqUniqueJobs::Connection
|
13
|
+
|
14
|
+
# includes "SidekiqUniqueJobs::Timing"
|
15
|
+
# @!parse include SidekiqUniqueJobs::Timing
|
16
|
+
include SidekiqUniqueJobs::Timing
|
17
|
+
|
18
|
+
# includes "SidekiqUniqueJobs::JSON"
|
19
|
+
# @!parse include SidekiqUniqueJobs::JSON
|
20
|
+
include SidekiqUniqueJobs::JSON
|
21
|
+
|
22
|
+
#
|
23
|
+
# @!attribute [r] key
|
24
|
+
# @return [String] the entity redis key
|
25
|
+
attr_reader :key
|
26
|
+
|
27
|
+
#
|
28
|
+
# Initialize a locked lock
|
29
|
+
#
|
30
|
+
# @param [String] digest a unique digest
|
31
|
+
# @param [String] job_id a sidekiq JID
|
32
|
+
# @param [Hash] lock_info information about the lock
|
33
|
+
#
|
34
|
+
# @return [Lock] a newly lock that has been locked
|
35
|
+
#
|
36
|
+
def self.create(digest, job_id, lock_info = {})
|
37
|
+
lock = new(digest, time: Timing.now_f)
|
38
|
+
lock.lock(job_id, lock_info)
|
39
|
+
lock
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Initialize a new lock
|
44
|
+
#
|
45
|
+
# @param [String, Key] key either a digest or an instance of a {Key}
|
46
|
+
# @param [Timstamp, Float] time nil optional timestamp to initiate this lock with
|
47
|
+
#
|
48
|
+
def initialize(key, time: nil)
|
49
|
+
@key = get_key(key)
|
50
|
+
@created_at = time.is_a?(Float) ? time : time.to_f
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# Locks a job_id
|
55
|
+
#
|
56
|
+
# @note intended only for testing purposes
|
57
|
+
#
|
58
|
+
# @param [String] job_id a sidekiq JID
|
59
|
+
# @param [Hash] lock_info information about the lock
|
60
|
+
#
|
61
|
+
# @return [void]
|
62
|
+
#
|
63
|
+
def lock(job_id, lock_info = {})
|
64
|
+
redis do |conn|
|
65
|
+
conn.multi do |pipeline|
|
66
|
+
pipeline.set(key.digest, job_id)
|
67
|
+
pipeline.hset(key.locked, job_id, now_f)
|
68
|
+
info.set(lock_info, pipeline)
|
69
|
+
add_digest_to_set(pipeline, lock_info)
|
70
|
+
pipeline.zadd(key.changelog, now_f, changelog_json(job_id, "queue.lua", "Queued"))
|
71
|
+
pipeline.zadd(key.changelog, now_f, changelog_json(job_id, "lock.lua", "Locked"))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
#
|
77
|
+
# Create the :QUEUED key
|
78
|
+
#
|
79
|
+
# @note intended only for testing purposes
|
80
|
+
#
|
81
|
+
# @param [String] job_id a sidekiq JID
|
82
|
+
#
|
83
|
+
# @return [void]
|
84
|
+
#
|
85
|
+
def queue(job_id)
|
86
|
+
redis do |conn|
|
87
|
+
conn.lpush(key.queued, job_id)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
#
|
92
|
+
# Create the :PRIMED key
|
93
|
+
#
|
94
|
+
# @note intended only for testing purposes
|
95
|
+
#
|
96
|
+
# @param [String] job_id a sidekiq JID
|
97
|
+
#
|
98
|
+
# @return [void]
|
99
|
+
#
|
100
|
+
def prime(job_id)
|
101
|
+
redis do |conn|
|
102
|
+
conn.lpush(key.primed, job_id)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
#
|
107
|
+
# Unlock a specific job_id
|
108
|
+
#
|
109
|
+
# @param [String] job_id a sidekiq JID
|
110
|
+
#
|
111
|
+
# @return [true] when job_id was removed
|
112
|
+
# @return [false] when job_id wasn't locked
|
113
|
+
#
|
114
|
+
def unlock(job_id)
|
115
|
+
locked.del(job_id)
|
116
|
+
end
|
117
|
+
|
118
|
+
#
|
119
|
+
# Deletes all the redis keys for this lock
|
120
|
+
#
|
121
|
+
#
|
122
|
+
# @return [Integer] the number of keys deleted in redis
|
123
|
+
#
|
124
|
+
def del
|
125
|
+
redis do |conn|
|
126
|
+
conn.multi do |pipeline|
|
127
|
+
pipeline.zrem(DIGESTS, key.digest)
|
128
|
+
pipeline.del(key.digest, key.queued, key.primed, key.locked, key.info)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
#
|
134
|
+
# Returns either the time the lock was initialized with or
|
135
|
+
# the first changelog entry's timestamp
|
136
|
+
#
|
137
|
+
#
|
138
|
+
# @return [Float] a floaty timestamp represantation
|
139
|
+
#
|
140
|
+
def created_at
|
141
|
+
@created_at ||= changelogs.first&.[]("time")
|
142
|
+
end
|
143
|
+
|
144
|
+
#
|
145
|
+
# Returns all job_id's for this lock
|
146
|
+
#
|
147
|
+
# @note a JID can be present in 3 different places
|
148
|
+
#
|
149
|
+
#
|
150
|
+
# @return [Array<String>] an array with JIDs
|
151
|
+
#
|
152
|
+
def all_jids
|
153
|
+
(queued_jids + primed_jids + locked_jids).uniq
|
154
|
+
end
|
155
|
+
|
156
|
+
#
|
157
|
+
# Returns a collection of locked job_id's
|
158
|
+
#
|
159
|
+
# @param [true, false] with_values false provide the timestamp for the lock
|
160
|
+
#
|
161
|
+
# @return [Hash<String, Float>] when given `with_values: true`
|
162
|
+
# @return [Array<String>] when given `with_values: false`
|
163
|
+
#
|
164
|
+
def locked_jids(with_values: false)
|
165
|
+
locked.entries(with_values: with_values)
|
166
|
+
end
|
167
|
+
|
168
|
+
#
|
169
|
+
# Returns the queued JIDs
|
170
|
+
#
|
171
|
+
#
|
172
|
+
# @return [Array<String>] an array with queued job_ids
|
173
|
+
#
|
174
|
+
def queued_jids
|
175
|
+
queued.entries
|
176
|
+
end
|
177
|
+
|
178
|
+
#
|
179
|
+
# Returns the primed JIDs
|
180
|
+
#
|
181
|
+
#
|
182
|
+
# @return [Array<String>] an array with primed job_ids
|
183
|
+
#
|
184
|
+
def primed_jids
|
185
|
+
primed.entries
|
186
|
+
end
|
187
|
+
|
188
|
+
#
|
189
|
+
# Returns all matching changelog entries for this lock
|
190
|
+
#
|
191
|
+
#
|
192
|
+
# @return [Array<Hash>] an array with changelogs
|
193
|
+
#
|
194
|
+
def changelogs
|
195
|
+
changelog.entries(pattern: "*#{key.digest}*")
|
196
|
+
end
|
197
|
+
|
198
|
+
#
|
199
|
+
# The digest key
|
200
|
+
#
|
201
|
+
# @note Used for exists checks to avoid enqueuing
|
202
|
+
# the same lock twice
|
203
|
+
#
|
204
|
+
#
|
205
|
+
# @return [Redis::String] a string representation of the key
|
206
|
+
#
|
207
|
+
def digest
|
208
|
+
@digest ||= Redis::String.new(key.digest)
|
209
|
+
end
|
210
|
+
|
211
|
+
#
|
212
|
+
# The queued list
|
213
|
+
#
|
214
|
+
#
|
215
|
+
# @return [Redis::List] for queued JIDs
|
216
|
+
#
|
217
|
+
def queued
|
218
|
+
@queued ||= Redis::List.new(key.queued)
|
219
|
+
end
|
220
|
+
|
221
|
+
#
|
222
|
+
# The primed list
|
223
|
+
#
|
224
|
+
#
|
225
|
+
# @return [Redis::List] for primed JIDs
|
226
|
+
#
|
227
|
+
def primed
|
228
|
+
@primed ||= Redis::List.new(key.primed)
|
229
|
+
end
|
230
|
+
|
231
|
+
#
|
232
|
+
# The locked hash
|
233
|
+
#
|
234
|
+
#
|
235
|
+
# @return [Redis::Hash] for locked JIDs
|
236
|
+
#
|
237
|
+
def locked
|
238
|
+
@locked ||= Redis::Hash.new(key.locked)
|
239
|
+
end
|
240
|
+
|
241
|
+
#
|
242
|
+
# Information about the lock
|
243
|
+
#
|
244
|
+
#
|
245
|
+
# @return [Redis::Hash] with lock information
|
246
|
+
#
|
247
|
+
def info
|
248
|
+
@info ||= LockInfo.new(key.info)
|
249
|
+
end
|
250
|
+
|
251
|
+
#
|
252
|
+
# A sorted set with changelog entries
|
253
|
+
#
|
254
|
+
# @see Changelog for more information
|
255
|
+
#
|
256
|
+
#
|
257
|
+
# @return [Changelog]
|
258
|
+
#
|
259
|
+
def changelog
|
260
|
+
@changelog ||= Changelog.new
|
261
|
+
end
|
262
|
+
|
263
|
+
#
|
264
|
+
# A nicely formatted string with information about this lock
|
265
|
+
#
|
266
|
+
#
|
267
|
+
# @return [String]
|
268
|
+
#
|
269
|
+
def to_s
|
270
|
+
<<~MESSAGE
|
271
|
+
Lock status for #{key}
|
272
|
+
|
273
|
+
value: #{digest.value}
|
274
|
+
info: #{info.value}
|
275
|
+
queued_jids: #{queued_jids}
|
276
|
+
primed_jids: #{primed_jids}
|
277
|
+
locked_jids: #{locked_jids}
|
278
|
+
changelogs: #{changelogs}
|
279
|
+
MESSAGE
|
280
|
+
end
|
281
|
+
|
282
|
+
#
|
283
|
+
# @see to_s
|
284
|
+
#
|
285
|
+
def inspect
|
286
|
+
to_s
|
287
|
+
end
|
288
|
+
|
289
|
+
private
|
290
|
+
|
291
|
+
#
|
292
|
+
# Ensure the key is a {Key}
|
293
|
+
#
|
294
|
+
# @param [String, Key] key
|
295
|
+
#
|
296
|
+
# @return [Key]
|
297
|
+
#
|
298
|
+
def get_key(key)
|
299
|
+
if key.is_a?(SidekiqUniqueJobs::Key)
|
300
|
+
key
|
301
|
+
else
|
302
|
+
SidekiqUniqueJobs::Key.new(key)
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
#
|
307
|
+
# Generate a changelog entry for the given arguments
|
308
|
+
#
|
309
|
+
# @param [String] job_id a sidekiq JID
|
310
|
+
# @param [String] script the name of the script generating this entry
|
311
|
+
# @param [String] message a descriptive message for later review
|
312
|
+
#
|
313
|
+
# @return [String] a JSON string matching the Lua script structure
|
314
|
+
#
|
315
|
+
def changelog_json(job_id, script, message)
|
316
|
+
dump_json(
|
317
|
+
digest: key.digest,
|
318
|
+
job_id: job_id,
|
319
|
+
script: script,
|
320
|
+
message: message,
|
321
|
+
time: now_f,
|
322
|
+
)
|
323
|
+
end
|
324
|
+
|
325
|
+
#
|
326
|
+
# Add the digest to the correct sorted set
|
327
|
+
#
|
328
|
+
# @param [Object] pipeline a redis pipeline object for issue commands
|
329
|
+
# @param [Hash] lock_info the lock info relevant to the digest
|
330
|
+
#
|
331
|
+
# @return [nil]
|
332
|
+
#
|
333
|
+
def add_digest_to_set(pipeline, lock_info)
|
334
|
+
digest_string = key.digest
|
335
|
+
if lock_info["lock"] == :until_expired
|
336
|
+
pipeline.zadd(key.expiring_digests, now_f + lock_info["ttl"], digest_string)
|
337
|
+
else
|
338
|
+
pipeline.zadd(key.digests, now_f, digest_string)
|
339
|
+
end
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
# Handles uniqueness of sidekiq arguments
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
7
|
+
class LockArgs
|
8
|
+
include SidekiqUniqueJobs::Logging
|
9
|
+
include SidekiqUniqueJobs::SidekiqWorkerMethods
|
10
|
+
include SidekiqUniqueJobs::JSON
|
11
|
+
|
12
|
+
# Convenience method for returning a digest
|
13
|
+
# @param [Hash] item a Sidekiq job hash
|
14
|
+
# @return [String] a unique digest
|
15
|
+
def self.call(item)
|
16
|
+
new(item).lock_args
|
17
|
+
end
|
18
|
+
|
19
|
+
# The sidekiq job hash
|
20
|
+
# @return [Hash] the Sidekiq job hash
|
21
|
+
attr_reader :item
|
22
|
+
#
|
23
|
+
# @!attribute [r] args
|
24
|
+
# @return [Array<Objet>] the arguments passed to `perform_async`
|
25
|
+
attr_reader :args
|
26
|
+
|
27
|
+
# @param [Hash] item a Sidekiq job hash
|
28
|
+
def initialize(item)
|
29
|
+
@item = item
|
30
|
+
@args = item[ARGS]
|
31
|
+
self.job_class = item[CLASS]
|
32
|
+
end
|
33
|
+
|
34
|
+
# The unique arguments to use for creating a lock
|
35
|
+
# @return [Array] the arguments filters by the {#filtered_args} method if {#lock_args_enabled?}
|
36
|
+
def lock_args
|
37
|
+
@lock_args ||= filtered_args || []
|
38
|
+
end
|
39
|
+
|
40
|
+
# Checks if the worker class has enabled lock_args
|
41
|
+
# @return [true, false]
|
42
|
+
def lock_args_enabled?
|
43
|
+
# return false unless lock_args_method_valid?
|
44
|
+
|
45
|
+
lock_args_method
|
46
|
+
end
|
47
|
+
|
48
|
+
# Validate that the lock_args_method is acceptable
|
49
|
+
# @return [true, false]
|
50
|
+
def lock_args_method_valid?
|
51
|
+
[NilClass, TrueClass, FalseClass].none? { |klass| lock_args_method.is_a?(klass) }
|
52
|
+
end
|
53
|
+
|
54
|
+
# Checks if the worker class has disabled lock_args
|
55
|
+
# @return [true, false]
|
56
|
+
def lock_args_disabled?
|
57
|
+
!lock_args_method
|
58
|
+
end
|
59
|
+
|
60
|
+
# Filters unique arguments by proc or symbol
|
61
|
+
# @return [Array] {#filter_by_proc} when {#lock_args_method} is a Proc
|
62
|
+
# @return [Array] {#filter_by_symbol} when {#lock_args_method} is a Symbol
|
63
|
+
# @return [Array] args unfiltered when neither of the above
|
64
|
+
def filtered_args
|
65
|
+
return args if lock_args_disabled?
|
66
|
+
|
67
|
+
json_args = Normalizer.jsonify(args)
|
68
|
+
|
69
|
+
case lock_args_method
|
70
|
+
when Proc
|
71
|
+
filter_by_proc(json_args)
|
72
|
+
when Symbol
|
73
|
+
filter_by_symbol(json_args)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Filters unique arguments by proc configured in the sidekiq worker
|
78
|
+
# @param [Array] args the arguments passed to the sidekiq worker
|
79
|
+
# @return [Array] with the filtered arguments
|
80
|
+
def filter_by_proc(args)
|
81
|
+
lock_args_method.call(args)
|
82
|
+
end
|
83
|
+
|
84
|
+
# Filters unique arguments by method configured in the sidekiq worker
|
85
|
+
# @param [Array] args the arguments passed to the sidekiq worker
|
86
|
+
# @return [Array] unfiltered unless {#job_method_defined?}
|
87
|
+
# @return [Array] with the filtered arguments
|
88
|
+
def filter_by_symbol(args)
|
89
|
+
return args unless job_method_defined?(lock_args_method)
|
90
|
+
|
91
|
+
job_class.send(lock_args_method, args)
|
92
|
+
rescue ArgumentError
|
93
|
+
raise SidekiqUniqueJobs::InvalidUniqueArguments,
|
94
|
+
given: args,
|
95
|
+
job_class: job_class,
|
96
|
+
lock_args_method: lock_args_method
|
97
|
+
end
|
98
|
+
|
99
|
+
# The method to use for filtering unique arguments
|
100
|
+
def lock_args_method
|
101
|
+
@lock_args_method ||= job_options.slice(LOCK_ARGS_METHOD, UNIQUE_ARGS_METHOD).values.first
|
102
|
+
@lock_args_method ||= :lock_args if job_method_defined?(:lock_args)
|
103
|
+
@lock_args_method ||= :unique_args if job_method_defined?(:unique_args)
|
104
|
+
@lock_args_method ||= default_lock_args_method
|
105
|
+
end
|
106
|
+
|
107
|
+
# The global worker options defined in Sidekiq directly
|
108
|
+
def default_lock_args_method
|
109
|
+
default_job_options[LOCK_ARGS_METHOD] ||
|
110
|
+
default_job_options[UNIQUE_ARGS_METHOD]
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# The globally default worker options configured from Sidekiq
|
115
|
+
#
|
116
|
+
#
|
117
|
+
# @return [Hash<String, Object>]
|
118
|
+
#
|
119
|
+
def default_job_options
|
120
|
+
@default_job_options ||= if Sidekiq.respond_to?(:default_job_options)
|
121
|
+
Sidekiq.default_job_options.stringify_keys
|
122
|
+
else
|
123
|
+
Sidekiq.default_worker_options.stringify_keys
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|