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
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
# Interface to dealing with .lua files
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
7
|
+
module Script
|
8
|
+
include Brpoplpush::RedisScript::DSL
|
9
|
+
|
10
|
+
configure do |config|
|
11
|
+
config.scripts_path = Pathname.new(__FILE__).dirname.join("lua")
|
12
|
+
config.logger = Sidekiq.logger # TODO: This becomes a little weird
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
# Interface to dealing with .lua files
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
7
|
+
module Script
|
8
|
+
#
|
9
|
+
# Module Caller provides the convenience method #call_script
|
10
|
+
#
|
11
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
12
|
+
#
|
13
|
+
module Caller
|
14
|
+
module_function
|
15
|
+
|
16
|
+
# includes "SidekiqUniqueJobs::Connection"
|
17
|
+
# @!parse include SidekiqUniqueJobs::Connection
|
18
|
+
include SidekiqUniqueJobs::Connection
|
19
|
+
|
20
|
+
#
|
21
|
+
# Convenience method to reduce typing,
|
22
|
+
# calls redis lua scripts.
|
23
|
+
#
|
24
|
+
#
|
25
|
+
# @overload call_script(file_name, keys, argv, conn)
|
26
|
+
# Call script without options hash
|
27
|
+
# @param [Symbol] file_name the name of the file
|
28
|
+
# @param [Array<String>] keys to pass to the the script
|
29
|
+
# @param [Array<String>] argv arguments to pass to the script
|
30
|
+
# @param [Redis] conn a redis connection
|
31
|
+
# @overload call_script(file_name, conn, keys:, argv:)
|
32
|
+
# Call script with options hash
|
33
|
+
# @param [Symbol] file_name the name of the file
|
34
|
+
# @param [Redis] conn a redis connection
|
35
|
+
# @param [Hash] options arguments to pass to the script file
|
36
|
+
# @option options [Array] :keys to pass to the script
|
37
|
+
# @option options [Array] :argv arguments to pass to the script
|
38
|
+
#
|
39
|
+
# @return [true,false,String,Integer,Float,nil] returns the return value of the lua script
|
40
|
+
#
|
41
|
+
def call_script(file_name, *args)
|
42
|
+
conn, keys, argv = extract_args(*args)
|
43
|
+
return do_call(file_name, conn, keys, argv) if conn
|
44
|
+
|
45
|
+
pool = defined?(redis_pool) ? redis_pool : nil
|
46
|
+
|
47
|
+
redis(pool) do |new_conn|
|
48
|
+
do_call(file_name, new_conn, keys, argv)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Only used to reduce a little bit of duplication
|
53
|
+
# @see call_script
|
54
|
+
def do_call(file_name, conn, keys, argv)
|
55
|
+
argv = argv.dup.concat([
|
56
|
+
now_f,
|
57
|
+
debug_lua,
|
58
|
+
max_history,
|
59
|
+
file_name,
|
60
|
+
redis_version,
|
61
|
+
])
|
62
|
+
Script.execute(file_name, conn, keys: keys, argv: argv)
|
63
|
+
end
|
64
|
+
|
65
|
+
#
|
66
|
+
# Utility method to allow both symbol keys and arguments
|
67
|
+
#
|
68
|
+
# @overload call_script(file_name, keys, argv, conn)
|
69
|
+
# Call script without options hash
|
70
|
+
# @param [Symbol] file_name the name of the file
|
71
|
+
# @param [Array<String>] keys to pass to the the script
|
72
|
+
# @param [Array<String>] argv arguments to pass to the script
|
73
|
+
# @param [Redis] conn a redis connection
|
74
|
+
# @overload call_script(file_name, conn, keys:, argv:)
|
75
|
+
# Call script with options hash
|
76
|
+
# @param [Symbol] file_name the name of the file
|
77
|
+
# @param [Redis] conn a redis connection
|
78
|
+
# @param [Hash] options arguments to pass to the script file
|
79
|
+
# @option options [Array] :keys to pass to the script
|
80
|
+
# @option options [Array] :argv arguments to pass to the script
|
81
|
+
#
|
82
|
+
# @return [Array<Redis, Array, Array>] <description>
|
83
|
+
#
|
84
|
+
def extract_args(*args)
|
85
|
+
options = args.extract_options!
|
86
|
+
if options.length.positive?
|
87
|
+
[args.pop, options.fetch(:keys) { [] }, options.fetch(:argv) { [] }]
|
88
|
+
else
|
89
|
+
keys, argv = args.shift(2)
|
90
|
+
keys ||= []
|
91
|
+
argv ||= []
|
92
|
+
[args.pop, keys, argv]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
# @see SidekiqUniqueJobs#now_f
|
98
|
+
#
|
99
|
+
def now_f
|
100
|
+
SidekiqUniqueJobs.now_f
|
101
|
+
end
|
102
|
+
|
103
|
+
#
|
104
|
+
# @see SidekiqUniqueJobs::Config#debug_lua
|
105
|
+
#
|
106
|
+
def debug_lua
|
107
|
+
SidekiqUniqueJobs.config.debug_lua
|
108
|
+
end
|
109
|
+
|
110
|
+
#
|
111
|
+
# @see SidekiqUniqueJobs::Config#max_history
|
112
|
+
#
|
113
|
+
def max_history
|
114
|
+
SidekiqUniqueJobs.config.max_history
|
115
|
+
end
|
116
|
+
|
117
|
+
#
|
118
|
+
# @see SidekiqUniqueJobs::Config#max_history
|
119
|
+
#
|
120
|
+
def redis_version
|
121
|
+
SidekiqUniqueJobs.config.redis_version
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
# The unique sidekiq middleware for the server processor
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
7
|
+
class Server
|
8
|
+
DEATH_HANDLER ||= (lambda do |job, _ex|
|
9
|
+
return unless (digest = job["lock_digest"])
|
10
|
+
|
11
|
+
SidekiqUniqueJobs::Digests.new.delete_by_digest(digest)
|
12
|
+
end).freeze
|
13
|
+
#
|
14
|
+
# Configure the server middleware
|
15
|
+
#
|
16
|
+
#
|
17
|
+
# @return [Sidekiq] the sidekiq configuration
|
18
|
+
#
|
19
|
+
def self.configure(config)
|
20
|
+
config.on(:startup) { start }
|
21
|
+
config.on(:shutdown) { stop }
|
22
|
+
|
23
|
+
return unless config.respond_to?(:death_handlers)
|
24
|
+
|
25
|
+
config.death_handlers << death_handler
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.start
|
29
|
+
SidekiqUniqueJobs::UpdateVersion.call
|
30
|
+
SidekiqUniqueJobs::UpgradeLocks.call
|
31
|
+
SidekiqUniqueJobs::Orphans::Manager.start
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.stop
|
35
|
+
SidekiqUniqueJobs::Orphans::Manager.stop
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# A death handler for dead jobs
|
40
|
+
#
|
41
|
+
#
|
42
|
+
# @return [lambda]
|
43
|
+
#
|
44
|
+
def self.death_handler
|
45
|
+
DEATH_HANDLER
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -2,113 +2,140 @@
|
|
2
2
|
|
3
3
|
require "sidekiq/api"
|
4
4
|
|
5
|
+
#
|
6
|
+
# @api private
|
7
|
+
#
|
5
8
|
module Sidekiq
|
9
|
+
# See Sidekiq::Api
|
6
10
|
class SortedEntry
|
11
|
+
#
|
12
|
+
# Provides extensions for unlocking jobs that are removed and deleted
|
13
|
+
#
|
14
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
15
|
+
#
|
7
16
|
module UniqueExtension
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
def delete_ext
|
18
|
-
SidekiqUniqueJobs::Unlockable.unlock(item) if delete_orig
|
17
|
+
#
|
18
|
+
# Wraps the original method to ensure locks for the job are deleted
|
19
|
+
#
|
20
|
+
#
|
21
|
+
# @return [Hash] the deleted sidekiq job hash
|
22
|
+
#
|
23
|
+
def delete
|
24
|
+
SidekiqUniqueJobs::Unlockable.delete!(item) if super
|
25
|
+
item
|
19
26
|
end
|
20
27
|
|
21
28
|
private
|
22
29
|
|
23
|
-
|
24
|
-
|
25
|
-
|
30
|
+
#
|
31
|
+
# Wraps the original method to ensure locks for the job are deleted
|
32
|
+
#
|
33
|
+
#
|
34
|
+
# @yieldparam [Hash] message the sidekiq job hash
|
35
|
+
def remove_job
|
36
|
+
super do |message|
|
37
|
+
SidekiqUniqueJobs::Unlockable.delete!(Sidekiq.load_json(message))
|
26
38
|
yield message
|
27
39
|
end
|
28
40
|
end
|
29
41
|
end
|
30
42
|
|
31
|
-
|
43
|
+
prepend UniqueExtension
|
32
44
|
end
|
33
45
|
|
46
|
+
# See Sidekiq::Api
|
34
47
|
class ScheduledSet
|
48
|
+
#
|
49
|
+
# Provides extensions for unlocking jobs that are removed and deleted
|
50
|
+
#
|
51
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
52
|
+
#
|
35
53
|
module UniqueExtension
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
def
|
44
|
-
|
54
|
+
#
|
55
|
+
# Wraps the original method to ensure locks for the job are deleted
|
56
|
+
#
|
57
|
+
#
|
58
|
+
# @param [Integer, Float] score the score in the scheduled set
|
59
|
+
# @param [String] job_id the Sidekiq JID
|
60
|
+
#
|
61
|
+
def delete(score, job_id)
|
62
|
+
entry = find_job(job_id)
|
63
|
+
SidekiqUniqueJobs::Unlockable.delete!(entry.item) if super(score, job_id)
|
64
|
+
entry
|
45
65
|
end
|
46
66
|
end
|
47
|
-
|
67
|
+
|
68
|
+
prepend UniqueExtension
|
48
69
|
end
|
49
70
|
|
71
|
+
# See Sidekiq::Api
|
50
72
|
class Job
|
73
|
+
#
|
74
|
+
# Provides extensions for unlocking jobs that are removed and deleted
|
75
|
+
#
|
76
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
77
|
+
#
|
51
78
|
module UniqueExtension
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
def delete_ext
|
60
|
-
delete_orig
|
61
|
-
SidekiqUniqueJobs::Unlockable.unlock(item)
|
79
|
+
#
|
80
|
+
# Wraps the original method to ensure locks for the job are deleted
|
81
|
+
#
|
82
|
+
def delete
|
83
|
+
SidekiqUniqueJobs::Unlockable.delete!(item)
|
84
|
+
super
|
62
85
|
end
|
63
86
|
end
|
64
87
|
|
65
|
-
|
88
|
+
prepend UniqueExtension
|
66
89
|
end
|
67
90
|
|
91
|
+
# See Sidekiq::Api
|
68
92
|
class Queue
|
93
|
+
#
|
94
|
+
# Provides extensions for unlocking jobs that are removed and deleted
|
95
|
+
#
|
96
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
97
|
+
#
|
69
98
|
module UniqueExtension
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def clear_ext
|
99
|
+
#
|
100
|
+
# Wraps the original method to ensure locks for the job are deleted
|
101
|
+
#
|
102
|
+
def clear
|
78
103
|
each(&:delete)
|
79
|
-
|
104
|
+
super
|
80
105
|
end
|
81
106
|
end
|
82
107
|
|
83
|
-
|
108
|
+
prepend UniqueExtension
|
84
109
|
end
|
85
110
|
|
111
|
+
# See Sidekiq::Api
|
86
112
|
class JobSet
|
113
|
+
#
|
114
|
+
# Provides extensions for unlocking jobs that are removed and deleted
|
115
|
+
#
|
116
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
117
|
+
#
|
87
118
|
module UniqueExtension
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
alias_method :clear, :clear_ext
|
93
|
-
end
|
94
|
-
|
95
|
-
if base.method_defined?(:delete_by_value)
|
96
|
-
alias_method :delete_by_value_orig, :delete_by_value
|
97
|
-
alias_method :delete_by_value, :delete_by_value_ext
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def clear_ext
|
119
|
+
#
|
120
|
+
# Wraps the original method to ensure locks for the job are deleted
|
121
|
+
#
|
122
|
+
def clear
|
103
123
|
each(&:delete)
|
104
|
-
|
124
|
+
super
|
105
125
|
end
|
106
126
|
|
107
|
-
|
108
|
-
|
127
|
+
#
|
128
|
+
# Wraps the original method to ensure locks for the job are deleted
|
129
|
+
#
|
130
|
+
#
|
131
|
+
# @param [String] name the name of the key
|
132
|
+
# @param [String] value a sidekiq job hash
|
133
|
+
#
|
134
|
+
def delete_by_value(name, value)
|
135
|
+
SidekiqUniqueJobs::Unlockable.delete!(Sidekiq.load_json(value)) if super(name, value)
|
109
136
|
end
|
110
137
|
end
|
111
138
|
|
112
|
-
|
139
|
+
prepend UniqueExtension
|
113
140
|
end
|
114
141
|
end
|
@@ -3,73 +3,172 @@
|
|
3
3
|
#
|
4
4
|
# Contains configuration and utility methods that belongs top level
|
5
5
|
#
|
6
|
-
# @author Mikael Henriksson <mikael@
|
6
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
7
7
|
module SidekiqUniqueJobs
|
8
8
|
include SidekiqUniqueJobs::Connection
|
9
|
+
extend SidekiqUniqueJobs::JSON
|
9
10
|
|
10
11
|
module_function
|
11
12
|
|
12
|
-
|
13
|
-
:default_lock_timeout,
|
14
|
-
:enabled,
|
15
|
-
:unique_prefix,
|
16
|
-
:logger,
|
17
|
-
)
|
18
|
-
|
13
|
+
#
|
19
14
|
# The current configuration (See: {.configure} on how to configure)
|
15
|
+
#
|
16
|
+
#
|
17
|
+
# @return [SidekiqUniqueJobs::Config] the gem configuration
|
18
|
+
#
|
20
19
|
def config
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
20
|
+
@config ||= reset!
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# The current strategies
|
25
|
+
#
|
26
|
+
#
|
27
|
+
# @return [Hash<Symbol, SidekiqUniqueJobs::Strategy>] the configured locks
|
28
|
+
#
|
29
|
+
def strategies
|
30
|
+
config.strategies
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# The current locks
|
35
|
+
#
|
36
|
+
#
|
37
|
+
# @return [Hash<Symbol, SidekiqUniqueJobs::BaseLock>] the configured locks
|
38
|
+
#
|
39
|
+
def locks
|
40
|
+
config.locks
|
28
41
|
end
|
29
42
|
|
43
|
+
#
|
30
44
|
# The current logger
|
45
|
+
#
|
46
|
+
#
|
31
47
|
# @return [Logger] the configured logger
|
48
|
+
#
|
32
49
|
def logger
|
33
50
|
config.logger
|
34
51
|
end
|
35
52
|
|
36
|
-
#
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
else
|
45
|
-
yield
|
46
|
-
end
|
53
|
+
#
|
54
|
+
# The current gem version
|
55
|
+
#
|
56
|
+
#
|
57
|
+
# @return [String] the current gem version
|
58
|
+
#
|
59
|
+
def version
|
60
|
+
VERSION
|
47
61
|
end
|
48
62
|
|
63
|
+
#
|
49
64
|
# Set a new logger
|
50
|
-
#
|
65
|
+
#
|
66
|
+
# @param [Logger] other another logger
|
67
|
+
#
|
68
|
+
# @return [Logger] the new logger
|
69
|
+
#
|
51
70
|
def logger=(other)
|
52
71
|
config.logger = other
|
53
72
|
end
|
54
73
|
|
55
|
-
#
|
74
|
+
#
|
75
|
+
# Temporarily use another configuration and reset to the old config after yielding
|
76
|
+
#
|
77
|
+
# @param [Hash] tmp_config the temporary configuration to use
|
78
|
+
#
|
79
|
+
# @return [void]
|
80
|
+
#
|
56
81
|
# @yield control to the caller
|
57
|
-
def use_config(tmp_config)
|
82
|
+
def use_config(tmp_config = {})
|
58
83
|
raise ::ArgumentError, "#{name}.#{__method__} needs a block" unless block_given?
|
59
84
|
|
60
85
|
old_config = config.to_h
|
86
|
+
reset!
|
61
87
|
configure(tmp_config)
|
62
88
|
yield
|
63
|
-
|
89
|
+
ensure
|
90
|
+
reset!
|
91
|
+
configure(old_config.to_h)
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# Resets configuration to deafult
|
96
|
+
#
|
97
|
+
#
|
98
|
+
# @return [SidekiqUniqueJobs::Config] a default gem configuration
|
99
|
+
#
|
100
|
+
def reset!
|
101
|
+
@config = SidekiqUniqueJobs::Config.default
|
102
|
+
end
|
103
|
+
|
104
|
+
#
|
105
|
+
# Enable SidekiqUniuqeJobs either temporarily in a block or for good
|
106
|
+
#
|
107
|
+
#
|
108
|
+
# @return [true] when not given a block
|
109
|
+
# @return [true, false] the previous value of enable when given a block
|
110
|
+
#
|
111
|
+
# @yieldreturn [void] temporarily enable sidekiq unique jobs while executing a block of code
|
112
|
+
def enable!(&block)
|
113
|
+
toggle(true, &block)
|
114
|
+
end
|
115
|
+
|
116
|
+
#
|
117
|
+
# Disable SidekiqUniuqeJobs either temporarily in a block or for good
|
118
|
+
#
|
119
|
+
#
|
120
|
+
# @return [false] when not given a block
|
121
|
+
# @return [true, false] the previous value of enable when given a block
|
122
|
+
#
|
123
|
+
# @yieldreturn [void] temporarily disable sidekiq unique jobs while executing a block of code
|
124
|
+
def disable!(&block)
|
125
|
+
toggle(false, &block)
|
126
|
+
end
|
127
|
+
|
128
|
+
#
|
129
|
+
# Checks if the gem has been disabled
|
130
|
+
#
|
131
|
+
# @return [true] when config.enabled is true
|
132
|
+
# @return [false] when config.enabled is false
|
133
|
+
#
|
134
|
+
def enabled?
|
135
|
+
config.enabled
|
136
|
+
end
|
137
|
+
|
138
|
+
#
|
139
|
+
# Checks if the gem has been disabled
|
140
|
+
#
|
141
|
+
# @return [true] when config.enabled is false
|
142
|
+
# @return [false] when config.enabled is true
|
143
|
+
#
|
144
|
+
def disabled?
|
145
|
+
!enabled?
|
146
|
+
end
|
147
|
+
|
148
|
+
#
|
149
|
+
# Toggles enabled on or off
|
150
|
+
#
|
151
|
+
# @api private
|
152
|
+
# :nodoc:
|
153
|
+
def toggle(enabled)
|
154
|
+
if block_given?
|
155
|
+
enabled_was = config.enabled
|
156
|
+
config.enabled = enabled
|
157
|
+
yield
|
158
|
+
config.enabled = enabled_was
|
159
|
+
else
|
160
|
+
config.enabled = enabled
|
161
|
+
end
|
64
162
|
end
|
65
163
|
|
66
164
|
# Configure the gem
|
67
165
|
#
|
68
166
|
# This is usually called once at startup of an application
|
69
167
|
# @param [Hash] options global gem options
|
70
|
-
# @option options [Integer] :
|
168
|
+
# @option options [Integer] :lock_timeout (default is 0)
|
169
|
+
# @option options [Integer] :lock_ttl (default is 0)
|
71
170
|
# @option options [true,false] :enabled (default is true)
|
72
|
-
# @option options [String] :
|
171
|
+
# @option options [String] :lock_prefix (default is 'uniquejobs')
|
73
172
|
# @option options [Logger] :logger (default is Sidekiq.logger)
|
74
173
|
# @yield control to the caller when given block
|
75
174
|
def configure(options = {})
|
@@ -82,7 +181,59 @@ module SidekiqUniqueJobs
|
|
82
181
|
end
|
83
182
|
end
|
84
183
|
|
85
|
-
|
86
|
-
|
184
|
+
#
|
185
|
+
# Returns the current redis version
|
186
|
+
#
|
187
|
+
#
|
188
|
+
# @return [String] a string like `5.0.2`
|
189
|
+
#
|
190
|
+
def fetch_redis_version
|
191
|
+
redis { |conn| conn.info("server")["redis_version"] }
|
192
|
+
end
|
193
|
+
|
194
|
+
#
|
195
|
+
# Current time as float
|
196
|
+
#
|
197
|
+
#
|
198
|
+
# @return [Float]
|
199
|
+
#
|
200
|
+
def now_f
|
201
|
+
now.to_f
|
202
|
+
end
|
203
|
+
|
204
|
+
#
|
205
|
+
# Current time
|
206
|
+
#
|
207
|
+
#
|
208
|
+
# @return [Time]
|
209
|
+
#
|
210
|
+
def now
|
211
|
+
Time.now
|
212
|
+
end
|
213
|
+
|
214
|
+
#
|
215
|
+
# Checks that the worker is valid with the given options
|
216
|
+
#
|
217
|
+
# @param [Hash] options the `sidekiq_options` to validate
|
218
|
+
#
|
219
|
+
# @return [Boolean]
|
220
|
+
#
|
221
|
+
def validate_worker(options)
|
222
|
+
raise NotUniqueWorker, options unless (lock_type = options[LOCK])
|
223
|
+
|
224
|
+
lock_class = locks[lock_type]
|
225
|
+
lock_class.validate_options(options)
|
226
|
+
end
|
227
|
+
|
228
|
+
#
|
229
|
+
# Checks that the worker is valid with the given options
|
230
|
+
#
|
231
|
+
# @param [Hash] options the `sidekiq_options` to validate
|
232
|
+
#
|
233
|
+
# @raise [InvalidWorker] when {#validate_worker} returns false or nil
|
234
|
+
#
|
235
|
+
def validate_worker!(options)
|
236
|
+
lock_config = validate_worker(options)
|
237
|
+
raise InvalidWorker, lock_config unless lock_config.errors.empty?
|
87
238
|
end
|
88
239
|
end
|