sidekiq-unique-jobs 5.0.11 → 6.0.0.rc1
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/.codeclimate.yml +17 -9
- data/.gitignore +1 -3
- data/.reek +105 -0
- data/.rubocop.yml +36 -1
- data/.simplecov +7 -2
- data/.travis.yml +11 -10
- data/Appraisals +3 -7
- data/CHANGELOG.md +17 -0
- data/Gemfile +16 -13
- data/Guardfile +55 -0
- data/README.md +85 -73
- data/examples/another_unique_job.rb +13 -0
- data/examples/custom_queue_job.rb +12 -0
- data/examples/custom_queue_job_with_filter_method.rb +13 -0
- data/examples/custom_queue_job_with_filter_proc.rb +16 -0
- data/examples/expiring_job.rb +12 -0
- data/examples/inline_worker.rb +12 -0
- data/examples/just_a_worker.rb +13 -0
- data/examples/long_running_job.rb +12 -0
- data/examples/main_job.rb +13 -0
- data/examples/my_job.rb +12 -0
- data/examples/my_unique_job.rb +16 -0
- data/examples/my_unique_job_with_filter_method.rb +21 -0
- data/examples/my_unique_job_with_filter_proc.rb +19 -0
- data/examples/notify_worker.rb +14 -0
- data/examples/plain_class.rb +13 -0
- data/examples/simple_worker.rb +15 -0
- data/examples/spawn_simple_worker.rb +12 -0
- data/examples/test_class.rb +9 -0
- data/examples/unique_across_workers_job.rb +20 -0
- data/examples/unique_job_with_conditional_parameter.rb +18 -0
- data/examples/unique_job_with_filter_method.rb +18 -0
- data/examples/unique_job_with_nil_unique_args.rb +20 -0
- data/examples/unique_job_with_no_unique_args_method.rb +16 -0
- data/examples/unique_job_withthout_unique_args_parameter.rb +18 -0
- data/examples/unique_on_all_queues_job.rb +16 -0
- data/examples/until_and_while_executing_job.rb +13 -0
- data/examples/until_executed_2_job.rb +24 -0
- data/examples/until_executed_job.rb +25 -0
- data/examples/until_executing_job.rb +11 -0
- data/examples/until_expired_job.rb +12 -0
- data/examples/until_global_expired_job.rb +12 -0
- data/examples/while_executing_job.rb +15 -0
- data/examples/while_executing_reject_job.rb +14 -0
- data/examples/without_argument_job.rb +13 -0
- data/lib/sidekiq-unique-jobs.rb +1 -91
- data/lib/sidekiq/simulator.rb +15 -16
- data/lib/sidekiq_unique_jobs.rb +79 -0
- data/lib/sidekiq_unique_jobs/cli.rb +9 -18
- data/lib/sidekiq_unique_jobs/client/middleware.rb +16 -18
- data/lib/sidekiq_unique_jobs/connection.rb +18 -0
- data/lib/sidekiq_unique_jobs/constants.rb +13 -17
- data/lib/sidekiq_unique_jobs/core_ext.rb +28 -55
- data/lib/sidekiq_unique_jobs/exceptions.rb +30 -0
- data/lib/sidekiq_unique_jobs/lock/base_lock.rb +84 -0
- data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +11 -6
- data/lib/sidekiq_unique_jobs/lock/until_executed.rb +6 -58
- data/lib/sidekiq_unique_jobs/lock/until_executing.rb +6 -5
- data/lib/sidekiq_unique_jobs/lock/until_expired.rb +17 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +20 -34
- data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +78 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing_requeue.rb +20 -0
- data/lib/sidekiq_unique_jobs/locksmith.rb +149 -0
- data/lib/sidekiq_unique_jobs/logging.rb +30 -0
- data/lib/sidekiq_unique_jobs/options_with_fallback.rb +27 -41
- data/lib/sidekiq_unique_jobs/scripts.rb +25 -24
- data/lib/sidekiq_unique_jobs/server/middleware.rb +12 -20
- data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +5 -5
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +42 -0
- data/lib/sidekiq_unique_jobs/testing.rb +40 -50
- data/lib/sidekiq_unique_jobs/timeout.rb +15 -0
- data/lib/sidekiq_unique_jobs/timeout/calculator.rb +49 -0
- data/lib/sidekiq_unique_jobs/unique_args.rb +33 -77
- data/lib/sidekiq_unique_jobs/unlockable.rb +5 -14
- data/lib/sidekiq_unique_jobs/util.rb +28 -90
- data/lib/sidekiq_unique_jobs/version.rb +1 -1
- data/redis/acquire_lock.lua +5 -3
- data/redis/create.lua +58 -0
- data/redis/delete.lua +11 -0
- data/redis/release_stale_locks.lua +90 -0
- data/redis/signal.lua +21 -0
- data/sidekiq-unique-jobs.gemspec +15 -8
- metadata +108 -32
- data/lib/sidekiq_unique_jobs/config.rb +0 -17
- data/lib/sidekiq_unique_jobs/lock.rb +0 -12
- data/lib/sidekiq_unique_jobs/lock/until_timeout.rb +0 -17
- data/lib/sidekiq_unique_jobs/run_lock_failed.rb +0 -3
- data/lib/sidekiq_unique_jobs/script_mock.rb +0 -66
- data/lib/sidekiq_unique_jobs/scripts/acquire_lock.rb +0 -47
- data/lib/sidekiq_unique_jobs/scripts/release_lock.rb +0 -49
- data/lib/sidekiq_unique_jobs/testing/sidekiq_overrides.rb +0 -50
- data/lib/sidekiq_unique_jobs/timeout_calculator.rb +0 -67
- data/redis/synchronize.lua +0 -16
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Logging
|
5
|
+
# :reek:UtilityFunction { enabled: false }
|
6
|
+
def logger
|
7
|
+
SidekiqUniqueJobs.logger
|
8
|
+
end
|
9
|
+
|
10
|
+
def log_debug(message_or_exception = nil, &block)
|
11
|
+
logger.debug(message_or_exception, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def log_info(message_or_exception = nil, &block)
|
15
|
+
logger.info(message_or_exception, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def log_warn(message_or_exception = nil, &block)
|
19
|
+
logger.warn(message_or_exception, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def log_error(message_or_exception = nil, &block)
|
23
|
+
logger.error(message_or_exception, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def log_fatal(message_or_exception = nil, &block)
|
27
|
+
logger.fatal(message_or_exception, &block)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,13 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module SidekiqUniqueJobs
|
4
|
+
# Shared logic for dealing with options
|
5
|
+
# This class requires 3 things to be defined in the class including it
|
6
|
+
# 1. item (required)
|
7
|
+
# 2. options (can be nil)
|
8
|
+
# 3. worker_class (required, can be anything)
|
4
9
|
module OptionsWithFallback
|
10
|
+
LOCKS = {
|
11
|
+
until_and_while_executing: SidekiqUniqueJobs::Lock::UntilAndWhileExecuting,
|
12
|
+
until_executed: SidekiqUniqueJobs::Lock::UntilExecuted,
|
13
|
+
until_executing: SidekiqUniqueJobs::Lock::UntilExecuting,
|
14
|
+
until_expired: SidekiqUniqueJobs::Lock::UntilExpired,
|
15
|
+
until_timeout: SidekiqUniqueJobs::Lock::UntilExpired,
|
16
|
+
while_executing: SidekiqUniqueJobs::Lock::WhileExecuting,
|
17
|
+
while_executing_reject: SidekiqUniqueJobs::Lock::WhileExecutingReject,
|
18
|
+
}.freeze
|
19
|
+
|
5
20
|
def self.included(base)
|
6
|
-
base.send(:
|
21
|
+
base.send(:include, SidekiqUniqueJobs::SidekiqWorkerMethods)
|
7
22
|
end
|
8
23
|
|
9
24
|
def unique_enabled?
|
10
|
-
|
25
|
+
SidekiqUniqueJobs.config.enabled && lock_type
|
11
26
|
end
|
12
27
|
|
13
28
|
def unique_disabled?
|
@@ -19,55 +34,26 @@ module SidekiqUniqueJobs
|
|
19
34
|
end
|
20
35
|
|
21
36
|
def lock
|
22
|
-
@lock
|
37
|
+
@lock ||= lock_class.new(item, @redis_pool)
|
23
38
|
end
|
24
39
|
|
25
40
|
def lock_class
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
def unique_lock
|
30
|
-
@unique_lock ||=
|
31
|
-
if options.key?(UNIQUE_KEY) && options[UNIQUE_KEY].to_s == 'true'
|
32
|
-
warn('unique: true is no longer valid. Please set it to the type of lock required like: ' \
|
33
|
-
'`unique: :until_executed`')
|
34
|
-
options[UNIQUE_LOCK_KEY] || SidekiqUniqueJobs.default_lock
|
35
|
-
else
|
36
|
-
lock_type || SidekiqUniqueJobs.default_lock
|
41
|
+
@lock_class ||= begin
|
42
|
+
LOCKS.fetch(lock_type.to_sym) do
|
43
|
+
fail UnknownLock, "No implementation for `unique: :#{lock_type}`"
|
37
44
|
end
|
45
|
+
end
|
38
46
|
end
|
39
47
|
|
40
48
|
def lock_type
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
def lock_type_from(hash, key = UNIQUE_KEY)
|
45
|
-
return nil if hash[key].is_a?(TrueClass)
|
46
|
-
hash[key]
|
47
|
-
end
|
48
|
-
|
49
|
-
def lock_cache
|
50
|
-
self.class.lock_cache
|
51
|
-
end
|
52
|
-
|
53
|
-
def lock_cache=(obj)
|
54
|
-
self.class.lock_cache = obj
|
49
|
+
@lock_type ||= options[UNIQUE_KEY] || item[UNIQUE_KEY]
|
55
50
|
end
|
56
51
|
|
57
52
|
def options
|
58
|
-
@options ||=
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
63
|
-
|
64
|
-
module ClassMethods
|
65
|
-
def lock_cache
|
66
|
-
@lock_cache ||= {}
|
67
|
-
end
|
68
|
-
|
69
|
-
def lock_cache=(obj)
|
70
|
-
@lock_cache = obj
|
53
|
+
@options ||= begin
|
54
|
+
opts = default_worker_options.dup
|
55
|
+
opts.merge!(worker_options) if sidekiq_worker_class?
|
56
|
+
(opts || {}).stringify_keys
|
71
57
|
end
|
72
58
|
end
|
73
59
|
end
|
@@ -3,47 +3,48 @@
|
|
3
3
|
require 'pathname'
|
4
4
|
require 'digest/sha1'
|
5
5
|
require 'concurrent/map'
|
6
|
-
require 'sidekiq_unique_jobs/scripts/acquire_lock'
|
7
|
-
require 'sidekiq_unique_jobs/scripts/release_lock'
|
8
6
|
|
9
7
|
module SidekiqUniqueJobs
|
10
|
-
ScriptError = Class.new(StandardError)
|
11
|
-
UniqueKeyMissing = Class.new(ArgumentError)
|
12
|
-
JidMissing = Class.new(ArgumentError)
|
13
|
-
MaxLockTimeMissing = Class.new(ArgumentError)
|
14
|
-
UnexpectedValue = Class.new(StandardError)
|
15
|
-
|
16
8
|
module Scripts
|
17
9
|
LUA_PATHNAME ||= Pathname.new(__FILE__).dirname.join('../../redis').freeze
|
18
|
-
SOURCE_FILES ||= Dir[LUA_PATHNAME.join('**/*.lua')].compact.freeze
|
19
|
-
DEFINED_METHODS ||= [].freeze
|
20
10
|
SCRIPT_SHAS ||= Concurrent::Map.new
|
21
11
|
|
22
|
-
|
12
|
+
include SidekiqUniqueJobs::Connection
|
23
13
|
|
24
|
-
|
25
|
-
def_delegators :SidekiqUniqueJobs, :connection, :logger
|
14
|
+
module_function
|
26
15
|
|
27
16
|
def call(file_name, redis_pool, options = {})
|
28
|
-
|
17
|
+
execute_script(file_name, redis_pool, options)
|
29
18
|
rescue Redis::CommandError => ex
|
30
|
-
handle_error(ex, file_name
|
19
|
+
handle_error(ex, file_name) do
|
20
|
+
call(file_name, redis_pool, options)
|
21
|
+
end
|
31
22
|
end
|
32
23
|
|
33
|
-
def
|
34
|
-
|
35
|
-
|
36
|
-
conn.evalsha(
|
24
|
+
def execute_script(file_name, redis_pool, options = {})
|
25
|
+
redis(redis_pool) do |conn|
|
26
|
+
sha = script_sha(conn, file_name)
|
27
|
+
conn.evalsha(sha, options)
|
37
28
|
end
|
38
29
|
end
|
39
30
|
|
40
|
-
def
|
41
|
-
if
|
31
|
+
def script_sha(conn, file_name)
|
32
|
+
if (sha = SCRIPT_SHAS.get(file_name))
|
33
|
+
return sha
|
34
|
+
end
|
35
|
+
|
36
|
+
sha = conn.script(:load, script_source(file_name))
|
37
|
+
SCRIPT_SHAS.put(file_name, sha)
|
38
|
+
sha
|
39
|
+
end
|
40
|
+
|
41
|
+
def handle_error(ex, file_name)
|
42
|
+
if ex.message == 'NOSCRIPT No matching script. Please use EVAL.'
|
42
43
|
SCRIPT_SHAS.delete(file_name)
|
43
|
-
|
44
|
-
else
|
45
|
-
raise ScriptError, "Problem compiling #{file_name}. Invalid LUA syntax?"
|
44
|
+
return yield if block_given?
|
46
45
|
end
|
46
|
+
|
47
|
+
raise ScriptError, file_name: file_name, source_exception: ex
|
47
48
|
end
|
48
49
|
|
49
50
|
def script_source(file_name)
|
@@ -3,33 +3,25 @@
|
|
3
3
|
module SidekiqUniqueJobs
|
4
4
|
module Server
|
5
5
|
class Middleware
|
6
|
-
extend Forwardable
|
7
|
-
def_delegators :Sidekiq, :logger
|
8
|
-
def_instance_delegator :@worker, :class, :worker_class
|
9
|
-
|
10
6
|
include OptionsWithFallback
|
11
7
|
|
12
|
-
def call(
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@queue
|
16
|
-
|
17
|
-
return yield unless unique_enabled?
|
18
|
-
lock.send(:execute, after_unlock_hook, &blk)
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
8
|
+
def call(worker_class, item, queue)
|
9
|
+
@worker_class = worker_class
|
10
|
+
@item = item
|
11
|
+
@queue = queue
|
12
|
+
return yield if unique_disabled?
|
22
13
|
|
23
|
-
|
14
|
+
lock.execute(after_unlock_hook) do
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
end
|
24
18
|
|
25
19
|
protected
|
26
20
|
|
27
|
-
|
28
|
-
-> { worker.after_unlock if worker.respond_to?(:after_unlock) }
|
29
|
-
end
|
21
|
+
attr_reader :item
|
30
22
|
|
31
|
-
def
|
32
|
-
|
23
|
+
def after_unlock_hook
|
24
|
+
-> { worker_class.after_unlock if worker_method_defined?(:after_unlock) }
|
33
25
|
end
|
34
26
|
end
|
35
27
|
end
|
@@ -15,14 +15,14 @@ module Sidekiq
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def delete_ext
|
18
|
-
SidekiqUniqueJobs::Unlockable.
|
18
|
+
SidekiqUniqueJobs::Unlockable.delete(item) if delete_orig
|
19
19
|
end
|
20
20
|
|
21
21
|
private
|
22
22
|
|
23
23
|
def remove_job_ext
|
24
24
|
remove_job_orig do |message|
|
25
|
-
SidekiqUniqueJobs::Unlockable.
|
25
|
+
SidekiqUniqueJobs::Unlockable.delete(Sidekiq.load_json(message))
|
26
26
|
yield message
|
27
27
|
end
|
28
28
|
end
|
@@ -41,7 +41,7 @@ module Sidekiq
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def delete_ext
|
44
|
-
SidekiqUniqueJobs::Unlockable.
|
44
|
+
SidekiqUniqueJobs::Unlockable.delete(item) if delete_orig
|
45
45
|
end
|
46
46
|
end
|
47
47
|
include UniqueExtension
|
@@ -58,7 +58,7 @@ module Sidekiq
|
|
58
58
|
|
59
59
|
def delete_ext
|
60
60
|
delete_orig
|
61
|
-
SidekiqUniqueJobs::Unlockable.
|
61
|
+
SidekiqUniqueJobs::Unlockable.delete(item)
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -105,7 +105,7 @@ module Sidekiq
|
|
105
105
|
end
|
106
106
|
|
107
107
|
def delete_by_value_ext(name, value)
|
108
|
-
SidekiqUniqueJobs::Unlockable.
|
108
|
+
SidekiqUniqueJobs::Unlockable.delete(Sidekiq.load_json(value)) if delete_by_value_orig(name, value)
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module SidekiqWorkerMethods
|
5
|
+
def worker_method_defined?(method_sym)
|
6
|
+
worker_class.respond_to?(method_sym)
|
7
|
+
end
|
8
|
+
|
9
|
+
def worker_options
|
10
|
+
return {} unless sidekiq_worker_class?
|
11
|
+
worker_class.get_sidekiq_options.stringify_keys
|
12
|
+
end
|
13
|
+
|
14
|
+
def sidekiq_worker_class?
|
15
|
+
worker_method_defined?(:get_sidekiq_options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def worker_class
|
19
|
+
@_worker_class ||= worker_class_constantize # rubocop:disable Naming/MemoizedInstanceVariableName
|
20
|
+
end
|
21
|
+
|
22
|
+
# Attempt to constantize a string worker_class argument, always
|
23
|
+
# failing back to the original argument when the constant can't be found
|
24
|
+
#
|
25
|
+
# raises an error for other errors
|
26
|
+
def worker_class_constantize(klazz = @worker_class)
|
27
|
+
return klazz unless klazz.is_a?(String)
|
28
|
+
Object.const_get(klazz)
|
29
|
+
rescue NameError => ex
|
30
|
+
case ex.message
|
31
|
+
when /uninitialized constant/
|
32
|
+
klazz
|
33
|
+
else
|
34
|
+
raise
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def default_worker_options
|
39
|
+
Sidekiq.default_worker_options
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -1,72 +1,62 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require 'sidekiq_unique_jobs/script_mock'
|
3
|
+
# :nocov:
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
require 'sidekiq'
|
6
|
+
require 'sidekiq/testing'
|
7
|
+
|
8
|
+
module Sidekiq
|
9
|
+
def self.use_options(tmp_config = {})
|
10
|
+
old_config = default_worker_options
|
11
|
+
default_worker_options.clear
|
12
|
+
self.default_worker_options = tmp_config
|
13
|
+
|
14
|
+
yield
|
15
|
+
ensure
|
16
|
+
default_worker_options.clear
|
17
|
+
self.default_worker_options = old_config
|
14
18
|
end
|
15
19
|
|
16
|
-
module
|
20
|
+
module Worker
|
21
|
+
module ClassMethods
|
22
|
+
def use_options(tmp_config = {})
|
23
|
+
old_config = get_sidekiq_options
|
24
|
+
sidekiq_options(tmp_config)
|
25
|
+
|
26
|
+
yield
|
27
|
+
ensure
|
28
|
+
sidekiq_options(old_config)
|
29
|
+
end
|
30
|
+
|
31
|
+
def clear
|
32
|
+
jobs.each do |job|
|
33
|
+
SidekiqUniqueJobs::Unlockable.delete(job)
|
34
|
+
end
|
35
|
+
|
36
|
+
Sidekiq::Queues[queue].clear
|
37
|
+
jobs.clear
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
17
41
|
module Overrides
|
18
42
|
def self.included(base)
|
19
43
|
base.extend Testing
|
20
44
|
base.class_eval do
|
21
45
|
class << self
|
22
|
-
alias_method :
|
23
|
-
alias_method :
|
46
|
+
alias_method :clear_all_orig, :clear_all
|
47
|
+
alias_method :clear_all, :clear_all_ext
|
24
48
|
end
|
25
49
|
end
|
26
50
|
end
|
27
51
|
|
28
52
|
module Testing
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
else
|
33
|
-
call_orig(file_name, redis_pool, options)
|
34
|
-
end
|
53
|
+
def clear_all_ext
|
54
|
+
clear_all_orig
|
55
|
+
SidekiqUniqueJobs::Util.del('*', 1000)
|
35
56
|
end
|
36
57
|
end
|
37
58
|
end
|
38
59
|
|
39
60
|
include Overrides
|
40
61
|
end
|
41
|
-
|
42
|
-
module Client
|
43
|
-
class Middleware
|
44
|
-
alias call_real call
|
45
|
-
def call(worker_class, item, queue, redis_pool = nil)
|
46
|
-
worker_class = SidekiqUniqueJobs.worker_class_constantize(worker_class)
|
47
|
-
|
48
|
-
if Sidekiq::Testing.inline?
|
49
|
-
call_real(worker_class, item, queue, redis_pool) do
|
50
|
-
_server.call(worker_class.new, item, queue, redis_pool) do
|
51
|
-
yield
|
52
|
-
end
|
53
|
-
end
|
54
|
-
else
|
55
|
-
call_real(worker_class, item, queue, redis_pool) do
|
56
|
-
yield
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def _server
|
62
|
-
SidekiqUniqueJobs::Server::Middleware.new
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
class Testing
|
68
|
-
def mocking!
|
69
|
-
require 'sidekiq_unique_jobs/testing/mocking'
|
70
|
-
end
|
71
|
-
end
|
72
62
|
end
|