sidekiq 5.2.3 → 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 (64) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +61 -0
  3. data/.gitignore +1 -1
  4. data/.standard.yml +20 -0
  5. data/6.0-Upgrade.md +70 -0
  6. data/COMM-LICENSE +11 -9
  7. data/Changes.md +61 -0
  8. data/Ent-2.0-Upgrade.md +37 -0
  9. data/Ent-Changes.md +27 -1
  10. data/Gemfile +19 -9
  11. data/Gemfile.lock +196 -0
  12. data/Pro-5.0-Upgrade.md +25 -0
  13. data/Pro-Changes.md +19 -2
  14. data/README.md +17 -31
  15. data/Rakefile +6 -4
  16. data/bin/sidekiqload +27 -23
  17. data/bin/sidekiqmon +9 -0
  18. data/lib/generators/sidekiq/templates/worker_test.rb.erb +1 -1
  19. data/lib/generators/sidekiq/worker_generator.rb +12 -14
  20. data/lib/sidekiq.rb +56 -43
  21. data/lib/sidekiq/api.rb +138 -151
  22. data/lib/sidekiq/cli.rb +141 -206
  23. data/lib/sidekiq/client.rb +45 -46
  24. data/lib/sidekiq/delay.rb +5 -6
  25. data/lib/sidekiq/exception_handler.rb +10 -12
  26. data/lib/sidekiq/extensions/action_mailer.rb +10 -20
  27. data/lib/sidekiq/extensions/active_record.rb +9 -7
  28. data/lib/sidekiq/extensions/class_methods.rb +9 -7
  29. data/lib/sidekiq/extensions/generic_proxy.rb +4 -4
  30. data/lib/sidekiq/fetch.rb +5 -6
  31. data/lib/sidekiq/job_logger.rb +37 -7
  32. data/lib/sidekiq/job_retry.rb +55 -57
  33. data/lib/sidekiq/launcher.rb +59 -51
  34. data/lib/sidekiq/logger.rb +69 -0
  35. data/lib/sidekiq/manager.rb +7 -9
  36. data/lib/sidekiq/middleware/chain.rb +3 -2
  37. data/lib/sidekiq/middleware/i18n.rb +5 -7
  38. data/lib/sidekiq/monitor.rb +148 -0
  39. data/lib/sidekiq/paginator.rb +11 -12
  40. data/lib/sidekiq/processor.rb +68 -58
  41. data/lib/sidekiq/rails.rb +24 -29
  42. data/lib/sidekiq/redis_connection.rb +31 -37
  43. data/lib/sidekiq/scheduled.rb +17 -19
  44. data/lib/sidekiq/testing.rb +22 -23
  45. data/lib/sidekiq/testing/inline.rb +2 -1
  46. data/lib/sidekiq/util.rb +17 -14
  47. data/lib/sidekiq/version.rb +2 -1
  48. data/lib/sidekiq/web.rb +41 -49
  49. data/lib/sidekiq/web/action.rb +14 -10
  50. data/lib/sidekiq/web/application.rb +61 -58
  51. data/lib/sidekiq/web/helpers.rb +72 -66
  52. data/lib/sidekiq/web/router.rb +17 -14
  53. data/lib/sidekiq/worker.rb +134 -102
  54. data/sidekiq.gemspec +16 -18
  55. data/web/assets/javascripts/dashboard.js +2 -21
  56. data/web/assets/stylesheets/bootstrap.css +1 -1
  57. data/web/locales/ja.yml +2 -1
  58. data/web/views/queues.erb +1 -1
  59. metadata +31 -26
  60. data/.travis.yml +0 -14
  61. data/bin/sidekiqctl +0 -237
  62. data/lib/sidekiq/core_ext.rb +0 -1
  63. data/lib/sidekiq/logging.rb +0 -122
  64. data/lib/sidekiq/middleware/server/active_record.rb +0 -23
@@ -1,34 +1,35 @@
1
1
  # frozen_string_literal: true
2
+
3
+ require "sidekiq/worker"
4
+
2
5
  module Sidekiq
3
6
  class Rails < ::Rails::Engine
4
- # We need to setup this up before any application configuration which might
5
- # 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.
6
11
  #
