sidekiq 4.2.10 → 6.1.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.

Files changed (106) hide show
  1. checksums.yaml +5 -5
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +20 -0
  3. data/.github/workflows/ci.yml +41 -0
  4. data/.gitignore +2 -1
  5. data/.standard.yml +20 -0
  6. data/5.0-Upgrade.md +56 -0
  7. data/6.0-Upgrade.md +72 -0
  8. data/COMM-LICENSE +12 -10
  9. data/Changes.md +354 -1
  10. data/Ent-2.0-Upgrade.md +37 -0
  11. data/Ent-Changes.md +111 -3
  12. data/Gemfile +16 -21
  13. data/Gemfile.lock +192 -0
  14. data/LICENSE +1 -1
  15. data/Pro-4.0-Upgrade.md +35 -0
  16. data/Pro-5.0-Upgrade.md +25 -0
  17. data/Pro-Changes.md +181 -4
  18. data/README.md +19 -33
  19. data/Rakefile +6 -8
  20. data/bin/sidekiq +26 -2
  21. data/bin/sidekiqload +37 -34
  22. data/bin/sidekiqmon +8 -0
  23. data/lib/generators/sidekiq/templates/worker_spec.rb.erb +1 -1
  24. data/lib/generators/sidekiq/templates/worker_test.rb.erb +1 -1
  25. data/lib/generators/sidekiq/worker_generator.rb +21 -13
  26. data/lib/sidekiq.rb +86 -61
  27. data/lib/sidekiq/api.rb +320 -209
  28. data/lib/sidekiq/cli.rb +207 -217
  29. data/lib/sidekiq/client.rb +78 -51
  30. data/lib/sidekiq/delay.rb +41 -0
  31. data/lib/sidekiq/exception_handler.rb +12 -16
  32. data/lib/sidekiq/extensions/action_mailer.rb +13 -22
  33. data/lib/sidekiq/extensions/active_record.rb +13 -10
  34. data/lib/sidekiq/extensions/class_methods.rb +14 -11
  35. data/lib/sidekiq/extensions/generic_proxy.rb +10 -4
  36. data/lib/sidekiq/fetch.rb +29 -30
  37. data/lib/sidekiq/job_logger.rb +63 -0
  38. data/lib/sidekiq/job_retry.rb +262 -0
  39. data/lib/sidekiq/launcher.rb +102 -69
  40. data/lib/sidekiq/logger.rb +165 -0
  41. data/lib/sidekiq/manager.rb +16 -19
  42. data/lib/sidekiq/middleware/chain.rb +15 -5
  43. data/lib/sidekiq/middleware/i18n.rb +5 -7
  44. data/lib/sidekiq/monitor.rb +133 -0
  45. data/lib/sidekiq/paginator.rb +18 -14
  46. data/lib/sidekiq/processor.rb +161 -82
  47. data/lib/sidekiq/rails.rb +27 -100
  48. data/lib/sidekiq/redis_connection.rb +60 -20
  49. data/lib/sidekiq/scheduled.rb +61 -35
  50. data/lib/sidekiq/sd_notify.rb +149 -0
  51. data/lib/sidekiq/systemd.rb +24 -0
  52. data/lib/sidekiq/testing.rb +48 -28
  53. data/lib/sidekiq/testing/inline.rb +2 -1
  54. data/lib/sidekiq/util.rb +20 -16
  55. data/lib/sidekiq/version.rb +2 -1
  56. data/lib/sidekiq/web.rb +57 -57
  57. data/lib/sidekiq/web/action.rb +14 -14
  58. data/lib/sidekiq/web/application.rb +103 -84
  59. data/lib/sidekiq/web/csrf_protection.rb +158 -0
  60. data/lib/sidekiq/web/helpers.rb +126 -71
  61. data/lib/sidekiq/web/router.rb +18 -17
  62. data/lib/sidekiq/worker.rb +164 -41
  63. data/sidekiq.gemspec +15 -27
  64. data/web/assets/javascripts/application.js +25 -27
  65. data/web/assets/javascripts/dashboard.js +33 -37
  66. data/web/assets/stylesheets/application-dark.css +143 -0
  67. data/web/assets/stylesheets/application-rtl.css +246 -0
  68. data/web/assets/stylesheets/application.css +385 -10
  69. data/web/assets/stylesheets/bootstrap-rtl.min.css +9 -0
  70. data/web/assets/stylesheets/bootstrap.css +2 -2
  71. data/web/locales/ar.yml +81 -0
  72. data/web/locales/de.yml +14 -2
  73. data/web/locales/en.yml +4 -0
  74. data/web/locales/es.yml +4 -3
  75. data/web/locales/fa.yml +1 -0
  76. data/web/locales/fr.yml +2 -2
  77. data/web/locales/he.yml +79 -0
  78. data/web/locales/ja.yml +9 -4
  79. data/web/locales/lt.yml +83 -0
  80. data/web/locales/pl.yml +4 -4
  81. data/web/locales/ru.yml +4 -0
  82. data/web/locales/ur.yml +80 -0
  83. data/web/locales/vi.yml +83 -0
  84. data/web/views/_footer.erb +5 -2
  85. data/web/views/_job_info.erb +2 -1
  86. data/web/views/_nav.erb +4 -18
  87. data/web/views/_paging.erb +1 -1
  88. data/web/views/busy.erb +15 -8
  89. data/web/views/dashboard.erb +1 -1
  90. data/web/views/dead.erb +2 -2
  91. data/web/views/layout.erb +12 -2
  92. data/web/views/morgue.erb +9 -6
  93. data/web/views/queue.erb +18 -8
  94. data/web/views/queues.erb +11 -1
  95. data/web/views/retries.erb +14 -7
  96. data/web/views/retry.erb +2 -2
  97. data/web/views/scheduled.erb +7 -4
  98. metadata +41 -188
  99. data/.github/issue_template.md +0 -9
  100. data/.travis.yml +0 -18
  101. data/bin/sidekiqctl +0 -99
  102. data/lib/sidekiq/core_ext.rb +0 -119
  103. data/lib/sidekiq/logging.rb +0 -106
  104. data/lib/sidekiq/middleware/server/active_record.rb +0 -13
  105. data/lib/sidekiq/middleware/server/logging.rb +0 -31
  106. data/lib/sidekiq/middleware/server/retry_jobs.rb +0 -205
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
- require 'yaml'
2
+
3
+ require "yaml"
3
4
 
