sidekiq 6.3.1 → 6.4.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sidekiq might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Changes.md +56 -0
- data/LICENSE +3 -3
- data/README.md +7 -2
- data/bin/sidekiq +3 -3
- data/bin/sidekiqload +57 -65
- data/bin/sidekiqmon +1 -1
- data/lib/generators/sidekiq/job_generator.rb +57 -0
- data/lib/generators/sidekiq/templates/{worker.rb.erb → job.rb.erb} +2 -2
- data/lib/generators/sidekiq/templates/{worker_spec.rb.erb → job_spec.rb.erb} +1 -1
- data/lib/generators/sidekiq/templates/{worker_test.rb.erb → job_test.rb.erb} +1 -1
- data/lib/sidekiq/api.rb +68 -62
- data/lib/sidekiq/cli.rb +16 -6
- data/lib/sidekiq/client.rb +44 -64
- data/lib/sidekiq/delay.rb +2 -0
- data/lib/sidekiq/extensions/generic_proxy.rb +1 -1
- data/lib/sidekiq/fetch.rb +2 -2
- data/lib/sidekiq/job_logger.rb +15 -27
- data/lib/sidekiq/job_retry.rb +28 -26
- data/lib/sidekiq/job_util.rb +67 -0
- data/lib/sidekiq/launcher.rb +37 -36
- data/lib/sidekiq/logger.rb +8 -18
- data/lib/sidekiq/manager.rb +13 -15
- data/lib/sidekiq/middleware/chain.rb +4 -4
- data/lib/sidekiq/middleware/current_attributes.rb +6 -1
- data/lib/sidekiq/middleware/i18n.rb +4 -4
- data/lib/sidekiq/monitor.rb +1 -1
- data/lib/sidekiq/paginator.rb +8 -8
- data/lib/sidekiq/processor.rb +27 -27
- data/lib/sidekiq/rails.rb +11 -4
- data/lib/sidekiq/redis_connection.rb +2 -2
- data/lib/sidekiq/scheduled.rb +13 -3
- data/lib/sidekiq/testing/inline.rb +4 -4
- data/lib/sidekiq/testing.rb +36 -35
- data/lib/sidekiq/util.rb +13 -0
- data/lib/sidekiq/version.rb +1 -1
- data/lib/sidekiq/web/application.rb +5 -2
- data/lib/sidekiq/web/csrf_protection.rb +2 -2
- data/lib/sidekiq/web/helpers.rb +4 -4
- data/lib/sidekiq/web.rb +3 -3
- data/lib/sidekiq/worker.rb +71 -16
- data/lib/sidekiq.rb +27 -15
- data/web/assets/javascripts/application.js +58 -26
- data/web/assets/stylesheets/application-dark.css +13 -17
- data/web/assets/stylesheets/application.css +4 -5
- data/web/views/_summary.erb +1 -1
- data/web/views/busy.erb +3 -3
- metadata +8 -7
- data/lib/generators/sidekiq/worker_generator.rb +0 -57
data/lib/sidekiq/cli.rb
CHANGED
@@ -20,7 +20,7 @@ module Sidekiq
|
|
20
20
|
attr_accessor :launcher
|
21
21
|
attr_accessor :environment
|
22
22
|
|
23
|
-
def parse(args = ARGV)
|
23
|
+
def parse(args = ARGV.dup)
|
24
24
|
setup_options(args)
|
25
25
|
initialize_logger
|
26
26
|
validate!
|
@@ -115,8 +115,8 @@ module Sidekiq
|
|
115
115
|
begin
|
116
116
|
launcher.run
|
117
117
|
|
118
|
-
while
|
119
|
-
signal =
|
118
|
+
while self_read.wait_readable
|
119
|
+
signal = self_read.gets.strip
|
120
120
|
handle_signal(signal)
|
121
121
|
end
|
122
122
|
rescue Interrupt
|
@@ -295,7 +295,7 @@ module Sidekiq
|
|
295
295
|
(File.directory?(options[:require]) && !File.exist?("#{options[:require]}/config/application.rb"))
|
296
296
|
logger.info "=================================================================="
|
297
297
|
logger.info " Please point Sidekiq to a Rails application or a Ruby file "
|
298
|
-
logger.info " to load your
|
298
|
+
logger.info " to load your job classes with -r [DIR|FILE]."
|
299
299
|
logger.info "=================================================================="
|
300
300
|
logger.info @parser
|
301
301
|
die(1)
|
@@ -336,7 +336,7 @@ module Sidekiq
|
|
336
336
|
parse_queue opts, queue, weight
|
337
337
|
end
|
338
338
|
|
339
|
-
o.on "-r", "--require [PATH|DIR]", "Location of Rails application with
|
339
|
+
o.on "-r", "--require [PATH|DIR]", "Location of Rails application with jobs or file to require" do |arg|
|
340
340
|
opts[:require] = arg
|
341
341
|
end
|
342
342
|
|
@@ -380,7 +380,9 @@ module Sidekiq
|
|
380
380
|
end
|
381
381
|
|
382
382
|
def parse_config(path)
|
383
|
-
|
383
|
+
erb = ERB.new(File.read(path))
|
384
|
+
erb.filename = File.expand_path(path)
|
385
|
+
opts = load_yaml(erb.result) || {}
|
384
386
|
|
385
387
|
if opts.respond_to? :deep_symbolize_keys!
|
386
388
|
opts.deep_symbolize_keys!
|
@@ -396,6 +398,14 @@ module Sidekiq
|
|
396
398
|
opts
|
397
399
|
end
|
398
400
|
|
401
|
+
def load_yaml(src)
|
402
|
+
if Psych::VERSION > "4.0"
|
403
|
+
YAML.safe_load(src, permitted_classes: [Symbol], aliases: true)
|
404
|
+
else
|
405
|
+
YAML.load(src)
|
406
|
+
end
|
407
|
+
end
|
408
|
+
|
399
409
|
def parse_queues(opts, queues_and_weights)
|
400
410
|
queues_and_weights.each { |queue_and_weight| parse_queue(opts, *queue_and_weight) }
|
401
411
|
end
|
data/lib/sidekiq/client.rb
CHANGED
@@ -2,9 +2,12 @@
|
|
2
2
|
|
3
3
|
require "securerandom"
|
4
4
|
require "sidekiq/middleware/chain"
|
5
|
+
require "sidekiq/job_util"
|
5
6
|
|
6
7
|
module Sidekiq
|
7
8
|
class Client
|
9
|
+
include Sidekiq::JobUtil
|
10
|
+
|
8
11
|
##
|
9
12
|
# Define client-side middleware:
|
10
13
|
#
|
@@ -12,7 +15,7 @@ module Sidekiq
|
|
12
15
|
# client.middleware do |chain|
|
13
16
|
# chain.use MyClientMiddleware
|
14
17
|
# end
|
15
|
-
# client.push('class' => '
|
18
|
+
# client.push('class' => 'SomeJob', 'args' => [1,2,3])
|
16
19
|
#
|
17
20
|
# All client instances default to the globally-defined
|
18
21
|
# Sidekiq.client_middleware but you can change as necessary.
|
@@ -46,16 +49,16 @@ module Sidekiq
|
|
46
49
|
# The main method used to push a job to Redis. Accepts a number of options:
|
47
50
|
#
|
48
51
|
# queue - the named queue to use, default 'default'
|
49
|
-
# class - the
|
52
|
+
# class - the job class to call, required
|
50
53
|
# args - an array of simple arguments to the perform method, must be JSON-serializable
|
51
54
|
# at - timestamp to schedule the job (optional), must be Numeric (e.g. Time.now.to_f)
|
52
55
|
# retry - whether to retry this job if it fails, default true or an integer number of retries
|
53
56
|
# backtrace - whether to save any error backtrace, default false
|
54
57
|
#
|
55
58
|
# If class is set to the class name, the jobs' options will be based on Sidekiq's default
|
56
|
-
#
|
59
|
+
# job options. Otherwise, they will be based on the job class's options.
|
57
60
|
#
|
58
|
-
# Any options valid for a
|
61
|
+
# Any options valid for a job class's sidekiq_options are also available here.
|
59
62
|
#
|
60
63
|
# All options must be strings, not symbols. NB: because we are serializing to JSON, all
|
61
64
|
# symbols in 'args' will be converted to strings. Note that +backtrace: true+ can take quite a bit of
|
@@ -64,13 +67,15 @@ module Sidekiq
|
|
64
67
|
# Returns a unique Job ID. If middleware stops the job, nil will be returned instead.
|
65
68
|
#
|
66
69
|
# Example:
|
67
|
-
# push('queue' => 'my_queue', 'class' =>
|
70
|
+
# push('queue' => 'my_queue', 'class' => MyJob, 'args' => ['foo', 1, :bat => 'bar'])
|
68
71
|
#
|
69
72
|
def push(item)
|
70
73
|
normed = normalize_item(item)
|
71
|
-
payload =
|
72
|
-
|
74
|
+
payload = middleware.invoke(normed["class"], normed, normed["queue"], @redis_pool) do
|
75
|
+
normed
|
76
|
+
end
|
73
77
|
if payload
|
78
|
+
verify_json(payload)
|
74
79
|
raw_push([payload])
|
75
80
|
payload["jid"]
|
76
81
|
end
|
@@ -98,12 +103,17 @@ module Sidekiq
|
|
98
103
|
raise ArgumentError, "Job 'at' must be a Numeric or an Array of Numeric timestamps" if at && (Array(at).empty? || !Array(at).all? { |entry| entry.is_a?(Numeric) })
|
99
104
|
raise ArgumentError, "Job 'at' Array must have same size as 'args' Array" if at.is_a?(Array) && at.size != args.size
|
100
105
|
|
106
|
+
jid = items.delete("jid")
|
107
|
+
raise ArgumentError, "Explicitly passing 'jid' when pushing more than one job is not supported" if jid && args.size > 1
|
108
|
+
|
101
109
|
normed = normalize_item(items)
|
102
110
|
payloads = args.map.with_index { |job_args, index|
|
103
|
-
copy = normed.merge("args" => job_args, "jid" => SecureRandom.hex(12)
|
111
|
+
copy = normed.merge("args" => job_args, "jid" => SecureRandom.hex(12))
|
104
112
|
copy["at"] = (at.is_a?(Array) ? at[index] : at) if at
|
105
|
-
|
106
|
-
|
113
|
+
result = middleware.invoke(copy["class"], copy, copy["queue"], @redis_pool) do
|
114
|
+
verify_json(copy)
|
115
|
+
copy
|
116
|
+
end
|
107
117
|
result || nil
|
108
118
|
}.compact
|
109
119
|
|
@@ -116,8 +126,8 @@ module Sidekiq
|
|
116
126
|
#
|
117
127
|
# pool = ConnectionPool.new { Redis.new }
|
118
128
|
# Sidekiq::Client.via(pool) do
|
119
|
-
#
|
120
|
-
#
|
129
|
+
# SomeJob.perform_async(1,2,3)
|
130
|
+
# SomeOtherJob.perform_async(1,2,3)
|
121
131
|
# end
|
122
132
|
#
|
123
133
|
# Generally this is only needed for very large Sidekiq installs processing
|
@@ -142,10 +152,10 @@ module Sidekiq
|
|
142
152
|
end
|
143
153
|
|
144
154
|
# Resque compatibility helpers. Note all helpers
|
145
|
-
# should go through
|
155
|
+
# should go through Sidekiq::Job#client_push.
|
146
156
|
#
|
147
157
|
# Example usage:
|
148
|
-
# Sidekiq::Client.enqueue(
|
158
|
+
# Sidekiq::Client.enqueue(MyJob, 'foo', 1, :bat => 'bar')
|
149
159
|
#
|
150
160
|
# Messages are enqueued to the 'default' queue.
|
151
161
|
#
|
@@ -154,14 +164,14 @@ module Sidekiq
|
|
154
164
|
end
|
155
165
|
|
156
166
|
# Example usage:
|
157
|
-
# Sidekiq::Client.enqueue_to(:queue_name,
|
167
|
+
# Sidekiq::Client.enqueue_to(:queue_name, MyJob, 'foo', 1, :bat => 'bar')
|
158
168
|
#
|
159
169
|
def enqueue_to(queue, klass, *args)
|
160
170
|
klass.client_push("queue" => queue, "class" => klass, "args" => args)
|
161
171
|
end
|
162
172
|
|
163
173
|
# Example usage:
|
164
|
-
# Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes,
|
174
|
+
# Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes, MyJob, 'foo', 1, :bat => 'bar')
|
165
175
|
#
|
166
176
|
def enqueue_to_in(queue, interval, klass, *args)
|
167
177
|
int = interval.to_f
|
@@ -175,7 +185,7 @@ module Sidekiq
|
|
175
185
|
end
|
176
186
|
|
177
187
|
# Example usage:
|
178
|
-
# Sidekiq::Client.enqueue_in(3.minutes,
|
188
|
+
# Sidekiq::Client.enqueue_in(3.minutes, MyJob, 'foo', 1, :bat => 'bar')
|
179
189
|
#
|
180
190
|
def enqueue_in(interval, klass, *args)
|
181
191
|
klass.perform_in(interval, *args)
|
@@ -186,8 +196,23 @@ module Sidekiq
|
|
186
196
|
|
187
197
|
def raw_push(payloads)
|
188
198
|
@redis_pool.with do |conn|
|
189
|
-
|
190
|
-
|
199
|
+
retryable = true
|
200
|
+
begin
|
201
|
+
conn.pipelined do |pipeline|
|
202
|
+
atomic_push(pipeline, payloads)
|
203
|
+
end
|
204
|
+
rescue Redis::BaseError => ex
|
205
|
+
# 2550 Failover can cause the server to become a replica, need
|
206
|
+
# to disconnect and reopen the socket to get back to the primary.
|
207
|
+
# 4495 Use the same logic if we have a "Not enough replicas" error from the primary
|
208
|
+
# 4985 Use the same logic when a blocking command is force-unblocked
|
209
|
+
# The retry logic is copied from sidekiq.rb
|
210
|
+
if retryable && ex.message =~ /READONLY|NOREPLICAS|UNBLOCKED/
|
211
|
+
conn.disconnect!
|
212
|
+
retryable = false
|
213
|
+
retry
|
214
|
+
end
|
215
|
+
raise
|
191
216
|
end
|
192
217
|
end
|
193
218
|
true
|
@@ -210,50 +235,5 @@ module Sidekiq
|
|
210
235
|
conn.lpush("queue:#{queue}", to_push)
|
211
236
|
end
|
212
237
|
end
|
213
|
-
|
214
|
-
def process_single(worker_class, item)
|
215
|
-
queue = item["queue"]
|
216
|
-
|
217
|
-
middleware.invoke(worker_class, item, queue, @redis_pool) do
|
218
|
-
item
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
def validate(item)
|
223
|
-
raise(ArgumentError, "Job must be a Hash with 'class' and 'args' keys: `#{item}`") unless item.is_a?(Hash) && item.key?("class") && item.key?("args")
|
224
|
-
raise(ArgumentError, "Job args must be an Array: `#{item}`") unless item["args"].is_a?(Array)
|
225
|
-
raise(ArgumentError, "Job class must be either a Class or String representation of the class name: `#{item}`") unless item["class"].is_a?(Class) || item["class"].is_a?(String)
|
226
|
-
raise(ArgumentError, "Job 'at' must be a Numeric timestamp: `#{item}`") if item.key?("at") && !item["at"].is_a?(Numeric)
|
227
|
-
raise(ArgumentError, "Job tags must be an Array: `#{item}`") if item["tags"] && !item["tags"].is_a?(Array)
|
228
|
-
end
|
229
|
-
|
230
|
-
def normalize_item(item)
|
231
|
-
validate(item)
|
232
|
-
# raise(ArgumentError, "Arguments must be native JSON types, see https://github.com/mperham/sidekiq/wiki/Best-Practices") unless JSON.load(JSON.dump(item['args'])) == item['args']
|
233
|
-
|
234
|
-
# merge in the default sidekiq_options for the item's class and/or wrapped element
|
235
|
-
# this allows ActiveJobs to control sidekiq_options too.
|
236
|
-
defaults = normalized_hash(item["class"])
|
237
|
-
defaults = defaults.merge(item["wrapped"].get_sidekiq_options) if item["wrapped"].respond_to?("get_sidekiq_options")
|
238
|
-
item = defaults.merge(item)
|
239
|
-
|
240
|
-
raise(ArgumentError, "Job must include a valid queue name") if item["queue"].nil? || item["queue"] == ""
|
241
|
-
|
242
|
-
item["class"] = item["class"].to_s
|
243
|
-
item["queue"] = item["queue"].to_s
|
244
|
-
item["jid"] ||= SecureRandom.hex(12)
|
245
|
-
item["created_at"] ||= Time.now.to_f
|
246
|
-
|
247
|
-
item
|
248
|
-
end
|
249
|
-
|
250
|
-
def normalized_hash(item_class)
|
251
|
-
if item_class.is_a?(Class)
|
252
|
-
raise(ArgumentError, "Message must include a Sidekiq::Worker class, not class name: #{item_class.ancestors.inspect}") unless item_class.respond_to?("get_sidekiq_options")
|
253
|
-
item_class.get_sidekiq_options
|
254
|
-
else
|
255
|
-
Sidekiq.default_worker_options
|
256
|
-
end
|
257
|
-
end
|
258
238
|
end
|
259
239
|
end
|
data/lib/sidekiq/delay.rb
CHANGED
data/lib/sidekiq/fetch.rb
CHANGED
data/lib/sidekiq/job_logger.rb
CHANGED
@@ -12,46 +12,34 @@ module Sidekiq
|
|
12
12
|
|
13
13
|
yield
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
end
|
15
|
+
Sidekiq::Context.add(:elapsed, elapsed(start))
|
16
|
+
@logger.info("done")
|
18
17
|
rescue Exception
|
19
|
-
|
20
|
-
|
21
|
-
end
|
18
|
+
Sidekiq::Context.add(:elapsed, elapsed(start))
|
19
|
+
@logger.info("fail")
|
22
20
|
|
23
21
|
raise
|
24
22
|
end
|
25
23
|
|
26
24
|
def prepare(job_hash, &block)
|
27
|
-
level = job_hash["log_level"]
|
28
|
-
if level
|
29
|
-
@logger.log_at(level) do
|
30
|
-
Sidekiq::Context.with(job_hash_context(job_hash), &block)
|
31
|
-
end
|
32
|
-
else
|
33
|
-
Sidekiq::Context.with(job_hash_context(job_hash), &block)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def job_hash_context(job_hash)
|
38
25
|
# If we're using a wrapper class, like ActiveJob, use the "wrapped"
|
39
26
|
# attribute to expose the underlying thing.
|
40
27
|
h = {
|
41
28
|
class: job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"],
|
42
29
|
jid: job_hash["jid"]
|
43
30
|
}
|
44
|
-
h[:bid] = job_hash["bid"] if job_hash
|
45
|
-
h[:tags] = job_hash["tags"] if job_hash
|
46
|
-
h
|
47
|
-
end
|
48
|
-
|
49
|
-
def with_elapsed_time_context(start, &block)
|
50
|
-
Sidekiq::Context.with(elapsed_time_context(start), &block)
|
51
|
-
end
|
31
|
+
h[:bid] = job_hash["bid"] if job_hash.has_key?("bid")
|
32
|
+
h[:tags] = job_hash["tags"] if job_hash.has_key?("tags")
|
52
33
|
|
53
|
-
|
54
|
-
|
34
|
+
Thread.current[:sidekiq_context] = h
|
35
|
+
level = job_hash["log_level"]
|
36
|
+
if level
|
37
|
+
@logger.log_at(level, &block)
|
38
|
+
else
|
39
|
+
yield
|
40
|
+
end
|
41
|
+
ensure
|
42
|
+
Thread.current[:sidekiq_context] = nil
|
55
43
|
end
|
56
44
|
|
57
45
|
private
|
data/lib/sidekiq/job_retry.rb
CHANGED
@@ -25,18 +25,19 @@ module Sidekiq
|
|
25
25
|
#
|
26
26
|
# A job looks like:
|
27
27
|
#
|
28
|
-
# { 'class' => '
|
28
|
+
# { 'class' => 'HardJob', 'args' => [1, 2, 'foo'], 'retry' => true }
|
29
29
|
#
|
30
30
|
# The 'retry' option also accepts a number (in place of 'true'):
|
31
31
|
#
|
32
|
-
# { 'class' => '
|
32
|
+
# { 'class' => 'HardJob', 'args' => [1, 2, 'foo'], 'retry' => 5 }
|
33
33
|
#
|
34
34
|
# The job will be retried this number of times before giving up. (If simply
|
35
35
|
# 'true', Sidekiq retries 25 times)
|
36
36
|
#
|
37
|
-
#
|
37
|
+
# Relevant options for job retries:
|
38
38
|
#
|
39
|
-
# * 'queue' - the queue
|
39
|
+
# * 'queue' - the queue for the initial job
|
40
|
+
# * 'retry_queue' - if job retries should be pushed to a different (e.g. lower priority) queue
|
40
41
|
# * 'retry_count' - number of times we've retried so far.
|
41
42
|
# * 'error_message' - the message from the exception
|
42
43
|
# * 'error_class' - the exception class
|
@@ -52,11 +53,12 @@ module Sidekiq
|
|
52
53
|
#
|
53
54
|
# Sidekiq.options[:max_retries] = 7
|
54
55
|
#
|
55
|
-
# or limit the number of retries for a particular
|
56
|
+
# or limit the number of retries for a particular job and send retries to
|
57
|
+
# a low priority queue with:
|
56
58
|
#
|
57
|
-
# class
|
58
|
-
# include Sidekiq::
|
59
|
-
# sidekiq_options :
|
59
|
+
# class MyJob
|
60
|
+
# include Sidekiq::Job
|
61
|
+
# sidekiq_options retry: 10, retry_queue: 'low'
|
60
62
|
# end
|
61
63
|
#
|
62
64
|
class JobRetry
|
@@ -74,7 +76,7 @@ module Sidekiq
|
|
74
76
|
|
75
77
|
# The global retry handler requires only the barest of data.
|
76
78
|
# We want to be able to retry as much as possible so we don't
|
77
|
-
# require the
|
79
|
+
# require the job to be instantiated.
|
78
80
|
def global(jobstr, queue)
|
79
81
|
yield
|
80
82
|
rescue Handled => ex
|
@@ -101,14 +103,14 @@ module Sidekiq
|
|
101
103
|
end
|
102
104
|
|
103
105
|
# The local retry support means that any errors that occur within
|
104
|
-
# this block can be associated with the given
|
106
|
+
# this block can be associated with the given job instance.
|
105
107
|
# This is required to support the `sidekiq_retries_exhausted` block.
|
106
108
|
#
|
107
109
|
# Note that any exception from the block is wrapped in the Skip
|
108
110
|
# exception so the global block does not reprocess the error. The
|
109
111
|
# Skip exception is unwrapped within Sidekiq::Processor#process before
|
110
112
|
# calling the handle_exception handlers.
|
111
|
-
def local(
|
113
|
+
def local(jobinst, jobstr, queue)
|
112
114
|
yield
|
113
115
|
rescue Handled => ex
|
114
116
|
raise ex
|
@@ -121,11 +123,11 @@ module Sidekiq
|
|
121
123
|
|
122
124
|
msg = Sidekiq.load_json(jobstr)
|
123
125
|
if msg["retry"].nil?
|
124
|
-
msg["retry"] =
|
126
|
+
msg["retry"] = jobinst.class.get_sidekiq_options["retry"]
|
125
127
|
end
|
126
128
|
|
127
129
|
raise e unless msg["retry"]
|
128
|
-
attempt_retry(
|
130
|
+
attempt_retry(jobinst, msg, queue, e)
|
129
131
|
# We've handled this error associated with this job, don't
|
130
132
|
# need to handle it at the global level
|
131
133
|
raise Skip
|
@@ -133,10 +135,10 @@ module Sidekiq
|
|
133
135
|
|
134
136
|
private
|
135
137
|
|
136
|
-
# Note that +
|
137
|
-
# instantiate the
|
138
|
+
# Note that +jobinst+ can be nil here if an error is raised before we can
|
139
|
+
# instantiate the job instance. All access must be guarded and
|
138
140
|
# best effort.
|
139
|
-
def attempt_retry(
|
141
|
+
def attempt_retry(jobinst, msg, queue, exception)
|
140
142
|
max_retry_attempts = retry_attempts_from(msg["retry"], @max_retries)
|
141
143
|
|
142
144
|
msg["queue"] = (msg["retry_queue"] || queue)
|
@@ -168,7 +170,7 @@ module Sidekiq
|
|
168
170
|
end
|
169
171
|
|
170
172
|
if count < max_retry_attempts
|
171
|
-
delay = delay_for(
|
173
|
+
delay = delay_for(jobinst, count, exception)
|
172
174
|
# Logging here can break retries if the logging device raises ENOSPC #3979
|
173
175
|
# logger.debug { "Failure! Retry #{count} in #{delay} seconds" }
|
174
176
|
retry_at = Time.now.to_f + delay
|
@@ -178,13 +180,13 @@ module Sidekiq
|
|
178
180
|
end
|
179
181
|
else
|
180
182
|
# Goodbye dear message, you (re)tried your best I'm sure.
|
181
|
-
retries_exhausted(
|
183
|
+
retries_exhausted(jobinst, msg, exception)
|
182
184
|
end
|
183
185
|
end
|
184
186
|
|
185
|
-
def retries_exhausted(
|
187
|
+
def retries_exhausted(jobinst, msg, exception)
|
186
188
|
begin
|
187
|
-
block =
|
189
|
+
block = jobinst&.sidekiq_retries_exhausted_block
|
188
190
|
block&.call(msg, exception)
|
189
191
|
rescue => e
|
190
192
|
handle_exception(e, {context: "Error calling retries_exhausted", job: msg})
|
@@ -213,19 +215,19 @@ module Sidekiq
|
|
213
215
|
end
|
214
216
|
end
|
215
217
|
|
216
|
-
def delay_for(
|
218
|
+
def delay_for(jobinst, count, exception)
|
217
219
|
jitter = rand(10) * (count + 1)
|
218
|
-
if
|
219
|
-
custom_retry_in = retry_in(
|
220
|
+
if jobinst&.sidekiq_retry_in_block
|
221
|
+
custom_retry_in = retry_in(jobinst, count, exception).to_i
|
220
222
|
return custom_retry_in + jitter if custom_retry_in > 0
|
221
223
|
end
|
222
224
|
(count**4) + 15 + jitter
|
223
225
|
end
|
224
226
|
|
225
|
-
def retry_in(
|
226
|
-
|
227
|
+
def retry_in(jobinst, count, exception)
|
228
|
+
jobinst.sidekiq_retry_in_block.call(count, exception)
|
227
229
|
rescue Exception => e
|
228
|
-
handle_exception(e, {context: "Failure scheduling retry using the defined `sidekiq_retry_in` in #{
|
230
|
+
handle_exception(e, {context: "Failure scheduling retry using the defined `sidekiq_retry_in` in #{jobinst.class.name}, falling back to default"})
|
229
231
|
nil
|
230
232
|
end
|
231
233
|
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require "securerandom"
|
2
|
+
require "time"
|
3
|
+
|
4
|
+
module Sidekiq
|
5
|
+
module JobUtil
|
6
|
+
# These functions encapsulate various job utilities.
|
7
|
+
# They must be simple and free from side effects.
|
8
|
+
|
9
|
+
def validate(item)
|
10
|
+
raise(ArgumentError, "Job must be a Hash with 'class' and 'args' keys: `#{item}`") unless item.is_a?(Hash) && item.key?("class") && item.key?("args")
|
11
|
+
raise(ArgumentError, "Job args must be an Array: `#{item}`") unless item["args"].is_a?(Array)
|
12
|
+
raise(ArgumentError, "Job class must be either a Class or String representation of the class name: `#{item}`") unless item["class"].is_a?(Class) || item["class"].is_a?(String)
|
13
|
+
raise(ArgumentError, "Job 'at' must be a Numeric timestamp: `#{item}`") if item.key?("at") && !item["at"].is_a?(Numeric)
|
14
|
+
raise(ArgumentError, "Job tags must be an Array: `#{item}`") if item["tags"] && !item["tags"].is_a?(Array)
|
15
|
+
end
|
16
|
+
|
17
|
+
def verify_json(item)
|
18
|
+
job_class = item["wrapped"] || item["class"]
|
19
|
+
if Sidekiq.options[:on_complex_arguments] == :raise
|
20
|
+
msg = <<~EOM
|
21
|
+
Job arguments to #{job_class} must be native JSON types, see https://github.com/mperham/sidekiq/wiki/Best-Practices.
|
22
|
+
To disable this error, remove `Sidekiq.strict_args!` from your initializer.
|
23
|
+
EOM
|
24
|
+
raise(ArgumentError, msg) unless json_safe?(item)
|
25
|
+
elsif Sidekiq.options[:on_complex_arguments] == :warn
|
26
|
+
Sidekiq.logger.warn <<~EOM unless json_safe?(item)
|
27
|
+
Job arguments to #{job_class} do not serialize to JSON safely. This will raise an error in
|
28
|
+
Sidekiq 7.0. See https://github.com/mperham/sidekiq/wiki/Best-Practices or raise an error today
|
29
|
+
by calling `Sidekiq.strict_args!` during Sidekiq initialization.
|
30
|
+
EOM
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def normalize_item(item)
|
35
|
+
validate(item)
|
36
|
+
|
37
|
+
# merge in the default sidekiq_options for the item's class and/or wrapped element
|
38
|
+
# this allows ActiveJobs to control sidekiq_options too.
|
39
|
+
defaults = normalized_hash(item["class"])
|
40
|
+
defaults = defaults.merge(item["wrapped"].get_sidekiq_options) if item["wrapped"].respond_to?(:get_sidekiq_options)
|
41
|
+
item = defaults.merge(item)
|
42
|
+
|
43
|
+
raise(ArgumentError, "Job must include a valid queue name") if item["queue"].nil? || item["queue"] == ""
|
44
|
+
|
45
|
+
item["jid"] ||= SecureRandom.hex(12)
|
46
|
+
item["class"] = item["class"].to_s
|
47
|
+
item["queue"] = item["queue"].to_s
|
48
|
+
item["created_at"] ||= Time.now.to_f
|
49
|
+
item
|
50
|
+
end
|
51
|
+
|
52
|
+
def normalized_hash(item_class)
|
53
|
+
if item_class.is_a?(Class)
|
54
|
+
raise(ArgumentError, "Message must include a Sidekiq::Job class, not class name: #{item_class.ancestors.inspect}") unless item_class.respond_to?(:get_sidekiq_options)
|
55
|
+
item_class.get_sidekiq_options
|
56
|
+
else
|
57
|
+
Sidekiq.default_job_options
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def json_safe?(item)
|
64
|
+
JSON.parse(JSON.dump(item["args"])) == item["args"]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|