sidekiq 5.2.7 → 6.0.0

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 (61) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -2
  3. data/.standard.yml +20 -0
  4. data/6.0-Upgrade.md +70 -0
  5. data/Changes.md +34 -0
  6. data/Ent-2.0-Upgrade.md +37 -0
  7. data/Ent-Changes.md +12 -0
  8. data/Gemfile +12 -11
  9. data/Gemfile.lock +196 -0
  10. data/Pro-5.0-Upgrade.md +25 -0
  11. data/Pro-Changes.md +12 -3
  12. data/README.md +16 -30
  13. data/Rakefile +5 -4
  14. data/bin/sidekiqload +26 -22
  15. data/bin/sidekiqmon +9 -0
  16. data/lib/generators/sidekiq/templates/worker_test.rb.erb +1 -1
  17. data/lib/generators/sidekiq/worker_generator.rb +12 -14
  18. data/lib/sidekiq.rb +53 -42
  19. data/lib/sidekiq/api.rb +138 -151
  20. data/lib/sidekiq/cli.rb +97 -162
  21. data/lib/sidekiq/client.rb +45 -46
  22. data/lib/sidekiq/delay.rb +5 -6
  23. data/lib/sidekiq/exception_handler.rb +10 -12
  24. data/lib/sidekiq/extensions/action_mailer.rb +10 -20
  25. data/lib/sidekiq/extensions/active_record.rb +9 -7
  26. data/lib/sidekiq/extensions/class_methods.rb +9 -7
  27. data/lib/sidekiq/extensions/generic_proxy.rb +4 -4
  28. data/lib/sidekiq/fetch.rb +5 -6
  29. data/lib/sidekiq/job_logger.rb +37 -7
  30. data/lib/sidekiq/job_retry.rb +45 -58
  31. data/lib/sidekiq/launcher.rb +59 -51
  32. data/lib/sidekiq/logger.rb +69 -0
  33. data/lib/sidekiq/manager.rb +7 -9
  34. data/lib/sidekiq/middleware/chain.rb +3 -2
  35. data/lib/sidekiq/middleware/i18n.rb +5 -7
  36. data/lib/sidekiq/monitor.rb +148 -0
  37. data/lib/sidekiq/paginator.rb +11 -12
  38. data/lib/sidekiq/processor.rb +52 -49
  39. data/lib/sidekiq/rails.rb +23 -29
  40. data/lib/sidekiq/redis_connection.rb +31 -37
  41. data/lib/sidekiq/scheduled.rb +17 -19
  42. data/lib/sidekiq/testing.rb +22 -23
  43. data/lib/sidekiq/testing/inline.rb +2 -1
  44. data/lib/sidekiq/util.rb +17 -14
  45. data/lib/sidekiq/version.rb +2 -1
  46. data/lib/sidekiq/web.rb +41 -49
  47. data/lib/sidekiq/web/action.rb +14 -10
  48. data/lib/sidekiq/web/application.rb +60 -57
  49. data/lib/sidekiq/web/helpers.rb +66 -67
  50. data/lib/sidekiq/web/router.rb +17 -14
  51. data/lib/sidekiq/worker.rb +124 -97
  52. data/sidekiq.gemspec +16 -16
  53. data/web/assets/javascripts/dashboard.js +2 -21
  54. data/web/locales/ja.yml +2 -1
  55. metadata +19 -29
  56. data/.travis.yml +0 -11
  57. data/bin/sidekiqctl +0 -20
  58. data/lib/sidekiq/core_ext.rb +0 -1
  59. data/lib/sidekiq/ctl.rb +0 -221
  60. data/lib/sidekiq/logging.rb +0 -122
  61. data/lib/sidekiq/middleware/server/active_record.rb +0 -23
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Sidekiq
3
4
  module Paginator
4
-
5
- def page(key, pageidx=1, page_size=25, opts=nil)
5
+ def page(key, pageidx = 1, page_size = 25, opts = nil)
6
6
  current_page = pageidx.to_i < 1 ? 1 : pageidx.to_i
