sidekiq-unique-jobs 4.0.18 → 7.1.28
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 +5 -5
- data/CHANGELOG.md +1889 -94
- data/LICENSE.txt +21 -0
- data/README.md +927 -119
- data/bin/uniquejobs +7 -0
- data/lib/sidekiq-unique-jobs.rb +2 -78
- 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 +79 -21
- data/lib/sidekiq_unique_jobs/config.rb +310 -14
- data/lib/sidekiq_unique_jobs/connection.rb +23 -0
- data/lib/sidekiq_unique_jobs/constants.rb +52 -16
- data/lib/sidekiq_unique_jobs/core_ext.rb +114 -33
- data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
- data/lib/sidekiq_unique_jobs/digests.rb +111 -0
- data/lib/sidekiq_unique_jobs/exceptions.rb +105 -0
- data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
- data/lib/sidekiq_unique_jobs/job.rb +58 -0
- 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 +165 -0
- 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 +65 -7
- data/lib/sidekiq_unique_jobs/lock/until_executed.rb +34 -64
- data/lib/sidekiq_unique_jobs/lock/until_executing.rb +37 -4
- data/lib/sidekiq_unique_jobs/lock/until_expired.rb +42 -0
- data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +53 -35
- data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +21 -0
- data/lib/sidekiq_unique_jobs/lock.rb +338 -6
- 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/locksmith.rb +383 -0
- data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
- data/lib/sidekiq_unique_jobs/logging.rb +236 -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 +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 +34 -29
- data/lib/sidekiq_unique_jobs/normalizer.rb +11 -1
- data/lib/sidekiq_unique_jobs/on_conflict/log.rb +24 -0
- data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +16 -0
- data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +17 -0
- data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +118 -0
- data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +82 -0
- data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +35 -0
- data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +51 -0
- data/lib/sidekiq_unique_jobs/on_conflict.rb +44 -0
- data/lib/sidekiq_unique_jobs/options_with_fallback.rb +54 -31
- 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 +116 -104
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +304 -0
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +84 -0
- data/lib/sidekiq_unique_jobs/testing.rb +115 -50
- 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 +29 -33
- 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 +5 -1
- data/lib/sidekiq_unique_jobs/version_check.rb +114 -0
- data/lib/sidekiq_unique_jobs/web/helpers.rb +173 -0
- data/lib/sidekiq_unique_jobs/web/views/_paging.erb +10 -0
- data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
- data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
- data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
- data/lib/sidekiq_unique_jobs/web.rb +109 -0
- data/lib/sidekiq_unique_jobs.rb +81 -0
- data/lib/tasks/changelog.rake +23 -0
- metadata +173 -251
- data/.codeclimate.yml +0 -22
- data/.dockerignore +0 -4
- data/.editorconfig +0 -14
- data/.fasterer.yml +0 -23
- data/.gitignore +0 -14
- data/.rspec +0 -3
- data/.rubocop.yml +0 -45
- data/.simplecov +0 -12
- data/.travis.yml +0 -43
- data/Appraisals +0 -31
- data/Gemfile +0 -16
- data/LICENSE +0 -13
- data/Rakefile +0 -12
- data/bin/bench +0 -19
- data/bin/jobs +0 -6
- data/circle.yml +0 -38
- data/gemfiles/sidekiq_2.15.gemfile +0 -9
- data/gemfiles/sidekiq_2.16.gemfile +0 -9
- data/gemfiles/sidekiq_2.17.gemfile +0 -19
- data/gemfiles/sidekiq_3.0.gemfile +0 -19
- data/gemfiles/sidekiq_3.1.gemfile +0 -19
- data/gemfiles/sidekiq_3.2.gemfile +0 -19
- data/gemfiles/sidekiq_3.3.gemfile +0 -19
- data/gemfiles/sidekiq_4.0.gemfile +0 -19
- data/gemfiles/sidekiq_4.1.gemfile +0 -19
- data/gemfiles/sidekiq_develop.gemfile +0 -19
- data/lib/sidekiq/simulator.rb +0 -75
- data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -41
- data/lib/sidekiq_unique_jobs/lock/until_timeout.rb +0 -15
- data/lib/sidekiq_unique_jobs/run_lock_failed.rb +0 -1
- data/lib/sidekiq_unique_jobs/script_mock.rb +0 -68
- data/lib/sidekiq_unique_jobs/scripts.rb +0 -49
- data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -34
- data/lib/sidekiq_unique_jobs/testing/sidekiq_overrides.rb +0 -70
- data/lib/sidekiq_unique_jobs/timeout_calculator.rb +0 -65
- data/lib/sidekiq_unique_jobs/unique_args.rb +0 -134
- data/lib/sidekiq_unique_jobs/util.rb +0 -91
- data/rails_example/.env +0 -12
- data/rails_example/.gitignore +0 -13
- data/rails_example/.rspec +0 -2
- data/rails_example/Gemfile +0 -34
- data/rails_example/Procfile +0 -2
- data/rails_example/README.rdoc +0 -28
- data/rails_example/Rakefile +0 -6
- data/rails_example/app/assets/images/.keep +0 -0
- data/rails_example/app/assets/javascripts/application.js +0 -16
- data/rails_example/app/assets/stylesheets/application.css +0 -15
- data/rails_example/app/channels/appearance_channel.rb +0 -17
- data/rails_example/app/channels/application_cable/channel.rb +0 -4
- data/rails_example/app/channels/application_cable/connection.rb +0 -9
- data/rails_example/app/channels/post_channel.rb +0 -5
- data/rails_example/app/controllers/application_controller.rb +0 -5
- data/rails_example/app/controllers/concerns/.keep +0 -0
- data/rails_example/app/controllers/work_controller.rb +0 -13
- data/rails_example/app/helpers/application_helper.rb +0 -2
- data/rails_example/app/mailers/.keep +0 -0
- data/rails_example/app/models/.keep +0 -0
- data/rails_example/app/models/application_record.rb +0 -3
- data/rails_example/app/models/concerns/.keep +0 -0
- data/rails_example/app/models/guest.rb +0 -21
- data/rails_example/app/models/post.rb +0 -2
- data/rails_example/app/views/layouts/application.html.erb +0 -15
- data/rails_example/app/workers/simple_worker.rb +0 -12
- data/rails_example/app/workers/spawn_simple_worker.rb +0 -7
- data/rails_example/bin/bundle +0 -3
- data/rails_example/bin/check_or_setup_db +0 -57
- data/rails_example/bin/docker-setup +0 -20
- data/rails_example/bin/rails +0 -4
- data/rails_example/bin/rake +0 -4
- data/rails_example/bin/setup +0 -34
- data/rails_example/bin/update +0 -29
- data/rails_example/cable.ru +0 -6
- data/rails_example/common-services.yml +0 -50
- data/rails_example/config/application.rb +0 -27
- data/rails_example/config/boot.rb +0 -3
- data/rails_example/config/cable.yml +0 -9
- data/rails_example/config/database.docker.yml +0 -12
- data/rails_example/config/database.yml +0 -19
- data/rails_example/config/environment.rb +0 -5
- data/rails_example/config/environments/development.rb +0 -54
- data/rails_example/config/environments/production.rb +0 -86
- data/rails_example/config/environments/test.rb +0 -42
- data/rails_example/config/initializers/application_controller_renderer.rb +0 -6
- data/rails_example/config/initializers/assets.rb +0 -11
- data/rails_example/config/initializers/backtrace_silencers.rb +0 -9
- data/rails_example/config/initializers/cookies_serializer.rb +0 -5
- data/rails_example/config/initializers/filter_parameter_logging.rb +0 -4
- data/rails_example/config/initializers/inflections.rb +0 -16
- data/rails_example/config/initializers/mime_types.rb +0 -4
- data/rails_example/config/initializers/new_framework_defaults.rb +0 -23
- data/rails_example/config/initializers/session_store.rb +0 -3
- data/rails_example/config/initializers/sidekiq.rb +0 -13
- data/rails_example/config/initializers/wrap_parameters.rb +0 -14
- data/rails_example/config/locales/en.yml +0 -23
- data/rails_example/config/puma.rb +0 -38
- data/rails_example/config/routes.rb +0 -6
- data/rails_example/config/secrets.yml +0 -22
- data/rails_example/config/sidekiq.yml +0 -6
- data/rails_example/config/spring.rb +0 -6
- data/rails_example/config.ru +0 -4
- data/rails_example/db/migrate/20160724111322_create_posts.rb +0 -12
- data/rails_example/db/schema.rb +0 -25
- data/rails_example/db/seeds.rb +0 -7
- data/rails_example/dev-entrypoint.sh +0 -55
- data/rails_example/dev.env +0 -12
- data/rails_example/docker/rails.Dockerfile +0 -27
- data/rails_example/docker-compose.yml +0 -90
- data/rails_example/lib/assets/.keep +0 -0
- data/rails_example/lib/tasks/.keep +0 -0
- data/rails_example/log/.keep +0 -0
- data/rails_example/public/404.html +0 -67
- data/rails_example/public/422.html +0 -67
- data/rails_example/public/500.html +0 -66
- data/rails_example/public/favicon.ico +0 -0
- data/rails_example/public/robots.txt +0 -5
- data/rails_example/simple.ru +0 -12
- data/rails_example/spec/controllers/work_controller_spec.rb +0 -172
- data/rails_example/spec/factories/posts.rb +0 -8
- data/rails_example/spec/models/post_spec.rb +0 -4
- data/rails_example/spec/rails_helper.rb +0 -21
- data/rails_example/spec/spec_helper.rb +0 -20
- data/rails_example/spec/support/sidekiq_meta.rb +0 -12
- data/rails_example/spec/workers/simple_worker_spec.rb +0 -4
- data/rails_example/vendor/assets/javascripts/.keep +0 -0
- data/rails_example/vendor/assets/stylesheets/.keep +0 -0
- data/redis/acquire_lock.lua +0 -19
- data/redis/release_lock.lua +0 -16
- data/redis/synchronize.lua +0 -16
- data/sidekiq-unique-jobs.gemspec +0 -25
- data/spec/celluloid_with_fallback.rb +0 -18
- data/spec/jobs/another_unique_job.rb +0 -13
- data/spec/jobs/custom_queue_job.rb +0 -6
- data/spec/jobs/custom_queue_job_with_filter_method.rb +0 -7
- data/spec/jobs/custom_queue_job_with_filter_proc.rb +0 -10
- data/spec/jobs/expiring_job.rb +0 -4
- data/spec/jobs/inline_worker.rb +0 -8
- data/spec/jobs/just_a_worker.rb +0 -8
- data/spec/jobs/long_running_job.rb +0 -7
- data/spec/jobs/main_job.rb +0 -8
- data/spec/jobs/my_job.rb +0 -12
- data/spec/jobs/my_unique_job.rb +0 -12
- data/spec/jobs/my_unique_job_with_filter_method.rb +0 -17
- data/spec/jobs/my_unique_job_with_filter_proc.rb +0 -15
- data/spec/jobs/notify_worker.rb +0 -10
- data/spec/jobs/plain_class.rb +0 -4
- data/spec/jobs/simple_worker.rb +0 -11
- data/spec/jobs/spawn_simple_worker.rb +0 -8
- data/spec/jobs/test_class.rb +0 -4
- data/spec/jobs/unique_job_with_filter_method.rb +0 -18
- data/spec/jobs/unique_on_all_queues_job.rb +0 -13
- data/spec/jobs/until_and_while_executing_job.rb +0 -8
- data/spec/jobs/until_executed_job.rb +0 -17
- data/spec/jobs/until_executing_job.rb +0 -8
- data/spec/jobs/until_global_timeout_job.rb +0 -8
- data/spec/jobs/until_timeout_job.rb +0 -8
- data/spec/jobs/while_executing_job.rb +0 -12
- data/spec/lib/sidekiq_unique_jobs/client/middleware_spec.rb +0 -321
- data/spec/lib/sidekiq_unique_jobs/core_ext_spec.rb +0 -25
- data/spec/lib/sidekiq_unique_jobs/lock/until_and_while_executing_spec.rb +0 -43
- data/spec/lib/sidekiq_unique_jobs/lock/until_executed_spec.rb +0 -37
- data/spec/lib/sidekiq_unique_jobs/lock/until_timeout_spec.rb +0 -26
- data/spec/lib/sidekiq_unique_jobs/lock/while_executing_spec.rb +0 -86
- data/spec/lib/sidekiq_unique_jobs/normalizer_spec.rb +0 -21
- data/spec/lib/sidekiq_unique_jobs/options_with_fallback_spec.rb +0 -113
- data/spec/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator_spec.rb +0 -47
- data/spec/lib/sidekiq_unique_jobs/run_lock_timeout_calculator_spec.rb +0 -36
- data/spec/lib/sidekiq_unique_jobs/script_mock_spec.rb +0 -84
- data/spec/lib/sidekiq_unique_jobs/scripts_spec.rb +0 -77
- data/spec/lib/sidekiq_unique_jobs/server/middleware_spec.rb +0 -83
- data/spec/lib/sidekiq_unique_jobs/sidekiq_testing_enabled_spec.rb +0 -181
- data/spec/lib/sidekiq_unique_jobs/sidekiq_unique_ext_spec.rb +0 -80
- data/spec/lib/sidekiq_unique_jobs/sidekiq_unique_jobs_spec.rb +0 -41
- data/spec/lib/sidekiq_unique_jobs/timeout_calculator_spec.rb +0 -70
- data/spec/lib/sidekiq_unique_jobs/unique_args_spec.rb +0 -131
- data/spec/lib/sidekiq_unique_jobs/unlockable_spec.rb +0 -4
- data/spec/lib/sidekiq_unique_jobs/util_spec.rb +0 -53
- data/spec/spec_helper.rb +0 -65
- data/spec/support/matchers/redis_matchers.rb +0 -19
- data/spec/support/ruby_meta.rb +0 -10
- data/spec/support/sidekiq_meta.rb +0 -31
- data/spec/support/unique_macros.rb +0 -71
@@ -1,12 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SidekiqUniqueJobs
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
class Lock
|
5
|
+
# Locks jobs while the job is executing in the server process
|
6
|
+
# - Locks on perform_in or perform_async (see {UntilExecuting})
|
7
|
+
# - Unlocks before yielding to the worker's perform method (see {UntilExecuting})
|
8
|
+
# - Locks before yielding to the worker's perform method (see {WhileExecuting})
|
9
|
+
# - Unlocks after yielding to the worker's perform method (see {WhileExecuting})
|
10
|
+
#
|
11
|
+
# See {#lock} for more information about the client.
|
12
|
+
# See {#execute} for more information about the server
|
13
|
+
#
|
14
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
15
|
+
class UntilAndWhileExecuting < BaseLock
|
16
|
+
#
|
17
|
+
# Locks a sidekiq job
|
18
|
+
#
|
19
|
+
# @note Will call a conflict strategy if lock can't be achieved.
|
20
|
+
#
|
21
|
+
# @return [String, nil] the locked jid when properly locked, else nil.
|
22
|
+
#
|
23
|
+
# @yield to the caller when given a block
|
24
|
+
#
|
25
|
+
def lock(origin: :client, &block)
|
26
|
+
unless (token = locksmith.lock)
|
27
|
+
reflect(:lock_failed, item)
|
28
|
+
call_strategy(origin: origin, &block)
|
29
|
+
|
30
|
+
return
|
9
31
|
end
|
32
|
+
|
33
|
+
yield if block
|
34
|
+
|
35
|
+
token
|
36
|
+
end
|
37
|
+
|
38
|
+
# Executes in the Sidekiq server process
|
39
|
+
# @yield to the worker class perform method
|
40
|
+
def execute
|
41
|
+
if locksmith.unlock
|
42
|
+
# ensure_relocked do
|
43
|
+
runtime_lock.execute { return yield }
|
44
|
+
# end
|
45
|
+
else
|
46
|
+
reflect(:unlock_failed, item)
|
47
|
+
end
|
48
|
+
rescue Exception # rubocop:disable Lint/RescueException
|
49
|
+
reflect(:execution_failed, item)
|
50
|
+
locksmith.lock(wait: 2)
|
51
|
+
|
52
|
+
raise
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def ensure_relocked
|
58
|
+
yield
|
59
|
+
rescue Exception # rubocop:disable Lint/RescueException
|
60
|
+
reflect(:execution_failed, item)
|
61
|
+
locksmith.lock
|
62
|
+
|
63
|
+
raise
|
64
|
+
end
|
65
|
+
|
66
|
+
def runtime_lock
|
67
|
+
@runtime_lock ||= SidekiqUniqueJobs::Lock::WhileExecuting.new(item.dup, callback, redis_pool)
|
10
68
|
end
|
11
69
|
end
|
12
70
|
end
|
@@ -1,77 +1,47 @@
|
|
1
|
-
|
2
|
-
module Lock
|
3
|
-
class UntilExecuted
|
4
|
-
OK ||= 'OK'.freeze
|
5
|
-
|
6
|
-
include SidekiqUniqueJobs::Unlockable
|
7
|
-
|
8
|
-
extend Forwardable
|
9
|
-
def_delegators :Sidekiq, :logger
|
10
|
-
|
11
|
-
def initialize(item, redis_pool = nil)
|
12
|
-
@item = item
|
13
|
-
@redis_pool = redis_pool
|
14
|
-
end
|
1
|
+
# frozen_string_literal: true
|
15
2
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
class Lock
|
5
|
+
# Locks jobs until the server is done executing the job
|
6
|
+
# - Locks on perform_in or perform_async
|
7
|
+
# - Unlocks after yielding to the worker's perform method
|
8
|
+
#
|
9
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
10
|
+
class UntilExecuted < BaseLock
|
11
|
+
#
|
12
|
+
# Locks a sidekiq job
|
13
|
+
#
|
14
|
+
# @note Will call a conflict strategy if lock can't be achieved.
|
15
|
+
#
|
16
|
+
# @return [String, nil] the locked jid when properly locked, else nil.
|
17
|
+
#
|
18
|
+
# @yield to the caller when given a block
|
19
|
+
#
|
20
|
+
def lock(&block)
|
21
|
+
unless (token = locksmith.lock)
|
22
|
+
reflect(:lock_failed, item)
|
23
|
+
call_strategy(origin: :client, &block)
|
24
|
+
|
25
|
+
return
|
27
26
|
end
|
28
|
-
end
|
29
27
|
|
30
|
-
|
31
|
-
unless [:server, :api, :test].include?(scope)
|
32
|
-
raise ArgumentError, "#{scope} middleware can't #{__method__} #{unique_key}"
|
33
|
-
end
|
28
|
+
yield if block
|
34
29
|
|
35
|
-
|
30
|
+
token
|
36
31
|
end
|
37
32
|
|
38
|
-
#
|
39
|
-
|
40
|
-
|
41
|
-
|
33
|
+
# Executes in the Sidekiq server process
|
34
|
+
# @yield to the worker class perform method
|
35
|
+
def execute
|
36
|
+
executed = locksmith.execute do
|
37
|
+
yield
|
38
|
+
unlock_and_callback
|
42
39
|
end
|
43
40
|
|
44
|
-
|
45
|
-
keys: [unique_key],
|
46
|
-
argv: [item[JID_KEY], max_lock_time])
|
47
|
-
case result
|
48
|
-
when 1
|
49
|
-
logger.debug { "successfully locked #{unique_key} for #{max_lock_time} seconds" }
|
50
|
-
true
|
51
|
-
when 0
|
52
|
-
logger.debug { "failed to acquire lock for #{unique_key}" }
|
53
|
-
false
|
54
|
-
else
|
55
|
-
raise "#{__method__} returned an unexpected value (#{result})"
|
56
|
-
end
|
57
|
-
end
|
58
|
-
# rubocop:enable MethodLength
|
59
|
-
|
60
|
-
def unique_key
|
61
|
-
@unique_key ||= UniqueArgs.digest(item)
|
62
|
-
end
|
41
|
+
reflect(:execution_failed, item) unless executed
|
63
42
|
|
64
|
-
|
65
|
-
@max_lock_time ||= QueueLockTimeoutCalculator.for_item(item).seconds
|
43
|
+
nil
|
66
44
|
end
|
67
|
-
|
68
|
-
def after_yield_yield
|
69
|
-
yield
|
70
|
-
end
|
71
|
-
|
72
|
-
private
|
73
|
-
|
74
|
-
attr_reader :item, :redis_pool
|
75
45
|
end
|
76
46
|
end
|
77
47
|
end
|
@@ -1,9 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SidekiqUniqueJobs
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
4
|
+
class Lock
|
5
|
+
# Locks jobs until {#execute} starts
|
6
|
+
# - Locks on perform_in or perform_async
|
7
|
+
# - Unlocks before yielding to the worker's perform method
|
8
|
+
#
|
9
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
10
|
+
class UntilExecuting < BaseLock
|
11
|
+
#
|
12
|
+
# Locks a sidekiq job
|
13
|
+
#
|
14
|
+
# @note Will call a conflict strategy if lock can't be achieved.
|
15
|
+
#
|
16
|
+
# @return [String, nil] the locked jid when properly locked, else nil.
|
17
|
+
#
|
18
|
+
def lock(&block)
|
19
|
+
unless (token = locksmith.lock)
|
20
|
+
reflect(:lock_failed, item)
|
21
|
+
call_strategy(origin: :client, &block)
|
22
|
+
|
23
|
+
return
|
24
|
+
end
|
25
|
+
|
26
|
+
yield if block
|
27
|
+
|
28
|
+
token
|
29
|
+
end
|
30
|
+
|
31
|
+
# Executes in the Sidekiq server process
|
32
|
+
# @yield to the worker class perform method
|
33
|
+
def execute
|
34
|
+
callback_safely if locksmith.unlock
|
6
35
|
yield
|
36
|
+
rescue StandardError => ex
|
37
|
+
reflect(:execution_failed, item, ex)
|
38
|
+
locksmith.lock(wait: 1)
|
39
|
+
raise
|
7
40
|
end
|
8
41
|
end
|
9
42
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
class Lock
|
5
|
+
#
|
6
|
+
# UntilExpired locks until the job expires
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class UntilExpired < UntilExecuted
|
11
|
+
#
|
12
|
+
# Locks a sidekiq job
|
13
|
+
#
|
14
|
+
# @note Will call a conflict strategy if lock can't be achieved.
|
15
|
+
#
|
16
|
+
# @return [String, nil] the locked jid when properly locked, else nil.
|
17
|
+
#
|
18
|
+
# @yield to the caller when given a block
|
19
|
+
#
|
20
|
+
def lock(&block)
|
21
|
+
unless (token = locksmith.lock)
|
22
|
+
reflect(:lock_failed, item)
|
23
|
+
call_strategy(origin: :client, &block)
|
24
|
+
|
25
|
+
return
|
26
|
+
end
|
27
|
+
|
28
|
+
yield if block
|
29
|
+
|
30
|
+
token
|
31
|
+
end
|
32
|
+
|
33
|
+
# Executes in the Sidekiq server process
|
34
|
+
# @yield to the worker class perform method
|
35
|
+
def execute(&block)
|
36
|
+
executed = locksmith.execute(&block)
|
37
|
+
|
38
|
+
reflect(:execution_failed, item) unless executed
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
class Lock
|
5
|
+
#
|
6
|
+
# Validator base class to avoid some duplication
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class Validator
|
11
|
+
#
|
12
|
+
# @return [Hash] a hash mapping of deprecated keys and their new value
|
13
|
+
DEPRECATED_KEYS = {
|
14
|
+
UNIQUE.to_sym => LOCK.to_sym,
|
15
|
+
UNIQUE_ARGS.to_sym => LOCK_ARGS_METHOD.to_sym,
|
16
|
+
LOCK_ARGS.to_sym => LOCK_ARGS_METHOD.to_sym,
|
17
|
+
UNIQUE_PREFIX.to_sym => LOCK_PREFIX.to_sym,
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
#
|
21
|
+
# Shorthand for `new(options).validate`
|
22
|
+
#
|
23
|
+
# @param [Hash] options the sidekiq_options for the worker being validated
|
24
|
+
#
|
25
|
+
# @return [LockConfig] the lock configuration with errors if any
|
26
|
+
#
|
27
|
+
def self.validate(options)
|
28
|
+
new(options).validate
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# @!attribute [r] lock_config
|
33
|
+
# @return [LockConfig] the lock configuration for this worker
|
34
|
+
attr_reader :lock_config
|
35
|
+
|
36
|
+
#
|
37
|
+
# Initialize a new validator
|
38
|
+
#
|
39
|
+
# @param [Hash] options the sidekiq_options for the worker being validated
|
40
|
+
#
|
41
|
+
def initialize(options)
|
42
|
+
@options = options.transform_keys(&:to_sym)
|
43
|
+
@lock_config = LockConfig.new(options)
|
44
|
+
handle_deprecations
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# Validate the workers lock configuration
|
49
|
+
#
|
50
|
+
#
|
51
|
+
# @return [LockConfig] the lock configuration with errors if any
|
52
|
+
#
|
53
|
+
def validate
|
54
|
+
case lock_config.type
|
55
|
+
when :while_executing
|
56
|
+
validate_server
|
57
|
+
when :until_executing
|
58
|
+
validate_client
|
59
|
+
else
|
60
|
+
validate_client
|
61
|
+
validate_server
|
62
|
+
end
|
63
|
+
|
64
|
+
lock_config
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# Validate deprecated keys
|
69
|
+
# adds useful information about how to proceed with fixing handle_deprecations
|
70
|
+
#
|
71
|
+
# @return [void]
|
72
|
+
#
|
73
|
+
def handle_deprecations
|
74
|
+
DEPRECATED_KEYS.each do |old, new|
|
75
|
+
next unless @options.key?(old)
|
76
|
+
|
77
|
+
lock_config.errors[old] = "is deprecated, use `#{new}: #{@options[old]}` instead."
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
#
|
82
|
+
# Validates the client configuration
|
83
|
+
#
|
84
|
+
def validate_client
|
85
|
+
ClientValidator.validate(lock_config)
|
86
|
+
end
|
87
|
+
|
88
|
+
#
|
89
|
+
# Validates the server configuration
|
90
|
+
#
|
91
|
+
def validate_server
|
92
|
+
ServerValidator.validate(lock_config)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -1,49 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SidekiqUniqueJobs
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
class Lock
|
5
|
+
# Locks jobs while the job is executing in the server process
|
6
|
+
# - Locks before yielding to the worker's perform method
|
7
|
+
# - Unlocks after yielding to the worker's perform method
|
8
|
+
#
|
9
|
+
# See {#lock} for more information about the client.
|
10
|
+
# See {#execute} for more information about the server
|
11
|
+
#
|
12
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
13
|
+
class WhileExecuting < BaseLock
|
14
|
+
RUN_SUFFIX = ":RUN"
|
7
15
|
|
8
|
-
|
9
|
-
|
10
|
-
@mutex = Mutex.new
|
11
|
-
@redis_pool = redis_pool
|
12
|
-
@unique_digest = "#{create_digest}:run"
|
13
|
-
end
|
16
|
+
include SidekiqUniqueJobs::OptionsWithFallback
|
17
|
+
include SidekiqUniqueJobs::Logging::Middleware
|
14
18
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
raise
|
23
|
-
ensure
|
24
|
-
SidekiqUniqueJobs.connection(@redis_pool) { |c| c.del @unique_digest }
|
25
|
-
@mutex.unlock
|
19
|
+
# @param [Hash] item the Sidekiq job hash
|
20
|
+
# @param [Proc] callback callback to call after unlock
|
21
|
+
# @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
|
22
|
+
#
|
23
|
+
def initialize(item, callback, redis_pool = nil)
|
24
|
+
super(item, callback, redis_pool)
|
25
|
+
append_unique_key_suffix
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
# Simulate that a client lock was achieved.
|
29
|
+
# These locks should only ever be created in the server process.
|
30
|
+
# @return [true] always returns true
|
31
|
+
def lock
|
32
|
+
job_id = item[JID]
|
33
|
+
yield if block_given?
|
33
34
|
|
34
|
-
|
35
|
-
@max_lock_time ||= RunLockTimeoutCalculator.for_item(@item).seconds
|
35
|
+
job_id
|
36
36
|
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
# Executes in the Sidekiq server process.
|
39
|
+
# These jobs are locked in the server process not from the client
|
40
|
+
# @yield to the worker class perform method
|
41
|
+
def execute(&block)
|
42
|
+
with_logging_context do
|
43
|
+
executed = locksmith.execute do
|
44
|
+
yield
|
45
|
+
callback_safely if locksmith.unlock
|
46
|
+
ensure
|
47
|
+
locksmith.unlock
|
48
|
+
end
|
49
|
+
|
50
|
+
unless executed
|
51
|
+
reflect(:execution_failed, item)
|
52
|
+
call_strategy(origin: :server, &block)
|
53
|
+
end
|
41
54
|
end
|
42
55
|
end
|
43
56
|
|
44
|
-
|
45
|
-
|
46
|
-
|
57
|
+
private
|
58
|
+
|
59
|
+
# This is safe as the base_lock always creates a new digest
|
60
|
+
# The append there for needs to be done every time
|
61
|
+
def append_unique_key_suffix
|
62
|
+
return if (lock_digest = item[LOCK_DIGEST]).end_with?(RUN_SUFFIX)
|
63
|
+
|
64
|
+
item[LOCK_DIGEST] = lock_digest + RUN_SUFFIX
|
47
65
|
end
|
48
66
|
end
|
49
67
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
class Lock
|
5
|
+
# Locks jobs while executing
|
6
|
+
# Locks from the server process
|
7
|
+
# Unlocks after the server is done processing
|
8
|
+
#
|
9
|
+
# See {#lock} for more information about the client.
|
10
|
+
# See {#execute} for more information about the server
|
11
|
+
#
|
12
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
13
|
+
class WhileExecutingReject < WhileExecuting
|
14
|
+
# Overridden with a forced {OnConflict::Reject} strategy
|
15
|
+
# @return [OnConflict::Reject] a reject strategy
|
16
|
+
def server_strategy
|
17
|
+
@server_strategy ||= OnConflict.find_strategy(:reject).new(item, redis_pool)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|