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,298 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Orphans
|
5
|
+
#
|
6
|
+
# Class DeleteOrphans provides deletion of orphaned digests
|
7
|
+
#
|
8
|
+
# @note this is a much slower version of the lua script but does not crash redis
|
9
|
+
#
|
10
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
11
|
+
#
|
12
|
+
# rubocop:disable Metrics/ClassLength
|
13
|
+
class RubyReaper < Reaper
|
14
|
+
include SidekiqUniqueJobs::Timing
|
15
|
+
|
16
|
+
#
|
17
|
+
# @return [String] the suffix for :RUN locks
|
18
|
+
RUN_SUFFIX = ":RUN"
|
19
|
+
#
|
20
|
+
# @return [Integer] the maximum combined length of sidekiq queues for running the reaper
|
21
|
+
MAX_QUEUE_LENGTH = 1000
|
22
|
+
#
|
23
|
+
# @!attribute [r] digests
|
24
|
+
# @return [SidekiqUniqueJobs::Digests] digest collection
|
25
|
+
attr_reader :digests
|
26
|
+
#
|
27
|
+
# @!attribute [r] scheduled
|
28
|
+
# @return [Redis::SortedSet] the Sidekiq ScheduleSet
|
29
|
+
attr_reader :scheduled
|
30
|
+
#
|
31
|
+
# @!attribute [r] retried
|
32
|
+
# @return [Redis::SortedSet] the Sidekiq RetrySet
|
33
|
+
attr_reader :retried
|
34
|
+
|
35
|
+
#
|
36
|
+
# @!attribute [r] start_time
|
37
|
+
# @return [Integer] The timestamp this execution started represented as Time (used for locks)
|
38
|
+
attr_reader :start_time
|
39
|
+
|
40
|
+
#
|
41
|
+
# @!attribute [r] start_time
|
42
|
+
# @return [Integer] The clock stamp this execution started represented as integer
|
43
|
+
# (used for redis compatibility as it is more accurate than time)
|
44
|
+
attr_reader :start_source
|
45
|
+
|
46
|
+
#
|
47
|
+
# @!attribute [r] timeout_ms
|
48
|
+
# @return [Integer] The allowed ms before timeout
|
49
|
+
attr_reader :timeout_ms
|
50
|
+
|
51
|
+
#
|
52
|
+
# Initialize a new instance of DeleteOrphans
|
53
|
+
#
|
54
|
+
# @param [Redis] conn a connection to redis
|
55
|
+
#
|
56
|
+
def initialize(conn)
|
57
|
+
super(conn)
|
58
|
+
@digests = SidekiqUniqueJobs::Digests.new
|
59
|
+
@scheduled = Redis::SortedSet.new(SCHEDULE)
|
60
|
+
@retried = Redis::SortedSet.new(RETRY)
|
61
|
+
@start_time = Time.now
|
62
|
+
@start_source = time_source.call
|
63
|
+
@timeout_ms = SidekiqUniqueJobs.config.reaper_timeout * 1000
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# Delete orphaned digests
|
68
|
+
#
|
69
|
+
#
|
70
|
+
# @return [Integer] the number of reaped locks
|
71
|
+
#
|
72
|
+
def call
|
73
|
+
return if queues_very_full?
|
74
|
+
|
75
|
+
BatchDelete.call(expired_digests, conn)
|
76
|
+
BatchDelete.call(orphans, conn)
|
77
|
+
end
|
78
|
+
|
79
|
+
def expired_digests
|
80
|
+
max_score = (start_time - reaper_timeout).to_f
|
81
|
+
|
82
|
+
if VersionCheck.satisfied?(redis_version, ">= 6.2.0") && VersionCheck.satisfied?(::Redis::VERSION, ">= 4.6.0")
|
83
|
+
conn.zrange(EXPIRING_DIGESTS, 0, max_score, byscore: true)
|
84
|
+
else
|
85
|
+
conn.zrangebyscore(EXPIRING_DIGESTS, 0, max_score)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
#
|
90
|
+
# Find orphaned digests
|
91
|
+
#
|
92
|
+
#
|
93
|
+
# @return [Array<String>] an array of orphaned digests
|
94
|
+
#
|
95
|
+
def orphans # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
|
96
|
+
page = 0
|
97
|
+
per = reaper_count * 2
|
98
|
+
orphans = []
|
99
|
+
results = conn.zrange(digests.key, page * per, (page + 1) * per)
|
100
|
+
|
101
|
+
while results.size.positive?
|
102
|
+
results.each do |digest|
|
103
|
+
break if timeout?
|
104
|
+
next if belongs_to_job?(digest)
|
105
|
+
|
106
|
+
orphans << digest
|
107
|
+
break if orphans.size >= reaper_count
|
108
|
+
end
|
109
|
+
|
110
|
+
break if timeout?
|
111
|
+
break if orphans.size >= reaper_count
|
112
|
+
|
113
|
+
page += 1
|
114
|
+
results = conn.zrange(digests.key, page * per, (page + 1) * per)
|
115
|
+
end
|
116
|
+
|
117
|
+
orphans
|
118
|
+
end
|
119
|
+
|
120
|
+
def timeout?
|
121
|
+
elapsed_ms >= timeout_ms
|
122
|
+
end
|
123
|
+
|
124
|
+
def elapsed_ms
|
125
|
+
time_source.call - start_source
|
126
|
+
end
|
127
|
+
|
128
|
+
#
|
129
|
+
# Checks if the digest has a matching job.
|
130
|
+
# 1. It checks the scheduled set
|
131
|
+
# 2. It checks the retry set
|
132
|
+
# 3. It goes through all queues
|
133
|
+
#
|
134
|
+
#
|
135
|
+
# @param [String] digest the digest to search for
|
136
|
+
#
|
137
|
+
# @return [true] when either of the checks return true
|
138
|
+
# @return [false] when no job was found for this digest
|
139
|
+
#
|
140
|
+
def belongs_to_job?(digest)
|
141
|
+
scheduled?(digest) || retried?(digest) || enqueued?(digest) || active?(digest)
|
142
|
+
end
|
143
|
+
|
144
|
+
#
|
145
|
+
# Checks if the digest exists in the Sidekiq::ScheduledSet
|
146
|
+
#
|
147
|
+
# @param [String] digest the current digest
|
148
|
+
#
|
149
|
+
# @return [true] when digest exists in scheduled set
|
150
|
+
#
|
151
|
+
def scheduled?(digest)
|
152
|
+
in_sorted_set?(SCHEDULE, digest)
|
153
|
+
end
|
154
|
+
|
155
|
+
#
|
156
|
+
# Checks if the digest exists in the Sidekiq::RetrySet
|
157
|
+
#
|
158
|
+
# @param [String] digest the current digest
|
159
|
+
#
|
160
|
+
# @return [true] when digest exists in retry set
|
161
|
+
#
|
162
|
+
def retried?(digest)
|
163
|
+
in_sorted_set?(RETRY, digest)
|
164
|
+
end
|
165
|
+
|
166
|
+
#
|
167
|
+
# Checks if the digest exists in a Sidekiq::Queue
|
168
|
+
#
|
169
|
+
# @param [String] digest the current digest
|
170
|
+
#
|
171
|
+
# @return [true] when digest exists in any queue
|
172
|
+
#
|
173
|
+
def enqueued?(digest)
|
174
|
+
Sidekiq.redis do |conn|
|
175
|
+
queues(conn) do |queue|
|
176
|
+
entries(conn, queue) do |entry|
|
177
|
+
return true if entry.include?(digest)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
false
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
def active?(digest) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
186
|
+
Sidekiq.redis do |conn|
|
187
|
+
procs = conn.sscan_each("processes").to_a
|
188
|
+
return false if procs.empty?
|
189
|
+
|
190
|
+
procs.sort.each do |key|
|
191
|
+
valid, workers = conn.pipelined do |pipeline|
|
192
|
+
# TODO: Remove the if statement in the future
|
193
|
+
if pipeline.respond_to?(:exists?)
|
194
|
+
pipeline.exists?(key)
|
195
|
+
else
|
196
|
+
pipeline.exists(key)
|
197
|
+
end
|
198
|
+
pipeline.hgetall("#{key}:workers")
|
199
|
+
end
|
200
|
+
|
201
|
+
next unless valid
|
202
|
+
next unless workers.any?
|
203
|
+
|
204
|
+
workers.each_pair do |_tid, job|
|
205
|
+
next unless (item = safe_load_json(job))
|
206
|
+
|
207
|
+
payload = safe_load_json(item[PAYLOAD])
|
208
|
+
|
209
|
+
return true if match?(digest, payload[LOCK_DIGEST])
|
210
|
+
return true if considered_active?(payload[CREATED_AT])
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
false
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
def match?(key_one, key_two)
|
219
|
+
return false if key_one.nil? || key_two.nil?
|
220
|
+
|
221
|
+
key_one.delete_suffix(RUN_SUFFIX) == key_two.delete_suffix(RUN_SUFFIX)
|
222
|
+
end
|
223
|
+
|
224
|
+
def considered_active?(time_f)
|
225
|
+
(Time.now - reaper_timeout).to_f < time_f
|
226
|
+
end
|
227
|
+
|
228
|
+
#
|
229
|
+
# Loops through all the redis queues and yields them one by one
|
230
|
+
#
|
231
|
+
# @param [Redis] conn the connection to use for fetching queues
|
232
|
+
#
|
233
|
+
# @return [void]
|
234
|
+
#
|
235
|
+
# @yield queues one at a time
|
236
|
+
#
|
237
|
+
def queues(conn, &block)
|
238
|
+
conn.sscan_each("queues", &block)
|
239
|
+
end
|
240
|
+
|
241
|
+
def entries(conn, queue, &block) # rubocop:disable Metrics/MethodLength
|
242
|
+
queue_key = "queue:#{queue}"
|
243
|
+
initial_size = conn.llen(queue_key)
|
244
|
+
deleted_size = 0
|
245
|
+
page = 0
|
246
|
+
page_size = 50
|
247
|
+
|
248
|
+
loop do
|
249
|
+
range_start = (page * page_size) - deleted_size
|
250
|
+
|
251
|
+
range_end = range_start + page_size - 1
|
252
|
+
entries = conn.lrange(queue_key, range_start, range_end)
|
253
|
+
page += 1
|
254
|
+
|
255
|
+
break if entries.empty?
|
256
|
+
|
257
|
+
entries.each(&block)
|
258
|
+
|
259
|
+
deleted_size = initial_size - conn.llen(queue_key)
|
260
|
+
|
261
|
+
# The queue is growing, not shrinking, just keep looping
|
262
|
+
deleted_size = 0 if deleted_size.negative?
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
# If sidekiq queues are very full, it becomes highly inefficient for the reaper
|
267
|
+
# because it must check every queued job to verify a digest is safe to delete
|
268
|
+
# The reaper checks queued jobs in batches of 50, adding 2 reads per digest
|
269
|
+
# With a queue length of 1,000 jobs, that's over 20 extra reads per digest.
|
270
|
+
def queues_very_full?
|
271
|
+
total_queue_size = 0
|
272
|
+
Sidekiq.redis do |conn|
|
273
|
+
queues(conn) do |queue|
|
274
|
+
total_queue_size += conn.llen("queue:#{queue}")
|
275
|
+
|
276
|
+
return true if total_queue_size > MAX_QUEUE_LENGTH
|
277
|
+
end
|
278
|
+
end
|
279
|
+
false
|
280
|
+
end
|
281
|
+
|
282
|
+
#
|
283
|
+
# Checks a sorted set for the existance of this digest
|
284
|
+
#
|
285
|
+
#
|
286
|
+
# @param [String] key the key for the sorted set
|
287
|
+
# @param [String] digest the digest to scan for
|
288
|
+
#
|
289
|
+
# @return [true] when found
|
290
|
+
# @return [false] when missing
|
291
|
+
#
|
292
|
+
def in_sorted_set?(key, digest)
|
293
|
+
conn.zscan_each(key, match: "*#{digest}*", count: 1).to_a.any?
|
294
|
+
end
|
295
|
+
end
|
296
|
+
# rubocop:enable Metrics/ClassLength
|
297
|
+
end
|
298
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Redis
|
5
|
+
#
|
6
|
+
# Class Entity functions as a base class for redis types
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class Entity
|
11
|
+
# includes "SidekiqUniqueJobs::Logging"
|
12
|
+
# @!parse include SidekiqUniqueJobs::Logging
|
13
|
+
include SidekiqUniqueJobs::Logging
|
14
|
+
|
15
|
+
# includes "SidekiqUniqueJobs::Script::Caller"
|
16
|
+
# @!parse include SidekiqUniqueJobs::Script::Caller
|
17
|
+
include SidekiqUniqueJobs::Script::Caller
|
18
|
+
|
19
|
+
# includes "SidekiqUniqueJobs::JSON"
|
20
|
+
# @!parse include SidekiqUniqueJobs::JSON
|
21
|
+
include SidekiqUniqueJobs::JSON
|
22
|
+
|
23
|
+
# includes "SidekiqUniqueJobs::Timing"
|
24
|
+
# @!parse include SidekiqUniqueJobs::Timing
|
25
|
+
include SidekiqUniqueJobs::Timing
|
26
|
+
|
27
|
+
#
|
28
|
+
# @!attribute [r] key
|
29
|
+
# @return [String] the redis key for this entity
|
30
|
+
attr_reader :key
|
31
|
+
|
32
|
+
#
|
33
|
+
# Initialize a new Entity
|
34
|
+
#
|
35
|
+
# @param [String] key the redis key for this entity
|
36
|
+
#
|
37
|
+
def initialize(key)
|
38
|
+
@key = key
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# Checks if the key for this entity exists in redis
|
43
|
+
#
|
44
|
+
#
|
45
|
+
# @return [true] when exists
|
46
|
+
# @return [false] when not exists
|
47
|
+
#
|
48
|
+
def exist?
|
49
|
+
redis do |conn|
|
50
|
+
# TODO: Remove the if statement in the future
|
51
|
+
value =
|
52
|
+
if conn.respond_to?(:exists?)
|
53
|
+
conn.exists?(key)
|
54
|
+
else
|
55
|
+
conn.exists(key)
|
56
|
+
end
|
57
|
+
|
58
|
+
return value if boolean?(value)
|
59
|
+
|
60
|
+
value.to_i.positive?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# The number of microseconds until the key expires
|
66
|
+
#
|
67
|
+
#
|
68
|
+
# @return [Integer] expiration in milliseconds
|
69
|
+
#
|
70
|
+
def pttl
|
71
|
+
redis { |conn| conn.pttl(key) }
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# The number of seconds until the key expires
|
76
|
+
#
|
77
|
+
#
|
78
|
+
# @return [Integer] expiration in seconds
|
79
|
+
#
|
80
|
+
def ttl
|
81
|
+
redis { |conn| conn.ttl(key) }
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# Check if the entity has expiration
|
86
|
+
#
|
87
|
+
#
|
88
|
+
# @return [true] when entity is set to exire
|
89
|
+
# @return [false] when entity isn't expiring
|
90
|
+
#
|
91
|
+
def expires?
|
92
|
+
pttl.positive? || ttl.positive?
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# Returns the number of entries in this entity
|
97
|
+
#
|
98
|
+
#
|
99
|
+
# @return [Integer] 0
|
100
|
+
#
|
101
|
+
def count
|
102
|
+
0
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def boolean?(value)
|
108
|
+
[TrueClass, FalseClass].any? { |klazz| value.is_a?(klazz) }
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Redis
|
5
|
+
#
|
6
|
+
# Class Hash provides convenient access to redis hashes
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class Hash < Entity
|
11
|
+
#
|
12
|
+
# Return entries for this hash
|
13
|
+
#
|
14
|
+
# @param [true,false] with_values false return hash
|
15
|
+
#
|
16
|
+
# @return [Array<Object>] when given with_values: false
|
17
|
+
# @return [Hash<String, String>] when given with_values: true
|
18
|
+
#
|
19
|
+
def entries(with_values: false)
|
20
|
+
if with_values
|
21
|
+
redis { |conn| conn.hgetall(key) }
|
22
|
+
else
|
23
|
+
redis { |conn| conn.hkeys(key) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# Removes the key from redis
|
29
|
+
#
|
30
|
+
def del(*fields)
|
31
|
+
redis { |conn| conn.hdel(key, *fields) }
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# Get a members value
|
36
|
+
#
|
37
|
+
# @param [String] member the member who's value to get
|
38
|
+
#
|
39
|
+
# @return [Object] whatever is stored on this hash member
|
40
|
+
#
|
41
|
+
def [](member)
|
42
|
+
redis { |conn| conn.hget(key, member) }
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# Returns the count for this hash
|
47
|
+
#
|
48
|
+
#
|
49
|
+
# @return [Integer] the length of this hash
|
50
|
+
#
|
51
|
+
def count
|
52
|
+
redis { |conn| conn.hlen(key) }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Redis
|
5
|
+
#
|
6
|
+
# Class List provides convenient access to redis hashes
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class List < Entity
|
11
|
+
#
|
12
|
+
# Entries in this list
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# @return [Array<Object>] the elements in this list
|
16
|
+
#
|
17
|
+
def entries
|
18
|
+
redis { |conn| conn.lrange(key, 0, -1) }
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# The number of entries in this list
|
23
|
+
#
|
24
|
+
#
|
25
|
+
# @return [Integer] the total number of entries
|
26
|
+
#
|
27
|
+
def count
|
28
|
+
redis { |conn| conn.llen(key) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Redis
|
5
|
+
#
|
6
|
+
# Class Set provides convenient access to redis sets
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class Set < Entity
|
11
|
+
#
|
12
|
+
# Return entries for this set
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# @return [Array<String>]
|
16
|
+
#
|
17
|
+
def entries
|
18
|
+
redis { |conn| conn.smembers(key) }
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Returns the count for this sorted set
|
23
|
+
#
|
24
|
+
#
|
25
|
+
# @return [Integer] the number of entries
|
26
|
+
#
|
27
|
+
def count
|
28
|
+
redis { |conn| conn.scard(key) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Redis
|
5
|
+
#
|
6
|
+
# Class SortedSet provides convenient access to redis sorted sets
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class SortedSet < Entity
|
11
|
+
#
|
12
|
+
# Return entries for this sorted set
|
13
|
+
#
|
14
|
+
# @param [true,false] with_scores true return
|
15
|
+
#
|
16
|
+
# @return [Array<Object>] when given with_scores: false
|
17
|
+
# @return [Hash] when given with_scores: true
|
18
|
+
#
|
19
|
+
def entries(with_scores: true)
|
20
|
+
entrys = redis { |conn| conn.zrange(key, 0, -1, with_scores: with_scores) }
|
21
|
+
return entrys unless with_scores
|
22
|
+
|
23
|
+
entrys.each_with_object({}) { |pair, hash| hash[pair[0]] = pair[1] }
|
24
|
+
end
|
25
|
+
|
26
|
+
#
|
27
|
+
# Adds a value to the sorted set
|
28
|
+
#
|
29
|
+
# @param [Array<Float, String>, String] values the values to add
|
30
|
+
#
|
31
|
+
# @return [Boolean, Integer] <description>
|
32
|
+
#
|
33
|
+
def add(values)
|
34
|
+
redis do |conn|
|
35
|
+
if values.is_a?(Array)
|
36
|
+
conn.zadd(key, values)
|
37
|
+
else
|
38
|
+
conn.zadd(key, now_f, values)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Return the zrak of the member
|
45
|
+
#
|
46
|
+
# @param [String] member the member to pull rank on
|
47
|
+
#
|
48
|
+
# @return [Integer]
|
49
|
+
#
|
50
|
+
def rank(member)
|
51
|
+
redis { |conn| conn.zrank(key, member) }
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# Return score for a member
|
56
|
+
#
|
57
|
+
# @param [String] member the member for which score
|
58
|
+
#
|
59
|
+
# @return [Integer, Float]
|
60
|
+
#
|
61
|
+
def score(member)
|
62
|
+
redis { |conn| conn.zscore(key, member) }
|
63
|
+
end
|
64
|
+
|
65
|
+
#
|
66
|
+
# Clears the sorted set from all entries
|
67
|
+
#
|
68
|
+
#
|
69
|
+
# @return [Integer] number of entries removed
|
70
|
+
#
|
71
|
+
def clear
|
72
|
+
redis { |conn| conn.zremrangebyrank(key, 0, count) }
|
73
|
+
end
|
74
|
+
|
75
|
+
#
|
76
|
+
# Returns the count for this sorted set
|
77
|
+
#
|
78
|
+
#
|
79
|
+
# @return [Integer] the number of entries
|
80
|
+
#
|
81
|
+
def count
|
82
|
+
redis { |conn| conn.zcard(key) }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Redis
|
5
|
+
#
|
6
|
+
# Class String provides convenient access to redis strings
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class String < Entity
|
11
|
+
#
|
12
|
+
# Returns the value of the key
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# @return [String]
|
16
|
+
#
|
17
|
+
def value
|
18
|
+
redis { |conn| conn.get(key) }
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Sets the value of the key to given object
|
23
|
+
#
|
24
|
+
# @param [String] obj the object to update the key with
|
25
|
+
#
|
26
|
+
# @return [true, false]
|
27
|
+
#
|
28
|
+
def set(obj, pipeline = nil)
|
29
|
+
return pipeline.set(key, obj) if pipeline
|
30
|
+
|
31
|
+
redis { |conn| conn.set(key, obj) }
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# Removes the key from redis
|
36
|
+
#
|
37
|
+
def del(*)
|
38
|
+
redis { |conn| conn.del(key) }
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# Used only for compatibility with other keys
|
43
|
+
#
|
44
|
+
# @return [1] when key exists
|
45
|
+
# @return [0] when key does not exists
|
46
|
+
def count
|
47
|
+
exist? ? 1 : 0
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
#
|
5
|
+
# Module Reflectable provides a method to notify subscribers
|
6
|
+
#
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
|
+
#
|
9
|
+
module Reflectable
|
10
|
+
#
|
11
|
+
# Reflects on specific event
|
12
|
+
#
|
13
|
+
# @param [Symbol] reflection the reflected event
|
14
|
+
# @param [Array] args arguments to provide to reflector
|
15
|
+
#
|
16
|
+
# @return [void]
|
17
|
+
#
|
18
|
+
def reflect(reflection, *args)
|
19
|
+
SidekiqUniqueJobs.reflections.dispatch(reflection, *args)
|
20
|
+
nil
|
21
|
+
rescue UniqueJobsError => ex
|
22
|
+
SidekiqUniqueJobs.logger.error(ex)
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|