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
@@ -0,0 +1,304 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Contains configuration and utility methods that belongs top level
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
7
|
+
module SidekiqUniqueJobs # rubocop:disable Metrics/ModuleLength
|
8
|
+
include SidekiqUniqueJobs::Connection
|
9
|
+
extend SidekiqUniqueJobs::JSON
|
10
|
+
|
11
|
+
module_function
|
12
|
+
|
13
|
+
#
|
14
|
+
# The current configuration (See: {.configure} on how to configure)
|
15
|
+
#
|
16
|
+
#
|
17
|
+
# @return [SidekiqUniqueJobs::Config] the gem configuration
|
18
|
+
#
|
19
|
+
def config
|
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
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# The current logger
|
45
|
+
#
|
46
|
+
#
|
47
|
+
# @return [Logger] the configured logger
|
48
|
+
#
|
49
|
+
def logger
|
50
|
+
config.logger
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# The current gem version
|
55
|
+
#
|
56
|
+
#
|
57
|
+
# @return [String] the current gem version
|
58
|
+
#
|
59
|
+
def version
|
60
|
+
VERSION
|
61
|
+
end
|
62
|
+
|
63
|
+
#
|
64
|
+
# Set a new logger
|
65
|
+
#
|
66
|
+
# @param [Logger] other another logger
|
67
|
+
#
|
68
|
+
# @return [Logger] the new logger
|
69
|
+
#
|
70
|
+
def logger=(other)
|
71
|
+
config.logger = other
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# Check if logging is enabled
|
76
|
+
#
|
77
|
+
#
|
78
|
+
# @return [true, false]
|
79
|
+
#
|
80
|
+
def logging?
|
81
|
+
config.logger_enabled
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# Temporarily use another configuration and reset to the old config after yielding
|
86
|
+
#
|
87
|
+
# @param [Hash] tmp_config the temporary configuration to use
|
88
|
+
#
|
89
|
+
# @return [void]
|
90
|
+
#
|
91
|
+
# @yield control to the caller
|
92
|
+
def use_config(tmp_config = {})
|
93
|
+
raise ::ArgumentError, "#{name}.#{__method__} needs a block" unless block_given?
|
94
|
+
|
95
|
+
old_config = config.to_h
|
96
|
+
reset!
|
97
|
+
configure(tmp_config)
|
98
|
+
yield
|
99
|
+
ensure
|
100
|
+
reset!
|
101
|
+
configure(old_config.to_h)
|
102
|
+
end
|
103
|
+
|
104
|
+
#
|
105
|
+
# Resets configuration to deafult
|
106
|
+
#
|
107
|
+
#
|
108
|
+
# @return [SidekiqUniqueJobs::Config] a default gem configuration
|
109
|
+
#
|
110
|
+
def reset!
|
111
|
+
@config = SidekiqUniqueJobs::Config.default
|
112
|
+
end
|
113
|
+
|
114
|
+
#
|
115
|
+
# Enable SidekiqUniuqeJobs either temporarily in a block or for good
|
116
|
+
#
|
117
|
+
#
|
118
|
+
# @return [true] when not given a block
|
119
|
+
# @return [true, false] the previous value of enable when given a block
|
120
|
+
#
|
121
|
+
# @yieldreturn [void] temporarily enable sidekiq unique jobs while executing a block of code
|
122
|
+
def enable!(&block)
|
123
|
+
toggle(true, &block)
|
124
|
+
end
|
125
|
+
|
126
|
+
#
|
127
|
+
# Disable SidekiqUniuqeJobs either temporarily in a block or for good
|
128
|
+
#
|
129
|
+
#
|
130
|
+
# @return [false] when not given a block
|
131
|
+
# @return [true, false] the previous value of enable when given a block
|
132
|
+
#
|
133
|
+
# @yieldreturn [void] temporarily disable sidekiq unique jobs while executing a block of code
|
134
|
+
def disable!(&block)
|
135
|
+
toggle(false, &block)
|
136
|
+
end
|
137
|
+
|
138
|
+
#
|
139
|
+
# Checks if the gem has been disabled
|
140
|
+
#
|
141
|
+
# @return [true] when config.enabled is true
|
142
|
+
# @return [false] when config.enabled is false
|
143
|
+
#
|
144
|
+
def enabled?
|
145
|
+
config.enabled
|
146
|
+
end
|
147
|
+
|
148
|
+
#
|
149
|
+
# Checks if the gem has been disabled
|
150
|
+
#
|
151
|
+
# @return [true] when config.enabled is false
|
152
|
+
# @return [false] when config.enabled is true
|
153
|
+
#
|
154
|
+
def disabled?
|
155
|
+
!enabled?
|
156
|
+
end
|
157
|
+
|
158
|
+
#
|
159
|
+
# Toggles enabled on or off
|
160
|
+
#
|
161
|
+
# @api private
|
162
|
+
# :nodoc:
|
163
|
+
def toggle(enabled)
|
164
|
+
if block_given?
|
165
|
+
enabled_was = config.enabled
|
166
|
+
config.enabled = enabled
|
167
|
+
yield
|
168
|
+
config.enabled = enabled_was
|
169
|
+
else
|
170
|
+
config.enabled = enabled
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# Configure the gem
|
175
|
+
#
|
176
|
+
# This is usually called once at startup of an application
|
177
|
+
# @param [Hash] options global gem options
|
178
|
+
# @option options [Integer] :lock_timeout (default is 0)
|
179
|
+
# @option options [Integer] :lock_ttl (default is 0)
|
180
|
+
# @option options [true,false] :enabled (default is true)
|
181
|
+
# @option options [String] :lock_prefix (default is 'uniquejobs')
|
182
|
+
# @option options [Logger] :logger (default is Sidekiq.logger)
|
183
|
+
# @yield control to the caller when given block
|
184
|
+
def configure(options = {})
|
185
|
+
if block_given?
|
186
|
+
yield config
|
187
|
+
else
|
188
|
+
options.each do |key, val|
|
189
|
+
config.send("#{key}=", val)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
#
|
195
|
+
# Returns the current redis version
|
196
|
+
#
|
197
|
+
#
|
198
|
+
# @return [String] a string like `5.0.2`
|
199
|
+
#
|
200
|
+
def fetch_redis_version
|
201
|
+
Sidekiq.redis_info["redis_version"]
|
202
|
+
end
|
203
|
+
|
204
|
+
#
|
205
|
+
# Current time as float
|
206
|
+
#
|
207
|
+
#
|
208
|
+
# @return [Float]
|
209
|
+
#
|
210
|
+
def now_f
|
211
|
+
now.to_f
|
212
|
+
end
|
213
|
+
|
214
|
+
#
|
215
|
+
# Current time
|
216
|
+
#
|
217
|
+
#
|
218
|
+
# @return [Time]
|
219
|
+
#
|
220
|
+
def now
|
221
|
+
Time.now
|
222
|
+
end
|
223
|
+
|
224
|
+
#
|
225
|
+
# Checks that the worker is valid with the given options
|
226
|
+
#
|
227
|
+
# @param [Hash] options the `sidekiq_options` to validate
|
228
|
+
#
|
229
|
+
# @return [Boolean]
|
230
|
+
#
|
231
|
+
def validate_worker(options)
|
232
|
+
raise NotUniqueWorker, options unless (lock_type = options[LOCK])
|
233
|
+
|
234
|
+
lock_class = locks[lock_type]
|
235
|
+
lock_class.validate_options(options)
|
236
|
+
end
|
237
|
+
|
238
|
+
#
|
239
|
+
# Checks that the worker is valid with the given options
|
240
|
+
#
|
241
|
+
# @param [Hash] options the `sidekiq_options` to validate
|
242
|
+
#
|
243
|
+
# @raise [InvalidWorker] when {#validate_worker} returns false or nil
|
244
|
+
#
|
245
|
+
def validate_worker!(options)
|
246
|
+
lock_config = validate_worker(options)
|
247
|
+
raise InvalidWorker, lock_config unless lock_config.errors.empty?
|
248
|
+
end
|
249
|
+
|
250
|
+
# Attempt to constantize a string worker_class argument, always
|
251
|
+
# failing back to the original argument when the constant can't be found
|
252
|
+
#
|
253
|
+
# @return [Sidekiq::Worker]
|
254
|
+
def constantize(str)
|
255
|
+
return str.class if str.is_a?(Sidekiq::Worker) # sidekiq v6.x
|
256
|
+
return str unless str.is_a?(String)
|
257
|
+
return Object.const_get(str) unless str.include?("::")
|
258
|
+
|
259
|
+
names = str.split("::")
|
260
|
+
names.shift if names.empty? || names.first.empty?
|
261
|
+
|
262
|
+
names.inject(Object) do |constant, name|
|
263
|
+
# the false flag limits search for name to under the constant namespace
|
264
|
+
# which mimics Rails' behaviour
|
265
|
+
constant.const_get(name, false)
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
# Attempt to constantize a string worker_class argument, always
|
270
|
+
# failing back to the original argument when the constant can't be found
|
271
|
+
#
|
272
|
+
# @return [Sidekiq::Worker, String]
|
273
|
+
def safe_constantize(str)
|
274
|
+
constantize(str)
|
275
|
+
rescue NameError => ex
|
276
|
+
case ex.message
|
277
|
+
when /uninitialized constant/
|
278
|
+
str
|
279
|
+
else
|
280
|
+
raise
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
#
|
285
|
+
# Collection with notifications
|
286
|
+
#
|
287
|
+
#
|
288
|
+
# @return [Reflections]
|
289
|
+
#
|
290
|
+
def reflections
|
291
|
+
@reflections ||= Reflections.new
|
292
|
+
end
|
293
|
+
|
294
|
+
#
|
295
|
+
# Yields notification stack for sidekiq unique jobs to configure notifications
|
296
|
+
#
|
297
|
+
#
|
298
|
+
# @return [void] <description>
|
299
|
+
#
|
300
|
+
# @yieldparam [Reflections] x used to configure notifications
|
301
|
+
def reflect
|
302
|
+
yield reflections if block_given?
|
303
|
+
end
|
304
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
# Module with convenience methods for the Sidekiq::Worker class
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
7
|
+
module SidekiqWorkerMethods
|
8
|
+
#
|
9
|
+
# @!attribute [r] job_class
|
10
|
+
# @return [Sidekiq::Worker] The Sidekiq::Worker implementation
|
11
|
+
attr_reader :job_class
|
12
|
+
|
13
|
+
# Avoids duplicating worker_class.respond_to? in multiple places
|
14
|
+
# @return [true, false]
|
15
|
+
def job_method_defined?(method_sym)
|
16
|
+
job_class.respond_to?(method_sym)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Wraps #get_sidekiq_options to always work with a hash
|
20
|
+
# @return [Hash] of the worker class sidekiq options
|
21
|
+
def job_options
|
22
|
+
return {} unless sidekiq_job_class?
|
23
|
+
|
24
|
+
job_class.get_sidekiq_options.deep_stringify_keys
|
25
|
+
end
|
26
|
+
|
27
|
+
# Tests that the
|
28
|
+
# @return [true] if job_class responds to get_sidekiq_options
|
29
|
+
# @return [false] if job_class does not respond to get_sidekiq_options
|
30
|
+
def sidekiq_job_class?
|
31
|
+
job_method_defined?(:get_sidekiq_options)
|
32
|
+
end
|
33
|
+
|
34
|
+
def job_class=(obj)
|
35
|
+
# this is what was originally passed in, it can be an instance or a class depending on sidekiq version
|
36
|
+
@original_job_class = obj
|
37
|
+
@job_class = job_class_constantize(obj)
|
38
|
+
end
|
39
|
+
|
40
|
+
# The hook to call after a successful unlock
|
41
|
+
# @return [Proc]
|
42
|
+
def after_unlock_hook # rubocop:disable Metrics/MethodLength
|
43
|
+
lambda do
|
44
|
+
if @original_job_class.respond_to?(:after_unlock)
|
45
|
+
# instance method in sidekiq v6
|
46
|
+
if @original_job_class.method(:after_unlock).arity.positive? # arity check to maintain backwards compatibility
|
47
|
+
@original_job_class.after_unlock(item)
|
48
|
+
else
|
49
|
+
@original_job_class.after_unlock
|
50
|
+
end
|
51
|
+
elsif job_class.respond_to?(:after_unlock)
|
52
|
+
# class method regardless of sidekiq version
|
53
|
+
if job_class.method(:after_unlock).arity.positive? # arity check to maintain backwards compatibility
|
54
|
+
job_class.after_unlock(item)
|
55
|
+
else
|
56
|
+
job_class.after_unlock
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Attempt to constantize a string worker_class argument, always
|
63
|
+
# failing back to the original argument when the constant can't be found
|
64
|
+
#
|
65
|
+
# @return [Sidekiq::Worker]
|
66
|
+
def job_class_constantize(klazz = @job_class)
|
67
|
+
SidekiqUniqueJobs.safe_constantize(klazz)
|
68
|
+
end
|
69
|
+
|
70
|
+
#
|
71
|
+
# Returns the default worker options from Sidekiq
|
72
|
+
#
|
73
|
+
#
|
74
|
+
# @return [Hash<Symbol, Object>]
|
75
|
+
#
|
76
|
+
def default_job_options
|
77
|
+
if Sidekiq.respond_to?(:default_job_options)
|
78
|
+
Sidekiq.default_job_options
|
79
|
+
else
|
80
|
+
Sidekiq.default_worker_options
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -1,70 +1,135 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# :nocov:
|
4
|
+
# :nodoc:
|
5
|
+
|
6
|
+
require "sidekiq"
|
7
|
+
require "sidekiq/testing"
|
8
|
+
require "sidekiq_unique_jobs/rspec/matchers"
|
9
|
+
require "sidekiq_unique_jobs/lock/validator"
|
10
|
+
require "sidekiq_unique_jobs/lock/client_validator"
|
11
|
+
require "sidekiq_unique_jobs/lock/server_validator"
|
12
|
+
|
13
|
+
#
|
14
|
+
# See Sidekiq gem for more details
|
15
|
+
#
|
16
|
+
module Sidekiq
|
17
|
+
#
|
18
|
+
# Temporarily turn Sidekiq's options into something different
|
19
|
+
#
|
20
|
+
# @note this method will restore the original options after yielding
|
21
|
+
#
|
22
|
+
# @param [Hash<Symbol, Object>] tmp_config the temporary config to use
|
23
|
+
#
|
24
|
+
def self.use_options(tmp_config = {}) # rubocop:disable Metrics/MethodLength
|
25
|
+
if respond_to?(:default_job_options)
|
26
|
+
default_job_options.clear
|
27
|
+
self.default_job_options = tmp_config
|
9
28
|
else
|
10
|
-
|
29
|
+
default_worker_options.clear
|
30
|
+
self.default_worker_options = tmp_config
|
31
|
+
end
|
32
|
+
|
33
|
+
yield
|
34
|
+
ensure
|
35
|
+
if respond_to?(:default_job_options)
|
36
|
+
default_job_options.clear
|
37
|
+
self.default_job_options = default_job_options
|
38
|
+
else
|
39
|
+
default_worker_options.clear
|
40
|
+
self.default_worker_options = DEFAULT_WORKER_OPTIONS
|
11
41
|
end
|
12
42
|
end
|
13
43
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
44
|
+
#
|
45
|
+
# See Sidekiq::Worker in Sidekiq gem for more details
|
46
|
+
#
|
47
|
+
module Worker
|
48
|
+
#
|
49
|
+
# Adds class methods to Sidekiq::Worker
|
50
|
+
#
|
51
|
+
module ClassMethods
|
52
|
+
#
|
53
|
+
# Temporarily turn a workers sidekiq_options into something different
|
54
|
+
#
|
55
|
+
# @note this method will restore the original configuration after yielding
|
56
|
+
#
|
57
|
+
# @param [Hash<Symbol, Object>] tmp_config the temporary config to use
|
58
|
+
#
|
59
|
+
def use_options(tmp_config = {})
|
60
|
+
old_options = sidekiq_options_hash.dup
|
61
|
+
sidekiq_options(old_options.merge(tmp_config))
|
25
62
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
63
|
+
yield
|
64
|
+
ensure
|
65
|
+
self.sidekiq_options_hash =
|
66
|
+
if Sidekiq.respond_to?(:default_job_options)
|
67
|
+
Sidekiq.default_job_options
|
30
68
|
else
|
31
|
-
|
69
|
+
DEFAULT_WORKER_OPTIONS
|
32
70
|
end
|
33
|
-
|
71
|
+
|
72
|
+
sidekiq_options(old_options)
|
34
73
|
end
|
35
74
|
end
|
36
75
|
|
37
|
-
|
38
|
-
|
76
|
+
#
|
77
|
+
# Prepends deletion of locks to clear_all
|
78
|
+
#
|
79
|
+
module Overrides
|
80
|
+
#
|
81
|
+
# Overrides sidekiq_options on the worker class to prepend validation
|
82
|
+
#
|
83
|
+
# @param [Hash] options worker options
|
84
|
+
#
|
85
|
+
# @return [void]
|
86
|
+
#
|
87
|
+
def sidekiq_options(options = {})
|
88
|
+
SidekiqUniqueJobs.validate_worker!(options) if SidekiqUniqueJobs.config.raise_on_config_error
|
39
89
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
call_real(worker_class, item, queue, redis_pool) do
|
54
|
-
yield
|
90
|
+
super(options)
|
91
|
+
end
|
92
|
+
|
93
|
+
#
|
94
|
+
# Prepends deletion of locks to clear
|
95
|
+
#
|
96
|
+
module ClassMethods
|
97
|
+
#
|
98
|
+
# Clears the jobs for this worker and removes all locks
|
99
|
+
#
|
100
|
+
def clear
|
101
|
+
jobs.each do |job|
|
102
|
+
SidekiqUniqueJobs::Unlockable.unlock(job)
|
55
103
|
end
|
104
|
+
|
105
|
+
super
|
56
106
|
end
|
57
107
|
end
|
108
|
+
end
|
58
109
|
|
59
|
-
|
60
|
-
|
61
|
-
|
110
|
+
prepend Overrides
|
111
|
+
|
112
|
+
#
|
113
|
+
# Prepends methods to Sidekiq::Worker
|
114
|
+
#
|
115
|
+
module ClassMethods
|
116
|
+
prepend Overrides::ClassMethods
|
62
117
|
end
|
63
|
-
end
|
64
118
|
|
65
|
-
|
66
|
-
|
67
|
-
|
119
|
+
#
|
120
|
+
# Prepends singleton methods to Sidekiq::Worker
|
121
|
+
#
|
122
|
+
module SignletonOverrides
|
123
|
+
#
|
124
|
+
# Clears all jobs for this worker and removes all locks
|
125
|
+
#
|
126
|
+
def clear_all
|
127
|
+
super
|
128
|
+
|
129
|
+
SidekiqUniqueJobs::Digests.new.delete_by_pattern("*", count: 10_000)
|
130
|
+
end
|
68
131
|
end
|
132
|
+
|
133
|
+
singleton_class.prepend SignletonOverrides
|
69
134
|
end
|
70
135
|
end
|