sidekiq 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 (121) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +61 -0
  3. data/.github/contributing.md +32 -0
  4. data/.github/issue_template.md +11 -0
  5. data/.gitignore +13 -0
  6. data/.standard.yml +20 -0
  7. data/3.0-Upgrade.md +70 -0
  8. data/4.0-Upgrade.md +53 -0
  9. data/5.0-Upgrade.md +56 -0
  10. data/6.0-Upgrade.md +70 -0
  11. data/COMM-LICENSE +97 -0
  12. data/Changes.md +1570 -0
  13. data/Ent-2.0-Upgrade.md +37 -0
  14. data/Ent-Changes.md +250 -0
  15. data/Gemfile +24 -0
  16. data/Gemfile.lock +196 -0
  17. data/LICENSE +9 -0
  18. data/Pro-2.0-Upgrade.md +138 -0
  19. data/Pro-3.0-Upgrade.md +44 -0
  20. data/Pro-4.0-Upgrade.md +35 -0
  21. data/Pro-5.0-Upgrade.md +25 -0
  22. data/Pro-Changes.md +768 -0
  23. data/README.md +95 -0
  24. data/Rakefile +10 -0
  25. data/bin/sidekiq +18 -0
  26. data/bin/sidekiqload +153 -0
  27. data/bin/sidekiqmon +9 -0
  28. data/code_of_conduct.md +50 -0
  29. data/lib/generators/sidekiq/templates/worker.rb.erb +9 -0
  30. data/lib/generators/sidekiq/templates/worker_spec.rb.erb +6 -0
  31. data/lib/generators/sidekiq/templates/worker_test.rb.erb +8 -0
  32. data/lib/generators/sidekiq/worker_generator.rb +47 -0
  33. data/lib/sidekiq.rb +248 -0
  34. data/lib/sidekiq/api.rb +927 -0
  35. data/lib/sidekiq/cli.rb +380 -0
  36. data/lib/sidekiq/client.rb +242 -0
  37. data/lib/sidekiq/delay.rb +41 -0
  38. data/lib/sidekiq/exception_handler.rb +27 -0
  39. data/lib/sidekiq/extensions/action_mailer.rb +47 -0
  40. data/lib/sidekiq/extensions/active_record.rb +42 -0
  41. data/lib/sidekiq/extensions/class_methods.rb +42 -0
  42. data/lib/sidekiq/extensions/generic_proxy.rb +31 -0
  43. data/lib/sidekiq/fetch.rb +80 -0
  44. data/lib/sidekiq/job_logger.rb +55 -0
  45. data/lib/sidekiq/job_retry.rb +249 -0
  46. data/lib/sidekiq/launcher.rb +181 -0
  47. data/lib/sidekiq/logger.rb +69 -0
  48. data/lib/sidekiq/manager.rb +135 -0
  49. data/lib/sidekiq/middleware/chain.rb +151 -0
  50. data/lib/sidekiq/middleware/i18n.rb +40 -0
  51. data/lib/sidekiq/monitor.rb +148 -0
  52. data/lib/sidekiq/paginator.rb +42 -0
  53. data/lib/sidekiq/processor.rb +282 -0
  54. data/lib/sidekiq/rails.rb +52 -0
  55. data/lib/sidekiq/redis_connection.rb +138 -0
  56. data/lib/sidekiq/scheduled.rb +172 -0
  57. data/lib/sidekiq/testing.rb +332 -0
  58. data/lib/sidekiq/testing/inline.rb +30 -0
  59. data/lib/sidekiq/util.rb +69 -0
  60. data/lib/sidekiq/version.rb +5 -0
  61. data/lib/sidekiq/web.rb +205 -0
  62. data/lib/sidekiq/web/action.rb +93 -0
  63. data/lib/sidekiq/web/application.rb +356 -0
  64. data/lib/sidekiq/web/helpers.rb +324 -0
  65. data/lib/sidekiq/web/router.rb +103 -0
  66. data/lib/sidekiq/worker.rb +247 -0
  67. data/sidekiq.gemspec +21 -0
  68. data/web/assets/images/favicon.ico +0 -0
  69. data/web/assets/images/logo.png +0 -0
  70. data/web/assets/images/status.png +0 -0
  71. data/web/assets/javascripts/application.js +92 -0
  72. data/web/assets/javascripts/dashboard.js +296 -0
  73. data/web/assets/stylesheets/application-rtl.css +246 -0
  74. data/web/assets/stylesheets/application.css +1144 -0
  75. data/web/assets/stylesheets/bootstrap-rtl.min.css +9 -0
  76. data/web/assets/stylesheets/bootstrap.css +5 -0
  77. data/web/locales/ar.yml +81 -0
  78. data/web/locales/cs.yml +78 -0
  79. data/web/locales/da.yml +68 -0
  80. data/web/locales/de.yml +69 -0
  81. data/web/locales/el.yml +68 -0
  82. data/web/locales/en.yml +81 -0
  83. data/web/locales/es.yml +70 -0
  84. data/web/locales/fa.yml +80 -0
  85. data/web/locales/fr.yml +78 -0
  86. data/web/locales/he.yml +79 -0
  87. data/web/locales/hi.yml +75 -0
  88. data/web/locales/it.yml +69 -0
  89. data/web/locales/ja.yml +81 -0
  90. data/web/locales/ko.yml +68 -0
  91. data/web/locales/nb.yml +77 -0
  92. data/web/locales/nl.yml +68 -0
  93. data/web/locales/pl.yml +59 -0
  94. data/web/locales/pt-br.yml +68 -0
  95. data/web/locales/pt.yml +67 -0
  96. data/web/locales/ru.yml +78 -0
  97. data/web/locales/sv.yml +68 -0
  98. data/web/locales/ta.yml +75 -0
  99. data/web/locales/uk.yml +76 -0
  100. data/web/locales/ur.yml +80 -0
  101. data/web/locales/zh-cn.yml +68 -0
  102. data/web/locales/zh-tw.yml +68 -0
  103. data/web/views/_footer.erb +20 -0
  104. data/web/views/_job_info.erb +88 -0
  105. data/web/views/_nav.erb +52 -0
  106. data/web/views/_paging.erb +23 -0
  107. data/web/views/_poll_link.erb +7 -0
  108. data/web/views/_status.erb +4 -0
  109. data/web/views/_summary.erb +40 -0
  110. data/web/views/busy.erb +98 -0
  111. data/web/views/dashboard.erb +75 -0
  112. data/web/views/dead.erb +34 -0
  113. data/web/views/layout.erb +40 -0
  114. data/web/views/morgue.erb +75 -0
  115. data/web/views/queue.erb +46 -0
  116. data/web/views/queues.erb +30 -0
  117. data/web/views/retries.erb +80 -0
  118. data/web/views/retry.erb +34 -0
  119. data/web/views/scheduled.erb +54 -0
  120. data/web/views/scheduled_job_info.erb +8 -0
  121. metadata +220 -0