7
7
  pageidx = current_page - 1
8
8
  total_size = 0
@@ -14,30 +14,29 @@ module Sidekiq
14
14
  type = conn.type(key)
15
15
 
16
16
  case type
17
- when 'zset'
17
+ when "zset"
18
18
  rev = opts && opts[:reverse]
19
- total_size, items = conn.multi do
19
+ total_size, items = conn.multi {
20
20
  conn.zcard(key)
21
21
  if rev
22
- conn.zrevrange(key, starting, ending, :with_scores => true)
22
+ conn.zrevrange(key, starting, ending, with_scores: true)
23
23
  else
24
- conn.zrange(key, starting, ending, :with_scores => true)
24
+ conn.zrange(key, starting, ending, with_scores: true)
25
25
  end
26
- end
26
+ }
27
27
  [current_page, total_size, items]
28
- when 'list'
29
- total_size, items = conn.multi do
28
+ when "list"
29
+ total_size, items = conn.multi {
30
30
  conn.llen(key)
31
31
  conn.lrange(key, starting, ending)
32
- end
32
+ }
33
33
  [current_page, total_size, items]
34
- when 'none'
34
+ when "none"
35
35
  [1, 0, []]
36
36
  else
37
37
  raise "can't page a #{type}"
38
38
  end
39
39
  end
40
40
  end
41
-
42
41
  end
43
42
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
- require 'sidekiq/util'
3
- require 'sidekiq/fetch'
4
- require 'sidekiq/job_logger'
5
- require 'sidekiq/job_retry'
6
- require 'thread'
2
+
3
+ require "sidekiq/util"
4
+ require "sidekiq/fetch"
5
+ require "sidekiq/job_logger"
6
+ require "sidekiq/job_retry"
7
7
 
8
8
  module Sidekiq
9
9
  ##
@@ -23,7 +23,6 @@ module Sidekiq
23
23
  # to replace itself and exits.
24
24
  #
25
25
  class Processor
26
-
27
26
  include Util
28
27
 
29
28
  attr_reader :thread
@@ -37,19 +36,19 @@ module Sidekiq
37
36
  @thread = nil
38
37
  @strategy = (mgr.options[:fetch] || Sidekiq::BasicFetch).new(mgr.options)
39
38
  @reloader = Sidekiq.options[:reloader]
40
- @logging = (mgr.options[:job_logger] || Sidekiq::JobLogger).new
39
+ @job_logger = (mgr.options[:job_logger] || Sidekiq::JobLogger).new
41
40
  @retrier = Sidekiq::JobRetry.new
42
41
  end
43
42
 
44
- def terminate(wait=false)
43
+ def terminate(wait = false)
45
44
  @done = true
46
- return if !@thread
45
+ return unless @thread
47
46
  @thread.value if wait
48
47
  end
49
48
 
50
- def kill(wait=false)
49
+ def kill(wait = false)
51
50
  @done = true
52
- return if !@thread
51
+ return unless @thread
53
52
  # unlike the other actors, terminate does not wait
54
53
  # for the thread to finish because we don't know how
55
54
  # long the job will take to finish. Instead we
@@ -66,16 +65,12 @@ module Sidekiq
66
65
  private unless $TESTING
67
66
 
68
67
  def run
69
- begin
70
- while !@done
71
- process_one
72
- end
73
- @mgr.processor_stopped(self)
74
- rescue Sidekiq::Shutdown
75
- @mgr.processor_stopped(self)
76
- rescue Exception => ex
77
- @mgr.processor_died(self, ex)
78
- end
68
+ process_one until @done
69
+ @mgr.processor_stopped(self)
70
+ rescue Sidekiq::Shutdown
71
+ @mgr.processor_stopped(self)
72
+ rescue Exception => ex
73
+ @mgr.processor_died(self, ex)
79
74
  end