7
- # This hook happens after `Rails::Application` is inherited within
8
- # config/application.rb and before config is touched, usually within the
9
- # class block. Definitely before config/environments/*.rb and
10
- # config/initializers/*.rb.
11
- config.before_configuration do
12
- if ::Rails::VERSION::MAJOR < 5 && defined?(::ActiveRecord)
13
- Sidekiq.server_middleware do |chain|
14
- require 'sidekiq/middleware/server/active_record'
15
- chain.add Sidekiq::Middleware::Server::ActiveRecord
16
- 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)
17
21
  end
18
22
  end
19
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.
20
30
  config.after_initialize do
21
- # This hook happens after all initializers are run, just before returning
22
- # from config/environment.rb back to sidekiq/cli.rb.
23
- # We have to add the reloader after initialize to see if cache_classes has
24
- # been turned on.
25
- #
26
- # None of this matters on the client-side, only within the Sidekiq process itself.
27
- #
28
31
  Sidekiq.configure_server do |_|
29
- if ::Rails::VERSION::MAJOR >= 5
30
- Sidekiq.options[:reloader] = Sidekiq::Rails::Reloader.new
31
- end
32
+ Sidekiq.options[:reloader] = Sidekiq::Rails::Reloader.new
32
33
  end
33
34
  end
34
35
 
@@ -47,11 +48,5 @@ module Sidekiq
47
48
  "#<Sidekiq::Rails::Reloader @app=#{@app.class.name}>"
48
49
  end
49
50
  end
50
- end if defined?(::Rails)
51
+ end
51
52
  end
52
-
53
- if defined?(::Rails) && ::Rails::VERSION::MAJOR < 4
54
- $stderr.puts("**************************************************")
55
- $stderr.puts("⛔️ WARNING: Sidekiq server is no longer supported by Rails 3.2 - please ensure your server/workers are updated")
56
- $stderr.puts("**************************************************")
57
- 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
@@ -1,14 +1,15 @@
1
1
  # frozen_string_literal: true
2
- require 'sidekiq'
3
- require 'sidekiq/util'
4
- require 'sidekiq/api'
2
+
3
+ require "sidekiq"
4
+ require "sidekiq/util"
5
+ require "sidekiq/api"
5
6
 
6
7
  module Sidekiq
7
8
  module Scheduled
8
- SETS = %w(retry schedule)
9
+ SETS = %w[retry schedule]
9
10
 
10
11
  class Enq
11
- def enqueue_jobs(now=Time.now.to_f.to_s, sorted_sets=SETS)
12
+ def enqueue_jobs(now = Time.now.to_f.to_s, sorted_sets = SETS)
12
13
  # A job's "score" in Redis is the time at which it should be processed.
13
14
  # Just check Redis for the set of jobs with a timestamp before now.
14
15
  Sidekiq.redis do |conn|
@@ -17,14 +18,14 @@ module Sidekiq
17
18
  # We need to go through the list one at a time to reduce the risk of something
18
19
  # going wrong between the time jobs are popped from the scheduled queue and when
19
20
  # they are pushed onto a work queue and losing the jobs.
20
- while job = conn.zrangebyscore(sorted_set, '-inf', now, :limit => [0, 1]).first do
21
+ while (job = conn.zrangebyscore(sorted_set, "-inf", now, limit: [0, 1]).first)
21
22
 
22
23
  # Pop item off the queue and add it to the work queue. If the job can't be popped from
23
24
  # the queue, it's because another process already popped it so we can move on to the
24
25
  # next one.
25
26
  if conn.zrem(sorted_set, job)
26
27
  Sidekiq::Client.push(Sidekiq.load_json(job))
27
- Sidekiq::Logging.logger.debug { "enqueued #{sorted_set}: #{job}" }
28
+ Sidekiq.logger.debug { "enqueued #{sorted_set}: #{job}" }
28
29
  end
29
30
  end
30
31
  end
@@ -61,26 +62,24 @@ module Sidekiq
61
62
  end
62
63
 
63
64
  def start
64
- @thread ||= safe_thread("scheduler") do
65
+ @thread ||= safe_thread("scheduler") {
65
66
  initial_wait
66
67
 
67
- while !@done
68
+ until @done
68
69
  enqueue
69
70
  wait
70
71
  end
71
72
  Sidekiq.logger.info("Scheduler exiting...")
72
- end
73
+ }
73
74
  end
