sidekiq-unique-jobs 6.0.7 → 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 +1880 -156
- data/README.md +818 -238
- data/bin/uniquejobs +2 -2
- data/lib/sidekiq-unique-jobs.rb +1 -1
- 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 +68 -25
- 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 +50 -20
- 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 -87
- 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 +47 -13
- 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 +45 -5
- data/lib/sidekiq_unique_jobs/lock/until_executed.rb +30 -4
- 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 -7
- data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -11
- 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 +324 -95
- data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
- data/lib/sidekiq_unique_jobs/logging.rb +208 -30
- 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 +33 -30
- 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 +2 -2
- 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 +26 -7
- data/lib/sidekiq_unique_jobs/on_conflict.rb +34 -16
- 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 +115 -66
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +304 -0
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +51 -29
- data/lib/sidekiq_unique_jobs/testing.rb +104 -31
- 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 +114 -0
- data/lib/sidekiq_unique_jobs/web/helpers.rb +140 -15
- 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 +86 -29
- data/lib/sidekiq_unique_jobs.rb +78 -105
- data/lib/tasks/changelog.rake +23 -0
- metadata +154 -218
- data/.codeclimate.yml +0 -35
- data/.csslintrc +0 -2
- data/.dockerignore +0 -4
- data/.editorconfig +0 -14
- data/.eslintignore +0 -1
- data/.eslintrc +0 -213
- data/.fasterer.yml +0 -23
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -31
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -17
- data/.gitignore +0 -28
- data/.reek.yml +0 -87
- data/.rspec +0 -2
- data/.rubocop.yml +0 -127
- data/.simplecov +0 -20
- data/.travis.yml +0 -47
- data/.yardopts +0 -7
- data/Appraisals +0 -29
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -24
- data/Guardfile +0 -55
- data/Rakefile +0 -12
- data/_config.yml +0 -1
- data/assets/unique_digests_1.png +0 -0
- data/assets/unique_digests_2.png +0 -0
- data/bin/bench +0 -20
- data/examples/another_unique_job.rb +0 -15
- data/examples/custom_queue_job.rb +0 -12
- data/examples/custom_queue_job_with_filter_method.rb +0 -13
- data/examples/custom_queue_job_with_filter_proc.rb +0 -16
- data/examples/expiring_job.rb +0 -12
- data/examples/inline_worker.rb +0 -12
- data/examples/just_a_worker.rb +0 -13
- data/examples/long_running_job.rb +0 -14
- data/examples/main_job.rb +0 -14
- data/examples/my_job.rb +0 -12
- data/examples/my_unique_job.rb +0 -15
- data/examples/my_unique_job_with_filter_method.rb +0 -21
- data/examples/my_unique_job_with_filter_proc.rb +0 -19
- data/examples/notify_worker.rb +0 -14
- data/examples/plain_class.rb +0 -13
- data/examples/simple_worker.rb +0 -15
- data/examples/spawn_simple_worker.rb +0 -12
- data/examples/test_class.rb +0 -9
- data/examples/unique_across_workers_job.rb +0 -20
- data/examples/unique_job_on_conflict_raise.rb +0 -14
- data/examples/unique_job_on_conflict_reject.rb +0 -14
- data/examples/unique_job_on_conflict_reschedule.rb +0 -14
- data/examples/unique_job_with_conditional_parameter.rb +0 -18
- data/examples/unique_job_with_filter_method.rb +0 -21
- data/examples/unique_job_with_nil_unique_args.rb +0 -20
- data/examples/unique_job_with_no_unique_args_method.rb +0 -16
- data/examples/unique_job_withthout_unique_args_parameter.rb +0 -18
- data/examples/unique_on_all_queues_job.rb +0 -16
- data/examples/until_and_while_executing_job.rb +0 -17
- data/examples/until_executed_2_job.rb +0 -24
- data/examples/until_executed_job.rb +0 -25
- data/examples/until_executing_job.rb +0 -11
- data/examples/until_expired_job.rb +0 -12
- data/examples/until_global_expired_job.rb +0 -12
- data/examples/while_executing_job.rb +0 -15
- data/examples/while_executing_reject_job.rb +0 -14
- data/examples/without_argument_job.rb +0 -13
- data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
- data/lib/sidekiq_unique_jobs/scripts.rb +0 -89
- data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -40
- 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 -149
- 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 -42
- data/redis/acquire_lock.lua +0 -21
- data/redis/delete.lua +0 -14
- data/redis/delete_by_digest.lua +0 -24
- data/redis/delete_job_by_digest.lua +0 -60
- data/redis/lock.lua +0 -58
- data/redis/release_stale_locks.lua +0 -90
- data/redis/unlock.lua +0 -32
- data/sidekiq-unique-jobs.gemspec +0 -42
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Middleware
|
5
|
+
# The unique sidekiq middleware for the client push
|
6
|
+
#
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
|
+
class Client
|
9
|
+
include Sidekiq::ClientMiddleware if defined?(Sidekiq::ClientMiddleware)
|
10
|
+
|
11
|
+
# prepend "SidekiqUniqueJobs::Middleware"
|
12
|
+
# @!parse prepends SidekiqUniqueJobs::Middleware
|
13
|
+
prepend SidekiqUniqueJobs::Middleware
|
14
|
+
# includes "SidekiqUniqueJobs::Reflectable"
|
15
|
+
# @!parse include SidekiqUniqueJobs::Reflectable
|
16
|
+
include SidekiqUniqueJobs::Reflectable
|
17
|
+
|
18
|
+
# Calls this client middleware
|
19
|
+
# Used from Sidekiq.process_single
|
20
|
+
#
|
21
|
+
# @see SidekiqUniqueJobs::Middleware#call
|
22
|
+
#
|
23
|
+
# @see https://github.com/mperham/sidekiq/wiki/Job-Format
|
24
|
+
# @see https://github.com/mperham/sidekiq/wiki/Middleware
|
25
|
+
#
|
26
|
+
# @yield when uniqueness is disable
|
27
|
+
# @yield when the lock is successful
|
28
|
+
def call(*, &block)
|
29
|
+
lock(&block)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def lock
|
35
|
+
lock_instance.lock do
|
36
|
+
reflect(:locked, item)
|
37
|
+
return yield
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Middleware
|
5
|
+
# The unique sidekiq middleware for the server processor
|
6
|
+
#
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
|
+
class Server
|
9
|
+
include Sidekiq::ServerMiddleware if defined?(Sidekiq::ServerMiddleware)
|
10
|
+
|
11
|
+
# prepend "SidekiqUniqueJobs::Middleware"
|
12
|
+
# @!parse prepends SidekiqUniqueJobs::Middleware
|
13
|
+
prepend SidekiqUniqueJobs::Middleware
|
14
|
+
|
15
|
+
#
|
16
|
+
#
|
17
|
+
# Runs the server middleware (used from Sidekiq::Processor#process)
|
18
|
+
#
|
19
|
+
# @see SidekiqUniqueJobs::Middleware#call
|
20
|
+
#
|
21
|
+
# @see https://github.com/mperham/sidekiq/wiki/Job-Format
|
22
|
+
# @see https://github.com/mperham/sidekiq/wiki/Middleware
|
23
|
+
#
|
24
|
+
# @yield when uniqueness is disabled
|
25
|
+
# @yield when owning the lock
|
26
|
+
def call(*, &block)
|
27
|
+
lock_instance.execute(&block)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -1,42 +1,45 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'sidekiq'
|
4
|
-
|
5
3
|
module SidekiqUniqueJobs
|
4
|
+
#
|
5
|
+
# Provides the sidekiq middleware that makes the gem work
|
6
|
+
#
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
|
+
#
|
6
9
|
module Middleware
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
11
|
-
end
|
10
|
+
include SidekiqUniqueJobs::Logging::Middleware
|
11
|
+
include SidekiqUniqueJobs::OptionsWithFallback
|
12
|
+
include SidekiqUniqueJobs::JSON
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
14
|
+
# The sidekiq job hash
|
15
|
+
# @return [Hash] the Sidekiq job hash
|
16
|
+
attr_reader :item
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
#
|
19
|
+
# This method runs before (prepended) the actual middleware implementation.
|
20
|
+
# This is done to reduce duplication
|
21
|
+
#
|
22
|
+
# @param [Sidekiq::Worker] worker_class
|
23
|
+
# @param [Hash] item a sidekiq job hash
|
24
|
+
# @param [String] queue name of the queue
|
25
|
+
# @param [ConnectionPool] redis_pool only used for compatility reasons
|
26
|
+
#
|
27
|
+
# @return [yield<super>] <description>
|
28
|
+
#
|
29
|
+
# @yieldparam [<type>] if <description>
|
30
|
+
# @yieldreturn [<type>] <describe what yield should return>
|
31
|
+
def call(worker_class, item, queue, redis_pool = nil)
|
32
|
+
@item = item
|
33
|
+
@queue = queue
|
34
|
+
@redis_pool = redis_pool
|
35
|
+
self.job_class = worker_class
|
36
|
+
return yield if unique_disabled?
|
24
37
|
|
25
|
-
|
26
|
-
require 'sidekiq_unique_jobs/server/middleware'
|
27
|
-
chain.add SidekiqUniqueJobs::Server::Middleware
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
38
|
+
SidekiqUniqueJobs::Job.prepare(item) unless item[LOCK_DIGEST]
|
31
39
|
|
32
|
-
|
33
|
-
|
34
|
-
config.client_middleware do |chain|
|
35
|
-
require 'sidekiq_unique_jobs/client/middleware'
|
36
|
-
chain.add SidekiqUniqueJobs::Client::Middleware
|
37
|
-
end
|
40
|
+
with_logging_context do
|
41
|
+
super
|
38
42
|
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
end
|
42
|
-
SidekiqUniqueJobs.send(:extend, SidekiqUniqueJobs::Middleware)
|
@@ -1,17 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'json'
|
4
|
-
|
5
3
|
module SidekiqUniqueJobs
|
6
4
|
# Normalizes hashes by dumping them to json and loading them from json
|
7
5
|
#
|
8
|
-
# @author Mikael Henriksson <mikael@
|
6
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
7
|
module Normalizer
|
8
|
+
extend SidekiqUniqueJobs::JSON
|
9
|
+
|
10
10
|
# Changes hash to a json compatible hash
|
11
11
|
# @param [Hash] args
|
12
12
|
# @return [Hash] a json compatible hash
|
13
13
|
def self.jsonify(args)
|
14
|
-
|
14
|
+
load_json(dump_json(args))
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -4,16 +4,20 @@ module SidekiqUniqueJobs
|
|
4
4
|
module OnConflict
|
5
5
|
# Strategy to log information about conflict
|
6
6
|
#
|
7
|
-
# @author Mikael Henriksson <mikael@
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
8
|
class Log < OnConflict::Strategy
|
9
9
|
include SidekiqUniqueJobs::Logging
|
10
10
|
|
11
|
+
#
|
11
12
|
# Logs an informational message about that the job was not unique
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# @return [void]
|
16
|
+
#
|
12
17
|
def call
|
13
|
-
log_info(
|
14
|
-
|
15
|
-
|
16
|
-
)
|
18
|
+
log_info(<<~MESSAGE.chomp)
|
19
|
+
Skipping job with id (#{item[JID]}) because lock_digest: (#{item[LOCK_DIGEST]}) already exists
|
20
|
+
MESSAGE
|
17
21
|
end
|
18
22
|
end
|
19
23
|
end
|
@@ -4,7 +4,7 @@ module SidekiqUniqueJobs
|
|
4
4
|
module OnConflict
|
5
5
|
# Default conflict strategy class that does nothing
|
6
6
|
#
|
7
|
-
# @author Mikael Henriksson <mikael@
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
8
|
class NullStrategy < OnConflict::Strategy
|
9
9
|
# Do nothing on conflict
|
10
10
|
# @return [nil]
|
@@ -4,13 +4,13 @@ module SidekiqUniqueJobs
|
|
4
4
|
module OnConflict
|
5
5
|
# Strategy to raise an error on conflict
|
6
6
|
#
|
7
|
-
# @author Mikael Henriksson <mikael@
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
8
|
class Raise < OnConflict::Strategy
|
9
9
|
# Raise an error on conflict.
|
10
10
|
# This will cause Sidekiq to retry the job
|
11
11
|
# @raise [SidekiqUniqueJobs::Conflict]
|
12
12
|
def call
|
13
|
-
|
13
|
+
raise SidekiqUniqueJobs::Conflict, item
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -4,16 +4,13 @@ module SidekiqUniqueJobs
|
|
4
4
|
module OnConflict
|
5
5
|
# Strategy to send jobs to dead queue
|
6
6
|
#
|
7
|
-
# @author Mikael Henriksson <mikael@
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
8
|
class Reject < OnConflict::Strategy
|
9
|
+
include SidekiqUniqueJobs::Timing
|
10
|
+
|
9
11
|
# Send jobs to dead queue
|
10
12
|
def call
|
11
|
-
|
12
|
-
send_to_deadset
|
13
|
-
end
|
14
|
-
|
15
|
-
def send_to_deadset
|
16
|
-
log_info { "Adding dead #{item[CLASS_KEY]} job #{item[JID_KEY]}" }
|
13
|
+
log_info { "Adding dead #{item[CLASS]} job #{item[JID]}" }
|
17
14
|
|
18
15
|
if deadset_kill?
|
19
16
|
deadset_kill
|
@@ -22,10 +19,25 @@ module SidekiqUniqueJobs
|
|
22
19
|
end
|
23
20
|
end
|
24
21
|
|
22
|
+
#
|
23
|
+
# Sidekiq version compatibility check
|
24
|
+
# @api private
|
25
|
+
#
|
26
|
+
#
|
27
|
+
# @return [true, false] depending on if Sidekiq::Deadset responds to kill
|
28
|
+
#
|
25
29
|
def deadset_kill?
|
26
30
|
deadset.respond_to?(:kill)
|
27
31
|
end
|
28
32
|
|
33
|
+
#
|
34
|
+
# Use Sidekiqs built in Sidekiq::DeadSet#kill
|
35
|
+
# to get rid of the job
|
36
|
+
# @api private
|
37
|
+
#
|
38
|
+
#
|
39
|
+
# @return [void]
|
40
|
+
#
|
29
41
|
def deadset_kill
|
30
42
|
if kill_with_options?
|
31
43
|
kill_job_with_options
|
@@ -34,38 +46,72 @@ module SidekiqUniqueJobs
|
|
34
46
|
end
|
35
47
|
end
|
36
48
|
|
49
|
+
#
|
50
|
+
# Sidekiq version compatibility check
|
51
|
+
# @api private
|
52
|
+
#
|
53
|
+
#
|
54
|
+
# @return [true] when Sidekiq::Deadset#kill takes more than 1 argument
|
55
|
+
# @return [false] when Sidekiq::Deadset#kill does not take multiple arguments
|
56
|
+
#
|
37
57
|
def kill_with_options?
|
38
58
|
Sidekiq::DeadSet.instance_method(:kill).arity > 1
|
39
59
|
end
|
40
60
|
|
61
|
+
#
|
62
|
+
# Executes the kill instructions without arguments
|
63
|
+
# @api private
|
64
|
+
#
|
65
|
+
# @return [void]
|
66
|
+
#
|
41
67
|
def kill_job_without_options
|
42
68
|
deadset.kill(payload)
|
43
69
|
end
|
44
70
|
|
71
|
+
#
|
72
|
+
# Executes the kill instructions with arguments
|
73
|
+
# @api private
|
74
|
+
#
|
75
|
+
# @return [void]
|
76
|
+
#
|
45
77
|
def kill_job_with_options
|
46
78
|
deadset.kill(payload, notify_failure: false)
|
47
79
|
end
|
48
80
|
|
81
|
+
#
|
82
|
+
# An instance of Sidekiq::Deadset
|
83
|
+
# @api private
|
84
|
+
#
|
85
|
+
# @return [Sidekiq::Deadset]>
|
86
|
+
#
|
49
87
|
def deadset
|
50
88
|
@deadset ||= Sidekiq::DeadSet.new
|
51
89
|
end
|
52
90
|
|
91
|
+
#
|
92
|
+
# Used for compatibility with older Sidekiq versions
|
93
|
+
#
|
94
|
+
#
|
95
|
+
# @return [void]
|
96
|
+
#
|
53
97
|
def push_to_deadset
|
54
|
-
|
55
|
-
conn.multi do
|
56
|
-
|
57
|
-
|
58
|
-
|
98
|
+
redis do |conn|
|
99
|
+
conn.multi do |pipeline|
|
100
|
+
pipeline.zadd("dead", now_f, payload)
|
101
|
+
pipeline.zremrangebyscore("dead", "-inf", now_f - Sidekiq::DeadSet.timeout)
|
102
|
+
pipeline.zremrangebyrank("dead", 0, -Sidekiq::DeadSet.max_jobs)
|
59
103
|
end
|
60
104
|
end
|
61
105
|
end
|
62
106
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
107
|
+
#
|
108
|
+
# The Sidekiq job hash as JSON
|
109
|
+
#
|
110
|
+
#
|
111
|
+
# @return [String] a JSON formatted string
|
112
|
+
#
|
67
113
|
def payload
|
68
|
-
@payload ||=
|
114
|
+
@payload ||= dump_json(item)
|
69
115
|
end
|
70
116
|
end
|
71
117
|
end
|
@@ -4,38 +4,78 @@ module SidekiqUniqueJobs
|
|
4
4
|
module OnConflict
|
5
5
|
# Strategy to replace the job on conflict
|
6
6
|
#
|
7
|
-
# @author Mikael Henriksson <mikael@
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
8
|
class Replace < OnConflict::Strategy
|
9
|
-
|
9
|
+
#
|
10
|
+
# @!attribute [r] queue
|
11
|
+
# @return [String] rthe sidekiq queue this job belongs to
|
12
|
+
attr_reader :queue
|
13
|
+
#
|
14
|
+
# @!attribute [r] lock_digest
|
15
|
+
# @return [String] the unique digest to use for locking
|
16
|
+
attr_reader :lock_digest
|
10
17
|
|
18
|
+
#
|
19
|
+
# Initialize a new Replace strategy
|
20
|
+
#
|
11
21
|
# @param [Hash] item sidekiq job hash
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@
|
22
|
+
#
|
23
|
+
def initialize(item, redis_pool = nil)
|
24
|
+
super(item, redis_pool)
|
25
|
+
@queue = item[QUEUE]
|
26
|
+
@lock_digest = item[LOCK_DIGEST]
|
16
27
|
end
|
17
28
|
|
29
|
+
#
|
18
30
|
# Replace the old job in the queue
|
31
|
+
#
|
32
|
+
#
|
33
|
+
# @return [void] <description>
|
34
|
+
#
|
19
35
|
# @yield to retry the lock after deleting the old one
|
36
|
+
#
|
20
37
|
def call(&block)
|
21
|
-
return unless delete_job_by_digest
|
38
|
+
return unless (deleted_job = delete_job_by_digest)
|
39
|
+
|
40
|
+
log_info("Deleted job: #{deleted_job}")
|
41
|
+
if (del_count = delete_lock)
|
42
|
+
log_info("Deleted `#{del_count}` keys for #{lock_digest}")
|
43
|
+
end
|
22
44
|
|
23
|
-
delete_lock
|
24
45
|
block&.call
|
25
46
|
end
|
26
47
|
|
48
|
+
#
|
27
49
|
# Delete the job from either schedule, retry or the queue
|
50
|
+
#
|
51
|
+
#
|
52
|
+
# @return [String] the deleted job hash
|
53
|
+
# @return [nil] when deleting nothing
|
54
|
+
#
|
28
55
|
def delete_job_by_digest
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
keys: ["#{QUEUE_KEY}:#{queue}", SCHEDULE_SET, RETRY_SET], argv: [unique_digest],
|
33
|
-
)
|
56
|
+
call_script(:delete_job_by_digest,
|
57
|
+
keys: ["#{QUEUE}:#{queue}", SCHEDULE, RETRY],
|
58
|
+
argv: [lock_digest])
|
34
59
|
end
|
35
60
|
|
61
|
+
#
|
36
62
|
# Delete the keys belonging to the job
|
63
|
+
#
|
64
|
+
#
|
65
|
+
# @return [Integer] the number of keys deleted
|
66
|
+
#
|
37
67
|
def delete_lock
|
38
|
-
|
68
|
+
digests.delete_by_digest(lock_digest)
|
69
|
+
end
|
70
|
+
|
71
|
+
#
|
72
|
+
# Access to the {Digests}
|
73
|
+
#
|
74
|
+
#
|
75
|
+
# @return [Digests] and instance with digests
|
76
|
+
#
|
77
|
+
def digests
|
78
|
+
@digests ||= SidekiqUniqueJobs::Digests.new
|
39
79
|
end
|
40
80
|
end
|
41
81
|
end
|
@@ -4,20 +4,31 @@ module SidekiqUniqueJobs
|
|
4
4
|
module OnConflict
|
5
5
|
# Strategy to reschedule job on conflict
|
6
6
|
#
|
7
|
-
# @author Mikael Henriksson <mikael@
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
8
|
class Reschedule < OnConflict::Strategy
|
9
9
|
include SidekiqUniqueJobs::SidekiqWorkerMethods
|
10
|
+
include SidekiqUniqueJobs::Logging
|
11
|
+
include SidekiqUniqueJobs::JSON
|
12
|
+
include SidekiqUniqueJobs::Reflectable
|
10
13
|
|
11
14
|
# @param [Hash] item sidekiq job hash
|
12
|
-
def initialize(item)
|
13
|
-
super
|
14
|
-
|
15
|
+
def initialize(item, redis_pool = nil)
|
16
|
+
super(item, redis_pool)
|
17
|
+
self.job_class = item[CLASS]
|
15
18
|
end
|
16
19
|
|
17
20
|
# Create a new job from the current one.
|
18
21
|
# This will mess up sidekiq stats because a new job is created
|
19
22
|
def call
|
20
|
-
|
23
|
+
if sidekiq_job_class?
|
24
|
+
if job_class.set(queue: item["queue"].to_sym).perform_in(5, *item[ARGS])
|
25
|
+
reflect(:rescheduled, item)
|
26
|
+
else
|
27
|
+
reflect(:reschedule_failed, item)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
reflect(:unknown_sidekiq_worker, item)
|
31
|
+
end
|
21
32
|
end
|
22
33
|
end
|
23
34
|
end
|
@@ -5,25 +5,44 @@ module SidekiqUniqueJobs
|
|
5
5
|
# Abstract conflict strategy class
|
6
6
|
#
|
7
7
|
# @abstract
|
8
|
-
# @author Mikael Henriksson <mikael@
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
9
|
class Strategy
|
10
|
+
include SidekiqUniqueJobs::JSON
|
10
11
|
include SidekiqUniqueJobs::Logging
|
12
|
+
include SidekiqUniqueJobs::Script::Caller
|
13
|
+
include SidekiqUniqueJobs::Timing
|
11
14
|
|
12
|
-
#
|
13
|
-
#
|
15
|
+
# @!attribute [r] item
|
16
|
+
# @return [Hash] sidekiq job hash
|
14
17
|
attr_reader :item
|
18
|
+
# @!attribute [r] redis_pool
|
19
|
+
# @return [Sidekiq::RedisConnection, ConnectionPool, NilClass] the redis connection
|
20
|
+
attr_reader :redis_pool
|
15
21
|
|
16
|
-
#
|
17
|
-
|
18
|
-
|
22
|
+
#
|
23
|
+
# Initialize a new Strategy
|
24
|
+
#
|
25
|
+
# @param [Hash] item sidekiq job hash
|
26
|
+
# @param [ConnectionPool] redis_pool the connection pool instance
|
27
|
+
#
|
28
|
+
def initialize(item, redis_pool = nil)
|
29
|
+
@item = item
|
30
|
+
@redis_pool = redis_pool
|
19
31
|
end
|
20
32
|
|
21
33
|
# Use strategy on conflict
|
22
34
|
# @raise [NotImplementedError] needs to be implemented in child class
|
23
35
|
def call
|
24
|
-
|
36
|
+
raise NotImplementedError, "needs to be implemented in child class"
|
25
37
|
end
|
26
38
|
|
39
|
+
#
|
40
|
+
# Check if the strategy is kind of {Replace}
|
41
|
+
#
|
42
|
+
#
|
43
|
+
# @return [true] when the strategy is a {Replace}
|
44
|
+
# @return [false] when the strategy is not a {Replace}
|
45
|
+
#
|
27
46
|
def replace?
|
28
47
|
is_a?(Replace)
|
29
48
|
end
|
@@ -1,26 +1,44 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
3
|
+
require_relative "on_conflict/strategy"
|
4
|
+
require_relative "on_conflict/null_strategy"
|
5
|
+
require_relative "on_conflict/log"
|
6
|
+
require_relative "on_conflict/raise"
|
7
|
+
require_relative "on_conflict/reject"
|
8
|
+
require_relative "on_conflict/replace"
|
9
|
+
require_relative "on_conflict/reschedule"
|
10
10
|
|
11
11
|
module SidekiqUniqueJobs
|
12
|
+
#
|
13
|
+
# Provides lock conflict resolutions
|
14
|
+
#
|
15
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
16
|
+
#
|
12
17
|
module OnConflict
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
replace: OnConflict::Replace,
|
18
|
-
reschedule: OnConflict::Reschedule,
|
19
|
-
}.freeze
|
18
|
+
# A convenience method for using the configured strategies
|
19
|
+
def self.strategies
|
20
|
+
SidekiqUniqueJobs.strategies
|
21
|
+
end
|
20
22
|
|
21
|
-
#
|
23
|
+
#
|
24
|
+
# Find a strategy to use for conflicting locks
|
25
|
+
#
|
26
|
+
# @param [Symbol] strategy the key for the strategy
|
27
|
+
#
|
28
|
+
# @return [OnConflict::Strategy] when found
|
29
|
+
# @return [OnConflict::NullStrategy] when no other could be found
|
30
|
+
#
|
22
31
|
def self.find_strategy(strategy)
|
23
|
-
|
32
|
+
return OnConflict::NullStrategy unless strategy
|
33
|
+
|
34
|
+
strategies.fetch(strategy.to_sym) do
|
35
|
+
SidekiqUniqueJobs.logger.warn(
|
36
|
+
"No matching implementation for strategy: #{strategy}, returning OnConflict::NullStrategy." \
|
37
|
+
" Available strategies are (#{strategies.inspect})",
|
38
|
+
)
|
39
|
+
|
40
|
+
OnConflict::NullStrategy
|
41
|
+
end
|
24
42
|
end
|
25
43
|
end
|
26
44
|
end
|