80
75
 
81
76
  def process_one
@@ -85,14 +80,15 @@ module Sidekiq
85
80
  end
86
81
 
87
82
  def get_one
88
- begin
89
- work = @strategy.retrieve_work
90
- (logger.info { "Redis is online, #{::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - @down} sec downtime" }; @down = nil) if @down
91
- work
92
- rescue Sidekiq::Shutdown
93
- rescue => ex
94
- handle_fetch_exception(ex)
83
+ work = @strategy.retrieve_work
84
+ if @down
85
+ logger.info { "Redis is online, #{::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - @down} sec downtime" }
86
+ @down = nil
95
87
  end
88
+ work
89
+ rescue Sidekiq::Shutdown
90
+ rescue => ex
91
+ handle_fetch_exception(ex)
96
92
  end
97
93
 
98
94
  def fetch
@@ -106,7 +102,7 @@ module Sidekiq
106
102
  end
107
103
 
108
104
  def handle_fetch_exception(ex)
109
- if !@down
105
+ unless @down
110
106
  @down = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
111
107
  logger.error("Error fetching job: #{ex}")
112
108
  handle_exception(ex)
@@ -121,18 +117,18 @@ module Sidekiq
121
117
  # job structure to the Web UI
122
118
  pristine = cloned(job_hash)
123
119
 
124
- Sidekiq::Logging.with_job_hash_context(job_hash) do
120
+ @job_logger.with_job_hash_context(job_hash) do
125
121
  @retrier.global(pristine, queue) do
126
- @logging.call(job_hash, queue) do
122
+ @job_logger.call(job_hash, queue) do
127
123
  stats(pristine, queue) do
128
124
  # Rails 5 requires a Reloader to wrap code execution. In order to
129
125
  # constantize the worker and instantiate an instance, we have to call
130
126
  # the Reloader. It handles code loading, db connection management, etc.
131
127
  # Effectively this block denotes a "unit of work" to Rails.
132
128
  @reloader.call do
133
- klass = constantize(job_hash['class'])
129
+ klass = constantize(job_hash["class"])
134
130
  worker = klass.new
135
- worker.jid = job_hash['jid']
131
+ worker.jid = job_hash["jid"]
136
132
  @retrier.local(worker, pristine, queue) do
137
133
  yield worker
138
134
  end
@@ -152,39 +148,44 @@ module Sidekiq
152
148
  begin
153
149
  job_hash = Sidekiq.load_json(jobstr)
154
150
  rescue => ex
155
- handle_exception(ex, { :context => "Invalid JSON for job", :jobstr => jobstr })
151
+ handle_exception(ex, {context: "Invalid JSON for job", jobstr: jobstr})
156
152
  # we can't notify because the job isn't a valid hash payload.
157
153
  DeadSet.new.kill(jobstr, notify_failure: false)
158
154
  return work.acknowledge
159
155
  end
160
156
 
161
- ack = true
157
+ ack = false
162
158
  begin
163
159
  dispatch(job_hash, queue) do |worker|
164
160
  Sidekiq.server_middleware.invoke(worker, job_hash, queue) do
165
- execute_job(worker, cloned(job_hash['args']))
161
+ execute_job(worker, cloned(job_hash["args"]))
166
162
  end
167
163
  end
164
+ ack = true
168
165
  rescue Sidekiq::Shutdown
169
166
  # Had to force kill this job because it didn't finish
170
167
  # within the timeout. Don't acknowledge the work since
171
168
  # we didn't properly finish it.
172
- ack = false
173
169
  rescue Sidekiq::JobRetry::Handled => h
174
170
  # this is the common case: job raised error and Sidekiq::JobRetry::Handled
175
171
  # signals that we created a retry successfully. We can acknowlege the job.