74
75
 
75
76
  def enqueue
76
- begin
77
- @enq.enqueue_jobs
78
- rescue => ex
79
- # Most likely a problem with redis networking.
80
- # Punt and try again at the next interval
81
- logger.error ex.message
82
- handle_exception(ex)
83
- end
77
+ @enq.enqueue_jobs
78
+ rescue => ex
79
+ # Most likely a problem with redis networking.
80
+ # Punt and try again at the next interval
81
+ logger.error ex.message
82
+ handle_exception(ex)
84
83
  end
85
84
 
86
85
  private
@@ -168,7 +167,6 @@ module Sidekiq
168
167
  @sleeper.pop(total)
169
168
  rescue Timeout::Error
170
169
  end
171
-
172
170
  end
173
171
  end
174
172
  end
@@ -1,16 +1,16 @@
1
1
  # frozen_string_literal: true
2
- require 'securerandom'
3
- require 'sidekiq'
4
2
 
5
- module Sidekiq
3
+ require "securerandom"
4
+ require "sidekiq"
6
5
 
6
+ module Sidekiq
7
7
  class Testing
8
8
  class << self
9
9
  attr_accessor :__test_mode
10
10
 
11
11
  def __set_test_mode(mode)
12
12
  if block_given?
13
- current_mode = self.__test_mode
13
+ current_mode = __test_mode
14
14
  begin
15
15
  self.__test_mode = mode
16
16
  yield
@@ -35,19 +35,19 @@ module Sidekiq
35
35
  end
36
36
 
37
37
  def enabled?
38
- self.__test_mode != :disable
38
+ __test_mode != :disable
39
39
  end
40
40
 
41
41
  def disabled?
42
- self.__test_mode == :disable
42
+ __test_mode == :disable
43
43
  end
44
44
 
45
45
  def fake?
46
- self.__test_mode == :fake
46
+ __test_mode == :fake
47
47
  end
48
48
 
49
49
  def inline?
50
- self.__test_mode == :inline
50
+ __test_mode == :inline
51
51
  end
52
52
 
53
53
  def server_middleware
@@ -57,7 +57,7 @@ module Sidekiq
57
57
  end
58
58
 
59
59
  def constantize(str)
60
- names = str.split('::')
60
+ names = str.split("::")
61
61
  names.shift if names.empty? || names.first.empty?
62
62
 
63
63
  names.inject(Object) do |constant, name|
@@ -77,14 +77,14 @@ module Sidekiq
77
77
  if Sidekiq::Testing.fake?
78
78
  payloads.each do |job|
79
79
  job = Sidekiq.load_json(Sidekiq.dump_json(job))
80
- job.merge!('enqueued_at' => Time.now.to_f) unless job['at']
81
- Queues.push(job['queue'], job['class'], job)
80
+ job["enqueued_at"] = Time.now.to_f unless job["at"]
81
+ Queues.push(job["queue"], job["class"], job)
82
82
  end
83
83
  true
84
84
  elsif Sidekiq::Testing.inline?
85
85
  payloads.each do |job|
86
- klass = Sidekiq::Testing.constantize(job['class'])
87
- job['id'] ||= SecureRandom.hex(12)
86
+ klass = Sidekiq::Testing.constantize(job["class"])
87
+ job["id"] ||= SecureRandom.hex(12)
88
88
  job_hash = Sidekiq.load_json(Sidekiq.dump_json(job))
89
89
  klass.process_job(job_hash)
90
90
  end
@@ -255,27 +255,26 @@ module Sidekiq
255
255
  # Then I should receive a welcome email to "foo@example.com"
256
256
  #
257
257
  module ClassMethods
258
-
259
258
  # Queue for this worker
260
259
  def queue
261
- self.sidekiq_options["queue"]
260
+ get_sidekiq_options["queue"]
262
261
  end
263
262
 
264
263
  # Jobs queued for this worker
265
264
  def jobs
266
- Queues.jobs_by_worker[self.to_s]
265
+ Queues.jobs_by_worker[to_s]
267
266
  end
268
267
 
269
268
  # Clear all jobs for this worker
