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.
- checksums.yaml +5 -5
- data/.github/ISSUE_TEMPLATE/bug_report.md +20 -0
- data/.github/workflows/ci.yml +41 -0
- data/.gitignore +2 -1
- data/.standard.yml +20 -0
- data/5.0-Upgrade.md +56 -0
- data/6.0-Upgrade.md +72 -0
- data/COMM-LICENSE +12 -10
- data/Changes.md +354 -1
- data/Ent-2.0-Upgrade.md +37 -0
- data/Ent-Changes.md +111 -3
- data/Gemfile +16 -21
- data/Gemfile.lock +192 -0
- data/LICENSE +1 -1
- data/Pro-4.0-Upgrade.md +35 -0
- data/Pro-5.0-Upgrade.md +25 -0
- data/Pro-Changes.md +181 -4
- data/README.md +19 -33
- data/Rakefile +6 -8
- data/bin/sidekiq +26 -2
- data/bin/sidekiqload +37 -34
- data/bin/sidekiqmon +8 -0
- data/lib/generators/sidekiq/templates/worker_spec.rb.erb +1 -1
- data/lib/generators/sidekiq/templates/worker_test.rb.erb +1 -1
- data/lib/generators/sidekiq/worker_generator.rb +21 -13
- data/lib/sidekiq.rb +86 -61
- data/lib/sidekiq/api.rb +320 -209
- data/lib/sidekiq/cli.rb +207 -217
- data/lib/sidekiq/client.rb +78 -51
- data/lib/sidekiq/delay.rb +41 -0
- data/lib/sidekiq/exception_handler.rb +12 -16
- data/lib/sidekiq/extensions/action_mailer.rb +13 -22
- data/lib/sidekiq/extensions/active_record.rb +13 -10
- data/lib/sidekiq/extensions/class_methods.rb +14 -11
- data/lib/sidekiq/extensions/generic_proxy.rb +10 -4
- data/lib/sidekiq/fetch.rb +29 -30
- data/lib/sidekiq/job_logger.rb +63 -0
- data/lib/sidekiq/job_retry.rb +262 -0
- data/lib/sidekiq/launcher.rb +102 -69
- data/lib/sidekiq/logger.rb +165 -0
- data/lib/sidekiq/manager.rb +16 -19
- data/lib/sidekiq/middleware/chain.rb +15 -5
- data/lib/sidekiq/middleware/i18n.rb +5 -7
- data/lib/sidekiq/monitor.rb +133 -0
- data/lib/sidekiq/paginator.rb +18 -14
- data/lib/sidekiq/processor.rb +161 -82
- data/lib/sidekiq/rails.rb +27 -100
- data/lib/sidekiq/redis_connection.rb +60 -20
- data/lib/sidekiq/scheduled.rb +61 -35
- data/lib/sidekiq/sd_notify.rb +149 -0
- data/lib/sidekiq/systemd.rb +24 -0
- data/lib/sidekiq/testing.rb +48 -28
- data/lib/sidekiq/testing/inline.rb +2 -1
- data/lib/sidekiq/util.rb +20 -16
- data/lib/sidekiq/version.rb +2 -1
- data/lib/sidekiq/web.rb +57 -57
- data/lib/sidekiq/web/action.rb +14 -14
- data/lib/sidekiq/web/application.rb +103 -84
- data/lib/sidekiq/web/csrf_protection.rb +158 -0
- data/lib/sidekiq/web/helpers.rb +126 -71
- data/lib/sidekiq/web/router.rb +18 -17
- data/lib/sidekiq/worker.rb +164 -41
- data/sidekiq.gemspec +15 -27
- data/web/assets/javascripts/application.js +25 -27
- data/web/assets/javascripts/dashboard.js +33 -37
- data/web/assets/stylesheets/application-dark.css +143 -0
- data/web/assets/stylesheets/application-rtl.css +246 -0
- data/web/assets/stylesheets/application.css +385 -10
- data/web/assets/stylesheets/bootstrap-rtl.min.css +9 -0
- data/web/assets/stylesheets/bootstrap.css +2 -2
- data/web/locales/ar.yml +81 -0
- data/web/locales/de.yml +14 -2
- data/web/locales/en.yml +4 -0
- data/web/locales/es.yml +4 -3
- data/web/locales/fa.yml +1 -0
- data/web/locales/fr.yml +2 -2
- data/web/locales/he.yml +79 -0
- data/web/locales/ja.yml +9 -4
- data/web/locales/lt.yml +83 -0
- data/web/locales/pl.yml +4 -4
- data/web/locales/ru.yml +4 -0
- data/web/locales/ur.yml +80 -0
- data/web/locales/vi.yml +83 -0
- data/web/views/_footer.erb +5 -2
- data/web/views/_job_info.erb +2 -1
- data/web/views/_nav.erb +4 -18
- data/web/views/_paging.erb +1 -1
- data/web/views/busy.erb +15 -8
- data/web/views/dashboard.erb +1 -1
- data/web/views/dead.erb +2 -2
- data/web/views/layout.erb +12 -2
- data/web/views/morgue.erb +9 -6
- data/web/views/queue.erb +18 -8
- data/web/views/queues.erb +11 -1
- data/web/views/retries.erb +14 -7
- data/web/views/retry.erb +2 -2
- data/web/views/scheduled.erb +7 -4
- metadata +41 -188
- data/.github/issue_template.md +0 -9
- data/.travis.yml +0 -18
- data/bin/sidekiqctl +0 -99
- data/lib/sidekiq/core_ext.rb +0 -119
- data/lib/sidekiq/logging.rb +0 -106
- data/lib/sidekiq/middleware/server/active_record.rb +0 -13
- data/lib/sidekiq/middleware/server/logging.rb +0 -31
- data/lib/sidekiq/middleware/server/retry_jobs.rb +0 -205
data/.github/issue_template.md
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
Ruby version:
|
2
|
-
Sidekiq / Pro / Enterprise version(s):
|
3
|
-
|
4
|
-
Please include your initializer and any error message with the full backtrace.
|
5
|
-
|
6
|
-
Are you using an old version?
|
7
|
-
Have you checked the changelog to see if your issue has been fixed in a later version?
|
8
|
-
|
9
|
-
https://github.com/mperham/sidekiq/blob/master/Changes.md
|
data/.travis.yml
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
sudo: false
|
3
|
-
cache: bundler
|
4
|
-
services:
|
5
|
-
- redis-server
|
6
|
-
before_install:
|
7
|
-
- gem install bundler
|
8
|
-
- gem update bundler
|
9
|
-
matrix:
|
10
|
-
include:
|
11
|
-
- rvm: 2.2.4
|
12
|
-
env: "PERCY_ENABLE=0"
|
13
|
-
- rvm: 2.3.0
|
14
|
-
env: "PERCY_ENABLE=0"
|
15
|
-
- rvm: 2.4.0
|
16
|
-
env: "PERCY_ENABLE=1"
|
17
|
-
- rvm: jruby-9.1.6.0
|
18
|
-
env: "PERCY_ENABLE=0"
|
data/bin/sidekiqctl
DELETED
@@ -1,99 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'fileutils'
|
4
|
-
|
5
|
-
class Sidekiqctl
|
6
|
-
DEFAULT_KILL_TIMEOUT = 10
|
7
|
-
|
8
|
-
attr_reader :stage, :pidfile, :kill_timeout
|
9
|
-
|
10
|
-
def self.print_usage
|
11
|
-
puts "#{File.basename($0)} - stop a Sidekiq process from the command line."
|
12
|
-
puts
|
13
|
-
puts "Usage: #{File.basename($0)} <command> <pidfile> <kill_timeout>"
|
14
|
-
puts " where <command> is either 'quiet' or 'stop'"
|
15
|
-
puts " <pidfile> is path to a pidfile"
|
16
|
-
puts " <kill_timeout> is number of seconds to wait until Sidekiq exits"
|
17
|
-
puts " (default: #{Sidekiqctl::DEFAULT_KILL_TIMEOUT}), after which Sidekiq will be KILL'd"
|
18
|
-
puts
|
19
|
-
puts "Be sure to set the kill_timeout LONGER than Sidekiq's -t timeout. If you want"
|
20
|
-
puts "to wait 60 seconds for jobs to finish, use `sidekiq -t 60` and `sidekiqctl stop"
|
21
|
-
puts " path_to_pidfile 61`"
|
22
|
-
puts
|
23
|
-
end
|
24
|
-
|
25
|
-
def initialize(stage, pidfile, timeout)
|
26
|
-
@stage = stage
|
27
|
-
@pidfile = pidfile
|
28
|
-
@kill_timeout = timeout
|
29
|
-
|
30
|
-
done('No pidfile given', :error) if !pidfile
|
31
|
-
done("Pidfile #{pidfile} does not exist", :warn) if !File.exist?(pidfile)
|
32
|
-
done('Invalid pidfile content', :error) if pid == 0
|
33
|
-
|
34
|
-
fetch_process
|
35
|
-
|
36
|
-
begin
|
37
|
-
send(stage)
|
38
|
-
rescue NoMethodError
|
39
|
-
done "Invalid command: #{stage}", :error
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def fetch_process
|
44
|
-
Process.kill(0, pid)
|
45
|
-
rescue Errno::ESRCH
|
46
|
-
done "Process doesn't exist", :error
|
47
|
-
# We were not allowed to send a signal, but the process must have existed
|
48
|
-
# when Process.kill() was called.
|
49
|
-
rescue Errno::EPERM
|
50
|
-
return pid
|
51
|
-
end
|
52
|
-
|
53
|
-
def done(msg, error = nil)
|
54
|
-
puts msg
|
55
|
-
exit(exit_signal(error))
|
56
|
-
end
|
57
|
-
|
58
|
-
def exit_signal(error)
|
59
|
-
(error == :error) ? 1 : 0
|
60
|
-
end
|
61
|
-
|
62
|
-
def pid
|
63
|
-
@pid ||= File.read(pidfile).to_i
|
64
|
-
end
|
65
|
-
|
66
|
-
def quiet
|
67
|
-
`kill -USR1 #{pid}`
|
68
|
-
end
|
69
|
-
|
70
|
-
def stop
|
71
|
-
`kill -TERM #{pid}`
|
72
|
-
kill_timeout.times do
|
73
|
-
begin
|
74
|
-
Process.kill(0, pid)
|
75
|
-
rescue Errno::ESRCH
|
76
|
-
FileUtils.rm_f pidfile
|
77
|
-
done 'Sidekiq shut down gracefully.'
|
78
|
-
rescue Errno::EPERM
|
79
|
-
done 'Not permitted to shut down Sidekiq.'
|
80
|
-
end
|
81
|
-
sleep 1
|
82
|
-
end
|
83
|
-
`kill -9 #{pid}`
|
84
|
-
FileUtils.rm_f pidfile
|
85
|
-
done 'Sidekiq shut down forcefully.'
|
86
|
-
end
|
87
|
-
alias_method :shutdown, :stop
|
88
|
-
end
|
89
|
-
|
90
|
-
if ARGV.length < 2
|
91
|
-
Sidekiqctl.print_usage
|
92
|
-
else
|
93
|
-
stage = ARGV[0]
|
94
|
-
pidfile = ARGV[1]
|
95
|
-
timeout = ARGV[2].to_i
|
96
|
-
timeout = Sidekiqctl::DEFAULT_KILL_TIMEOUT if timeout == 0
|
97
|
-
|
98
|
-
Sidekiqctl.new(stage, pidfile, timeout)
|
99
|
-
end
|
data/lib/sidekiq/core_ext.rb
DELETED
@@ -1,119 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
begin
|
3
|
-
require 'active_support/core_ext/class/attribute'
|
4
|
-
rescue LoadError
|
5
|
-
|
6
|
-
# A dumbed down version of ActiveSupport's
|
7
|
-
# Class#class_attribute helper.
|
8
|
-
class Class
|
9
|
-
def class_attribute(*attrs)
|
10
|
-
instance_writer = true
|
11
|
-
|
12
|
-
attrs.each do |name|
|
13
|
-
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
14
|
-
def self.#{name}() nil end
|
15
|
-
def self.#{name}?() !!#{name} end
|
16
|
-
|
17
|
-
def self.#{name}=(val)
|
18
|
-
singleton_class.class_eval do
|
19
|
-
define_method(:#{name}) { val }
|
20
|
-
end
|
21
|
-
|
22
|
-
if singleton_class?
|
23
|
-
class_eval do
|
24
|
-
def #{name}
|
25
|
-
defined?(@#{name}) ? @#{name} : singleton_class.#{name}
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
val
|
30
|
-
end
|
31
|
-
|
32
|
-
def #{name}
|
33
|
-
defined?(@#{name}) ? @#{name} : self.class.#{name}
|
34
|
-
end
|
35
|
-
|
36
|
-
def #{name}?
|
37
|
-
!!#{name}
|
38
|
-
end
|
39
|
-
RUBY
|
40
|
-
|
41
|
-
attr_writer name if instance_writer
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
def singleton_class?
|
47
|
-
ancestors.first != self
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
begin
|
53
|
-
require 'active_support/core_ext/hash/keys'
|
54
|
-
require 'active_support/core_ext/hash/deep_merge'
|
55
|
-
rescue LoadError
|
56
|
-
class Hash
|
57
|
-
def stringify_keys
|
58
|
-
keys.each do |key|
|
59
|
-
self[key.to_s] = delete(key)
|
60
|
-
end
|
61
|
-
self
|
62
|
-
end if !{}.respond_to?(:stringify_keys)
|
63
|
-
|
64
|
-
def symbolize_keys
|
65
|
-
keys.each do |key|
|
66
|
-
self[(key.to_sym rescue key) || key] = delete(key)
|
67
|
-
end
|
68
|
-
self
|
69
|
-
end if !{}.respond_to?(:symbolize_keys)
|
70
|
-
|
71
|
-
def deep_merge(other_hash, &block)
|
72
|
-
dup.deep_merge!(other_hash, &block)
|
73
|
-
end if !{}.respond_to?(:deep_merge)
|
74
|
-
|
75
|
-
def deep_merge!(other_hash, &block)
|
76
|
-
other_hash.each_pair do |k,v|
|
77
|
-
tv = self[k]
|
78
|
-
if tv.is_a?(Hash) && v.is_a?(Hash)
|
79
|
-
self[k] = tv.deep_merge(v, &block)
|
80
|
-
else
|
81
|
-
self[k] = block && tv ? block.call(k, tv, v) : v
|
82
|
-
end
|
83
|
-
end
|
84
|
-
self
|
85
|
-
end if !{}.respond_to?(:deep_merge!)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
begin
|
90
|
-
require 'active_support/core_ext/string/inflections'
|
91
|
-
rescue LoadError
|
92
|
-
class String
|
93
|
-
def constantize
|
94
|
-
names = self.split('::')
|
95
|
-
names.shift if names.empty? || names.first.empty?
|
96
|
-
|
97
|
-
constant = Object
|
98
|
-
names.each do |name|
|
99
|
-
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
|
100
|
-
end
|
101
|
-
constant
|
102
|
-
end
|
103
|
-
end if !"".respond_to?(:constantize)
|
104
|
-
end
|
105
|
-
|
106
|
-
|
107
|
-
begin
|
108
|
-
require 'active_support/core_ext/kernel/reporting'
|
109
|
-
rescue LoadError
|
110
|
-
module Kernel
|
111
|
-
module_function
|
112
|
-
def silence_warnings
|
113
|
-
old_verbose, $VERBOSE = $VERBOSE, nil
|
114
|
-
yield
|
115
|
-
ensure
|
116
|
-
$VERBOSE = old_verbose
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
data/lib/sidekiq/logging.rb
DELETED
@@ -1,106 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'time'
|
3
|
-
require 'logger'
|
4
|
-
require 'fcntl'
|
5
|
-
|
6
|
-
module Sidekiq
|
7
|
-
module Logging
|
8
|
-
|
9
|
-
class Pretty < Logger::Formatter
|
10
|
-
SPACE = " "
|
11
|
-
|
12
|
-
# Provide a call() method that returns the formatted message.
|
13
|
-
def call(severity, time, program_name, message)
|
14
|
-
"#{time.utc.iso8601(3)} #{::Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{context} #{severity}: #{message}\n"
|
15
|
-
end
|
16
|
-
|
17
|
-
def context
|
18
|
-
c = Thread.current[:sidekiq_context]
|
19
|
-
" #{c.join(SPACE)}" if c && c.any?
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class WithoutTimestamp < Pretty
|
24
|
-
def call(severity, time, program_name, message)
|
25
|
-
"#{::Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{context} #{severity}: #{message}\n"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.with_context(msg)
|
30
|
-
Thread.current[:sidekiq_context] ||= []
|
31
|
-
Thread.current[:sidekiq_context] << msg
|
32
|
-
yield
|
33
|
-
ensure
|
34
|
-
Thread.current[:sidekiq_context].pop
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.initialize_logger(log_target = STDOUT)
|
38
|
-
oldlogger = defined?(@logger) ? @logger : nil
|
39
|
-
@logger = Logger.new(log_target)
|
40
|
-
@logger.level = Logger::INFO
|
41
|
-
@logger.formatter = ENV['DYNO'] ? WithoutTimestamp.new : Pretty.new
|
42
|
-
oldlogger.close if oldlogger && !$TESTING # don't want to close testing's STDOUT logging
|
43
|
-
@logger
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.logger
|
47
|
-
defined?(@logger) ? @logger : initialize_logger
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.logger=(log)
|
51
|
-
@logger = (log ? log : Logger.new(File::NULL))
|
52
|
-
end
|
53
|
-
|
54
|
-
# This reopens ALL logfiles in the process that have been rotated
|
55
|
-
# using logrotate(8) (without copytruncate) or similar tools.
|
56
|
-
# A +File+ object is considered for reopening if it is:
|
57
|
-
# 1) opened with the O_APPEND and O_WRONLY flags
|
58
|
-
# 2) the current open file handle does not match its original open path
|
59
|
-
# 3) unbuffered (as far as userspace buffering goes, not O_SYNC)
|
60
|
-
# Returns the number of files reopened
|
61
|
-
def self.reopen_logs
|
62
|
-
to_reopen = []
|
63
|
-
append_flags = File::WRONLY | File::APPEND
|
64
|
-
|
65
|
-
ObjectSpace.each_object(File) do |fp|
|
66
|
-
begin
|
67
|
-
if !fp.closed? && fp.stat.file? && fp.sync && (fp.fcntl(Fcntl::F_GETFL) & append_flags) == append_flags
|
68
|
-
to_reopen << fp
|
69
|
-
end
|
70
|
-
rescue IOError, Errno::EBADF
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
nr = 0
|
75
|
-
to_reopen.each do |fp|
|
76
|
-
orig_st = begin
|
77
|
-
fp.stat
|
78
|
-
rescue IOError, Errno::EBADF
|
79
|
-
next
|
80
|
-
end
|
81
|
-
|
82
|
-
begin
|
83
|
-
b = File.stat(fp.path)
|
84
|
-
next if orig_st.ino == b.ino && orig_st.dev == b.dev
|
85
|
-
rescue Errno::ENOENT
|
86
|
-
end
|
87
|
-
|
88
|
-
begin
|
89
|
-
File.open(fp.path, 'a') { |tmpfp| fp.reopen(tmpfp) }
|
90
|
-
fp.sync = true
|
91
|
-
nr += 1
|
92
|
-
rescue IOError, Errno::EBADF
|
93
|
-
# not much we can do...
|
94
|
-
end
|
95
|
-
end
|
96
|
-
nr
|
97
|
-
rescue RuntimeError => ex
|
98
|
-
# RuntimeError: ObjectSpace is disabled; each_object will only work with Class, pass -X+O to enable
|
99
|
-
puts "Unable to reopen logs: #{ex.message}"
|
100
|
-
end
|
101
|
-
|
102
|
-
def logger
|
103
|
-
Sidekiq::Logging.logger
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module Sidekiq
|
2
|
-
module Middleware
|
3
|
-
module Server
|
4
|
-
class Logging
|
5
|
-
|
6
|
-
def call(worker, item, queue)
|
7
|
-
begin
|
8
|
-
start = Time.now
|
9
|
-
logger.info("start".freeze)
|
10
|
-
yield
|
11
|
-
logger.info("done: #{elapsed(start)} sec")
|
12
|
-
rescue Exception
|
13
|
-
logger.info("fail: #{elapsed(start)} sec")
|
14
|
-
raise
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def elapsed(start)
|
21
|
-
(Time.now - start).round(3)
|
22
|
-
end
|
23
|
-
|
24
|
-
def logger
|
25
|
-
Sidekiq.logger
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
@@ -1,205 +0,0 @@
|
|
1
|
-
require 'sidekiq/scheduled'
|
2
|
-
require 'sidekiq/api'
|
3
|
-
|
4
|
-
module Sidekiq
|
5
|
-
module Middleware
|
6
|
-
module Server
|
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' => 'HardWorker', 'args' => [1, 2, 'foo'], 'retry' => true }
|
26
|
-
#
|
27
|
-
# The 'retry' option also accepts a number (in place of 'true'):
|
28
|
-
#
|
29
|
-
# { 'class' => 'HardWorker', '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
|
-
# We'll add a bit more data to the job to support retries:
|
35
|
-
#
|
36
|
-
# * 'queue' - the queue to use
|
37
|
-
# * 'retry_count' - number of times we've retried so far.
|
38
|
-
# * 'error_message' - the message from the exception
|
39
|
-
# * 'error_class' - the exception class
|
40
|
-
# * 'failed_at' - the first time it failed
|
41
|
-
# * 'retried_at' - the last time it was retried
|
42
|
-
# * 'backtrace' - the number of lines of error backtrace to store
|
43
|
-
#
|
44
|
-
# We don't store the backtrace by default as that can add a lot of overhead
|
45
|
-
# to the job and everyone is using an error service, right?
|
46
|
-
#
|
47
|
-
# The default number of retry attempts is 25 which works out to about 3 weeks
|
48
|
-
# of retries. You can pass a value for the max number of retry attempts when
|
49
|
-
# adding the middleware using the options hash:
|
50
|
-
#
|
51
|
-
# Sidekiq.configure_server do |config|
|
52
|
-
# config.server_middleware do |chain|
|
53
|
-
# chain.add Sidekiq::Middleware::Server::RetryJobs, :max_retries => 7
|
54
|
-
# end
|
55
|
-
# end
|
56
|
-
#
|
57
|
-
# or limit the number of retries for a particular worker with:
|
58
|
-
#
|
59
|
-
# class MyWorker
|
60
|
-
# include Sidekiq::Worker
|
61
|
-
# sidekiq_options :retry => 10
|
62
|
-
# end
|
63
|
-
#
|
64
|
-
class RetryJobs
|
65
|
-
include Sidekiq::Util
|
66
|
-
|
67
|
-
DEFAULT_MAX_RETRY_ATTEMPTS = 25
|
68
|
-
|
69
|
-
def initialize(options = {})
|
70
|
-
@max_retries = options.fetch(:max_retries, DEFAULT_MAX_RETRY_ATTEMPTS)
|
71
|
-
end
|
72
|
-
|
73
|
-
def call(worker, msg, queue)
|
74
|
-
yield
|
75
|
-
rescue Sidekiq::Shutdown
|
76
|
-
# ignore, will be pushed back onto queue during hard_shutdown
|
77
|
-
raise
|
78
|
-
rescue Exception => e
|
79
|
-
# ignore, will be pushed back onto queue during hard_shutdown
|
80
|
-
raise Sidekiq::Shutdown if exception_caused_by_shutdown?(e)
|
81
|
-
|
82
|
-
raise e unless msg['retry']
|
83
|
-
attempt_retry(worker, msg, queue, e)
|
84
|
-
end
|
85
|
-
|
86
|
-
private
|
87
|
-
|
88
|
-
def attempt_retry(worker, msg, queue, exception)
|
89
|
-
max_retry_attempts = retry_attempts_from(msg['retry'], @max_retries)
|
90
|
-
|
91
|
-
msg['queue'] = if msg['retry_queue']
|
92
|
-
msg['retry_queue']
|
93
|
-
else
|
94
|
-
queue
|
95
|
-
end
|
96
|
-
|
97
|
-
# App code can stuff all sorts of crazy binary data into the error message
|
98
|
-
# that won't convert to JSON.
|
99
|
-
m = exception.message.to_s[0, 10_000]
|
100
|
-
if m.respond_to?(:scrub!)
|
101
|
-
m.force_encoding("utf-8")
|
102
|
-
m.scrub!
|
103
|
-
end
|
104
|
-
|
105
|
-
msg['error_message'] = m
|
106
|
-
msg['error_class'] = exception.class.name
|
107
|
-
count = if msg['retry_count']
|
108
|
-
msg['retried_at'] = Time.now.to_f
|
109
|
-
msg['retry_count'] += 1
|
110
|
-
else
|
111
|
-
msg['failed_at'] = Time.now.to_f
|
112
|
-
msg['retry_count'] = 0
|
113
|
-
end
|
114
|
-
|
115
|
-
if msg['backtrace'] == true
|
116
|
-
msg['error_backtrace'] = exception.backtrace
|
117
|
-
elsif !msg['backtrace']
|
118
|
-
# do nothing
|
119
|
-
elsif msg['backtrace'].to_i != 0
|
120
|
-
msg['error_backtrace'] = exception.backtrace[0...msg['backtrace'].to_i]
|
121
|
-
end
|
122
|
-
|
123
|
-
if count < max_retry_attempts
|
124
|
-
delay = delay_for(worker, count, exception)
|
125
|
-
logger.debug { "Failure! Retry #{count} in #{delay} seconds" }
|
126
|
-
retry_at = Time.now.to_f + delay
|
127
|
-
payload = Sidekiq.dump_json(msg)
|
128
|
-
Sidekiq.redis do |conn|
|
129
|
-
conn.zadd('retry', retry_at.to_s, payload)
|
130
|
-
end
|
131
|
-
else
|
132
|
-
# Goodbye dear message, you (re)tried your best I'm sure.
|
133
|
-
retries_exhausted(worker, msg, exception)
|
134
|
-
end
|
135
|
-
|
136
|
-
raise exception
|
137
|
-
end
|
138
|
-
|
139
|
-
def retries_exhausted(worker, msg, exception)
|
140
|
-
logger.debug { "Retries exhausted for job" }
|
141
|
-
begin
|
142
|
-
block = worker.sidekiq_retries_exhausted_block || Sidekiq.default_retries_exhausted
|
143
|
-
block.call(msg, exception) if block
|
144
|
-
rescue => e
|
145
|
-
handle_exception(e, { context: "Error calling retries_exhausted for #{worker.class}", job: msg })
|
146
|
-
end
|
147
|
-
|
148
|
-
send_to_morgue(msg) unless msg['dead'] == false
|
149
|
-
end
|
150
|
-
|
151
|
-
def send_to_morgue(msg)
|
152
|
-
Sidekiq.logger.info { "Adding dead #{msg['class']} job #{msg['jid']}" }
|
153
|
-
payload = Sidekiq.dump_json(msg)
|
154
|
-
now = Time.now.to_f
|
155
|
-
Sidekiq.redis do |conn|
|
156
|
-
conn.multi do
|
157
|
-
conn.zadd('dead', now, payload)
|
158
|
-
conn.zremrangebyscore('dead', '-inf', now - DeadSet.timeout)
|
159
|
-
conn.zremrangebyrank('dead', 0, -DeadSet.max_jobs)
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
def retry_attempts_from(msg_retry, default)
|
165
|
-
if msg_retry.is_a?(Integer)
|
166
|
-
msg_retry
|
167
|
-
else
|
168
|
-
default
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
def delay_for(worker, count, exception)
|
173
|
-
worker.sidekiq_retry_in_block? && retry_in(worker, count, exception) || seconds_to_delay(count)
|
174
|
-
end
|
175
|
-
|
176
|
-
# delayed_job uses the same basic formula
|
177
|
-
def seconds_to_delay(count)
|
178
|
-
(count ** 4) + 15 + (rand(30)*(count+1))
|
179
|
-
end
|
180
|
-
|
181
|
-
def retry_in(worker, count, exception)
|
182
|
-
begin
|
183
|
-
worker.sidekiq_retry_in_block.call(count, exception).to_i
|
184
|
-
rescue Exception => e
|
185
|
-
handle_exception(e, { context: "Failure scheduling retry using the defined `sidekiq_retry_in` in #{worker.class.name}, falling back to default" })
|
186
|
-
nil
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
def exception_caused_by_shutdown?(e, checked_causes = [])
|
191
|
-
# In Ruby 2.1.0 only, check if exception is a result of shutdown.
|
192
|
-
return false unless defined?(e.cause)
|
193
|
-
|
194
|
-
# Handle circular causes
|
195
|
-
checked_causes << e.object_id
|
196
|
-
return false if checked_causes.include?(e.cause.object_id)
|
197
|
-
|
198
|
-
e.cause.instance_of?(Sidekiq::Shutdown) ||
|
199
|
-
exception_caused_by_shutdown?(e.cause, checked_causes)
|
200
|
-
end
|
201
|
-
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|