sidekiq 3.4.1 → 7.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Changes.md +1118 -4
- data/LICENSE.txt +9 -0
- data/README.md +55 -47
- data/bin/multi_queue_bench +271 -0
- data/bin/sidekiq +26 -3
- data/bin/sidekiqload +247 -0
- data/bin/sidekiqmon +11 -0
- 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/job_spec.rb.erb +6 -0
- data/lib/generators/sidekiq/templates/job_test.rb.erb +8 -0
- data/lib/sidekiq/api.rb +714 -312
- data/lib/sidekiq/capsule.rb +130 -0
- data/lib/sidekiq/cli.rb +275 -241
- data/lib/sidekiq/client.rb +141 -110
- data/lib/sidekiq/component.rb +68 -0
- data/lib/sidekiq/config.rb +291 -0
- data/lib/sidekiq/deploy.rb +62 -0
- data/lib/sidekiq/embedded.rb +61 -0
- data/lib/sidekiq/fetch.rb +53 -121
- data/lib/sidekiq/iterable_job.rb +53 -0
- data/lib/sidekiq/job/interrupt_handler.rb +22 -0
- data/lib/sidekiq/job/iterable/active_record_enumerator.rb +53 -0
- data/lib/sidekiq/job/iterable/csv_enumerator.rb +47 -0
- data/lib/sidekiq/job/iterable/enumerators.rb +135 -0
- data/lib/sidekiq/job/iterable.rb +231 -0
- data/lib/sidekiq/job.rb +385 -0
- data/lib/sidekiq/job_logger.rb +64 -0
- data/lib/sidekiq/job_retry.rb +305 -0
- data/lib/sidekiq/job_util.rb +107 -0
- data/lib/sidekiq/launcher.rb +241 -66
- data/lib/sidekiq/logger.rb +131 -0
- data/lib/sidekiq/manager.rb +91 -192
- data/lib/sidekiq/metrics/query.rb +156 -0
- data/lib/sidekiq/metrics/shared.rb +95 -0
- data/lib/sidekiq/metrics/tracking.rb +140 -0
- data/lib/sidekiq/middleware/chain.rb +114 -56
- data/lib/sidekiq/middleware/current_attributes.rb +111 -0
- data/lib/sidekiq/middleware/i18n.rb +8 -7
- data/lib/sidekiq/middleware/modules.rb +21 -0
- data/lib/sidekiq/monitor.rb +146 -0
- data/lib/sidekiq/paginator.rb +29 -16
- data/lib/sidekiq/processor.rb +248 -112
- data/lib/sidekiq/rails.rb +61 -27
- data/lib/sidekiq/redis_client_adapter.rb +114 -0
- data/lib/sidekiq/redis_connection.rb +68 -48
- data/lib/sidekiq/ring_buffer.rb +29 -0
- data/lib/sidekiq/scheduled.rb +173 -52
- data/lib/sidekiq/sd_notify.rb +149 -0
- data/lib/sidekiq/systemd.rb +24 -0
- data/lib/sidekiq/testing/inline.rb +7 -5
- data/lib/sidekiq/testing.rb +206 -65
- data/lib/sidekiq/transaction_aware_client.rb +51 -0
- data/lib/sidekiq/version.rb +4 -1
- data/lib/sidekiq/web/action.rb +99 -0
- data/lib/sidekiq/web/application.rb +479 -0
- data/lib/sidekiq/web/csrf_protection.rb +183 -0
- data/lib/sidekiq/web/helpers.rb +415 -0
- data/lib/sidekiq/web/router.rb +104 -0
- data/lib/sidekiq/web.rb +158 -200
- data/lib/sidekiq/worker_compatibility_alias.rb +13 -0
- data/lib/sidekiq.rb +100 -132
- data/sidekiq.gemspec +27 -23
- data/web/assets/images/apple-touch-icon.png +0 -0
- data/web/assets/images/favicon.ico +0 -0
- data/web/assets/javascripts/application.js +177 -72
- data/web/assets/javascripts/base-charts.js +106 -0
- data/web/assets/javascripts/chart.min.js +13 -0
- data/web/assets/javascripts/chartjs-plugin-annotation.min.js +7 -0
- data/web/assets/javascripts/dashboard-charts.js +192 -0
- data/web/assets/javascripts/dashboard.js +37 -286
- data/web/assets/javascripts/metrics.js +298 -0
- data/web/assets/stylesheets/application-dark.css +147 -0
- data/web/assets/stylesheets/application-rtl.css +163 -0
- data/web/assets/stylesheets/application.css +228 -247
- data/web/assets/stylesheets/bootstrap-rtl.min.css +9 -0
- data/web/assets/stylesheets/bootstrap.css +4 -8
- data/web/locales/ar.yml +87 -0
- data/web/locales/cs.yml +62 -52
- data/web/locales/da.yml +60 -53
- data/web/locales/de.yml +65 -53
- data/web/locales/el.yml +43 -24
- data/web/locales/en.yml +86 -61
- data/web/locales/es.yml +70 -53
- data/web/locales/fa.yml +80 -0
- data/web/locales/fr.yml +86 -56
- data/web/locales/gd.yml +99 -0
- data/web/locales/he.yml +80 -0
- data/web/locales/hi.yml +59 -59
- data/web/locales/it.yml +53 -53
- data/web/locales/ja.yml +78 -56
- data/web/locales/ko.yml +52 -52
- data/web/locales/lt.yml +83 -0
- data/web/locales/{no.yml → nb.yml} +62 -54
- data/web/locales/nl.yml +52 -52
- data/web/locales/pl.yml +45 -45
- data/web/locales/pt-br.yml +83 -55
- data/web/locales/pt.yml +51 -51
- data/web/locales/ru.yml +68 -60
- data/web/locales/sv.yml +53 -53
- data/web/locales/ta.yml +60 -60
- data/web/locales/tr.yml +101 -0
- data/web/locales/uk.yml +77 -0
- data/web/locales/ur.yml +80 -0
- data/web/locales/vi.yml +83 -0
- data/web/locales/zh-cn.yml +43 -16
- data/web/locales/zh-tw.yml +42 -8
- data/web/views/_footer.erb +22 -9
- data/web/views/_job_info.erb +27 -6
- data/web/views/_metrics_period_select.erb +12 -0
- data/web/views/_nav.erb +8 -22
- data/web/views/_paging.erb +3 -1
- data/web/views/_poll_link.erb +4 -0
- data/web/views/_summary.erb +7 -7
- data/web/views/busy.erb +91 -31
- data/web/views/dashboard.erb +52 -22
- data/web/views/dead.erb +5 -4
- data/web/views/filtering.erb +7 -0
- data/web/views/layout.erb +19 -7
- data/web/views/metrics.erb +91 -0
- data/web/views/metrics_for_job.erb +59 -0
- data/web/views/morgue.erb +26 -20
- data/web/views/queue.erb +36 -25
- data/web/views/queues.erb +24 -7
- data/web/views/retries.erb +29 -21
- data/web/views/retry.erb +6 -5
- data/web/views/scheduled.erb +20 -17
- data/web/views/scheduled_job_info.erb +2 -1
- metadata +101 -232
- data/.gitignore +0 -12
- data/.travis.yml +0 -16
- data/3.0-Upgrade.md +0 -70
- data/COMM-LICENSE +0 -85
- data/Contributing.md +0 -32
- data/Gemfile +0 -22
- data/LICENSE +0 -9
- data/Pro-2.0-Upgrade.md +0 -138
- data/Pro-Changes.md +0 -412
- data/Rakefile +0 -9
- data/bin/sidekiqctl +0 -93
- data/lib/generators/sidekiq/templates/worker_spec.rb.erb +0 -6
- data/lib/generators/sidekiq/templates/worker_test.rb.erb +0 -8
- data/lib/generators/sidekiq/worker_generator.rb +0 -49
- data/lib/sidekiq/actor.rb +0 -39
- data/lib/sidekiq/core_ext.rb +0 -105
- data/lib/sidekiq/exception_handler.rb +0 -30
- data/lib/sidekiq/extensions/action_mailer.rb +0 -56
- data/lib/sidekiq/extensions/active_record.rb +0 -39
- data/lib/sidekiq/extensions/class_methods.rb +0 -39
- data/lib/sidekiq/extensions/generic_proxy.rb +0 -24
- data/lib/sidekiq/logging.rb +0 -104
- data/lib/sidekiq/middleware/server/active_record.rb +0 -13
- data/lib/sidekiq/middleware/server/logging.rb +0 -35
- data/lib/sidekiq/middleware/server/retry_jobs.rb +0 -206
- data/lib/sidekiq/util.rb +0 -55
- data/lib/sidekiq/web_helpers.rb +0 -234
- data/lib/sidekiq/worker.rb +0 -89
- data/test/config.yml +0 -9
- data/test/env_based_config.yml +0 -11
- data/test/fake_env.rb +0 -0
- data/test/fixtures/en.yml +0 -2
- data/test/helper.rb +0 -39
- data/test/test_api.rb +0 -494
- data/test/test_cli.rb +0 -365
- data/test/test_client.rb +0 -269
- data/test/test_exception_handler.rb +0 -55
- data/test/test_extensions.rb +0 -120
- data/test/test_fetch.rb +0 -104
- data/test/test_logging.rb +0 -34
- data/test/test_manager.rb +0 -164
- data/test/test_middleware.rb +0 -159
- data/test/test_processor.rb +0 -166
- data/test/test_redis_connection.rb +0 -127
- data/test/test_retry.rb +0 -373
- data/test/test_scheduled.rb +0 -120
- data/test/test_scheduling.rb +0 -71
- data/test/test_sidekiq.rb +0 -69
- data/test/test_testing.rb +0 -82
- data/test/test_testing_fake.rb +0 -271
- data/test/test_testing_inline.rb +0 -93
- data/test/test_web.rb +0 -594
- data/test/test_web_helpers.rb +0 -52
- data/test/test_worker_generator.rb +0 -17
- data/web/assets/images/bootstrap/glyphicons-halflings-white.png +0 -0
- data/web/assets/images/bootstrap/glyphicons-halflings.png +0 -0
- data/web/assets/images/status/active.png +0 -0
- data/web/assets/images/status/idle.png +0 -0
- data/web/assets/javascripts/locales/README.md +0 -27
- data/web/assets/javascripts/locales/jquery.timeago.ar.js +0 -96
- data/web/assets/javascripts/locales/jquery.timeago.bg.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.bs.js +0 -49
- data/web/assets/javascripts/locales/jquery.timeago.ca.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.cs.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.cy.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.da.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.de.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.el.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.en-short.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.en.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.es.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.et.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.fa.js +0 -22
- data/web/assets/javascripts/locales/jquery.timeago.fi.js +0 -28
- data/web/assets/javascripts/locales/jquery.timeago.fr-short.js +0 -16
- data/web/assets/javascripts/locales/jquery.timeago.fr.js +0 -17
- data/web/assets/javascripts/locales/jquery.timeago.he.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.hr.js +0 -49
- data/web/assets/javascripts/locales/jquery.timeago.hu.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.hy.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.id.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.it.js +0 -16
- data/web/assets/javascripts/locales/jquery.timeago.ja.js +0 -19
- data/web/assets/javascripts/locales/jquery.timeago.ko.js +0 -17
- data/web/assets/javascripts/locales/jquery.timeago.lt.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.mk.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.nl.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.no.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.pl.js +0 -31
- data/web/assets/javascripts/locales/jquery.timeago.pt-br.js +0 -16
- data/web/assets/javascripts/locales/jquery.timeago.pt.js +0 -16
- data/web/assets/javascripts/locales/jquery.timeago.ro.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.rs.js +0 -49
- data/web/assets/javascripts/locales/jquery.timeago.ru.js +0 -34
- data/web/assets/javascripts/locales/jquery.timeago.sk.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.sl.js +0 -44
- data/web/assets/javascripts/locales/jquery.timeago.sv.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.th.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.tr.js +0 -16
- data/web/assets/javascripts/locales/jquery.timeago.uk.js +0 -34
- data/web/assets/javascripts/locales/jquery.timeago.uz.js +0 -19
- data/web/assets/javascripts/locales/jquery.timeago.zh-cn.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.zh-tw.js +0 -20
- data/web/views/_poll.erb +0 -10
- /data/web/assets/images/{status-sd8051fd480.png → status.png} +0 -0
@@ -0,0 +1,305 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "zlib"
|
4
|
+
require "sidekiq/component"
|
5
|
+
|
6
|
+
module Sidekiq
|
7
|
+
##
|
8
|
+
# Automatically retry jobs that fail in Sidekiq.
|
9
|
+
# Sidekiq's retry support assumes a typical development lifecycle:
|
10
|
+
#
|
11
|
+
# 0. Push some code changes with a bug in it.
|
12
|
+
# 1. Bug causes job processing to fail, Sidekiq's middleware captures
|
13
|
+
# the job and pushes it onto a retry queue.
|
14
|
+
# 2. Sidekiq retries jobs in the retry queue multiple times with
|
15
|
+
# an exponential delay, the job continues to fail.
|
16
|
+
# 3. After a few days, a developer deploys a fix. The job is
|
17
|
+
# reprocessed successfully.
|
18
|
+
# 4. Once retries are exhausted, Sidekiq will give up and move the
|
19
|
+
# job to the Dead Job Queue (aka morgue) where it must be dealt with
|
20
|
+
# manually in the Web UI.
|
21
|
+
# 5. After 6 months on the DJQ, Sidekiq will discard the job.
|
22
|
+
#
|
23
|
+
# A job looks like:
|
24
|
+
#
|
25
|
+
# { 'class' => 'HardJob', 'args' => [1, 2, 'foo'], 'retry' => true }
|
26
|
+
#
|
27
|
+
# The 'retry' option also accepts a number (in place of 'true'):
|
28
|
+
#
|
29
|
+
# { 'class' => 'HardJob', 'args' => [1, 2, 'foo'], 'retry' => 5 }
|
30
|
+
#
|
31
|
+
# The job will be retried this number of times before giving up. (If simply
|
32
|
+
# 'true', Sidekiq retries 25 times)
|
33
|
+
#
|
34
|
+
# Relevant options for job retries:
|
35
|
+
#
|
36
|
+
# * 'queue' - the queue for the initial job
|
37
|
+
# * 'retry_queue' - if job retries should be pushed to a different (e.g. lower priority) queue
|
38
|
+
# * 'retry_count' - number of times we've retried so far.
|
39
|
+
# * 'error_message' - the message from the exception
|
40
|
+
# * 'error_class' - the exception class
|
41
|
+
# * 'failed_at' - the first time it failed
|
42
|
+
# * 'retried_at' - the last time it was retried
|
43
|
+
# * 'backtrace' - the number of lines of error backtrace to store
|
44
|
+
#
|
45
|
+
# We don't store the backtrace by default as that can add a lot of overhead
|
46
|
+
# to the job and everyone is using an error service, right?
|
47
|
+
#
|
48
|
+
# The default number of retries is 25 which works out to about 3 weeks
|
49
|
+
# You can change the default maximum number of retries in your initializer:
|
50
|
+
#
|
51
|
+
# Sidekiq.default_configuration[:max_retries] = 7
|
52
|
+
#
|
53
|
+
# or limit the number of retries for a particular job and send retries to
|
54
|
+
# a low priority queue with:
|
55
|
+
#
|
56
|
+
# class MyJob
|
57
|
+
# include Sidekiq::Job
|
58
|
+
# sidekiq_options retry: 10, retry_queue: 'low'
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
class JobRetry
|
62
|
+
# Handled means the job failed but has been dealt with
|
63
|
+
# (by creating a retry, rescheduling it, etc). It still
|
64
|
+
# needs to be logged and dispatched to error_handlers.
|
65
|
+
class Handled < ::RuntimeError; end
|
66
|
+
|
67
|
+
# Skip means the job failed but Sidekiq does not need to
|
68
|
+
# create a retry, log it or send to error_handlers.
|
69
|
+
class Skip < Handled; end
|
70
|
+
|
71
|
+
include Sidekiq::Component
|
72
|
+
|
73
|
+
DEFAULT_MAX_RETRY_ATTEMPTS = 25
|
74
|
+
|
75
|
+
def initialize(capsule)
|
76
|
+
@config = @capsule = capsule
|
77
|
+
@max_retries = Sidekiq.default_configuration[:max_retries] || DEFAULT_MAX_RETRY_ATTEMPTS
|
78
|
+
@backtrace_cleaner = Sidekiq.default_configuration[:backtrace_cleaner]
|
79
|
+
end
|
80
|
+
|
81
|
+
# The global retry handler requires only the barest of data.
|
82
|
+
# We want to be able to retry as much as possible so we don't
|
83
|
+
# require the job to be instantiated.
|
84
|
+
def global(jobstr, queue)
|
85
|
+
yield
|
86
|
+
rescue Handled => ex
|
87
|
+
raise ex
|
88
|
+
rescue Sidekiq::Shutdown => ey
|
89
|
+
# ignore, will be pushed back onto queue during hard_shutdown
|
90
|
+
raise ey
|
91
|
+
rescue Exception => e
|
92
|
+
# ignore, will be pushed back onto queue during hard_shutdown
|
93
|
+
raise Sidekiq::Shutdown if exception_caused_by_shutdown?(e)
|
94
|
+
|
95
|
+
msg = Sidekiq.load_json(jobstr)
|
96
|
+
if msg["retry"]
|
97
|
+
process_retry(nil, msg, queue, e)
|
98
|
+
else
|
99
|
+
@capsule.config.death_handlers.each do |handler|
|
100
|
+
handler.call(msg, e)
|
101
|
+
rescue => handler_ex
|
102
|
+
handle_exception(handler_ex, {context: "Error calling death handler", job: msg})
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
raise Handled
|
107
|
+
end
|
108
|
+
|
109
|
+
# The local retry support means that any errors that occur within
|
110
|
+
# this block can be associated with the given job instance.
|
111
|
+
# This is required to support the `sidekiq_retries_exhausted` block.
|
112
|
+
#
|
113
|
+
# Note that any exception from the block is wrapped in the Skip
|
114
|
+
# exception so the global block does not reprocess the error. The
|
115
|
+
# Skip exception is unwrapped within Sidekiq::Processor#process before
|
116
|
+
# calling the handle_exception handlers.
|
117
|
+
def local(jobinst, jobstr, queue)
|
118
|
+
yield
|
119
|
+
rescue Handled => ex
|
120
|
+
raise ex
|
121
|
+
rescue Sidekiq::Shutdown => ey
|
122
|
+
# ignore, will be pushed back onto queue during hard_shutdown
|
123
|
+
raise ey
|
124
|
+
rescue Exception => e
|
125
|
+
# ignore, will be pushed back onto queue during hard_shutdown
|
126
|
+
raise Sidekiq::Shutdown if exception_caused_by_shutdown?(e)
|
127
|
+
|
128
|
+
msg = Sidekiq.load_json(jobstr)
|
129
|
+
if msg["retry"].nil?
|
130
|
+
msg["retry"] = jobinst.class.get_sidekiq_options["retry"]
|
131
|
+
end
|
132
|
+
|
133
|
+
raise e unless msg["retry"]
|
134
|
+
process_retry(jobinst, msg, queue, e)
|
135
|
+
# We've handled this error associated with this job, don't
|
136
|
+
# need to handle it at the global level
|
137
|
+
raise Handled
|
138
|
+
end
|
139
|
+
|
140
|
+
private
|
141
|
+
|
142
|
+
# Note that +jobinst+ can be nil here if an error is raised before we can
|
143
|
+
# instantiate the job instance. All access must be guarded and
|
144
|
+
# best effort.
|
145
|
+
def process_retry(jobinst, msg, queue, exception)
|
146
|
+
max_retry_attempts = retry_attempts_from(msg["retry"], @max_retries)
|
147
|
+
|
148
|
+
msg["queue"] = (msg["retry_queue"] || queue)
|
149
|
+
|
150
|
+
m = exception_message(exception)
|
151
|
+
if m.respond_to?(:scrub!)
|
152
|
+
m.force_encoding("utf-8")
|
153
|
+
m.scrub!
|
154
|
+
end
|
155
|
+
|
156
|
+
msg["error_message"] = m
|
157
|
+
msg["error_class"] = exception.class.name
|
158
|
+
count = if msg["retry_count"]
|
159
|
+
msg["retried_at"] = Time.now.to_f
|
160
|
+
msg["retry_count"] += 1
|
161
|
+
else
|
162
|
+
msg["failed_at"] = Time.now.to_f
|
163
|
+
msg["retry_count"] = 0
|
164
|
+
end
|
165
|
+
|
166
|
+
if msg["backtrace"]
|
167
|
+
backtrace = @backtrace_cleaner.call(exception.backtrace)
|
168
|
+
lines = if msg["backtrace"] == true
|
169
|
+
backtrace
|
170
|
+
else
|
171
|
+
backtrace[0...msg["backtrace"].to_i]
|
172
|
+
end
|
173
|
+
|
174
|
+
msg["error_backtrace"] = compress_backtrace(lines)
|
175
|
+
end
|
176
|
+
|
177
|
+
return retries_exhausted(jobinst, msg, exception) if count >= max_retry_attempts
|
178
|
+
|
179
|
+
rf = msg["retry_for"]
|
180
|
+
return retries_exhausted(jobinst, msg, exception) if rf && ((msg["failed_at"] + rf) < Time.now.to_f)
|
181
|
+
|
182
|
+
strategy, delay = delay_for(jobinst, count, exception, msg)
|
183
|
+
case strategy
|
184
|
+
when :discard
|
185
|
+
return # poof!
|
186
|
+
when :kill
|
187
|
+
return retries_exhausted(jobinst, msg, exception)
|
188
|
+
end
|
189
|
+
|
190
|
+
# Logging here can break retries if the logging device raises ENOSPC #3979
|
191
|
+
# logger.debug { "Failure! Retry #{count} in #{delay} seconds" }
|
192
|
+
jitter = rand(10) * (count + 1)
|
193
|
+
retry_at = Time.now.to_f + delay + jitter
|
194
|
+
payload = Sidekiq.dump_json(msg)
|
195
|
+
redis do |conn|
|
196
|
+
conn.zadd("retry", retry_at.to_s, payload)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
# returns (strategy, seconds)
|
201
|
+
def delay_for(jobinst, count, exception, msg)
|
202
|
+
rv = begin
|
203
|
+
# sidekiq_retry_in can return two different things:
|
204
|
+
# 1. When to retry next, as an integer of seconds
|
205
|
+
# 2. A symbol which re-routes the job elsewhere, e.g. :discard, :kill, :default
|
206
|
+
block = jobinst&.sidekiq_retry_in_block
|
207
|
+
|
208
|
+
# the sidekiq_retry_in_block can be defined in a wrapped class (ActiveJob for instance)
|
209
|
+
unless msg["wrapped"].nil?
|
210
|
+
wrapped = Object.const_get(msg["wrapped"])
|
211
|
+
block = wrapped.respond_to?(:sidekiq_retry_in_block) ? wrapped.sidekiq_retry_in_block : nil
|
212
|
+
end
|
213
|
+
block&.call(count, exception, msg)
|
214
|
+
rescue Exception => e
|
215
|
+
handle_exception(e, {context: "Failure scheduling retry using the defined `sidekiq_retry_in` in #{jobinst.class.name}, falling back to default"})
|
216
|
+
nil
|
217
|
+
end
|
218
|
+
|
219
|
+
rv = rv.to_i if rv.respond_to?(:to_i)
|
220
|
+
delay = (count**4) + 15
|
221
|
+
if Integer === rv && rv > 0
|
222
|
+
delay = rv
|
223
|
+
elsif rv == :discard
|
224
|
+
return [:discard, nil] # do nothing, job goes poof
|
225
|
+
elsif rv == :kill
|
226
|
+
return [:kill, nil]
|
227
|
+
end
|
228
|
+
|
229
|
+
[:default, delay]
|
230
|
+
end
|
231
|
+
|
232
|
+
def retries_exhausted(jobinst, msg, exception)
|
233
|
+
rv = begin
|
234
|
+
block = jobinst&.sidekiq_retries_exhausted_block
|
235
|
+
|
236
|
+
# the sidekiq_retries_exhausted_block can be defined in a wrapped class (ActiveJob for instance)
|
237
|
+
unless msg["wrapped"].nil?
|
238
|
+
wrapped = Object.const_get(msg["wrapped"])
|
239
|
+
block = wrapped.respond_to?(:sidekiq_retries_exhausted_block) ? wrapped.sidekiq_retries_exhausted_block : nil
|
240
|
+
end
|
241
|
+
block&.call(msg, exception)
|
242
|
+
rescue => e
|
243
|
+
handle_exception(e, {context: "Error calling retries_exhausted", job: msg})
|
244
|
+
end
|
245
|
+
|
246
|
+
return if rv == :discard # poof!
|
247
|
+
send_to_morgue(msg) unless msg["dead"] == false
|
248
|
+
|
249
|
+
@capsule.config.death_handlers.each do |handler|
|
250
|
+
handler.call(msg, exception)
|
251
|
+
rescue => e
|
252
|
+
handle_exception(e, {context: "Error calling death handler", job: msg})
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
def send_to_morgue(msg)
|
257
|
+
logger.info { "Adding dead #{msg["class"]} job #{msg["jid"]}" }
|
258
|
+
payload = Sidekiq.dump_json(msg)
|
259
|
+
now = Time.now.to_f
|
260
|
+
|
261
|
+
redis do |conn|
|
262
|
+
conn.multi do |xa|
|
263
|
+
xa.zadd("dead", now.to_s, payload)
|
264
|
+
xa.zremrangebyscore("dead", "-inf", now - @capsule.config[:dead_timeout_in_seconds])
|
265
|
+
xa.zremrangebyrank("dead", 0, - @capsule.config[:dead_max_jobs])
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
def retry_attempts_from(msg_retry, default)
|
271
|
+
if msg_retry.is_a?(Integer)
|
272
|
+
msg_retry
|
273
|
+
else
|
274
|
+
default
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
def exception_caused_by_shutdown?(e, checked_causes = [])
|
279
|
+
return false unless e.cause
|
280
|
+
|
281
|
+
# Handle circular causes
|
282
|
+
checked_causes << e.object_id
|
283
|
+
return false if checked_causes.include?(e.cause.object_id)
|
284
|
+
|
285
|
+
e.cause.instance_of?(Sidekiq::Shutdown) ||
|
286
|
+
exception_caused_by_shutdown?(e.cause, checked_causes)
|
287
|
+
end
|
288
|
+
|
289
|
+
# Extract message from exception.
|
290
|
+
# Set a default if the message raises an error
|
291
|
+
def exception_message(exception)
|
292
|
+
# App code can stuff all sorts of crazy binary data into the error message
|
293
|
+
# that won't convert to JSON.
|
294
|
+
exception.message.to_s[0, 10_000]
|
295
|
+
rescue
|
296
|
+
+"!!! ERROR MESSAGE THREW AN ERROR !!!"
|
297
|
+
end
|
298
|
+
|
299
|
+
def compress_backtrace(backtrace)
|
300
|
+
serialized = Sidekiq.dump_json(backtrace)
|
301
|
+
compressed = Zlib::Deflate.deflate(serialized)
|
302
|
+
[compressed].pack("m0") # Base64.strict_encode64
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require "securerandom"
|
2
|
+
require "time"
|
3
|
+
|
4
|
+
module Sidekiq
|
5
|
+
module JobUtil
|
6
|
+
# These functions encapsulate various job utilities.
|
7
|
+
|
8
|
+
TRANSIENT_ATTRIBUTES = %w[]
|
9
|
+
|
10
|
+
def validate(item)
|
11
|
+
raise(ArgumentError, "Job must be a Hash with 'class' and 'args' keys: `#{item}`") unless item.is_a?(Hash) && item.key?("class") && item.key?("args")
|
12
|
+
raise(ArgumentError, "Job args must be an Array: `#{item}`") unless item["args"].is_a?(Array) || item["args"].is_a?(Enumerator::Lazy)
|
13
|
+
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)
|
14
|
+
raise(ArgumentError, "Job 'at' must be a Numeric timestamp: `#{item}`") if item.key?("at") && !item["at"].is_a?(Numeric)
|
15
|
+
raise(ArgumentError, "Job tags must be an Array: `#{item}`") if item["tags"] && !item["tags"].is_a?(Array)
|
16
|
+
raise(ArgumentError, "retry_for must be a relative amount of time, e.g. 48.hours `#{item}`") if item["retry_for"] && item["retry_for"] > 1_000_000_000
|
17
|
+
end
|
18
|
+
|
19
|
+
def verify_json(item)
|
20
|
+
job_class = item["wrapped"] || item["class"]
|
21
|
+
args = item["args"]
|
22
|
+
mode = Sidekiq::Config::DEFAULTS[:on_complex_arguments]
|
23
|
+
|
24
|
+
if mode == :raise || mode == :warn
|
25
|
+
if (unsafe_item = json_unsafe?(args))
|
26
|
+
msg = <<~EOM
|
27
|
+
Job arguments to #{job_class} must be native JSON types, but #{unsafe_item.inspect} is a #{unsafe_item.class}.
|
28
|
+
See https://github.com/sidekiq/sidekiq/wiki/Best-Practices
|
29
|
+
To disable this error, add `Sidekiq.strict_args!(false)` to your initializer.
|
30
|
+
EOM
|
31
|
+
|
32
|
+
if mode == :raise
|
33
|
+
raise(ArgumentError, msg)
|
34
|
+
else
|
35
|
+
warn(msg)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def normalize_item(item)
|
42
|
+
validate(item)
|
43
|
+
|
44
|
+
# merge in the default sidekiq_options for the item's class and/or wrapped element
|
45
|
+
# this allows ActiveJobs to control sidekiq_options too.
|
46
|
+
defaults = normalized_hash(item["class"])
|
47
|
+
defaults = defaults.merge(item["wrapped"].get_sidekiq_options) if item["wrapped"].respond_to?(:get_sidekiq_options)
|
48
|
+
item = defaults.merge(item)
|
49
|
+
|
50
|
+
raise(ArgumentError, "Job must include a valid queue name") if item["queue"].nil? || item["queue"] == ""
|
51
|
+
|
52
|
+
# remove job attributes which aren't necessary to persist into Redis
|
53
|
+
TRANSIENT_ATTRIBUTES.each { |key| item.delete(key) }
|
54
|
+
|
55
|
+
item["jid"] ||= SecureRandom.hex(12)
|
56
|
+
item["class"] = item["class"].to_s
|
57
|
+
item["queue"] = item["queue"].to_s
|
58
|
+
item["retry_for"] = item["retry_for"].to_i if item["retry_for"]
|
59
|
+
item["created_at"] ||= Time.now.to_f
|
60
|
+
item
|
61
|
+
end
|
62
|
+
|
63
|
+
def normalized_hash(item_class)
|
64
|
+
if item_class.is_a?(Class)
|
65
|
+
raise(ArgumentError, "Message must include a Sidekiq::Job class, not class name: #{item_class.ancestors.inspect}") unless item_class.respond_to?(:get_sidekiq_options)
|
66
|
+
item_class.get_sidekiq_options
|
67
|
+
else
|
68
|
+
Sidekiq.default_job_options
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
RECURSIVE_JSON_UNSAFE = {
|
75
|
+
Integer => ->(val) {},
|
76
|
+
Float => ->(val) {},
|
77
|
+
TrueClass => ->(val) {},
|
78
|
+
FalseClass => ->(val) {},
|
79
|
+
NilClass => ->(val) {},
|
80
|
+
String => ->(val) {},
|
81
|
+
Array => ->(val) {
|
82
|
+
val.each do |e|
|
83
|
+
unsafe_item = RECURSIVE_JSON_UNSAFE[e.class].call(e)
|
84
|
+
return unsafe_item unless unsafe_item.nil?
|
85
|
+
end
|
86
|
+
nil
|
87
|
+
},
|
88
|
+
Hash => ->(val) {
|
89
|
+
val.each do |k, v|
|
90
|
+
return k unless String === k
|
91
|
+
|
92
|
+
unsafe_item = RECURSIVE_JSON_UNSAFE[v.class].call(v)
|
93
|
+
return unsafe_item unless unsafe_item.nil?
|
94
|
+
end
|
95
|
+
nil
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
RECURSIVE_JSON_UNSAFE.default = ->(val) { val }
|
100
|
+
RECURSIVE_JSON_UNSAFE.compare_by_identity
|
101
|
+
private_constant :RECURSIVE_JSON_UNSAFE
|
102
|
+
|
103
|
+
def json_unsafe?(item)
|
104
|
+
RECURSIVE_JSON_UNSAFE[item.class].call(item)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|