4
5
  module Sidekiq
5
6
  module Extensions
7
+ SIZE_LIMIT = 8_192
8
+
6
9
  class Proxy < BasicObject
7
- def initialize(performable, target, options={})
10
+ def initialize(performable, target, options = {})
8
11
  @performable = performable
9
12
  @target = target
10
13
  @opts = options
@@ -17,9 +20,12 @@ module Sidekiq
17
20
  # to JSON and then deserialized on the other side back into a
18
21
  # Ruby object.
19
22
  obj = [@target, name, args]
20
- @performable.client_push({ 'class' => @performable, 'args' => [::YAML.dump(obj)] }.merge(@opts))
23
+ marshalled = ::YAML.dump(obj)
24
+ if marshalled.size > SIZE_LIMIT
25
+ ::Sidekiq.logger.warn { "#{@target}.#{name} job argument is #{marshalled.bytesize} bytes, you should refactor it to reduce the size" }
26
+ end
27
+ @performable.client_push({"class" => @performable, "args" => [marshalled]}.merge(@opts))
21
28
  end
22
29
  end
23
-
24
30
  end
25
31
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
- require 'sidekiq'
2
+
3
+ require "sidekiq"
3
4
 
4
5
  module Sidekiq
5
6
  class BasicFetch
@@ -7,27 +8,29 @@ module Sidekiq
7
8
  # can check if the process is shutting down.
8
9
  TIMEOUT = 2
9
10
 