176
- e = h.cause ? h.cause : h
177
- handle_exception(e, { :context => "Job raised exception", :job => job_hash, :jobstr => jobstr })
172
+ ack = true
173
+ e = h.cause || h
174
+ handle_exception(e, {context: "Job raised exception", job: job_hash, jobstr: jobstr})
178
175
  raise e
179
176
  rescue Exception => ex
180
177
  # Unexpected error! This is very bad and indicates an exception that got past
181
178
  # the retry subsystem (e.g. network partition). We won't acknowledge the job
182
179
  # so it can be rescued when using Sidekiq Pro.
183
- ack = false
184
- handle_exception(ex, { :context => "Internal exception!", :job => job_hash, :jobstr => jobstr })
180
+ handle_exception(ex, {context: "Internal exception!", job: job_hash, jobstr: jobstr})
185
181
  raise e
186
182
  ensure
187
- work.acknowledge if ack
183
+ if ack
184
+ # We don't want a shutdown signal to interrupt job acknowledgment.
185
+ Thread.handle_interrupt(Sidekiq::Shutdown => :never) do
186
+ work.acknowledge
187
+ end
188
+ end
188
189
  end
189
190
  end
190
191
 
@@ -201,12 +202,16 @@ module Sidekiq
201
202
  @lock = Mutex.new
202
203
  end
203
204
 
204
- def incr(amount=1)
205
- @lock.synchronize { @value = @value + amount }
205
+ def incr(amount = 1)
206
+ @lock.synchronize { @value += amount }
206
207
  end
207
208
 
208
209
  def reset
209
- @lock.synchronize { val = @value; @value = 0; val }
210
+ @lock.synchronize {
211
+ val = @value
212
+ @value = 0
213
+ val
214
+ }
210
215
  end
211
216
  end
212
217
 
@@ -243,8 +248,7 @@ module Sidekiq
243
248
  WORKER_STATE = SharedWorkerState.new
244
249
 
245
250
  def stats(job_hash, queue)
246
- tid = Sidekiq::Logging.tid
247
- WORKER_STATE.set(tid, {:queue => queue, :payload => job_hash, :run_at => Time.now.to_i })
251
+ WORKER_STATE.set(tid, {queue: queue, payload: job_hash, run_at: Time.now.to_i})
248
252
 
249
253
  begin
250
254
  yield
@@ -265,7 +269,7 @@ module Sidekiq
265
269
  end
266
270
 
267
271
  def constantize(str)
268
- names = str.split('::')
272
+ names = str.split("::")
269
273
  names.shift if names.empty? || names.first.empty?
270
274
 
271
275
  names.inject(Object) do |constant, name|
@@ -274,6 +278,5 @@ module Sidekiq
274
278
  constant.const_defined?(name, false) ? constant.const_get(name, false) : constant.const_missing(name)
275
279
  end
276
280
  end
277
-
278
281
  end
279
282
  end
@@ -1,35 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "sidekiq/worker"
4
+
3
5
  module Sidekiq
4
6
  class Rails < ::Rails::Engine
5
- # We need to setup this up before any application configuration which might
6
- # change Sidekiq middleware.
7
+ # By including the Options module, we allow AJs to directly control sidekiq features
8
+ # via the *sidekiq_options* class method and, for instance, not use AJ's retry system.
9
+ # AJ retries don't show up in the Sidekiq UI Retries tab, save any error data, can't be
10
+ # manually retried, don't automatically die, etc.
7
11
  #
8
- # This hook happens after `Rails::Application` is inherited within
9
- # config/application.rb and before config is touched, usually within the
10
- # class block. Definitely before config/environments/*.rb and
11
- # config/initializers/*.rb.
12
- config.before_configuration do
13
- if ::Rails::VERSION::MAJOR < 5 && defined?(::ActiveRecord)
14
- Sidekiq.server_middleware do |chain|
15
- require 'sidekiq/middleware/server/active_record'
16
- chain.add Sidekiq::Middleware::Server::ActiveRecord
17
- end
12
+ # class SomeJob < ActiveJob::Base
13
+ # queue_as :default
14
+ # sidekiq_options retry: 3, backtrace: 10
15
+ # def perform
16
+ # end
17
+ # end
18
+ initializer "sidekiq.active_job_integration" do
19
+ ActiveSupport.on_load(:active_job) do
20
+ include ::Sidekiq::Worker::Options unless respond_to?(:sidekiq_options)
18
21
  end
