sidekiq-unique-jobs 7.0.0.beta9 → 7.0.0.beta10
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 +4 -4
- data/README.md +13 -13
- data/lib/sidekiq_unique_jobs.rb +4 -2
- data/lib/sidekiq_unique_jobs/batch_delete.rb +2 -0
- data/lib/sidekiq_unique_jobs/cli.rb +7 -1
- data/lib/sidekiq_unique_jobs/config.rb +47 -3
- data/lib/sidekiq_unique_jobs/constants.rb +2 -0
- data/lib/sidekiq_unique_jobs/exceptions.rb +12 -7
- data/lib/sidekiq_unique_jobs/job.rb +32 -11
- data/lib/sidekiq_unique_jobs/lock/base_lock.rb +4 -4
- data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +1 -1
- data/lib/sidekiq_unique_jobs/lock/validator.rb +22 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +1 -1
- data/lib/sidekiq_unique_jobs/lock_args.rb +118 -0
- data/lib/sidekiq_unique_jobs/lock_config.rb +26 -1
- data/lib/sidekiq_unique_jobs/lock_digest.rb +77 -0
- data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
- data/lib/sidekiq_unique_jobs/{time_calculator.rb → lock_ttl.rb} +22 -37
- data/lib/sidekiq_unique_jobs/locksmith.rb +5 -3
- data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +1 -1
- data/lib/sidekiq_unique_jobs/middleware.rb +1 -1
- data/lib/sidekiq_unique_jobs/on_conflict/log.rb +1 -1
- data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +7 -1
- data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +2 -2
- data/lib/sidekiq_unique_jobs/options_with_fallback.rb +1 -11
- data/lib/sidekiq_unique_jobs/orphans/reaper.rb +9 -1
- data/lib/sidekiq_unique_jobs/rspec/matchers.rb +5 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +9 -1
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +17 -5
- data/lib/sidekiq_unique_jobs/testing.rb +15 -7
- data/lib/sidekiq_unique_jobs/unlockable.rb +3 -3
- data/lib/sidekiq_unique_jobs/upgrade_locks.rb +4 -0
- data/lib/sidekiq_unique_jobs/version.rb +1 -1
- data/lib/sidekiq_unique_jobs/version_check.rb +1 -1
- data/lib/sidekiq_unique_jobs/web.rb +1 -1
- data/lib/sidekiq_unique_jobs/web/helpers.rb +6 -4
- data/lib/sidekiq_unique_jobs/web/views/lock.erb +2 -2
- data/lib/tasks/changelog.rake +2 -2
- metadata +23 -15
- data/lib/sidekiq_unique_jobs/unique_args.rb +0 -148
@@ -1,148 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module SidekiqUniqueJobs
|
4
|
-
# Handles uniqueness of sidekiq arguments
|
5
|
-
#
|
6
|
-
# @author Mikael Henriksson <mikael@zoolutions.se>
|
7
|
-
class UniqueArgs
|
8
|
-
include SidekiqUniqueJobs::Logging
|
9
|
-
include SidekiqUniqueJobs::SidekiqWorkerMethods
|
10
|
-
include SidekiqUniqueJobs::JSON
|
11
|
-
|
12
|
-
# Convenience method for returning a digest
|
13
|
-
# @param [Hash] item a Sidekiq job hash
|
14
|
-
# @return [String] a unique digest
|
15
|
-
def self.digest(item)
|
16
|
-
new(item).unique_digest
|
17
|
-
end
|
18
|
-
|
19
|
-
# The sidekiq job hash
|
20
|
-
# @return [Hash] the Sidekiq job hash
|
21
|
-
attr_reader :item
|
22
|
-
|
23
|
-
# @param [Hash] item a Sidekiq job hash
|
24
|
-
def initialize(item)
|
25
|
-
@item = item
|
26
|
-
@worker_class = item[CLASS]
|
27
|
-
|
28
|
-
add_uniqueness_to_item
|
29
|
-
end
|
30
|
-
|
31
|
-
# Appends the keys unique_prefix, unique_args and {#unique_digest} to the sidekiq job hash {#item}
|
32
|
-
# @return [void]
|
33
|
-
def add_uniqueness_to_item
|
34
|
-
item[UNIQUE_PREFIX] ||= unique_prefix
|
35
|
-
item[UNIQUE_ARGS] = unique_args(item[ARGS])
|
36
|
-
item[UNIQUE_DIGEST] = unique_digest
|
37
|
-
end
|
38
|
-
|
39
|
-
# Memoized unique_digest
|
40
|
-
# @return [String] a unique digest
|
41
|
-
def unique_digest
|
42
|
-
@unique_digest ||= create_digest
|
43
|
-
end
|
44
|
-
|
45
|
-
# Creates a namespaced unique digest based on the {#digestable_hash} and the {#unique_prefix}
|
46
|
-
# @return [String] a unique digest
|
47
|
-
def create_digest
|
48
|
-
digest = Digest::MD5.hexdigest(dump_json(digestable_hash))
|
49
|
-
"#{unique_prefix}:#{digest}"
|
50
|
-
end
|
51
|
-
|
52
|
-
# A prefix to use as namespace for the {#unique_digest}
|
53
|
-
# @return [String] a unique digest
|
54
|
-
def unique_prefix
|
55
|
-
worker_options[UNIQUE_PREFIX] || SidekiqUniqueJobs.config.unique_prefix
|
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, UNIQUE_ARGS).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
|
-
# The unique arguments to use for creating a lock
|
68
|
-
# @return [Array] the arguments filters by the {#filtered_args} method if {#unique_args_enabled?}
|
69
|
-
def unique_args(args)
|
70
|
-
return filtered_args(args) if unique_args_enabled?
|
71
|
-
|
72
|
-
args
|
73
|
-
end
|
74
|
-
|
75
|
-
# Checks if we should disregard the queue when creating the unique digest
|
76
|
-
# @return [true, false]
|
77
|
-
def unique_across_queues?
|
78
|
-
item[UNIQUE_ACROSS_QUEUES] || worker_options[UNIQUE_ACROSS_QUEUES]
|
79
|
-
end
|
80
|
-
|
81
|
-
# Checks if we should disregard the worker when creating the unique digest
|
82
|
-
# @return [true, false]
|
83
|
-
def unique_across_workers?
|
84
|
-
item[UNIQUE_ACROSS_WORKERS] || worker_options[UNIQUE_ACROSS_WORKERS]
|
85
|
-
end
|
86
|
-
|
87
|
-
# Checks if the worker class has been enabled for unique_args?
|
88
|
-
# @return [true, false]
|
89
|
-
def unique_args_enabled?
|
90
|
-
unique_args_method # && !unique_args_method.is_a?(Boolean)
|
91
|
-
end
|
92
|
-
|
93
|
-
# Filters unique arguments by proc or symbol
|
94
|
-
# @param [Array] args the arguments passed to the sidekiq worker
|
95
|
-
# @return [Array] {#filter_by_proc} when {#unique_args_method} is a Proc
|
96
|
-
# @return [Array] {#filter_by_symbol} when {#unique_args_method} is a Symbol
|
97
|
-
# @return [Array] args unfiltered when neither of the above
|
98
|
-
def filtered_args(args)
|
99
|
-
return args if args.empty?
|
100
|
-
|
101
|
-
json_args = Normalizer.jsonify(args)
|
102
|
-
|
103
|
-
case unique_args_method
|
104
|
-
when Proc
|
105
|
-
filter_by_proc(json_args)
|
106
|
-
when Symbol
|
107
|
-
filter_by_symbol(json_args)
|
108
|
-
else
|
109
|
-
log_debug("#{__method__} arguments not filtered (using all arguments for uniqueness)")
|
110
|
-
json_args
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
# Filters unique arguments by proc configured in the sidekiq worker
|
115
|
-
# @param [Array] args the arguments passed to the sidekiq worker
|
116
|
-
# @return [Array] with the filtered arguments
|
117
|
-
def filter_by_proc(args)
|
118
|
-
unique_args_method.call(args)
|
119
|
-
end
|
120
|
-
|
121
|
-
# Filters unique arguments by method configured in the sidekiq worker
|
122
|
-
# @param [Array] args the arguments passed to the sidekiq worker
|
123
|
-
# @return [Array] unfiltered unless {#worker_method_defined?}
|
124
|
-
# @return [Array] with the filtered arguments
|
125
|
-
def filter_by_symbol(args)
|
126
|
-
return args unless worker_method_defined?(unique_args_method)
|
127
|
-
|
128
|
-
worker_class.send(unique_args_method, args)
|
129
|
-
rescue ArgumentError
|
130
|
-
raise SidekiqUniqueJobs::InvalidUniqueArguments,
|
131
|
-
given: args,
|
132
|
-
worker_class: worker_class,
|
133
|
-
unique_args_method: unique_args_method
|
134
|
-
end
|
135
|
-
|
136
|
-
# The method to use for filtering unique arguments
|
137
|
-
def unique_args_method
|
138
|
-
@unique_args_method ||= worker_options[UNIQUE_ARGS]
|
139
|
-
@unique_args_method ||= :unique_args if worker_method_defined?(:unique_args)
|
140
|
-
@unique_args_method ||= default_unique_args_method
|
141
|
-
end
|
142
|
-
|
143
|
-
# The global worker options defined in Sidekiq directly
|
144
|
-
def default_unique_args_method
|
145
|
-
Sidekiq.default_worker_options.stringify_keys[UNIQUE_ARGS]
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|