sidekiq-unique-jobs 6.0.7 → 7.1.33
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1880 -156
- data/README.md +818 -238
- data/bin/uniquejobs +2 -2
- data/lib/sidekiq-unique-jobs.rb +1 -1
- data/lib/sidekiq_unique_jobs/batch_delete.rb +124 -0
- data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
- data/lib/sidekiq_unique_jobs/cli.rb +68 -25
- data/lib/sidekiq_unique_jobs/config.rb +319 -0
- data/lib/sidekiq_unique_jobs/connection.rb +6 -5
- data/lib/sidekiq_unique_jobs/constants.rb +50 -20
- data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
- data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
- data/lib/sidekiq_unique_jobs/digests.rb +70 -87
- data/lib/sidekiq_unique_jobs/exceptions.rb +88 -12
- data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
- data/lib/sidekiq_unique_jobs/job.rb +47 -13
- data/lib/sidekiq_unique_jobs/json.rb +47 -0
- data/lib/sidekiq_unique_jobs/key.rb +98 -0
- data/lib/sidekiq_unique_jobs/lock/base_lock.rb +111 -82
- data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
- data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
- data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +45 -5
- data/lib/sidekiq_unique_jobs/lock/until_executed.rb +30 -4
- data/lib/sidekiq_unique_jobs/lock/until_executing.rb +26 -2
- data/lib/sidekiq_unique_jobs/lock/until_expired.rb +27 -15
- data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +26 -7
- data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -11
- data/lib/sidekiq_unique_jobs/lock.rb +342 -0
- data/lib/sidekiq_unique_jobs/lock_args.rb +127 -0
- data/lib/sidekiq_unique_jobs/lock_config.rb +126 -0
- data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
- data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
- data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
- data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
- data/lib/sidekiq_unique_jobs/lock_type.rb +37 -0
- data/lib/sidekiq_unique_jobs/locksmith.rb +324 -95
- data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
- data/lib/sidekiq_unique_jobs/logging.rb +208 -30
- data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
- data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
- data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
- data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
- data/lib/sidekiq_unique_jobs/lua/lock.lua +99 -0
- data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
- data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
- data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
- data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +122 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
- data/lib/sidekiq_unique_jobs/lua/unlock.lua +107 -0
- data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
- data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
- data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
- data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -0
- data/lib/sidekiq_unique_jobs/middleware.rb +33 -30
- data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
- data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
- data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
- data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +2 -2
- data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +63 -17
- data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
- data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +16 -5
- data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +26 -7
- data/lib/sidekiq_unique_jobs/on_conflict.rb +34 -16
- data/lib/sidekiq_unique_jobs/options_with_fallback.rb +39 -36
- data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
- data/lib/sidekiq_unique_jobs/orphans/manager.rb +241 -0
- data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
- data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
- data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
- data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
- data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +298 -0
- data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
- data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
- data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
- data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
- data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
- data/lib/sidekiq_unique_jobs/redis/string.rb +51 -0
- data/lib/sidekiq_unique_jobs/redis.rb +11 -0
- data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
- data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
- data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
- data/lib/sidekiq_unique_jobs/script.rb +15 -0
- data/lib/sidekiq_unique_jobs/server.rb +61 -0
- data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +115 -66
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +304 -0
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +51 -29
- data/lib/sidekiq_unique_jobs/testing.rb +104 -31
- data/lib/sidekiq_unique_jobs/timer_task.rb +299 -0
- data/lib/sidekiq_unique_jobs/timing.rb +58 -0
- data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
- data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
- data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
- data/lib/sidekiq_unique_jobs/version.rb +3 -1
- data/lib/sidekiq_unique_jobs/version_check.rb +114 -0
- data/lib/sidekiq_unique_jobs/web/helpers.rb +140 -15
- data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
- data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
- data/lib/sidekiq_unique_jobs/web/views/lock.erb +110 -0
- data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
- data/lib/sidekiq_unique_jobs/web.rb +86 -29
- data/lib/sidekiq_unique_jobs.rb +78 -105
- data/lib/tasks/changelog.rake +23 -0
- metadata +154 -218
- data/.codeclimate.yml +0 -35
- data/.csslintrc +0 -2
- data/.dockerignore +0 -4
- data/.editorconfig +0 -14
- data/.eslintignore +0 -1
- data/.eslintrc +0 -213
- data/.fasterer.yml +0 -23
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -31
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -17
- data/.gitignore +0 -28
- data/.reek.yml +0 -87
- data/.rspec +0 -2
- data/.rubocop.yml +0 -127
- data/.simplecov +0 -20
- data/.travis.yml +0 -47
- data/.yardopts +0 -7
- data/Appraisals +0 -29
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -24
- data/Guardfile +0 -55
- data/Rakefile +0 -12
- data/_config.yml +0 -1
- data/assets/unique_digests_1.png +0 -0
- data/assets/unique_digests_2.png +0 -0
- data/bin/bench +0 -20
- data/examples/another_unique_job.rb +0 -15
- data/examples/custom_queue_job.rb +0 -12
- data/examples/custom_queue_job_with_filter_method.rb +0 -13
- data/examples/custom_queue_job_with_filter_proc.rb +0 -16
- data/examples/expiring_job.rb +0 -12
- data/examples/inline_worker.rb +0 -12
- data/examples/just_a_worker.rb +0 -13
- data/examples/long_running_job.rb +0 -14
- data/examples/main_job.rb +0 -14
- data/examples/my_job.rb +0 -12
- data/examples/my_unique_job.rb +0 -15
- data/examples/my_unique_job_with_filter_method.rb +0 -21
- data/examples/my_unique_job_with_filter_proc.rb +0 -19
- data/examples/notify_worker.rb +0 -14
- data/examples/plain_class.rb +0 -13
- data/examples/simple_worker.rb +0 -15
- data/examples/spawn_simple_worker.rb +0 -12
- data/examples/test_class.rb +0 -9
- data/examples/unique_across_workers_job.rb +0 -20
- data/examples/unique_job_on_conflict_raise.rb +0 -14
- data/examples/unique_job_on_conflict_reject.rb +0 -14
- data/examples/unique_job_on_conflict_reschedule.rb +0 -14
- data/examples/unique_job_with_conditional_parameter.rb +0 -18
- data/examples/unique_job_with_filter_method.rb +0 -21
- data/examples/unique_job_with_nil_unique_args.rb +0 -20
- data/examples/unique_job_with_no_unique_args_method.rb +0 -16
- data/examples/unique_job_withthout_unique_args_parameter.rb +0 -18
- data/examples/unique_on_all_queues_job.rb +0 -16
- data/examples/until_and_while_executing_job.rb +0 -17
- data/examples/until_executed_2_job.rb +0 -24
- data/examples/until_executed_job.rb +0 -25
- data/examples/until_executing_job.rb +0 -11
- data/examples/until_expired_job.rb +0 -12
- data/examples/until_global_expired_job.rb +0 -12
- data/examples/while_executing_job.rb +0 -15
- data/examples/while_executing_reject_job.rb +0 -14
- data/examples/without_argument_job.rb +0 -13
- data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
- data/lib/sidekiq_unique_jobs/scripts.rb +0 -89
- data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -40
- data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
- data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
- data/lib/sidekiq_unique_jobs/unique_args.rb +0 -149
- data/lib/sidekiq_unique_jobs/util.rb +0 -103
- data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
- data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -42
- data/redis/acquire_lock.lua +0 -21
- data/redis/delete.lua +0 -14
- data/redis/delete_by_digest.lua +0 -24
- data/redis/delete_job_by_digest.lua +0 -60
- data/redis/lock.lua +0 -58
- data/redis/release_stale_locks.lua +0 -90
- data/redis/unlock.lua +0 -32
- data/sidekiq-unique-jobs.gemspec +0 -42
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
#
|
5
|
+
# Gathers all configuration for a lock
|
6
|
+
# which helps reduce the amount of instance variables
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
#
|
10
|
+
class LockConfig
|
11
|
+
#
|
12
|
+
# @!attribute [r] type
|
13
|
+
# @return [Symbol] the type of lock
|
14
|
+
attr_reader :type
|
15
|
+
#
|
16
|
+
# @!attribute [r] job
|
17
|
+
# @return [Symbol] the job class
|
18
|
+
attr_reader :job
|
19
|
+
#
|
20
|
+
# @!attribute [r] limit
|
21
|
+
# @return [Integer] the number of simultaneous locks
|
22
|
+
attr_reader :limit
|
23
|
+
#
|
24
|
+
# @!attribute [r] timeout
|
25
|
+
# @return [Integer, nil] the time to wait for a lock
|
26
|
+
attr_reader :timeout
|
27
|
+
#
|
28
|
+
# @!attribute [r] ttl
|
29
|
+
# @return [Integer, nil] the time (in seconds) to live after successful
|
30
|
+
attr_reader :ttl
|
31
|
+
#
|
32
|
+
# @!attribute [r] ttl
|
33
|
+
# @return [Integer, nil] the time (in milliseconds) to live after successful
|
34
|
+
attr_reader :pttl
|
35
|
+
#
|
36
|
+
# @!attribute [r] lock_info
|
37
|
+
# @return [Boolean] indicate wether to use lock_info or not
|
38
|
+
attr_reader :lock_info
|
39
|
+
#
|
40
|
+
# @!attribute [r] on_conflict
|
41
|
+
# @return [Symbol, Hash<Symbol, Symbol>] the strategies to use as conflict resolution
|
42
|
+
attr_reader :on_conflict
|
43
|
+
#
|
44
|
+
# @!attribute [r] errors
|
45
|
+
# @return [Array<Hash<Symbol, Array<String>] a collection of configuration errors
|
46
|
+
attr_reader :errors
|
47
|
+
|
48
|
+
#
|
49
|
+
# Instantiate a new lock_config based on sidekiq options in worker
|
50
|
+
#
|
51
|
+
# @param [Hash] options sidekiq_options for worker
|
52
|
+
#
|
53
|
+
# @return [LockConfig]
|
54
|
+
#
|
55
|
+
def self.from_worker(options)
|
56
|
+
new(options.deep_stringify_keys)
|
57
|
+
end
|
58
|
+
|
59
|
+
def initialize(job_hash = {})
|
60
|
+
@type = job_hash[LOCK]&.to_sym
|
61
|
+
@job = SidekiqUniqueJobs.safe_constantize(job_hash[CLASS])
|
62
|
+
@limit = job_hash.fetch(LOCK_LIMIT, 1)&.to_i
|
63
|
+
@timeout = job_hash.fetch(LOCK_TIMEOUT, 0)&.to_i
|
64
|
+
@ttl = job_hash.fetch(LOCK_TTL) { job_hash.fetch(LOCK_EXPIRATION, nil) }.to_i
|
65
|
+
@pttl = ttl * 1_000
|
66
|
+
@lock_info = job_hash.fetch(LOCK_INFO) { SidekiqUniqueJobs.config.lock_info }
|
67
|
+
@on_conflict = job_hash.fetch(ON_CONFLICT, nil)
|
68
|
+
@errors = job_hash.fetch(ERRORS) { {} }
|
69
|
+
|
70
|
+
@on_client_conflict = job_hash[ON_CLIENT_CONFLICT]
|
71
|
+
@on_server_conflict = job_hash[ON_SERVER_CONFLICT]
|
72
|
+
end
|
73
|
+
|
74
|
+
def lock_info?
|
75
|
+
lock_info
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# Indicate if timeout was set
|
80
|
+
#
|
81
|
+
#
|
82
|
+
# @return [true,false]
|
83
|
+
#
|
84
|
+
def wait_for_lock?
|
85
|
+
timeout.nil? || timeout.positive?
|
86
|
+
end
|
87
|
+
|
88
|
+
#
|
89
|
+
# Is the configuration valid?
|
90
|
+
#
|
91
|
+
#
|
92
|
+
# @return [true,false]
|
93
|
+
#
|
94
|
+
def valid?
|
95
|
+
errors.empty?
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
# Return a nice descriptive message with all validation errors
|
100
|
+
#
|
101
|
+
#
|
102
|
+
# @return [String]
|
103
|
+
#
|
104
|
+
def errors_as_string
|
105
|
+
return if valid?
|
106
|
+
|
107
|
+
@errors_as_string ||= begin
|
108
|
+
error_msg = +"\t"
|
109
|
+
error_msg << errors.map { |key, val| "#{key}: :#{val}" }.join("\n\t")
|
110
|
+
error_msg
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# the strategy to use as conflict resolution from sidekiq client
|
115
|
+
def on_client_conflict
|
116
|
+
@on_client_conflict ||= on_conflict["client"] || on_conflict[:client] if on_conflict.is_a?(Hash)
|
117
|
+
@on_client_conflict ||= on_conflict
|
118
|
+
end
|
119
|
+
|
120
|
+
# the strategy to use as conflict resolution from sidekiq server
|
121
|
+
def on_server_conflict
|
122
|
+
@on_server_conflict ||= on_conflict["server"] || on_conflict[:server] if on_conflict.is_a?(Hash)
|
123
|
+
@on_server_conflict ||= on_conflict
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "openssl"
|
4
|
+
|
5
|
+
module SidekiqUniqueJobs
|
6
|
+
# Handles uniqueness of sidekiq arguments
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
class LockDigest
|
10
|
+
include SidekiqUniqueJobs::Logging
|
11
|
+
include SidekiqUniqueJobs::JSON
|
12
|
+
include SidekiqUniqueJobs::SidekiqWorkerMethods
|
13
|
+
|
14
|
+
#
|
15
|
+
# Generates a new digest
|
16
|
+
#
|
17
|
+
# @param [Hash] item a sidekiq job hash
|
18
|
+
#
|
19
|
+
# @return [String] a unique digest for the given arguments
|
20
|
+
#
|
21
|
+
def self.call(item)
|
22
|
+
new(item).lock_digest
|
23
|
+
end
|
24
|
+
|
25
|
+
# The sidekiq job hash
|
26
|
+
# @return [Hash] the Sidekiq job hash
|
27
|
+
attr_reader :item
|
28
|
+
#
|
29
|
+
# @!attribute [r] args
|
30
|
+
# @return [Array<Objet>] the arguments passed to `perform_async`
|
31
|
+
attr_reader :lock_args
|
32
|
+
#
|
33
|
+
# @!attribute [r] args
|
34
|
+
# @return [String] the prefix for the unique key
|
35
|
+
attr_reader :lock_prefix
|
36
|
+
|
37
|
+
# @param [Hash] item a Sidekiq job hash
|
38
|
+
def initialize(item)
|
39
|
+
@item = item
|
40
|
+
@lock_args = item[LOCK_ARGS] || item[UNIQUE_ARGS] # TODO: Deprecate UNIQUE_ARGS
|
41
|
+
@lock_prefix = item[LOCK_PREFIX] || item[UNIQUE_PREFIX] # TODO: Deprecate UNIQUE_PREFIX
|
42
|
+
self.job_class = item[CLASS]
|
43
|
+
end
|
44
|
+
|
45
|
+
# Memoized lock_digest
|
46
|
+
# @return [String] a unique digest
|
47
|
+
def lock_digest
|
48
|
+
@lock_digest ||= create_digest
|
49
|
+
end
|
50
|
+
|
51
|
+
# Creates a namespaced unique digest based on the {#digestable_hash} and the {#lock_prefix}
|
52
|
+
# @return [String] a unique digest
|
53
|
+
def create_digest
|
54
|
+
digest = OpenSSL::Digest::MD5.hexdigest(dump_json(digestable_hash.sort))
|
55
|
+
"#{lock_prefix}:#{digest}"
|
56
|
+
end
|
57
|
+
|
58
|
+
# Filter a hash to use for digest
|
59
|
+
# @return [Hash] to use for digest
|
60
|
+
def digestable_hash
|
61
|
+
@item.slice(CLASS, QUEUE, LOCK_ARGS, APARTMENT).tap do |hash|
|
62
|
+
hash.delete(QUEUE) if unique_across_queues?
|
63
|
+
hash.delete(CLASS) if unique_across_workers?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Checks if we should disregard the queue when creating the unique digest
|
68
|
+
# @return [true, false]
|
69
|
+
def unique_across_queues?
|
70
|
+
item[UNIQUE_ACROSS_QUEUES] || job_options[UNIQUE_ACROSS_QUEUES]
|
71
|
+
end
|
72
|
+
|
73
|
+
# Checks if we should disregard the worker when creating the unique digest
|
74
|
+
# @return [true, false]
|
75
|
+
def unique_across_workers?
|
76
|
+
item[UNIQUE_ACROSS_WORKERS] || job_options[UNIQUE_ACROSS_WORKERS]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
#
|
5
|
+
# Class Info provides information about a lock
|
6
|
+
#
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
|
+
#
|
9
|
+
class LockInfo < Redis::String
|
10
|
+
#
|
11
|
+
# Returns the value for this key as a hash
|
12
|
+
#
|
13
|
+
#
|
14
|
+
# @return [Hash]
|
15
|
+
#
|
16
|
+
def value
|
17
|
+
@value ||= load_json(super)
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Check if this redis string is blank
|
22
|
+
#
|
23
|
+
#
|
24
|
+
# @return [Boolean]
|
25
|
+
#
|
26
|
+
def none?
|
27
|
+
value.nil? || value.empty?
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Check if this redis string has a value
|
32
|
+
#
|
33
|
+
#
|
34
|
+
# @return [Boolean]
|
35
|
+
#
|
36
|
+
def present?
|
37
|
+
!none?
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Quick access to the hash members for the value
|
42
|
+
#
|
43
|
+
# @param [String, Symbol] key the key who's value to retrieve
|
44
|
+
#
|
45
|
+
# @return [Object]
|
46
|
+
#
|
47
|
+
def [](key)
|
48
|
+
value[key.to_s] if value.is_a?(Hash)
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Writes the lock info to redis
|
53
|
+
#
|
54
|
+
# @param [Hash] obj the information to store at key
|
55
|
+
#
|
56
|
+
# @return [Hash]
|
57
|
+
#
|
58
|
+
def set(obj, pipeline = nil)
|
59
|
+
return unless SidekiqUniqueJobs.config.lock_info
|
60
|
+
raise InvalidArgument, "argument `obj` (#{obj}) needs to be a hash" unless obj.is_a?(Hash)
|
61
|
+
|
62
|
+
json = dump_json(obj)
|
63
|
+
@value = load_json(json)
|
64
|
+
super(json, pipeline)
|
65
|
+
value
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
# Calculates timeout and expiration
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
7
|
+
class LockTimeout
|
8
|
+
# includes "SidekiqUniqueJobs::SidekiqWorkerMethods"
|
9
|
+
# @!parse include SidekiqUniqueJobs::SidekiqWorkerMethods
|
10
|
+
include SidekiqUniqueJobs::SidekiqWorkerMethods
|
11
|
+
|
12
|
+
#
|
13
|
+
# Calculates the timeout for a Sidekiq job
|
14
|
+
#
|
15
|
+
# @param [Hash] item sidekiq job hash
|
16
|
+
#
|
17
|
+
# @return [Integer] timeout in seconds
|
18
|
+
#
|
19
|
+
def self.calculate(item)
|
20
|
+
new(item).calculate
|
21
|
+
end
|
22
|
+
|
23
|
+
# @!attribute [r] item
|
24
|
+
# @return [Hash] the Sidekiq job hash
|
25
|
+
attr_reader :item
|
26
|
+
|
27
|
+
# @param [Hash] item the Sidekiq job hash
|
28
|
+
# @option item [Integer, nil] :lock_ttl the configured lock expiration
|
29
|
+
# @option item [Integer, nil] :lock_timeout the configured lock timeout
|
30
|
+
# @option item [String] :class the class of the sidekiq worker
|
31
|
+
# @option item [Float] :at the unix time the job is scheduled at
|
32
|
+
def initialize(item)
|
33
|
+
@item = item
|
34
|
+
self.job_class = item[CLASS]
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# Finds a lock timeout in either of
|
39
|
+
# default worker options, {default_lock_timeout} or provided worker_options
|
40
|
+
#
|
41
|
+
#
|
42
|
+
# @return [Integer, nil]
|
43
|
+
#
|
44
|
+
def calculate
|
45
|
+
timeout = default_job_options[LOCK_TIMEOUT]
|
46
|
+
timeout = default_lock_timeout if default_lock_timeout
|
47
|
+
timeout = job_options[LOCK_TIMEOUT] if job_options.key?(LOCK_TIMEOUT)
|
48
|
+
timeout
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# The configured default_lock_timeout
|
53
|
+
# @see SidekiqUniqueJobs::Config#lock_timeout
|
54
|
+
#
|
55
|
+
#
|
56
|
+
# @return [Integer, nil]
|
57
|
+
#
|
58
|
+
def default_lock_timeout
|
59
|
+
SidekiqUniqueJobs.config.lock_timeout
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
# Calculates timeout and expiration
|
5
|
+
#
|
6
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
7
|
+
class LockTTL
|
8
|
+
# includes "SidekiqUniqueJobs::SidekiqWorkerMethods"
|
9
|
+
# @!parse include SidekiqUniqueJobs::SidekiqWorkerMethods
|
10
|
+
include SidekiqUniqueJobs::SidekiqWorkerMethods
|
11
|
+
|
12
|
+
#
|
13
|
+
# Computes lock ttl from job arguments, sidekiq_options.
|
14
|
+
# Falls back to {SidekiqUniqueJobs::Config#lock_ttl}
|
15
|
+
#
|
16
|
+
# @note this method takes into consideration the time
|
17
|
+
# until a job is scheduled
|
18
|
+
#
|
19
|
+
#
|
20
|
+
# @return [Integer] the number of seconds to live
|
21
|
+
#
|
22
|
+
def self.calculate(item)
|
23
|
+
new(item).calculate
|
24
|
+
end
|
25
|
+
|
26
|
+
# @!attribute [r] item
|
27
|
+
# @return [Hash] the Sidekiq job hash
|
28
|
+
attr_reader :item
|
29
|
+
|
30
|
+
# @param [Hash] item the Sidekiq job hash
|
31
|
+
# @option item [Integer, nil] :lock_ttl the configured lock expiration
|
32
|
+
# @option item [Integer, nil] :lock_timeout the configured lock timeout
|
33
|
+
# @option item [String] :class the class of the sidekiq worker
|
34
|
+
# @option item [Float] :at the unix time the job is scheduled at
|
35
|
+
def initialize(item)
|
36
|
+
@item = item
|
37
|
+
self.job_class = item[CLASS]
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Calculates the time until the job is scheduled starting from now
|
42
|
+
#
|
43
|
+
#
|
44
|
+
# @return [Integer] the number of seconds until job is scheduled
|
45
|
+
#
|
46
|
+
def time_until_scheduled
|
47
|
+
return 0 unless scheduled_at
|
48
|
+
|
49
|
+
scheduled_at.to_i - Time.now.utc.to_i
|
50
|
+
end
|
51
|
+
|
52
|
+
# The time a job is scheduled
|
53
|
+
# @return [Float] the exact unix time the job is scheduled at
|
54
|
+
def scheduled_at
|
55
|
+
@scheduled_at ||= item[AT]
|
56
|
+
end
|
57
|
+
|
58
|
+
#
|
59
|
+
# Computes lock ttl from job arguments, sidekiq_options.
|
60
|
+
# Falls back to {SidekiqUniqueJobs::Config#lock_ttl}
|
61
|
+
#
|
62
|
+
# @note this method takes into consideration the time
|
63
|
+
# until a job is scheduled
|
64
|
+
#
|
65
|
+
#
|
66
|
+
# @return [Integer] the number of seconds to live
|
67
|
+
#
|
68
|
+
def calculate
|
69
|
+
ttl = item[LOCK_TTL]
|
70
|
+
ttl ||= job_options[LOCK_TTL]
|
71
|
+
ttl ||= item[LOCK_EXPIRATION] # TODO: Deprecate at some point
|
72
|
+
ttl ||= job_options[LOCK_EXPIRATION] # TODO: Deprecate at some point
|
73
|
+
ttl ||= SidekiqUniqueJobs.config.lock_ttl
|
74
|
+
ttl && (ttl.to_i + time_until_scheduled)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
# Calculates the lock type
|
5
|
+
#
|
6
|
+
class LockType
|
7
|
+
# includes "SidekiqUniqueJobs::SidekiqWorkerMethods"
|
8
|
+
# @!parse include SidekiqUniqueJobs::SidekiqWorkerMethods
|
9
|
+
include SidekiqUniqueJobs::SidekiqWorkerMethods
|
10
|
+
|
11
|
+
#
|
12
|
+
# Computes lock type from job arguments, sidekiq_options.
|
13
|
+
#
|
14
|
+
# @return [Symbol] the lock type
|
15
|
+
# @return [NilClass] if no lock type is found.
|
16
|
+
#
|
17
|
+
def self.call(item)
|
18
|
+
new(item).call
|
19
|
+
end
|
20
|
+
|
21
|
+
# @!attribute [r] item
|
22
|
+
# @return [Hash] the Sidekiq job hash
|
23
|
+
attr_reader :item
|
24
|
+
|
25
|
+
# @param [Hash] item the Sidekiq job hash
|
26
|
+
# @option item [Symbol, nil] :lock the type of lock to use.
|
27
|
+
# @option item [String] :class the class of the sidekiq worker
|
28
|
+
def initialize(item)
|
29
|
+
@item = item
|
30
|
+
self.job_class = item[CLASS]
|
31
|
+
end
|
32
|
+
|
33
|
+
def call
|
34
|
+
item[LOCK] || job_options[LOCK] || default_job_options[LOCK]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|