19
22
  end
20
23
 
24
+ # This hook happens after all initializers are run, just before returning
25
+ # from config/environment.rb back to sidekiq/cli.rb.
26
+ # We have to add the reloader after initialize to see if cache_classes has
27
+ # been turned on.
28
+ #
29
+ # None of this matters on the client-side, only within the Sidekiq process itself.
21
30
  config.after_initialize do
22
- # This hook happens after all initializers are run, just before returning
23
- # from config/environment.rb back to sidekiq/cli.rb.
24
- # We have to add the reloader after initialize to see if cache_classes has
25
- # been turned on.
26
- #
27
- # None of this matters on the client-side, only within the Sidekiq process itself.
28
- #
29
31
  Sidekiq.configure_server do |_|
30
- if ::Rails::VERSION::MAJOR >= 5
31
- Sidekiq.options[:reloader] = Sidekiq::Rails::Reloader.new
32
- end
32
+ Sidekiq.options[:reloader] = Sidekiq::Rails::Reloader.new
33
33
  end
34
34
  end
35
35
 
@@ -48,11 +48,5 @@ module Sidekiq
48
48
  "#<Sidekiq::Rails::Reloader @app=#{@app.class.name}>"
49
49
  end
50
50
  end
51
- end if defined?(::Rails)
52
- end
53
-
54
- if defined?(::Rails) && ::Rails::VERSION::MAJOR < 4
55
- $stderr.puts("**************************************************")
56
- $stderr.puts("⛔️ WARNING: Sidekiq server is no longer supported by Rails 3.2 - please ensure your server/workers are updated")
57
- $stderr.puts("**************************************************")
51
+ end
58
52
  end
@@ -1,36 +1,38 @@
1
1
  # frozen_string_literal: true
2
- require 'connection_pool'
3
- require 'redis'
4
- require 'uri'
2
+
3
+ require "connection_pool"
4
+ require "redis"
5
+ require "uri"
5
6
 
6
7
  module Sidekiq
7
8
  class RedisConnection
8
9
  class << self
9
-
10
- def create(options={})
10
+ def create(options = {})
11
11
  options.keys.each do |key|
12
12
  options[key.to_sym] = options.delete(key)
13
13
  end
14
14
 
15
- options[:id] = "Sidekiq-#{Sidekiq.server? ? "server" : "client"}-PID-#{$$}" if !options.has_key?(:id)
15
+ options[:id] = "Sidekiq-#{Sidekiq.server? ? "server" : "client"}-PID-#{::Process.pid}" unless options.key?(:id)
16
16
  options[:url] ||= determine_redis_provider
17
17
 
18
18
  size = if options[:size]
19
- options[:size]
20
- elsif Sidekiq.server?
21
- Sidekiq.options[:concurrency] + 5
22
- elsif ENV['RAILS_MAX_THREADS']
23
- Integer(ENV['RAILS_MAX_THREADS'])
24
- else
25
- 5
26
- end
19
+ options[:size]
20
+ elsif Sidekiq.server?
21
+ # Give ourselves plenty of connections. pool is lazy
22
+ # so we won't create them until we need them.
23
+ Sidekiq.options[:concurrency] + 5
24
+ elsif ENV["RAILS_MAX_THREADS"]
25
+ Integer(ENV["RAILS_MAX_THREADS"])
26
+ else
27
+ 5
28
+ end
27
29
 
28
30
  verify_sizing(size, Sidekiq.options[:concurrency]) if Sidekiq.server?
29
31
 
30
32
  pool_timeout = options[:pool_timeout] || 1