10
- UnitOfWork = Struct.new(:queue, :job) do
11
+ UnitOfWork = Struct.new(:queue, :job) {
11
12
  def acknowledge
12
13
  # nothing to do
13
14
  end
14
15
 
15
16
  def queue_name
16
- queue.sub(/.*queue:/, ''.freeze)
17
+ queue.delete_prefix("queue:")
17
18
  end
18
19
 
19
20
  def requeue
20
21
  Sidekiq.redis do |conn|
21
- conn.rpush("queue:#{queue_name}", job)
22
+ conn.rpush(queue, job)
22
23
  end
23
24
  end
24
- end
25
+ }
25
26
 
26
27
  def initialize(options)
27
- @strictly_ordered_queues = !!options[:strict]
28
- @queues = options[:queues].map { |q| "queue:#{q}" }
28
+ raise ArgumentError, "missing queue list" unless options[:queues]
29
+ @options = options
30
+ @strictly_ordered_queues = !!@options[:strict]
31
+ @queues = @options[:queues].map { |q| "queue:#{q}" }
29
32
  if @strictly_ordered_queues
30
- @queues = @queues.uniq
33
+ @queues.uniq!
31
34
  @queues << TIMEOUT
32
35
  end
33
36
  end
@@ -37,38 +40,20 @@ module Sidekiq
37
40
  UnitOfWork.new(*work) if work
38
41
  end
39
42
 
40
- # Creating the Redis#brpop command takes into account any
41
- # configured queue weights. By default Redis#brpop returns
42
- # data from the first queue that has pending elements. We
43
- # recreate the queue command each time we invoke Redis#brpop
44
- # to honor weights and avoid queue starvation.
45
- def queues_cmd
46
- if @strictly_ordered_queues
47
- @queues
48
- else
49
- queues = @queues.shuffle.uniq
50
- queues << TIMEOUT
51
- queues
52
- end
53
- end
54
-
55
-
56
- # By leaving this as a class method, it can be pluggable and used by the Manager actor. Making it
57
- # an instance method will make it async to the Fetcher actor
58
- def self.bulk_requeue(inprogress, options)
43
+ def bulk_requeue(inprogress, options)
59
44
  return if inprogress.empty?
60
45
 
61
46
  Sidekiq.logger.debug { "Re-queueing terminated jobs" }
62
47
  jobs_to_requeue = {}
63
48
  inprogress.each do |unit_of_work|
64
- jobs_to_requeue[unit_of_work.queue_name] ||= []
65
- jobs_to_requeue[unit_of_work.queue_name] << unit_of_work.job
49
+ jobs_to_requeue[unit_of_work.queue] ||= []
50
+ jobs_to_requeue[unit_of_work.queue] << unit_of_work.job
66
51
  end
67
52
 
68
53
  Sidekiq.redis do |conn|
69
54
  conn.pipelined do
70
55
  jobs_to_requeue.each do |queue, jobs|
71
- conn.rpush("queue:#{queue}", jobs)
56
+ conn.rpush(queue, jobs)
72
57
  end
73
58
  end
74
59
  end
@@ -77,5 +62,19 @@ module Sidekiq
77
62
  Sidekiq.logger.warn("Failed to requeue #{inprogress.size} jobs: #{ex.message}")
78
63
  end
79
64
 
65
+ # Creating the Redis#brpop command takes into account any
66
+ # configured queue weights. By default Redis#brpop returns
67
+ # data from the first queue that has pending elements. We
68
+ # recreate the queue command each time we invoke Redis#brpop
69
+ # to honor weights and avoid queue starvation.
70
+ def queues_cmd
71
+ if @strictly_ordered_queues
72
+ @queues
73
+ else
74
+ queues = @queues.shuffle!.uniq
75
+ queues << TIMEOUT
76
+ queues
77
+ end
78
+ end
80
79
  end