@@ -0,0 +1,95 @@
1
+ Sidekiq
2
+ ==============
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/sidekiq.svg)](https://rubygems.org/gems/sidekiq)
5
+ [![Code Climate](https://codeclimate.com/github/mperham/sidekiq.svg)](https://codeclimate.com/github/mperham/sidekiq)
6
+ [![Build Status](https://circleci.com/gh/mperham/sidekiq/tree/master.svg?style=svg)](https://circleci.com/gh/mperham/sidekiq/tree/master)
7
+ [![Gitter Chat](https://badges.gitter.im/mperham/sidekiq.svg)](https://gitter.im/mperham/sidekiq)
8
+
9
+
10
+ Simple, efficient background processing for Ruby.
11
+
12
+ Sidekiq uses threads to handle many jobs at the same time in the
13
+ same process. It does not require Rails but will integrate tightly with
14
+ Rails to make background processing dead simple.
15
+
16
+ Performance
17
+ ---------------
18
+
19
+ Version | Latency | Garbage created for 10k jobs | Time to process 100k jobs | Throughput | Ruby
20
+ -----------------|------|---------|---------|------------------------|-----
21
+ Sidekiq 6.0.0 | 3 ms | 156 MB | 19 sec | **5200 jobs/sec** | MRI 2.6.3
22
+ Sidekiq 4.0.0 | 10 ms | 151 MB | 22 sec | 4500 jobs/sec |
23
+ Sidekiq 3.5.1 | 22 ms | 1257 MB | 125 sec | 800 jobs/sec |
24
+ Resque 1.25.2 | - | - | 420 sec | 240 jobs/sec |
25
+ DelayedJob 4.1.1 | - | - | 465 sec | 215 jobs/sec |
26
+
27
+ This benchmark can be found in `bin/sidekiqload` and assumes a Redis network latency of 1ms.
28
+
29
+ Requirements
30
+ -----------------
31
+
32
+ - Redis: 4.0+
33
+ - Ruby: MRI 2.5+ or JRuby 9.2+.
34
+
35
+ Sidekiq 6.0 supports Rails 5.0+ but does not require it.
36
+
37
+
38
+ Installation
39
+ -----------------
40
+
41
+ gem install sidekiq
42
+
43
+
44
+ Getting Started
45
+ -----------------
46
+
47
+ See the [Getting Started wiki page](https://github.com/mperham/sidekiq/wiki/Getting-Started) and follow the simple setup process.
48
+ You can watch [this Youtube playlist](https://www.youtube.com/playlist?list=PLjeHh2LSCFrWGT5uVjUuFKAcrcj5kSai1) to learn all about
49
+ Sidekiq and see its features in action. Here's the Web UI:
50
+
51
+ ![Web UI](https://github.com/mperham/sidekiq/raw/master/examples/web-ui.png)
52
+
53
+
54
+ Want to Upgrade?
55
+ -------------------
56
+
57
+ I also sell Sidekiq Pro and Sidekiq Enterprise, extensions to Sidekiq which provide more
58
+ features, a commercial-friendly license and allow you to support high
59
+ quality open source development all at the same time. Please see the
60
+ [Sidekiq](https://sidekiq.org/) homepage for more detail.
61
+
62
+ Subscribe to the **[quarterly newsletter](https://tinyletter.com/sidekiq)** to stay informed about the latest
63
+ features and changes to Sidekiq and its bigger siblings.
64
+
65
+
66
+ Problems?
67
+ -----------------
68
+
69
+ **Please do not directly email any Sidekiq committers with questions or problems.** A community is best served when discussions are held in public.
70
+
71
+ If you have a problem, please review the [FAQ](https://github.com/mperham/sidekiq/wiki/FAQ) and [Troubleshooting](https://github.com/mperham/sidekiq/wiki/Problems-and-Troubleshooting) wiki pages.
72
+ Searching the [issues](https://github.com/mperham/sidekiq/issues) for your problem is also a good idea.
73
+
74
+ Sidekiq Pro and Sidekiq Enterprise customers get private email support. You can purchase at https://sidekiq.org; email support@contribsys.com for help.
75
+
76
+ Useful resources:
77
+
78
+ * Product documentation is in the [wiki](https://github.com/mperham/sidekiq/wiki).
79
+ * Occasional announcements are made to the [@sidekiq](https://twitter.com/sidekiq) Twitter account.
80
+ * The [Sidekiq tag](https://stackoverflow.com/questions/tagged/sidekiq) on Stack Overflow has lots of useful Q & A.
81
+
82
+ Every Friday morning is Sidekiq happy hour: I video chat and answer questions.
83
+ See the [Sidekiq support page](https://sidekiq.org/support.html) for details.
84
+
85
+
86
+ License
87
+ -----------------
88
+
89
+ Please see [LICENSE](https://github.com/mperham/sidekiq/blob/master/LICENSE) for licensing details.
90
+
91
+
92
+ Author
93
+ -----------------
94
+
95
+ Mike Perham, [@mperham@mastodon.xyz](https://mastodon.xyz/@mperham) / [@sidekiq](https://twitter.com/sidekiq), [https://www.mikeperham.com](https://www.mikeperham.com) / [https://www.contribsys.com](https://www.contribsys.com)
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "standard/rake"
4
+
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.warning = true
7
+ test.pattern = "test/**/test_*.rb"
8
+ end
9
+
10
+ task default: [:standard, :test]
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Quiet some warnings we see when running in warning mode:
4
+ # RUBYOPT=-w bundle exec sidekiq
5
+ $TESTING = false
6
+
7
+ require_relative '../lib/sidekiq/cli'
8
+
9
+ begin
10
+ cli = Sidekiq::CLI.instance
11
+ cli.parse
12
+ cli.run
13
+ rescue => e
14
+ raise e if $DEBUG
15
+ STDERR.puts e.message
16
+ STDERR.puts e.backtrace.join("\n")
17
+ exit 1
18
+ end
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Quiet some warnings we see when running in warning mode:
4
+ # RUBYOPT=-w bundle exec sidekiq
5
+ $TESTING = false
6
+
7
+ #require 'ruby-prof'
8
+ Bundler.require(:default)
9
+
10
+ require_relative '../lib/sidekiq/cli'
11
+ require_relative '../lib/sidekiq/launcher'
12
+
13
+ include Sidekiq::Util
14
+
15
+ Sidekiq.configure_server do |config|
16
+ config.options[:concurrency] = 10
17
+ config.redis = { db: 13, port: 6380, driver: :hiredis }
18
+ config.options[:queues] << 'default'
19
+ config.logger.level = Logger::ERROR
20
+ config.average_scheduled_poll_interval = 2
21
+ config.reliable! if defined?(Sidekiq::Pro)
22
+ end
23
+
24
+ class LoadWorker
25
+ include Sidekiq::Worker
26
+ sidekiq_options retry: 1
27
+ sidekiq_retry_in do |x|
28
+ 1
29
+ end
30
+
31
+ def perform(idx, ts=nil)
32
+ puts(Time.now.to_f - ts) if ts != nil
33
+ #raise idx.to_s if idx % 100 == 1
34
+ end
35
+ end
36
+
37
+ # brew tap shopify/shopify
38
+ # brew install toxiproxy
39
+ # gem install toxiproxy
40
+ # run `toxiproxy-server` in a separate terminal window.
41
+ require 'toxiproxy'
42
+ # simulate a non-localhost network for realer-world conditions.
43
+ # adding 1ms of network latency has an ENORMOUS impact on benchmarks
44
+ Toxiproxy.populate([{
45
+ "name": "redis",
46
+ "listen": "127.0.0.1:6380",
47
+ "upstream": "127.0.0.1:6379"
48
+ }])
49
+
50
+ self_read, self_write = IO.pipe
51
+ %w(INT TERM TSTP TTIN).each do |sig|
52
+ begin
53
+ trap sig do
54
+ self_write.puts(sig)
55
+ end
56
+ rescue ArgumentError
57
+ puts "Signal #{sig} not supported"
58
+ end
59
+ end
60
+
61
+ Sidekiq.redis {|c| c.flushdb}
62
+ def handle_signal(launcher, sig)
63
+ Sidekiq.logger.debug "Got #{sig} signal"
64
+ case sig
65
+ when 'INT'
66
+ # Handle Ctrl-C in JRuby like MRI
67
+ # http://jira.codehaus.org/browse/JRUBY-4637
68
+ raise Interrupt
69
+ when 'TERM'
70
+ # Heroku sends TERM and then waits 30 seconds for process to exit.
71
+ raise Interrupt
72
+ when 'TSTP'
73
+ Sidekiq.logger.info "Received TSTP, no longer accepting new work"
74
+ launcher.quiet
75
+ when 'TTIN'
76
+ Thread.list.each do |thread|
77
+ Sidekiq.logger.warn "Thread TID-#{(thread.object_id ^ ::Process.pid).to_s(36)} #{thread['label']}"
78
+ if thread.backtrace
79
+ Sidekiq.logger.warn thread.backtrace.join("\n")
80
+ else
81
+ Sidekiq.logger.warn "<no backtrace available>"
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ def Process.rss
88
+ `ps -o rss= -p #{Process.pid}`.chomp.to_i
89
+ end
90
+
91
+ iter = 10
92
+ count = 10_000
93
+
94
+ iter.times do
95
+ arr = Array.new(count) do
96
+ []
97
+ end
98
+ count.times do |idx|
99
+ arr[idx][0] = idx
100
+ end
101
+ Sidekiq::Client.push_bulk('class' => LoadWorker, 'args' => arr)
102
+ end
103
+ Sidekiq.logger.error "Created #{count*iter} jobs"
104
+
105
+ Monitoring = Thread.new do
106
+ watchdog("monitor thread") do
107
+ while true
108
+ sleep 0.5
109
+ qsize = Sidekiq.redis do |conn|
110
+ conn.llen "queue:default"
111
+ end
112
+ total = qsize
113
+ Sidekiq.logger.error("RSS: #{Process.rss} Pending: #{total}")
114
+ if total == 0
115
+ Sidekiq.logger.error("Done, now here's the latency for three jobs")
116
+
117
+ LoadWorker.perform_async(1, Time.now.to_f)
118
+ LoadWorker.perform_async(2, Time.now.to_f)
119
+ LoadWorker.perform_async(3, Time.now.to_f)
120
+
121
+ sleep 0.2
122
+ exit(0)
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ begin
129
+ #RubyProf::exclude_threads = [ Monitoring ]
130
+ #RubyProf.start
131
+ fire_event(:startup)
132
+ Sidekiq.logger.error "Simulating 1ms of latency between Sidekiq and redis"
133
+ Toxiproxy[:redis].downstream(:latency, latency: 1).apply do
134
+ launcher = Sidekiq::Launcher.new(Sidekiq.options)
135
+ launcher.run
136
+
137
+ while readable_io = IO.select([self_read])
138
+ signal = readable_io.first[0].gets.strip
139
+ handle_signal(launcher, signal)
140
+ end
141
+ end
142
+ rescue SystemExit => e
143
+ #Sidekiq.logger.error("Profiling...")
144
+ #result = RubyProf.stop
145
+ #printer = RubyProf::GraphHtmlPrinter.new(result)
146
+ #printer.print(File.new("output.html", "w"), :min_percent => 1)
147
+ # normal
148
+ rescue => e
149
+ raise e if $DEBUG
150
+ STDERR.puts e.message
151
+ STDERR.puts e.backtrace.join("\n")
152
+ exit 1
153
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'sidekiq/monitor'
4
+
5
+ if ARGV[0] == 'status'
6
+ Sidekiq::Monitor::Status.new.display(ARGV[1])
7
+ else
8
+ Sidekiq::Monitor.print_usage
9
+ end
@@ -0,0 +1,50 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This Code of Conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting the project maintainer at mperham AT gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+
45
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
46
+ version 1.3.0, available at
47
+ [http://contributor-covenant.org/version/1/3/0/][version]
48
+
49
+ [homepage]: http://contributor-covenant.org
50
+ [version]: http://contributor-covenant.org/version/1/3/0/
@@ -0,0 +1,9 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Worker
3
+ include Sidekiq::Worker
4
+
5
+ def perform(*args)
6
+ # Do something
7
+ end
8
+ end
9
+ <% end -%>
@@ -0,0 +1,6 @@
1
+ require 'rails_helper'
2
+ <% module_namespacing do -%>
3
+ RSpec.describe <%= class_name %>Worker, type: :worker do
4
+ pending "add some examples to (or delete) #{__FILE__}"
5
+ end
6
+ <% end -%>
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+ <% module_namespacing do -%>
3
+ class <%= class_name %>WorkerTest < Minitest::Test
4
+ def test_example
5
+ skip "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
8
+ <% end -%>
@@ -0,0 +1,47 @@
1
+ require "rails/generators/named_base"
2
+
3
+ module Sidekiq
4
+ module Generators # :nodoc:
5
+ class WorkerGenerator < ::Rails::Generators::NamedBase # :nodoc:
6
+ desc "This generator creates a Sidekiq Worker in app/workers and a corresponding test"
7
+
8
+ check_class_collision suffix: "Worker"
9
+
10
+ def self.default_generator_root
11
+ File.dirname(__FILE__)
12
+ end
13
+
14
+ def create_worker_file
15
+ template "worker.rb.erb", File.join("app/workers", class_path, "#{file_name}_worker.rb")
16
+ end
17
+
18
+ def create_test_file
19
+ if defined?(RSpec)
20
+ create_worker_spec
21
+ else
22
+ create_worker_test
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def create_worker_spec
29
+ template_file = File.join(
30
+ "spec/workers",
31
+ class_path,
32
+ "#{file_name}_worker_spec.rb"
33
+ )
34
+ template "worker_spec.rb.erb", template_file
35
+ end
36
+
37
+ def create_worker_test
38
+ template_file = File.join(
39
+ "test/workers",
40
+ class_path,
41
+ "#{file_name}_worker_test.rb"
42
+ )
43
+ template "worker_test.rb.erb", template_file
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,248 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq/version"
4
+ fail "Sidekiq #{Sidekiq::VERSION} does not support Ruby versions below 2.5.0." if RUBY_PLATFORM != "java" && Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.5.0")
5
+
6
+ require "sidekiq/logger"
7
+ require "sidekiq/client"
8
+ require "sidekiq/worker"
9
+ require "sidekiq/redis_connection"
10
+ require "sidekiq/delay"
11
+
12
+ require "json"
13
+
14
+ module Sidekiq
15
+ NAME = "Sidekiq"
16
+ LICENSE = "See LICENSE and the LGPL-3.0 for licensing details."
17
+
18
+ DEFAULTS = {
19
+ queues: [],
20
+ labels: [],
21
+ concurrency: 10,
22
+ require: ".",
23
+ environment: nil,
24
+ timeout: 25,
25
+ poll_interval_average: nil,
26
+ average_scheduled_poll_interval: 5,
27
+ error_handlers: [],
28
+ death_handlers: [],
29
+ lifecycle_events: {
30
+ startup: [],
31
+ quiet: [],
32
+ shutdown: [],
33
+ heartbeat: [],
34
+ },
35
+ dead_max_jobs: 10_000,
36
+ dead_timeout_in_seconds: 180 * 24 * 60 * 60, # 6 months
37
+ reloader: proc { |&block| block.call },
38
+ }
39
+
40
+ DEFAULT_WORKER_OPTIONS = {
41
+ "retry" => true,
42
+ "queue" => "default",
43
+ }
44
+
45
+ FAKE_INFO = {
46
+ "redis_version" => "9.9.9",
47
+ "uptime_in_days" => "9999",
48
+ "connected_clients" => "9999",
49
+ "used_memory_human" => "9P",
50
+ "used_memory_peak_human" => "9P",
51
+ }
52
+
53
+ def self.❨╯°□°❩╯︵┻━┻
54
+ puts "Calm down, yo."
55
+ end
56
+
57
+ def self.options
58
+ @options ||= DEFAULTS.dup
59
+ end
60
+
61
+ def self.options=(opts)
62
+ @options = opts
63
+ end
64
+
65
+ ##
66
+ # Configuration for Sidekiq server, use like:
67
+ #
68
+ # Sidekiq.configure_server do |config|
69
+ # config.redis = { :namespace => 'myapp', :size => 25, :url => 'redis://myhost:8877/0' }
70
+ # config.server_middleware do |chain|
71
+ # chain.add MyServerHook
72
+ # end
73
+ # end
74
+ def self.configure_server
75
+ yield self if server?
76
+ end
77
+
78
+ ##
79
+ # Configuration for Sidekiq client, use like:
80
+ #
81
+ # Sidekiq.configure_client do |config|
82
+ # config.redis = { :namespace => 'myapp', :size => 1, :url => 'redis://myhost:8877/0' }
83
+ # end
84
+ def self.configure_client
85
+ yield self unless server?
86
+ end
87
+
88
+ def self.server?
89
+ defined?(Sidekiq::CLI)
90
+ end
91
+
92
+ def self.redis
93
+ raise ArgumentError, "requires a block" unless block_given?
94
+ redis_pool.with do |conn|
95
+ retryable = true
96
+ begin
97
+ yield conn
98
+ rescue Redis::CommandError => ex
99
+ # 2550 Failover can cause the server to become a replica, need
100
+ # to disconnect and reopen the socket to get back to the primary.
101
+ if retryable && ex.message =~ /READONLY/
102
+ conn.disconnect!
103
+ retryable = false
104
+ retry
105
+ end
106
+ raise
107
+ end
108
+ end
109
+ end
110
+
111
+ def self.redis_info
112
+ redis do |conn|
113
+ # admin commands can't go through redis-namespace starting
114
+ # in redis-namespace 2.0
115
+ if conn.respond_to?(:namespace)
116
+ conn.redis.info
117
+ else
118
+ conn.info
119
+ end
120
+ rescue Redis::CommandError => ex
121
+ # 2850 return fake version when INFO command has (probably) been renamed
122
+ raise unless /unknown command/.match?(ex.message)
123
+ FAKE_INFO
124
+ end
125
+ end
126
+
127
+ def self.redis_pool
128
+ @redis ||= Sidekiq::RedisConnection.create
129
+ end
130
+
131
+ def self.redis=(hash)
132
+ @redis = if hash.is_a?(ConnectionPool)
133
+ hash
134
+ else
135
+ Sidekiq::RedisConnection.create(hash)
136
+ end
137
+ end
138
+
139
+ def self.client_middleware
140
+ @client_chain ||= Middleware::Chain.new
141
+ yield @client_chain if block_given?
142
+ @client_chain
143
+ end
144
+
145
+ def self.server_middleware
146
+ @server_chain ||= default_server_middleware
147
+ yield @server_chain if block_given?
148
+ @server_chain
149
+ end
150
+
151
+ def self.default_server_middleware
152
+ Middleware::Chain.new
153
+ end
154
+
155
+ def self.default_worker_options=(hash)
156
+ # stringify
157
+ @default_worker_options = default_worker_options.merge(Hash[hash.map { |k, v| [k.to_s, v] }])
158
+ end
159
+
160
+ def self.default_worker_options
161
+ defined?(@default_worker_options) ? @default_worker_options : DEFAULT_WORKER_OPTIONS
162
+ end
163
+
164
+ ##
165
+ # Death handlers are called when all retries for a job have been exhausted and
166
+ # the job dies. It's the notification to your application
167
+ # that this job will not succeed without manual intervention.
168
+ #
169
+ # Sidekiq.configure_server do |config|
170
+ # config.death_handlers << ->(job, ex) do
171
+ # end
172
+ # end
173
+ def self.death_handlers
174
+ options[:death_handlers]
175
+ end
176
+
177
+ def self.load_json(string)
178
+ JSON.parse(string)
179
+ end
180
+
181
+ def self.dump_json(object)
182
+ JSON.generate(object)
183
+ end
184
+
185
+ def self.log_formatter
186
+ @log_formatter ||= if ENV["DYNO"]
187
+ Sidekiq::Logger::Formatters::WithoutTimestamp.new
188
+ else
189
+ Sidekiq::Logger::Formatters::Pretty.new
190
+ end
191
+ end
192
+
193
+ def self.log_formatter=(log_formatter)
194
+ @log_formatter = log_formatter
195
+ end
196
+
197
+ def self.logger
198
+ @logger ||= Sidekiq::Logger.new(STDOUT, level: Logger::INFO)
199
+ end
200
+
201
+ def self.logger=(logger)
202
+ @logger = logger
203
+ end
204
+
205
+ # How frequently Redis should be checked by a random Sidekiq process for
206
+ # scheduled and retriable jobs. Each individual process will take turns by
207
+ # waiting some multiple of this value.
208
+ #
209
+ # See sidekiq/scheduled.rb for an in-depth explanation of this value
210
+ def self.average_scheduled_poll_interval=(interval)
211
+ options[:average_scheduled_poll_interval] = interval
212
+ end
213
+
214
+ # Register a proc to handle any error which occurs within the Sidekiq process.
215
+ #
216
+ # Sidekiq.configure_server do |config|
217
+ # config.error_handlers << proc {|ex,ctx_hash| MyErrorService.notify(ex, ctx_hash) }
218
+ # end
219
+ #
220
+ # The default error handler logs errors to Sidekiq.logger.
221
+ def self.error_handlers
222
+ options[:error_handlers]
223
+ end
224
+
225
+ # Register a block to run at a point in the Sidekiq lifecycle.
226
+ # :startup, :quiet or :shutdown are valid events.
227
+ #
228
+ # Sidekiq.configure_server do |config|
229
+ # config.on(:shutdown) do
230
+ # puts "Goodbye cruel world!"
231
+ # end
232
+ # end
233
+ def self.on(event, &block)
234
+ raise ArgumentError, "Symbols only please: #{event}" unless event.is_a?(Symbol)
235
+ raise ArgumentError, "Invalid event name: #{event}" unless options[:lifecycle_events].key?(event)
236
+ options[:lifecycle_events][event] << block
237
+ end
238
+
239
+ # We are shutting down Sidekiq but what about workers that
240
+ # are working on some long job? This error is
241
+ # raised in workers that have not finished within the hard
242
+ # timeout limit. This is needed to rollback db transactions,
243
+ # otherwise Ruby's Thread#kill will commit. See #377.
244
+ # DO NOT RESCUE THIS ERROR IN YOUR WORKERS
245
+ class Shutdown < Interrupt; end
246
+ end
247
+
248
+ require "sidekiq/rails" if defined?(::Rails::Engine)