31
33
  log_info(options)
32
34
 
33
- ConnectionPool.new(:timeout => pool_timeout, :size => size) do
35
+ ConnectionPool.new(timeout: pool_timeout, size: size) do
34
36
  build_client(options)
35
37
  end
36
38
  end
@@ -46,7 +48,7 @@ module Sidekiq
46
48
  # - enterprise's leader election
47
49
  # - enterprise's cron support
48
50
  def verify_sizing(size, concurrency)
49
- raise ArgumentError, "Your Redis connection pool is too small for Sidekiq to work. Your pool has #{size} connections but must have at least #{concurrency + 2}" if size <= concurrency
51
+ raise ArgumentError, "Your Redis connection pool is too small for Sidekiq to work. Your pool has #{size} connections but must have at least #{concurrency + 2}" if size < (concurrency + 2)
50
52
  end
51
53
 
52
54
  def build_client(options)
@@ -55,8 +57,8 @@ module Sidekiq
55
57
  client = Redis.new client_opts(options)
56
58
  if namespace
57
59
  begin
58
- require 'redis/namespace'
59
- Redis::Namespace.new(namespace, :redis => client)
60
+ require "redis/namespace"
61
+ Redis::Namespace.new(namespace, redis: client)
60
62
  rescue LoadError
61
63
  Sidekiq.logger.error("Your Redis configuration uses the namespace '#{namespace}' but the redis-namespace gem is not included in the Gemfile." \
62
64
  "Add the gem to your Gemfile to continue using a namespace. Otherwise, remove the namespace parameter.")
@@ -78,7 +80,7 @@ module Sidekiq
78
80
  opts.delete(:network_timeout)
79
81
  end
80
82
 
81
- opts[:driver] ||= Redis::Connection.drivers.last || 'ruby'
83
+ opts[:driver] ||= Redis::Connection.drivers.last || "ruby"
82
84
 
83
85
  # Issue #3303, redis-rb will silently retry an operation.
84
86
  # This can lead to duplicate jobs if Sidekiq::Client's LPUSH
@@ -115,30 +117,22 @@ module Sidekiq
115
117
  # REDIS_PROVIDER=MY_REDIS_URL
116
118
  # and Sidekiq will find your custom URL variable with no custom
117
119
  # initialization code at all.
118
- p = ENV['REDIS_PROVIDER']
120
+ #
121
+ p = ENV["REDIS_PROVIDER"]
119
122
  if p && p =~ /\:/
120
- Sidekiq.logger.error <<-EOM
121
-
122
- #################################################################################
123
-
124
- REDIS_PROVIDER should be set to the **name** of the variable which contains the Redis URL, not a URL itself.
125
- Platforms like Heroku sell addons that publish a *_URL variable. You tell Sidekiq with REDIS_PROVIDER, e.g.:
123
+ raise <<~EOM
124
+ REDIS_PROVIDER should be set to the name of the variable which contains the Redis URL, not a URL itself.
125
+ Platforms like Heroku will sell addons that publish a *_URL variable. You need to tell Sidekiq with REDIS_PROVIDER, e.g.:
126
126
 
127
- REDIS_PROVIDER=REDISTOGO_URL
128
- REDISTOGO_URL=redis://somehost.example.com:6379/4
129
-
130
- Use REDIS_URL if you wish to point Sidekiq to a URL directly.
131
-
132
- This configuration error will crash starting in Sidekiq 5.3.
133
-
134
- #################################################################################
135
- EOM
127
+ REDISTOGO_URL=redis://somehost.example.com:6379/4
128
+ REDIS_PROVIDER=REDISTOGO_URL
129
+ EOM
136
130
  end
131
+
137
132
  ENV[
138
- ENV['REDIS_PROVIDER'] || 'REDIS_URL'
133
+ p || "REDIS_URL"
139
134
  ]
140
135
  end
141
-
142
136
  end
143
137
  end
144
138
  end