81
80
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sidekiq
4
+ class JobLogger
5
+ def initialize(logger = Sidekiq.logger)
6
+ @logger = logger
7
+ end
8
+
9
+ def call(item, queue)
10
+ start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
11
+ @logger.info("start")
12
+
13
+ yield
14
+
15
+ with_elapsed_time_context(start) do
16
+ @logger.info("done")
17
+ end
18
+ rescue Exception
19
+ with_elapsed_time_context(start) do
20
+ @logger.info("fail")
21
+ end
22
+
23
+ raise
24
+ end
25
+
26
+ 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
+ # If we're using a wrapper class, like ActiveJob, use the "wrapped"
39
+ # attribute to expose the underlying thing.
40
+ h = {
41
+ class: job_hash["wrapped"] || job_hash["class"],
42
+ jid: job_hash["jid"]
43
+ }
44
+ h[:bid] = job_hash["bid"] if job_hash["bid"]
45
+ h[:tags] = job_hash["tags"] if job_hash["tags"]
46
+ h
47
+ end
48
+
49
+ def with_elapsed_time_context(start, &block)
50
+ Sidekiq::Context.with(elapsed_time_context(start), &block)
51
+ end
52
+
53
+ def elapsed_time_context(start)
54
+ {elapsed: elapsed(start).to_s}
55
+ end
56
+
57
+ private
58
+
59
+ def elapsed(start)
60
+ (::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start).round(3)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,262 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq/scheduled"
4
+ require "sidekiq/api"
5
+
6
+ require "zlib"
7
+ require "base64"
8
+
9
+ module Sidekiq
10
+ ##
11
+ # Automatically retry jobs that fail in Sidekiq.
12
+ # Sidekiq's retry support assumes a typical development lifecycle:
13
+ #
14
+ # 0. Push some code changes with a bug in it.
15
+ # 1. Bug causes job processing to fail, Sidekiq's middleware captures
16
+ # the job and pushes it onto a retry queue.
17
+ # 2. Sidekiq retries jobs in the retry queue multiple times with
18
+ # an exponential delay, the job continues to fail.
19
+ # 3. After a few days, a developer deploys a fix. The job is
20
+ # reprocessed successfully.
21
+ # 4. Once retries are exhausted, Sidekiq will give up and move the
22
+ # job to the Dead Job Queue (aka morgue) where it must be dealt with
23
+ # manually in the Web UI.
24
+ # 5. After 6 months on the DJQ, Sidekiq will discard the job.
25
+ #
26
+ # A job looks like:
27
+ #
28
+ # { 'class' => 'HardWorker', 'args' => [1, 2, 'foo'], 'retry' => true }
29
+ #
30
+ # The 'retry' option also accepts a number (in place of 'true'):
31
+ #
32
+ # { 'class' => 'HardWorker', 'args' => [1, 2, 'foo'], 'retry' => 5 }
33
+ #
34
+ # The job will be retried this number of times before giving up. (If simply
35
+ # 'true', Sidekiq retries 25 times)
36
+ #
37
+ # We'll add a bit more data to the job to support retries:
38
+ #
39
+ # * 'queue' - the queue to use
40
+ # * 'retry_count' - number of times we've retried so far.
41
+ # * 'error_message' - the message from the exception
42
+ # * 'error_class' - the exception class
43
+ # * 'failed_at' - the first time it failed
44
+ # * 'retried_at' - the last time it was retried
45
+ # * 'backtrace' - the number of lines of error backtrace to store
46
+ #
47
+ # We don't store the backtrace by default as that can add a lot of overhead
48
+ # to the job and everyone is using an error service, right?
49
+ #
50
+ # The default number of retries is 25 which works out to about 3 weeks
51
+ # You can change the default maximum number of retries in your initializer:
52
+ #
53
+ # Sidekiq.options[:max_retries] = 7
54
+ #
55
+ # or limit the number of retries for a particular worker with:
56
+ #
57
+ # class MyWorker
58
+ # include Sidekiq::Worker
59
+ # sidekiq_options :retry => 10
60
+ # end
61
+ #
62
+ class JobRetry
63
+ class Handled < ::RuntimeError; end
64
+ class Skip < Handled; end
65
+
66
+ include Sidekiq::Util
67
+
68
+ DEFAULT_MAX_RETRY_ATTEMPTS = 25
69
+
70
+ def initialize(options = {})
71
+ @max_retries = Sidekiq.options.merge(options).fetch(:max_retries, DEFAULT_MAX_RETRY_ATTEMPTS)
72
+ end
73
+
74
+ # The global retry handler requires only the barest of data.
75
+ # We want to be able to retry as much as possible so we don't
76
+ # require the worker to be instantiated.
77
+ def global(jobstr, queue)
78
+ yield
79
+ rescue Handled => ex
80
+ raise ex
81
+ rescue Sidekiq::Shutdown => ey
82
+ # ignore, will be pushed back onto queue during hard_shutdown
83
+ raise ey
84
+ rescue Exception => e
85
+ # ignore, will be pushed back onto queue during hard_shutdown
86
+ raise Sidekiq::Shutdown if exception_caused_by_shutdown?(e)
87
+
88
+ msg = Sidekiq.load_json(jobstr)
89
+ if msg["retry"]
90
+ attempt_retry(nil, msg, queue, e)
91
+ else
92
+ Sidekiq.death_handlers.each do |handler|
93
+ handler.call(msg, e)
94
+ rescue => handler_ex
95
+ handle_exception(handler_ex, {context: "Error calling death handler", job: msg})
96
+ end
97
+ end
98
+
99
+ raise Handled
100
+ end
101
+
102
+ # The local retry support means that any errors that occur within
103
+ # this block can be associated with the given worker instance.
104
+ # This is required to support the `sidekiq_retries_exhausted` block.
105
+ #
106
+ # Note that any exception from the block is wrapped in the Skip
107
+ # exception so the global block does not reprocess the error. The
108
+ # Skip exception is unwrapped within Sidekiq::Processor#process before
109
+ # calling the handle_exception handlers.
110
+ def local(worker, jobstr, queue)
111
+ yield
112
+ rescue Handled => ex
113
+ raise ex
114
+ rescue Sidekiq::Shutdown => ey
115
+ # ignore, will be pushed back onto queue during hard_shutdown
116
+ raise ey
117
+ rescue Exception => e
118
+ # ignore, will be pushed back onto queue during hard_shutdown
119
+ raise Sidekiq::Shutdown if exception_caused_by_shutdown?(e)
120
+
121
+ msg = Sidekiq.load_json(jobstr)
122
+ if msg["retry"].nil?
123
+ msg["retry"] = worker.class.get_sidekiq_options["retry"]
124
+ end
125
+
126
+ raise e unless msg["retry"]
127
+ attempt_retry(worker, msg, queue, e)
128
+ # We've handled this error associated with this job, don't
129
+ # need to handle it at the global level
130
+ raise Skip
131
+ end
132
+
133
+ private
134
+
135
+ # Note that +worker+ can be nil here if an error is raised before we can
136
+ # instantiate the worker instance. All access must be guarded and
137
+ # best effort.
138
+ def attempt_retry(worker, msg, queue, exception)
139
+ max_retry_attempts = retry_attempts_from(msg["retry"], @max_retries)
140
+
141
+ msg["queue"] = (msg["retry_queue"] || queue)
142
+
143
+ m = exception_message(exception)
144
+ if m.respond_to?(:scrub!)
145
+ m.force_encoding("utf-8")
146
+ m.scrub!
147
+ end
148
+
149
+ msg["error_message"] = m
150
+ msg["error_class"] = exception.class.name
151
+ count = if msg["retry_count"]
152
+ msg["retried_at"] = Time.now.to_f
153
+ msg["retry_count"] += 1
154
+ else
155
+ msg["failed_at"] = Time.now.to_f
156
+ msg["retry_count"] = 0
157
+ end
158
+
159
+ if msg["backtrace"]
160
+ lines = if msg["backtrace"] == true
161
+ exception.backtrace
162
+ else
163
+ exception.backtrace[0...msg["backtrace"].to_i]
164
+ end
165
+
166
+ msg["error_backtrace"] = compress_backtrace(lines)
167
+ end
168
+
169
+ if count < max_retry_attempts
170
+ delay = delay_for(worker, count, exception)
171
+ # Logging here can break retries if the logging device raises ENOSPC #3979
172
+ # logger.debug { "Failure! Retry #{count} in #{delay} seconds" }
173
+ retry_at = Time.now.to_f + delay
174
+ payload = Sidekiq.dump_json(msg)
175
+ Sidekiq.redis do |conn|
176
+ conn.zadd("retry", retry_at.to_s, payload)
177
+ end
178
+ else
179
+ # Goodbye dear message, you (re)tried your best I'm sure.
180
+ retries_exhausted(worker, msg, exception)
181
+ end
182
+ end
183
+
184
+ def retries_exhausted(worker, msg, exception)
185
+ begin
186
+ block = worker&.sidekiq_retries_exhausted_block
187
+ block&.call(msg, exception)
188
+ rescue => e
189
+ handle_exception(e, {context: "Error calling retries_exhausted", job: msg})
190
+ end
191
+
192
+ send_to_morgue(msg) unless msg["dead"] == false
193
+
194
+ Sidekiq.death_handlers.each do |handler|
195
+ handler.call(msg, exception)
196
+ rescue => e
197
+ handle_exception(e, {context: "Error calling death handler", job: msg})
198
+ end
199
+ end
200
+
201
+ def send_to_morgue(msg)
202
+ logger.info { "Adding dead #{msg["class"]} job #{msg["jid"]}" }
203
+ payload = Sidekiq.dump_json(msg)
204
+ DeadSet.new.kill(payload, notify_failure: false)
205
+ end
206
+
207
+ def retry_attempts_from(msg_retry, default)
208
+ if msg_retry.is_a?(Integer)
209
+ msg_retry
210
+ else
211
+ default
212
+ end
213
+ end
214
+
215
+ def delay_for(worker, count, exception)
216
+ if worker&.sidekiq_retry_in_block
217
+ custom_retry_in = retry_in(worker, count, exception).to_i
218
+ return custom_retry_in if custom_retry_in > 0
219
+ end
220
+ seconds_to_delay(count)
221
+ end
222
+
223
+ # delayed_job uses the same basic formula
224
+ def seconds_to_delay(count)
225
+ (count**4) + 15 + (rand(30) * (count + 1))
226
+ end
227
+
228
+ def retry_in(worker, count, exception)
229
+ worker.sidekiq_retry_in_block.call(count, exception)
230
+ rescue Exception => e
231
+ handle_exception(e, {context: "Failure scheduling retry using the defined `sidekiq_retry_in` in #{worker.class.name}, falling back to default"})
232
+ nil
233
+ end
234
+
235
+ def exception_caused_by_shutdown?(e, checked_causes = [])
236
+ return false unless e.cause
237
+
238
+ # Handle circular causes
239
+ checked_causes << e.object_id
240
+ return false if checked_causes.include?(e.cause.object_id)
241
+
242
+ e.cause.instance_of?(Sidekiq::Shutdown) ||
243
+ exception_caused_by_shutdown?(e.cause, checked_causes)
244
+ end
245
+
246
+ # Extract message from exception.
247
+ # Set a default if the message raises an error
248
+ def exception_message(exception)
249
+ # App code can stuff all sorts of crazy binary data into the error message
250
+ # that won't convert to JSON.
251
+ exception.message.to_s[0, 10_000]
252
+ rescue
253
+ +"!!! ERROR MESSAGE THREW AN ERROR !!!"
254
+ end
255
+
256
+ def compress_backtrace(backtrace)
257
+ serialized = Sidekiq.dump_json(backtrace)
258
+ compressed = Zlib::Deflate.deflate(serialized)
259
+ Base64.encode64(compressed)
260
+ end
261
+ end
262
+ end