sidekiq-unique-jobs 6.0.7 → 7.1.33
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1880 -156
- data/README.md +818 -238
- data/bin/uniquejobs +2 -2
- data/lib/sidekiq-unique-jobs.rb +1 -1
- data/lib/sidekiq_unique_jobs/batch_delete.rb +124 -0
- data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
- data/lib/sidekiq_unique_jobs/cli.rb +68 -25
- data/lib/sidekiq_unique_jobs/config.rb +319 -0
- data/lib/sidekiq_unique_jobs/connection.rb +6 -5
- data/lib/sidekiq_unique_jobs/constants.rb +50 -20
- data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
- data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
- data/lib/sidekiq_unique_jobs/digests.rb +70 -87
- data/lib/sidekiq_unique_jobs/exceptions.rb +88 -12
- data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
- data/lib/sidekiq_unique_jobs/job.rb +47 -13
- data/lib/sidekiq_unique_jobs/json.rb +47 -0
- data/lib/sidekiq_unique_jobs/key.rb +98 -0
- data/lib/sidekiq_unique_jobs/lock/base_lock.rb +111 -82
- data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
- data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
- data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +45 -5
- data/lib/sidekiq_unique_jobs/lock/until_executed.rb +30 -4
- data/lib/sidekiq_unique_jobs/lock/until_executing.rb +26 -2
- data/lib/sidekiq_unique_jobs/lock/until_expired.rb +27 -15
- data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +26 -7
- data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -11
- data/lib/sidekiq_unique_jobs/lock.rb +342 -0
- data/lib/sidekiq_unique_jobs/lock_args.rb +127 -0
- data/lib/sidekiq_unique_jobs/lock_config.rb +126 -0
- data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
- data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
- data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
- data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
- data/lib/sidekiq_unique_jobs/lock_type.rb +37 -0
- data/lib/sidekiq_unique_jobs/locksmith.rb +324 -95
- data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
- data/lib/sidekiq_unique_jobs/logging.rb +208 -30
- data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
- data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
- data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
- data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
- data/lib/sidekiq_unique_jobs/lua/lock.lua +99 -0
- data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
- data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
- data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
- data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +122 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
- data/lib/sidekiq_unique_jobs/lua/unlock.lua +107 -0
- data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
- data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
- data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
- data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -0
- data/lib/sidekiq_unique_jobs/middleware.rb +33 -30
- data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
- data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
- data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
- data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +2 -2
- data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +63 -17
- data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
- data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +16 -5
- data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +26 -7
- data/lib/sidekiq_unique_jobs/on_conflict.rb +34 -16
- data/lib/sidekiq_unique_jobs/options_with_fallback.rb +39 -36
- data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
- data/lib/sidekiq_unique_jobs/orphans/manager.rb +241 -0
- data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
- data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
- data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
- data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
- data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +298 -0
- data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
- data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
- data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
- data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
- data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
- data/lib/sidekiq_unique_jobs/redis/string.rb +51 -0
- data/lib/sidekiq_unique_jobs/redis.rb +11 -0
- data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
- data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
- data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
- data/lib/sidekiq_unique_jobs/script.rb +15 -0
- data/lib/sidekiq_unique_jobs/server.rb +61 -0
- data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +115 -66
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +304 -0
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +51 -29
- data/lib/sidekiq_unique_jobs/testing.rb +104 -31
- data/lib/sidekiq_unique_jobs/timer_task.rb +299 -0
- data/lib/sidekiq_unique_jobs/timing.rb +58 -0
- data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
- data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
- data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
- data/lib/sidekiq_unique_jobs/version.rb +3 -1
- data/lib/sidekiq_unique_jobs/version_check.rb +114 -0
- data/lib/sidekiq_unique_jobs/web/helpers.rb +140 -15
- data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
- data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
- data/lib/sidekiq_unique_jobs/web/views/lock.erb +110 -0
- data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
- data/lib/sidekiq_unique_jobs/web.rb +86 -29
- data/lib/sidekiq_unique_jobs.rb +78 -105
- data/lib/tasks/changelog.rake +23 -0
- metadata +154 -218
- data/.codeclimate.yml +0 -35
- data/.csslintrc +0 -2
- data/.dockerignore +0 -4
- data/.editorconfig +0 -14
- data/.eslintignore +0 -1
- data/.eslintrc +0 -213
- data/.fasterer.yml +0 -23
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -31
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -17
- data/.gitignore +0 -28
- data/.reek.yml +0 -87
- data/.rspec +0 -2
- data/.rubocop.yml +0 -127
- data/.simplecov +0 -20
- data/.travis.yml +0 -47
- data/.yardopts +0 -7
- data/Appraisals +0 -29
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -24
- data/Guardfile +0 -55
- data/Rakefile +0 -12
- data/_config.yml +0 -1
- data/assets/unique_digests_1.png +0 -0
- data/assets/unique_digests_2.png +0 -0
- data/bin/bench +0 -20
- data/examples/another_unique_job.rb +0 -15
- data/examples/custom_queue_job.rb +0 -12
- data/examples/custom_queue_job_with_filter_method.rb +0 -13
- data/examples/custom_queue_job_with_filter_proc.rb +0 -16
- data/examples/expiring_job.rb +0 -12
- data/examples/inline_worker.rb +0 -12
- data/examples/just_a_worker.rb +0 -13
- data/examples/long_running_job.rb +0 -14
- data/examples/main_job.rb +0 -14
- data/examples/my_job.rb +0 -12
- data/examples/my_unique_job.rb +0 -15
- data/examples/my_unique_job_with_filter_method.rb +0 -21
- data/examples/my_unique_job_with_filter_proc.rb +0 -19
- data/examples/notify_worker.rb +0 -14
- data/examples/plain_class.rb +0 -13
- data/examples/simple_worker.rb +0 -15
- data/examples/spawn_simple_worker.rb +0 -12
- data/examples/test_class.rb +0 -9
- data/examples/unique_across_workers_job.rb +0 -20
- data/examples/unique_job_on_conflict_raise.rb +0 -14
- data/examples/unique_job_on_conflict_reject.rb +0 -14
- data/examples/unique_job_on_conflict_reschedule.rb +0 -14
- data/examples/unique_job_with_conditional_parameter.rb +0 -18
- data/examples/unique_job_with_filter_method.rb +0 -21
- data/examples/unique_job_with_nil_unique_args.rb +0 -20
- data/examples/unique_job_with_no_unique_args_method.rb +0 -16
- data/examples/unique_job_withthout_unique_args_parameter.rb +0 -18
- data/examples/unique_on_all_queues_job.rb +0 -16
- data/examples/until_and_while_executing_job.rb +0 -17
- data/examples/until_executed_2_job.rb +0 -24
- data/examples/until_executed_job.rb +0 -25
- data/examples/until_executing_job.rb +0 -11
- data/examples/until_expired_job.rb +0 -12
- data/examples/until_global_expired_job.rb +0 -12
- data/examples/while_executing_job.rb +0 -15
- data/examples/while_executing_reject_job.rb +0 -14
- data/examples/without_argument_job.rb +0 -13
- data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
- data/lib/sidekiq_unique_jobs/scripts.rb +0 -89
- data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -40
- data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
- data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
- data/lib/sidekiq_unique_jobs/unique_args.rb +0 -149
- data/lib/sidekiq_unique_jobs/util.rb +0 -103
- data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
- data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -42
- data/redis/acquire_lock.lua +0 -21
- data/redis/delete.lua +0 -14
- data/redis/delete_by_digest.lua +0 -24
- data/redis/delete_job_by_digest.lua +0 -60
- data/redis/lock.lua +0 -58
- data/redis/release_stale_locks.lua +0 -90
- data/redis/unlock.lua +0 -32
- data/sidekiq-unique-jobs.gemspec +0 -42
@@ -6,27 +6,22 @@ module SidekiqUniqueJobs
|
|
6
6
|
# Requires the following methods to be defined in the including class
|
7
7
|
# 1. item (required)
|
8
8
|
# 2. options (can be nil)
|
9
|
-
# 3.
|
10
|
-
# @author Mikael Henriksson <mikael@
|
9
|
+
# 3. job_class (required, can be anything)
|
10
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
11
11
|
module OptionsWithFallback
|
12
|
-
LOCKS = {
|
13
|
-
until_and_while_executing: SidekiqUniqueJobs::Lock::UntilAndWhileExecuting,
|
14
|
-
until_executed: SidekiqUniqueJobs::Lock::UntilExecuted,
|
15
|
-
until_executing: SidekiqUniqueJobs::Lock::UntilExecuting,
|
16
|
-
until_expired: SidekiqUniqueJobs::Lock::UntilExpired,
|
17
|
-
until_timeout: SidekiqUniqueJobs::Lock::UntilExpired,
|
18
|
-
while_executing: SidekiqUniqueJobs::Lock::WhileExecuting,
|
19
|
-
while_executing_reject: SidekiqUniqueJobs::Lock::WhileExecutingReject,
|
20
|
-
}.freeze
|
21
|
-
|
22
12
|
def self.included(base)
|
23
13
|
base.send(:include, SidekiqUniqueJobs::SidekiqWorkerMethods)
|
24
14
|
end
|
25
15
|
|
16
|
+
# A convenience method for using the configured locks
|
17
|
+
def locks
|
18
|
+
SidekiqUniqueJobs.locks
|
19
|
+
end
|
20
|
+
|
26
21
|
# Check if unique has been enabled
|
27
22
|
# @return [true, false] indicate if the gem has been enabled
|
28
23
|
def unique_enabled?
|
29
|
-
SidekiqUniqueJobs.
|
24
|
+
SidekiqUniqueJobs.enabled? && lock_type
|
30
25
|
end
|
31
26
|
|
32
27
|
# Check if unique has been disabled
|
@@ -34,40 +29,48 @@ module SidekiqUniqueJobs
|
|
34
29
|
!unique_enabled?
|
35
30
|
end
|
36
31
|
|
37
|
-
#
|
38
|
-
|
39
|
-
|
32
|
+
#
|
33
|
+
# A new lock for this Sidekiq Job
|
34
|
+
#
|
35
|
+
#
|
36
|
+
# @return [Lock::BaseLock] an instance of a lock implementation
|
37
|
+
#
|
38
|
+
def lock_instance
|
39
|
+
@lock_instance ||= lock_class.new(item, after_unlock_hook, @redis_pool)
|
40
40
|
end
|
41
41
|
|
42
|
-
#
|
43
|
-
#
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
# Check if we should log duplicate payloads
|
49
|
-
# @return [SidekiqUniqueJobs::Lock::BaseLock] an instance of a child class
|
42
|
+
#
|
43
|
+
# Returns the corresponding class for the lock_type
|
44
|
+
#
|
45
|
+
#
|
46
|
+
# @return [Class]
|
47
|
+
#
|
50
48
|
def lock_class
|
51
|
-
@lock_class ||=
|
52
|
-
|
53
|
-
fail UnknownLock, "No implementation for `lock: :#{lock_type}`"
|
54
|
-
end
|
49
|
+
@lock_class ||= locks.fetch(lock_type.to_sym) do
|
50
|
+
raise UnknownLock, "No implementation for `lock: :#{lock_type}`"
|
55
51
|
end
|
56
52
|
end
|
57
53
|
|
58
|
-
#
|
54
|
+
#
|
55
|
+
# The type of lock for this worker
|
56
|
+
#
|
57
|
+
#
|
58
|
+
# @return [Symbol, NilClass]
|
59
|
+
#
|
59
60
|
def lock_type
|
60
|
-
@lock_type ||= options[
|
61
|
-
end
|
62
|
-
|
63
|
-
def unique_type
|
64
|
-
options[UNIQUE_KEY] || item[UNIQUE_KEY]
|
61
|
+
@lock_type ||= options[LOCK] || item[LOCK]
|
65
62
|
end
|
66
63
|
|
64
|
+
#
|
65
|
+
# The default options with any matching keys overridden from worker options
|
66
|
+
#
|
67
|
+
#
|
68
|
+
# @return [Hash<String, Object>]
|
69
|
+
#
|
67
70
|
def options
|
68
71
|
@options ||= begin
|
69
|
-
opts =
|
70
|
-
opts.merge!(
|
72
|
+
opts = default_job_options.dup
|
73
|
+
opts.merge!(job_options) if sidekiq_job_class?
|
71
74
|
(opts || {}).stringify_keys
|
72
75
|
end
|
73
76
|
end
|
@@ -0,0 +1,29 @@
|
|
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
|
+
class LuaReaper < Reaper
|
13
|
+
#
|
14
|
+
# Delete orphaned digests
|
15
|
+
#
|
16
|
+
#
|
17
|
+
# @return [Integer] the number of reaped locks
|
18
|
+
#
|
19
|
+
def call
|
20
|
+
call_script(
|
21
|
+
:reap_orphans,
|
22
|
+
conn,
|
23
|
+
keys: [DIGESTS, EXPIRING_DIGESTS, SCHEDULE, RETRY, PROCESSES],
|
24
|
+
argv: [reaper_count, (Time.now - reaper_timeout).to_f],
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,241 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "concurrent/version"
|
4
|
+
|
5
|
+
module SidekiqUniqueJobs
|
6
|
+
module Orphans
|
7
|
+
#
|
8
|
+
# Manages the orphan reaper
|
9
|
+
#
|
10
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
11
|
+
#
|
12
|
+
module Manager
|
13
|
+
module_function
|
14
|
+
|
15
|
+
#
|
16
|
+
# @return [Float] the amount to add to the reaper interval
|
17
|
+
DRIFT_FACTOR = 0.02
|
18
|
+
#
|
19
|
+
# @return [Symbol] allowed reapers (:ruby or :lua)
|
20
|
+
REAPERS = [:ruby, :lua].freeze
|
21
|
+
|
22
|
+
# includes "SidekiqUniqueJobs::Connection"
|
23
|
+
# @!parse include SidekiqUniqueJobs::Connection
|
24
|
+
include SidekiqUniqueJobs::Connection
|
25
|
+
# includes "SidekiqUniqueJobs::Logging"
|
26
|
+
# @!parse include SidekiqUniqueJobs::Logging
|
27
|
+
include SidekiqUniqueJobs::Logging
|
28
|
+
|
29
|
+
#
|
30
|
+
# Starts a separate thread that periodically reaps orphans
|
31
|
+
#
|
32
|
+
#
|
33
|
+
# @return [SidekiqUniqueJobs::TimerTask] the task that was started
|
34
|
+
#
|
35
|
+
def start(test_task = nil) # rubocop:disable
|
36
|
+
return if disabled?
|
37
|
+
return if registered?
|
38
|
+
|
39
|
+
self.task = test_task || default_task
|
40
|
+
|
41
|
+
with_logging_context do
|
42
|
+
register_reaper_process
|
43
|
+
log_info("Starting Reaper")
|
44
|
+
|
45
|
+
task.add_observer(Observer.new)
|
46
|
+
task.execute
|
47
|
+
task
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Stops the thread that reaps orphans
|
53
|
+
#
|
54
|
+
#
|
55
|
+
# @return [Boolean]
|
56
|
+
#
|
57
|
+
def stop
|
58
|
+
return if disabled?
|
59
|
+
return if unregistered?
|
60
|
+
|
61
|
+
with_logging_context do
|
62
|
+
log_info("Stopping Reaper")
|
63
|
+
unregister_reaper_process
|
64
|
+
task.shutdown
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
#
|
69
|
+
# The task that runs the reaper
|
70
|
+
#
|
71
|
+
#
|
72
|
+
# @return [<type>] <description>
|
73
|
+
#
|
74
|
+
def task
|
75
|
+
@task ||= default_task # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# A properly configured timer task
|
80
|
+
#
|
81
|
+
#
|
82
|
+
# @return [SidekiqUniqueJobs::TimerTask]
|
83
|
+
#
|
84
|
+
def default_task
|
85
|
+
SidekiqUniqueJobs::TimerTask.new(timer_task_options) do
|
86
|
+
with_logging_context do
|
87
|
+
redis do |conn|
|
88
|
+
refresh_reaper_mutex
|
89
|
+
Orphans::Reaper.call(conn)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# Store a task to use for scheduled execution
|
97
|
+
#
|
98
|
+
# @param [SidekiqUniqueJobs::TimerTask] task the task to use
|
99
|
+
#
|
100
|
+
# @return [void]
|
101
|
+
#
|
102
|
+
def task=(task)
|
103
|
+
@task = task # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
|
104
|
+
end
|
105
|
+
|
106
|
+
#
|
107
|
+
# Arguments passed on to the timer task
|
108
|
+
#
|
109
|
+
#
|
110
|
+
# @return [Hash]
|
111
|
+
#
|
112
|
+
def timer_task_options
|
113
|
+
{ run_now: true, execution_interval: reaper_interval }
|
114
|
+
end
|
115
|
+
|
116
|
+
#
|
117
|
+
# @see SidekiqUniqueJobs::Config#reaper
|
118
|
+
#
|
119
|
+
def reaper
|
120
|
+
SidekiqUniqueJobs.config.reaper
|
121
|
+
end
|
122
|
+
|
123
|
+
#
|
124
|
+
# @see SidekiqUniqueJobs::Config#reaper_interval
|
125
|
+
#
|
126
|
+
def reaper_interval
|
127
|
+
SidekiqUniqueJobs.config.reaper_interval
|
128
|
+
end
|
129
|
+
|
130
|
+
#
|
131
|
+
# A context to use for all log entries
|
132
|
+
#
|
133
|
+
#
|
134
|
+
# @return [Hash] when logger responds to `:with_context`
|
135
|
+
# @return [String] when logger does not responds to `:with_context`
|
136
|
+
#
|
137
|
+
def logging_context
|
138
|
+
if logger_context_hash?
|
139
|
+
{ "uniquejobs" => "reaper" }
|
140
|
+
else
|
141
|
+
"uniquejobs=orphan-reaper"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
#
|
146
|
+
# Checks if a reaper is registered
|
147
|
+
#
|
148
|
+
#
|
149
|
+
# @return [true, false]
|
150
|
+
#
|
151
|
+
def registered?
|
152
|
+
redis do |conn|
|
153
|
+
conn.get(UNIQUE_REAPER).to_i + drift_reaper_interval > current_timestamp
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
#
|
158
|
+
# Checks if that reapers are not registerd
|
159
|
+
#
|
160
|
+
# @see registered?
|
161
|
+
#
|
162
|
+
# @return [true, false]
|
163
|
+
#
|
164
|
+
def unregistered?
|
165
|
+
!registered?
|
166
|
+
end
|
167
|
+
|
168
|
+
#
|
169
|
+
# Checks if reaping is disabled
|
170
|
+
#
|
171
|
+
# @see enabled?
|
172
|
+
#
|
173
|
+
# @return [true, false]
|
174
|
+
#
|
175
|
+
def disabled?
|
176
|
+
!enabled?
|
177
|
+
end
|
178
|
+
|
179
|
+
#
|
180
|
+
# Checks if reaping is enabled
|
181
|
+
#
|
182
|
+
# @return [true, false]
|
183
|
+
#
|
184
|
+
def enabled?
|
185
|
+
REAPERS.include?(reaper)
|
186
|
+
end
|
187
|
+
|
188
|
+
#
|
189
|
+
# Writes a mutex key to redis
|
190
|
+
#
|
191
|
+
#
|
192
|
+
# @return [void]
|
193
|
+
#
|
194
|
+
def register_reaper_process
|
195
|
+
redis { |conn| conn.set(UNIQUE_REAPER, current_timestamp, nx: true, ex: drift_reaper_interval) }
|
196
|
+
end
|
197
|
+
|
198
|
+
#
|
199
|
+
# Updates mutex key
|
200
|
+
#
|
201
|
+
#
|
202
|
+
# @return [void]
|
203
|
+
#
|
204
|
+
def refresh_reaper_mutex
|
205
|
+
redis { |conn| conn.set(UNIQUE_REAPER, current_timestamp, ex: drift_reaper_interval) }
|
206
|
+
end
|
207
|
+
|
208
|
+
#
|
209
|
+
# Removes mutex key from redis
|
210
|
+
#
|
211
|
+
#
|
212
|
+
# @return [void]
|
213
|
+
#
|
214
|
+
def unregister_reaper_process
|
215
|
+
redis { |conn| conn.del(UNIQUE_REAPER) }
|
216
|
+
end
|
217
|
+
|
218
|
+
#
|
219
|
+
# Reaper interval with a little drift
|
220
|
+
# Redis isn't exact enough so to give a little bufffer,
|
221
|
+
# we add a tiny value to the reaper interval.
|
222
|
+
#
|
223
|
+
#
|
224
|
+
# @return [Integer] <description>
|
225
|
+
#
|
226
|
+
def drift_reaper_interval
|
227
|
+
reaper_interval + (reaper_interval * DRIFT_FACTOR).to_i
|
228
|
+
end
|
229
|
+
|
230
|
+
#
|
231
|
+
# Current time (as integer value)
|
232
|
+
#
|
233
|
+
#
|
234
|
+
# @return [Integer]
|
235
|
+
#
|
236
|
+
def current_timestamp
|
237
|
+
Time.now.to_i
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
@@ -0,0 +1,24 @@
|
|
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
|
+
class NullReaper < Reaper
|
13
|
+
#
|
14
|
+
# Delete orphaned digests
|
15
|
+
#
|
16
|
+
#
|
17
|
+
# @return [Integer] the number of reaped locks
|
18
|
+
#
|
19
|
+
def call
|
20
|
+
# NO OP
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
#
|
5
|
+
# Class DeleteOrphans provides deletion of orphaned digests
|
6
|
+
#
|
7
|
+
# @note this is a much slower version of the lua script but does not crash redis
|
8
|
+
#
|
9
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
10
|
+
#
|
11
|
+
module Orphans
|
12
|
+
#
|
13
|
+
# Observes the Orphan::Manager and provides information about each execution
|
14
|
+
#
|
15
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
16
|
+
#
|
17
|
+
class Observer
|
18
|
+
include SidekiqUniqueJobs::Logging
|
19
|
+
|
20
|
+
#
|
21
|
+
# Runs every time the {Manager} executes the TimerTask
|
22
|
+
# used for logging information about the reaping
|
23
|
+
#
|
24
|
+
# @param [Time] time the time of the execution
|
25
|
+
# @param [Object] result the result of the execution
|
26
|
+
# @param [Exception] ex any error raised from the TimerTask
|
27
|
+
#
|
28
|
+
# @return [<type>] <description>
|
29
|
+
#
|
30
|
+
def update(time, result, ex)
|
31
|
+
if result
|
32
|
+
log_info("(#{time}) Execution successfully returned #{result}")
|
33
|
+
elsif ex.is_a?(Concurrent::TimeoutError)
|
34
|
+
log_warn("(#{time}) Execution timed out")
|
35
|
+
else
|
36
|
+
log_info("(#{time}) Cleanup failed with error #{ex.message}")
|
37
|
+
log_error(ex)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,114 @@
|
|
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
|
+
class Reaper
|
13
|
+
include SidekiqUniqueJobs::Connection
|
14
|
+
include SidekiqUniqueJobs::Script::Caller
|
15
|
+
include SidekiqUniqueJobs::Logging
|
16
|
+
include SidekiqUniqueJobs::JSON
|
17
|
+
|
18
|
+
require_relative "lua_reaper"
|
19
|
+
require_relative "ruby_reaper"
|
20
|
+
require_relative "null_reaper"
|
21
|
+
|
22
|
+
#
|
23
|
+
# @return [Hash<Symbol, SidekiqUniqueJobs::Orphans::Reaper] the current implementation of reapers
|
24
|
+
REAPERS = {
|
25
|
+
lua: SidekiqUniqueJobs::Orphans::LuaReaper,
|
26
|
+
ruby: SidekiqUniqueJobs::Orphans::RubyReaper,
|
27
|
+
none: SidekiqUniqueJobs::Orphans::NullReaper,
|
28
|
+
nil => SidekiqUniqueJobs::Orphans::NullReaper,
|
29
|
+
false => SidekiqUniqueJobs::Orphans::NullReaper,
|
30
|
+
}.freeze
|
31
|
+
|
32
|
+
#
|
33
|
+
# Execute deletion of orphaned digests
|
34
|
+
#
|
35
|
+
# @param [Redis] conn nil a connection to redis
|
36
|
+
#
|
37
|
+
# @return [void]
|
38
|
+
#
|
39
|
+
def self.call(conn = nil)
|
40
|
+
return new(conn).call if conn
|
41
|
+
|
42
|
+
redis { |rcon| new(rcon).call }
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# @!attribute [r] conn
|
47
|
+
# @return [Redis] a redis connection
|
48
|
+
attr_reader :conn
|
49
|
+
|
50
|
+
#
|
51
|
+
# Initialize a new instance of DeleteOrphans
|
52
|
+
#
|
53
|
+
# @param [Redis] conn a connection to redis
|
54
|
+
#
|
55
|
+
def initialize(conn)
|
56
|
+
@conn = conn
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# Convenient access to the global configuration
|
61
|
+
#
|
62
|
+
#
|
63
|
+
# @return [SidekiqUniqueJobs::Config]
|
64
|
+
#
|
65
|
+
def config
|
66
|
+
SidekiqUniqueJobs.config
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# The reaper that was configured
|
71
|
+
#
|
72
|
+
#
|
73
|
+
# @return [Symbol]
|
74
|
+
#
|
75
|
+
def reaper
|
76
|
+
config.reaper
|
77
|
+
end
|
78
|
+
|
79
|
+
#
|
80
|
+
# The configured timeout for the reaper
|
81
|
+
#
|
82
|
+
#
|
83
|
+
# @return [Integer] timeout in seconds
|
84
|
+
#
|
85
|
+
def reaper_timeout
|
86
|
+
config.reaper_timeout
|
87
|
+
end
|
88
|
+
|
89
|
+
#
|
90
|
+
# The number of locks to reap at a time
|
91
|
+
#
|
92
|
+
#
|
93
|
+
# @return [Integer]
|
94
|
+
#
|
95
|
+
def reaper_count
|
96
|
+
config.reaper_count
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# Delete orphaned digests
|
101
|
+
#
|
102
|
+
#
|
103
|
+
# @return [Integer] the number of reaped locks
|
104
|
+
#
|
105
|
+
def call
|
106
|
+
if (implementation = REAPERS[reaper])
|
107
|
+
implementation.new(conn).call
|
108
|
+
else
|
109
|
+
log_fatal(":#{reaper} is invalid for `SidekiqUnqiueJobs.config.reaper`")
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|