270
269
  def clear
271
- Queues.clear_for(queue, self.to_s)
270
+ Queues.clear_for(queue, to_s)
272
271
  end
273
272
 
274
273
  # Drain and run all jobs for this worker
275
274
  def drain
276
275
  while jobs.any?
277
276
  next_job = jobs.first
278
- Queues.delete_for(next_job["jid"], next_job["queue"], self.to_s)
277
+ Queues.delete_for(next_job["jid"], next_job["queue"], to_s)
279
278
  process_job(next_job)
280
279
  end
281
280
  end
@@ -284,16 +283,16 @@ module Sidekiq
284
283
  def perform_one
285
284
  raise(EmptyQueueError, "perform_one called with empty job queue") if jobs.empty?
286
285
  next_job = jobs.first
287
- Queues.delete_for(next_job["jid"], queue, self.to_s)
286
+ Queues.delete_for(next_job["jid"], queue, to_s)
288
287
  process_job(next_job)
289
288
  end
290
289
 
291
290
  def process_job(job)
292
291
  worker = new
293
- worker.jid = job['jid']
294
- worker.bid = job['bid'] if worker.respond_to?(:bid=)
295
- Sidekiq::Testing.server_middleware.invoke(worker, job, job['queue']) do
296
- execute_job(worker, job['args'])
292
+ worker.jid = job["jid"]
293
+ worker.bid = job["bid"] if worker.respond_to?(:bid=)
294
+ Sidekiq::Testing.server_middleware.invoke(worker, job, job["queue"]) do
295
+ execute_job(worker, job["args"])
297
296
  end
298
297
  end
299
298
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
- require 'sidekiq/testing'
2
+
3
+ require "sidekiq/testing"
3
4
 
4
5
  ##
5
6
  # The Sidekiq inline infrastructure overrides perform_async so that it
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
- require 'socket'
3
- require 'securerandom'
4
- require 'sidekiq/exception_handler'
2
+
3
+ require "socket"
4
+ require "securerandom"
5
+ require "sidekiq/exception_handler"
5
6
 
6
7
  module Sidekiq
7
8
  ##
@@ -15,13 +16,13 @@ module Sidekiq
15
16
  def watchdog(last_words)
16
17
  yield
17
18
  rescue Exception => ex
18
- handle_exception(ex, { context: last_words })
19
+ handle_exception(ex, {context: last_words})
19
20
  raise ex
20
21
  end
21
22
 
22
23
  def safe_thread(name, &block)
23
24
  Thread.new do
24
- Thread.current['sidekiq_label'] = name
25
+ Thread.current.name = name
25
26
  watchdog(name, &block)
26
27
  end
27
28
  end
@@ -34,8 +35,12 @@ module Sidekiq
34
35
  Sidekiq.redis(&block)
35
36
  end
36
37
 
38
+ def tid
39
+ Thread.current["sidekiq_tid"] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
40
+ end
41
+
37
42
  def hostname
38
- ENV['DYNO'] || Socket.gethostname
43
+ ENV["DYNO"] || Socket.gethostname
39
44
  end
40
45
 
41
46
  def process_nonce
@@ -43,22 +48,20 @@ module Sidekiq
43
48
  end
44
49
 
45
50
  def identity
46
- @@identity ||= "#{hostname}:#{$$}:#{process_nonce}"
51
+ @@identity ||= "#{hostname}:#{::Process.pid}:#{process_nonce}"
47
52
  end
48
53
 
49
- def fire_event(event, options={})
54
+ def fire_event(event, options = {})
50
55
  reverse = options[:reverse]
51
56
  reraise = options[:reraise]
52
57
 
53
58
  arr = Sidekiq.options[:lifecycle_events][event]
54
59
  arr.reverse! if reverse
55
60
  arr.each do |block|
56
- begin
57
- block.call
58
- rescue => ex
59
- handle_exception(ex, { context: "Exception during Sidekiq lifecycle event.", event: event })
60
- raise ex if reraise
61
- end
61
+ block.call
62
+ rescue => ex
63
+ handle_exception(ex, {context: "Exception during Sidekiq lifecycle event.", event: event})
64
+ raise ex if reraise
62
65
  end
63
66
  arr.clear
64
67
  end