sidekiq 3.4.1 → 7.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Changes.md +1118 -4
- data/LICENSE.txt +9 -0
- data/README.md +55 -47
- data/bin/multi_queue_bench +271 -0
- data/bin/sidekiq +26 -3
- data/bin/sidekiqload +247 -0
- data/bin/sidekiqmon +11 -0
- data/lib/generators/sidekiq/job_generator.rb +57 -0
- data/lib/generators/sidekiq/templates/{worker.rb.erb → job.rb.erb} +2 -2
- data/lib/generators/sidekiq/templates/job_spec.rb.erb +6 -0
- data/lib/generators/sidekiq/templates/job_test.rb.erb +8 -0
- data/lib/sidekiq/api.rb +714 -312
- data/lib/sidekiq/capsule.rb +130 -0
- data/lib/sidekiq/cli.rb +275 -241
- data/lib/sidekiq/client.rb +141 -110
- data/lib/sidekiq/component.rb +68 -0
- data/lib/sidekiq/config.rb +291 -0
- data/lib/sidekiq/deploy.rb +62 -0
- data/lib/sidekiq/embedded.rb +61 -0
- data/lib/sidekiq/fetch.rb +53 -121
- data/lib/sidekiq/iterable_job.rb +53 -0
- data/lib/sidekiq/job/interrupt_handler.rb +22 -0
- data/lib/sidekiq/job/iterable/active_record_enumerator.rb +53 -0
- data/lib/sidekiq/job/iterable/csv_enumerator.rb +47 -0
- data/lib/sidekiq/job/iterable/enumerators.rb +135 -0
- data/lib/sidekiq/job/iterable.rb +231 -0
- data/lib/sidekiq/job.rb +385 -0
- data/lib/sidekiq/job_logger.rb +64 -0
- data/lib/sidekiq/job_retry.rb +305 -0
- data/lib/sidekiq/job_util.rb +107 -0
- data/lib/sidekiq/launcher.rb +241 -66
- data/lib/sidekiq/logger.rb +131 -0
- data/lib/sidekiq/manager.rb +91 -192
- data/lib/sidekiq/metrics/query.rb +156 -0
- data/lib/sidekiq/metrics/shared.rb +95 -0
- data/lib/sidekiq/metrics/tracking.rb +140 -0
- data/lib/sidekiq/middleware/chain.rb +114 -56
- data/lib/sidekiq/middleware/current_attributes.rb +111 -0
- data/lib/sidekiq/middleware/i18n.rb +8 -7
- data/lib/sidekiq/middleware/modules.rb +21 -0
- data/lib/sidekiq/monitor.rb +146 -0
- data/lib/sidekiq/paginator.rb +29 -16
- data/lib/sidekiq/processor.rb +248 -112
- data/lib/sidekiq/rails.rb +61 -27
- data/lib/sidekiq/redis_client_adapter.rb +114 -0
- data/lib/sidekiq/redis_connection.rb +68 -48
- data/lib/sidekiq/ring_buffer.rb +29 -0
- data/lib/sidekiq/scheduled.rb +173 -52
- data/lib/sidekiq/sd_notify.rb +149 -0
- data/lib/sidekiq/systemd.rb +24 -0
- data/lib/sidekiq/testing/inline.rb +7 -5
- data/lib/sidekiq/testing.rb +206 -65
- data/lib/sidekiq/transaction_aware_client.rb +51 -0
- data/lib/sidekiq/version.rb +4 -1
- data/lib/sidekiq/web/action.rb +99 -0
- data/lib/sidekiq/web/application.rb +479 -0
- data/lib/sidekiq/web/csrf_protection.rb +183 -0
- data/lib/sidekiq/web/helpers.rb +415 -0
- data/lib/sidekiq/web/router.rb +104 -0
- data/lib/sidekiq/web.rb +158 -200
- data/lib/sidekiq/worker_compatibility_alias.rb +13 -0
- data/lib/sidekiq.rb +100 -132
- data/sidekiq.gemspec +27 -23
- data/web/assets/images/apple-touch-icon.png +0 -0
- data/web/assets/images/favicon.ico +0 -0
- data/web/assets/javascripts/application.js +177 -72
- data/web/assets/javascripts/base-charts.js +106 -0
- data/web/assets/javascripts/chart.min.js +13 -0
- data/web/assets/javascripts/chartjs-plugin-annotation.min.js +7 -0
- data/web/assets/javascripts/dashboard-charts.js +192 -0
- data/web/assets/javascripts/dashboard.js +37 -286
- data/web/assets/javascripts/metrics.js +298 -0
- data/web/assets/stylesheets/application-dark.css +147 -0
- data/web/assets/stylesheets/application-rtl.css +163 -0
- data/web/assets/stylesheets/application.css +228 -247
- data/web/assets/stylesheets/bootstrap-rtl.min.css +9 -0
- data/web/assets/stylesheets/bootstrap.css +4 -8
- data/web/locales/ar.yml +87 -0
- data/web/locales/cs.yml +62 -52
- data/web/locales/da.yml +60 -53
- data/web/locales/de.yml +65 -53
- data/web/locales/el.yml +43 -24
- data/web/locales/en.yml +86 -61
- data/web/locales/es.yml +70 -53
- data/web/locales/fa.yml +80 -0
- data/web/locales/fr.yml +86 -56
- data/web/locales/gd.yml +99 -0
- data/web/locales/he.yml +80 -0
- data/web/locales/hi.yml +59 -59
- data/web/locales/it.yml +53 -53
- data/web/locales/ja.yml +78 -56
- data/web/locales/ko.yml +52 -52
- data/web/locales/lt.yml +83 -0
- data/web/locales/{no.yml → nb.yml} +62 -54
- data/web/locales/nl.yml +52 -52
- data/web/locales/pl.yml +45 -45
- data/web/locales/pt-br.yml +83 -55
- data/web/locales/pt.yml +51 -51
- data/web/locales/ru.yml +68 -60
- data/web/locales/sv.yml +53 -53
- data/web/locales/ta.yml +60 -60
- data/web/locales/tr.yml +101 -0
- data/web/locales/uk.yml +77 -0
- data/web/locales/ur.yml +80 -0
- data/web/locales/vi.yml +83 -0
- data/web/locales/zh-cn.yml +43 -16
- data/web/locales/zh-tw.yml +42 -8
- data/web/views/_footer.erb +22 -9
- data/web/views/_job_info.erb +27 -6
- data/web/views/_metrics_period_select.erb +12 -0
- data/web/views/_nav.erb +8 -22
- data/web/views/_paging.erb +3 -1
- data/web/views/_poll_link.erb +4 -0
- data/web/views/_summary.erb +7 -7
- data/web/views/busy.erb +91 -31
- data/web/views/dashboard.erb +52 -22
- data/web/views/dead.erb +5 -4
- data/web/views/filtering.erb +7 -0
- data/web/views/layout.erb +19 -7
- data/web/views/metrics.erb +91 -0
- data/web/views/metrics_for_job.erb +59 -0
- data/web/views/morgue.erb +26 -20
- data/web/views/queue.erb +36 -25
- data/web/views/queues.erb +24 -7
- data/web/views/retries.erb +29 -21
- data/web/views/retry.erb +6 -5
- data/web/views/scheduled.erb +20 -17
- data/web/views/scheduled_job_info.erb +2 -1
- metadata +101 -232
- data/.gitignore +0 -12
- data/.travis.yml +0 -16
- data/3.0-Upgrade.md +0 -70
- data/COMM-LICENSE +0 -85
- data/Contributing.md +0 -32
- data/Gemfile +0 -22
- data/LICENSE +0 -9
- data/Pro-2.0-Upgrade.md +0 -138
- data/Pro-Changes.md +0 -412
- data/Rakefile +0 -9
- data/bin/sidekiqctl +0 -93
- data/lib/generators/sidekiq/templates/worker_spec.rb.erb +0 -6
- data/lib/generators/sidekiq/templates/worker_test.rb.erb +0 -8
- data/lib/generators/sidekiq/worker_generator.rb +0 -49
- data/lib/sidekiq/actor.rb +0 -39
- data/lib/sidekiq/core_ext.rb +0 -105
- data/lib/sidekiq/exception_handler.rb +0 -30
- data/lib/sidekiq/extensions/action_mailer.rb +0 -56
- data/lib/sidekiq/extensions/active_record.rb +0 -39
- data/lib/sidekiq/extensions/class_methods.rb +0 -39
- data/lib/sidekiq/extensions/generic_proxy.rb +0 -24
- data/lib/sidekiq/logging.rb +0 -104
- data/lib/sidekiq/middleware/server/active_record.rb +0 -13
- data/lib/sidekiq/middleware/server/logging.rb +0 -35
- data/lib/sidekiq/middleware/server/retry_jobs.rb +0 -206
- data/lib/sidekiq/util.rb +0 -55
- data/lib/sidekiq/web_helpers.rb +0 -234
- data/lib/sidekiq/worker.rb +0 -89
- data/test/config.yml +0 -9
- data/test/env_based_config.yml +0 -11
- data/test/fake_env.rb +0 -0
- data/test/fixtures/en.yml +0 -2
- data/test/helper.rb +0 -39
- data/test/test_api.rb +0 -494
- data/test/test_cli.rb +0 -365
- data/test/test_client.rb +0 -269
- data/test/test_exception_handler.rb +0 -55
- data/test/test_extensions.rb +0 -120
- data/test/test_fetch.rb +0 -104
- data/test/test_logging.rb +0 -34
- data/test/test_manager.rb +0 -164
- data/test/test_middleware.rb +0 -159
- data/test/test_processor.rb +0 -166
- data/test/test_redis_connection.rb +0 -127
- data/test/test_retry.rb +0 -373
- data/test/test_scheduled.rb +0 -120
- data/test/test_scheduling.rb +0 -71
- data/test/test_sidekiq.rb +0 -69
- data/test/test_testing.rb +0 -82
- data/test/test_testing_fake.rb +0 -271
- data/test/test_testing_inline.rb +0 -93
- data/test/test_web.rb +0 -594
- data/test/test_web_helpers.rb +0 -52
- data/test/test_worker_generator.rb +0 -17
- data/web/assets/images/bootstrap/glyphicons-halflings-white.png +0 -0
- data/web/assets/images/bootstrap/glyphicons-halflings.png +0 -0
- data/web/assets/images/status/active.png +0 -0
- data/web/assets/images/status/idle.png +0 -0
- data/web/assets/javascripts/locales/README.md +0 -27
- data/web/assets/javascripts/locales/jquery.timeago.ar.js +0 -96
- data/web/assets/javascripts/locales/jquery.timeago.bg.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.bs.js +0 -49
- data/web/assets/javascripts/locales/jquery.timeago.ca.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.cs.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.cy.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.da.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.de.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.el.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.en-short.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.en.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.es.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.et.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.fa.js +0 -22
- data/web/assets/javascripts/locales/jquery.timeago.fi.js +0 -28
- data/web/assets/javascripts/locales/jquery.timeago.fr-short.js +0 -16
- data/web/assets/javascripts/locales/jquery.timeago.fr.js +0 -17
- data/web/assets/javascripts/locales/jquery.timeago.he.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.hr.js +0 -49
- data/web/assets/javascripts/locales/jquery.timeago.hu.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.hy.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.id.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.it.js +0 -16
- data/web/assets/javascripts/locales/jquery.timeago.ja.js +0 -19
- data/web/assets/javascripts/locales/jquery.timeago.ko.js +0 -17
- data/web/assets/javascripts/locales/jquery.timeago.lt.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.mk.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.nl.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.no.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.pl.js +0 -31
- data/web/assets/javascripts/locales/jquery.timeago.pt-br.js +0 -16
- data/web/assets/javascripts/locales/jquery.timeago.pt.js +0 -16
- data/web/assets/javascripts/locales/jquery.timeago.ro.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.rs.js +0 -49
- data/web/assets/javascripts/locales/jquery.timeago.ru.js +0 -34
- data/web/assets/javascripts/locales/jquery.timeago.sk.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.sl.js +0 -44
- data/web/assets/javascripts/locales/jquery.timeago.sv.js +0 -18
- data/web/assets/javascripts/locales/jquery.timeago.th.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.tr.js +0 -16
- data/web/assets/javascripts/locales/jquery.timeago.uk.js +0 -34
- data/web/assets/javascripts/locales/jquery.timeago.uz.js +0 -19
- data/web/assets/javascripts/locales/jquery.timeago.zh-cn.js +0 -20
- data/web/assets/javascripts/locales/jquery.timeago.zh-tw.js +0 -20
- data/web/views/_poll.erb +0 -10
- /data/web/assets/images/{status-sd8051fd480.png → status.png} +0 -0
data/lib/sidekiq/rails.rb
CHANGED
@@ -1,38 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sidekiq/job"
|
4
|
+
require "rails"
|
5
|
+
|
1
6
|
module Sidekiq
|
2
|
-
|
3
|
-
|
7
|
+
class Rails < ::Rails::Engine
|
8
|
+
class Reloader
|
9
|
+
def initialize(app = ::Rails.application)
|
10
|
+
@app = app
|
11
|
+
end
|
4
12
|
|
5
|
-
|
6
|
-
|
7
|
-
|
13
|
+
def call
|
14
|
+
params = (::Rails::VERSION::STRING >= "7.1") ? {source: "job.sidekiq"} : {}
|
15
|
+
@app.reloader.wrap(**params) do
|
16
|
+
yield
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def inspect
|
21
|
+
"#<Sidekiq::Rails::Reloader @app=#{@app.class.name}>"
|
22
|
+
end
|
8
23
|
|
9
|
-
|
10
|
-
|
24
|
+
def to_hash
|
25
|
+
{app: @app.class.name}
|
26
|
+
end
|
11
27
|
end
|
12
28
|
|
13
|
-
|
14
|
-
|
29
|
+
# By including the Options module, we allow AJs to directly control sidekiq features
|
30
|
+
# via the *sidekiq_options* class method and, for instance, not use AJ's retry system.
|
31
|
+
# AJ retries don't show up in the Sidekiq UI Retries tab, don't save any error data, can't be
|
32
|
+
# manually retried, don't automatically die, etc.
|
33
|
+
#
|
34
|
+
# class SomeJob < ActiveJob::Base
|
35
|
+
# queue_as :default
|
36
|
+
# sidekiq_options retry: 3, backtrace: 10
|
37
|
+
# def perform
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
initializer "sidekiq.active_job_integration" do
|
41
|
+
ActiveSupport.on_load(:active_job) do
|
42
|
+
include ::Sidekiq::Job::Options unless respond_to?(:sidekiq_options)
|
43
|
+
end
|
44
|
+
end
|
15
45
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
def self.remove_delay!
|
20
|
-
@delay_removed = true
|
21
|
-
|
22
|
-
[Extensions::ActiveRecord,
|
23
|
-
Extensions::ActionMailer,
|
24
|
-
Extensions::Klass].each do |mod|
|
25
|
-
mod.module_eval do
|
26
|
-
remove_method :delay if respond_to?(:delay)
|
27
|
-
remove_method :delay_for if respond_to?(:delay_for)
|
28
|
-
remove_method :delay_until if respond_to?(:delay_until)
|
46
|
+
initializer "sidekiq.backtrace_cleaner" do
|
47
|
+
Sidekiq.configure_server do |config|
|
48
|
+
config[:backtrace_cleaner] = ->(backtrace) { ::Rails.backtrace_cleaner.clean(backtrace) }
|
29
49
|
end
|
30
50
|
end
|
31
|
-
end
|
32
51
|
|
33
|
-
|
34
|
-
|
35
|
-
|
52
|
+
# This hook happens after all initializers are run, just before returning
|
53
|
+
# from config/environment.rb back to sidekiq/cli.rb.
|
54
|
+
#
|
55
|
+
# None of this matters on the client-side, only within the Sidekiq process itself.
|
56
|
+
config.after_initialize do
|
57
|
+
Sidekiq.configure_server do |config|
|
58
|
+
config[:reloader] = Sidekiq::Rails::Reloader.new
|
59
|
+
|
60
|
+
# This is the integration code necessary so that if a job uses `Rails.logger.info "Hello"`,
|
61
|
+
# it will appear in the Sidekiq console with all of the job context.
|
62
|
+
unless ::Rails.logger == config.logger || ::ActiveSupport::Logger.logger_outputs_to?(::Rails.logger, $stdout)
|
63
|
+
if ::Rails.logger.respond_to?(:broadcast_to)
|
64
|
+
::Rails.logger.broadcast_to(config.logger)
|
65
|
+
else
|
66
|
+
::Rails.logger.extend(::ActiveSupport::Logger.broadcast(config.logger))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
36
70
|
end
|
37
|
-
end
|
71
|
+
end
|
38
72
|
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "set"
|
4
|
+
require "redis_client"
|
5
|
+
require "redis_client/decorator"
|
6
|
+
|
7
|
+
module Sidekiq
|
8
|
+
class RedisClientAdapter
|
9
|
+
BaseError = RedisClient::Error
|
10
|
+
CommandError = RedisClient::CommandError
|
11
|
+
|
12
|
+
# You can add/remove items or clear the whole thing if you don't want deprecation warnings.
|
13
|
+
DEPRECATED_COMMANDS = %i[rpoplpush zrangebyscore zrevrange zrevrangebyscore getset hmset setex setnx].to_set
|
14
|
+
|
15
|
+
module CompatMethods
|
16
|
+
def info
|
17
|
+
@client.call("INFO") { |i| i.lines(chomp: true).map { |l| l.split(":", 2) }.select { |l| l.size == 2 }.to_h }
|
18
|
+
end
|
19
|
+
|
20
|
+
def evalsha(sha, keys, argv)
|
21
|
+
@client.call("EVALSHA", sha, keys.size, *keys, *argv)
|
22
|
+
end
|
23
|
+
|
24
|
+
# this is the set of Redis commands used by Sidekiq. Not guaranteed
|
25
|
+
# to be comprehensive, we use this as a performance enhancement to
|
26
|
+
# avoid calling method_missing on most commands
|
27
|
+
USED_COMMANDS = %w[bitfield bitfield_ro del exists expire flushdb
|
28
|
+
get hdel hget hgetall hincrby hlen hmget hset hsetnx incr incrby
|
29
|
+
lindex llen lmove lpop lpush lrange lrem mget mset ping pttl
|
30
|
+
publish rpop rpush sadd scard script set sismember smembers
|
31
|
+
srem ttl type unlink zadd zcard zincrby zrange zrem
|
32
|
+
zremrangebyrank zremrangebyscore]
|
33
|
+
|
34
|
+
USED_COMMANDS.each do |name|
|
35
|
+
define_method(name) do |*args, **kwargs|
|
36
|
+
@client.call(name, *args, **kwargs)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# this allows us to use methods like `conn.hmset(...)` instead of having to use
|
43
|
+
# redis-client's native `conn.call("hmset", ...)`
|
44
|
+
def method_missing(*args, &block)
|
45
|
+
warn("[sidekiq#5788] Redis has deprecated the `#{args.first}`command, called at #{caller(1..1)}") if DEPRECATED_COMMANDS.include?(args.first)
|
46
|
+
@client.call(*args, *block)
|
47
|
+
end
|
48
|
+
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
|
49
|
+
|
50
|
+
def respond_to_missing?(name, include_private = false)
|
51
|
+
super # Appease the linter. We can't tell what is a valid command.
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
CompatClient = RedisClient::Decorator.create(CompatMethods)
|
56
|
+
|
57
|
+
class CompatClient
|
58
|
+
def config
|
59
|
+
@client.config
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialize(options)
|
64
|
+
opts = client_opts(options)
|
65
|
+
@config = if opts.key?(:sentinels)
|
66
|
+
RedisClient.sentinel(**opts)
|
67
|
+
elsif opts.key?(:nodes)
|
68
|
+
# Sidekiq does not support Redis clustering but Sidekiq Enterprise's
|
69
|
+
# rate limiters are cluster-safe so we can scale to millions
|
70
|
+
# of rate limiters using a Redis cluster. This requires the
|
71
|
+
# `redis-cluster-client` gem.
|
72
|
+
# Sidekiq::Limiter.redis = { nodes: [...] }
|
73
|
+
RedisClient.cluster(**opts)
|
74
|
+
else
|
75
|
+
RedisClient.config(**opts)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def new_client
|
80
|
+
CompatClient.new(@config.new_client)
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def client_opts(options)
|
86
|
+
opts = options.dup
|
87
|
+
|
88
|
+
if opts[:namespace]
|
89
|
+
raise ArgumentError, "Your Redis configuration uses the namespace '#{opts[:namespace]}' but this feature is no longer supported in Sidekiq 7+. See https://github.com/sidekiq/sidekiq/blob/main/docs/7.0-Upgrade.md#redis-namespace."
|
90
|
+
end
|
91
|
+
|
92
|
+
opts.delete(:size)
|
93
|
+
opts.delete(:pool_timeout)
|
94
|
+
|
95
|
+
if opts[:network_timeout]
|
96
|
+
opts[:timeout] = opts[:network_timeout]
|
97
|
+
opts.delete(:network_timeout)
|
98
|
+
end
|
99
|
+
|
100
|
+
opts[:name] = opts.delete(:master_name) if opts.key?(:master_name)
|
101
|
+
opts[:role] = opts[:role].to_sym if opts.key?(:role)
|
102
|
+
opts[:driver] = opts[:driver].to_sym if opts.key?(:driver)
|
103
|
+
|
104
|
+
# Issue #3303, redis-rb will silently retry an operation.
|
105
|
+
# This can lead to duplicate jobs if Sidekiq::Client's LPUSH
|
106
|
+
# is performed twice but I believe this is much, much rarer
|
107
|
+
# than the reconnect silently fixing a problem; we keep it
|
108
|
+
# on by default.
|
109
|
+
opts[:reconnect_attempts] ||= 1
|
110
|
+
|
111
|
+
opts
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -1,77 +1,97 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "connection_pool"
|
4
|
+
require "uri"
|
5
|
+
require "sidekiq/redis_client_adapter"
|
4
6
|
|
5
7
|
module Sidekiq
|
6
|
-
|
8
|
+
module RedisConnection
|
7
9
|
class << self
|
10
|
+
def create(options = {})
|
11
|
+
symbolized_options = deep_symbolize_keys(options)
|
12
|
+
symbolized_options[:url] ||= determine_redis_provider
|
8
13
|
|
9
|
-
|
10
|
-
options
|
14
|
+
logger = symbolized_options.delete(:logger)
|
15
|
+
logger&.info { "Sidekiq #{Sidekiq::VERSION} connecting to Redis with options #{scrub(symbolized_options)}" }
|
11
16
|
|
12
|
-
|
13
|
-
size = options[:size] || (Sidekiq.server? ? (Sidekiq.options[:concurrency] + 2) : 5)
|
14
|
-
pool_timeout = options[:pool_timeout] || 1
|
17
|
+
raise "Sidekiq 7+ does not support Redis protocol 2" if symbolized_options[:protocol] == 2
|
15
18
|
|
16
|
-
|
19
|
+
safe = !!symbolized_options.delete(:cluster_safe)
|
20
|
+
raise ":nodes not allowed, Sidekiq is not safe to run on Redis Cluster" if !safe && symbolized_options.key?(:nodes)
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
22
|
+
size = symbolized_options.delete(:size) || 5
|
23
|
+
pool_timeout = symbolized_options.delete(:pool_timeout) || 1
|
24
|
+
pool_name = symbolized_options.delete(:pool_name)
|
24
25
|
|
25
|
-
|
26
|
-
|
26
|
+
# Default timeout in redis-client is 1 second, which can be too aggressive
|
27
|
+
# if the Sidekiq process is CPU-bound. With 10-15 threads and a thread quantum of 100ms,
|
28
|
+
# it can be easy to get the occasional ReadTimeoutError. You can still provide
|
29
|
+
# a smaller timeout explicitly:
|
30
|
+
# config.redis = { url: "...", timeout: 1 }
|
31
|
+
symbolized_options[:timeout] ||= 3
|
27
32
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
Redis::Namespace.new(namespace, :redis => client)
|
32
|
-
else
|
33
|
-
client
|
33
|
+
redis_config = Sidekiq::RedisClientAdapter.new(symbolized_options)
|
34
|
+
ConnectionPool.new(timeout: pool_timeout, size: size, name: pool_name) do
|
35
|
+
redis_config.new_client
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
37
|
-
|
38
|
-
opts = options.dup
|
39
|
-
if opts[:namespace]
|
40
|
-
opts.delete(:namespace)
|
41
|
-
end
|
39
|
+
private
|
42
40
|
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
def deep_symbolize_keys(object)
|
42
|
+
case object
|
43
|
+
when Hash
|
44
|
+
object.each_with_object({}) do |(key, value), result|
|
45
|
+
result[key.to_sym] = deep_symbolize_keys(value)
|
46
|
+
end
|
47
|
+
when Array
|
48
|
+
object.map { |e| deep_symbolize_keys(e) }
|
49
|
+
else
|
50
|
+
object
|
46
51
|
end
|
47
|
-
|
48
|
-
opts[:driver] = opts[:driver] || 'ruby'
|
49
|
-
|
50
|
-
opts
|
51
52
|
end
|
52
53
|
|
53
|
-
def
|
54
|
-
# Don't log Redis AUTH password
|
54
|
+
def scrub(options)
|
55
55
|
redacted = "REDACTED"
|
56
|
-
|
56
|
+
|
57
|
+
# Deep clone so we can muck with these options all we want and exclude
|
58
|
+
# params from dump-and-load that may contain objects that Marshal is
|
59
|
+
# unable to safely dump.
|
60
|
+
keys = options.keys - [:logger, :ssl_params]
|
61
|
+
scrubbed_options = Marshal.load(Marshal.dump(options.slice(*keys)))
|
57
62
|
if scrubbed_options[:url] && (uri = URI.parse(scrubbed_options[:url])) && uri.password
|
58
63
|
uri.password = redacted
|
59
64
|
scrubbed_options[:url] = uri.to_s
|
60
65
|
end
|
61
|
-
if scrubbed_options[:password]
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
Sidekiq.logger.info("Booting Sidekiq #{Sidekiq::VERSION} with redis options #{scrubbed_options}")
|
66
|
-
else
|
67
|
-
Sidekiq.logger.debug("#{Sidekiq::NAME} client with redis options #{scrubbed_options}")
|
66
|
+
scrubbed_options[:password] = redacted if scrubbed_options[:password]
|
67
|
+
scrubbed_options[:sentinel_password] = redacted if scrubbed_options[:sentinel_password]
|
68
|
+
scrubbed_options[:sentinels]&.each do |sentinel|
|
69
|
+
sentinel[:password] = redacted if sentinel[:password]
|
68
70
|
end
|
71
|
+
scrubbed_options
|
69
72
|
end
|
70
73
|
|
71
74
|
def determine_redis_provider
|
72
|
-
|
73
|
-
|
75
|
+
# If you have this in your environment:
|
76
|
+
# MY_REDIS_URL=redis://hostname.example.com:1238/4
|
77
|
+
# then set:
|
78
|
+
# REDIS_PROVIDER=MY_REDIS_URL
|
79
|
+
# and Sidekiq will find your custom URL variable with no custom
|
80
|
+
# initialization code at all.
|
81
|
+
#
|
82
|
+
p = ENV["REDIS_PROVIDER"]
|
83
|
+
if p && p =~ /:/
|
84
|
+
raise <<~EOM
|
85
|
+
REDIS_PROVIDER should be set to the name of the variable which contains the Redis URL, not a URL itself.
|
86
|
+
Platforms like Heroku will sell addons that publish a *_URL variable. You need to tell Sidekiq with REDIS_PROVIDER, e.g.:
|
87
|
+
|
88
|
+
REDISTOGO_URL=redis://somehost.example.com:6379/4
|
89
|
+
REDIS_PROVIDER=REDISTOGO_URL
|
90
|
+
EOM
|
91
|
+
end
|
74
92
|
|
93
|
+
ENV[p.to_s] || ENV["REDIS_URL"]
|
94
|
+
end
|
75
95
|
end
|
76
96
|
end
|
77
97
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "forwardable"
|
2
|
+
|
3
|
+
module Sidekiq
|
4
|
+
class RingBuffer
|
5
|
+
include Enumerable
|
6
|
+
extend Forwardable
|
7
|
+
def_delegators :@buf, :[], :each, :size
|
8
|
+
|
9
|
+
def initialize(size, default = 0)
|
10
|
+
@size = size
|
11
|
+
@buf = Array.new(size, default)
|
12
|
+
@index = 0
|
13
|
+
end
|
14
|
+
|
15
|
+
def <<(element)
|
16
|
+
@buf[@index % @size] = element
|
17
|
+
@index += 1
|
18
|
+
element
|
19
|
+
end
|
20
|
+
|
21
|
+
def buffer
|
22
|
+
@buf
|
23
|
+
end
|
24
|
+
|
25
|
+
def reset(default = 0)
|
26
|
+
@buf.fill(default)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/sidekiq/scheduled.rb
CHANGED
@@ -1,35 +1,66 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sidekiq"
|
4
|
+
require "sidekiq/component"
|
5
5
|
|
6
6
|
module Sidekiq
|
7
7
|
module Scheduled
|
8
|
-
SETS = %w
|
8
|
+
SETS = %w[retry schedule]
|
9
9
|
|
10
10
|
class Enq
|
11
|
-
|
11
|
+
include Sidekiq::Component
|
12
|
+
|
13
|
+
LUA_ZPOPBYSCORE = <<~LUA
|
14
|
+
local key, now = KEYS[1], ARGV[1]
|
15
|
+
local jobs = redis.call("zrange", key, "-inf", now, "byscore", "limit", 0, 1)
|
16
|
+
if jobs[1] then
|
17
|
+
redis.call("zrem", key, jobs[1])
|
18
|
+
return jobs[1]
|
19
|
+
end
|
20
|
+
LUA
|
21
|
+
|
22
|
+
def initialize(container)
|
23
|
+
@config = container
|
24
|
+
@client = Sidekiq::Client.new(config: container)
|
25
|
+
@done = false
|
26
|
+
@lua_zpopbyscore_sha = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def enqueue_jobs(sorted_sets = SETS)
|
12
30
|
# A job's "score" in Redis is the time at which it should be processed.
|
13
31
|
# Just check Redis for the set of jobs with a timestamp before now.
|
14
|
-
|
32
|
+
redis do |conn|
|
15
33
|
sorted_sets.each do |sorted_set|
|
16
|
-
# Get
|
34
|
+
# Get next item in the queue with score (time to execute) <= now.
|
17
35
|
# We need to go through the list one at a time to reduce the risk of something
|
18
36
|
# going wrong between the time jobs are popped from the scheduled queue and when
|
19
37
|
# they are pushed onto a work queue and losing the jobs.
|
20
|
-
while job = conn
|
21
|
-
|
22
|
-
|
23
|
-
# the queue, it's because another process already popped it so we can move on to the
|
24
|
-
# next one.
|
25
|
-
if conn.zrem(sorted_set, job)
|
26
|
-
Sidekiq::Client.push(Sidekiq.load_json(job))
|
27
|
-
Sidekiq::Logging.logger.debug { "enqueued #{sorted_set}: #{job}" }
|
28
|
-
end
|
38
|
+
while !@done && (job = zpopbyscore(conn, keys: [sorted_set], argv: [Time.now.to_f.to_s]))
|
39
|
+
@client.push(Sidekiq.load_json(job))
|
40
|
+
logger.debug { "enqueued #{sorted_set}: #{job}" }
|
29
41
|
end
|
30
42
|
end
|
31
43
|
end
|
32
44
|
end
|
45
|
+
|
46
|
+
def terminate
|
47
|
+
@done = true
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def zpopbyscore(conn, keys: nil, argv: nil)
|
53
|
+
if @lua_zpopbyscore_sha.nil?
|
54
|
+
@lua_zpopbyscore_sha = conn.script(:load, LUA_ZPOPBYSCORE)
|
55
|
+
end
|
56
|
+
|
57
|
+
conn.call("EVALSHA", @lua_zpopbyscore_sha, keys.size, *keys, *argv)
|
58
|
+
rescue RedisClient::CommandError => e
|
59
|
+
raise unless e.message.start_with?("NOSCRIPT")
|
60
|
+
|
61
|
+
@lua_zpopbyscore_sha = nil
|
62
|
+
retry
|
63
|
+
end
|
33
64
|
end
|
34
65
|
|
35
66
|
##
|
@@ -38,37 +69,94 @@ module Sidekiq
|
|
38
69
|
# just pops the job back onto its original queue so the
|
39
70
|
# workers can pick it up like any other job.
|
40
71
|
class Poller
|
41
|
-
include
|
42
|
-
include Actor
|
72
|
+
include Sidekiq::Component
|
43
73
|
|
44
74
|
INITIAL_WAIT = 10
|
45
75
|
|
46
|
-
def initialize
|
47
|
-
@
|
76
|
+
def initialize(config)
|
77
|
+
@config = config
|
78
|
+
@enq = (config[:scheduled_enq] || Sidekiq::Scheduled::Enq).new(config)
|
79
|
+
@sleeper = ConnectionPool::TimedStack.new
|
80
|
+
@done = false
|
81
|
+
@thread = nil
|
82
|
+
@count_calls = 0
|
83
|
+
end
|
84
|
+
|
85
|
+
# Shut down this instance, will pause until the thread is dead.
|
86
|
+
def terminate
|
87
|
+
@done = true
|
88
|
+
@enq.terminate
|
89
|
+
|
90
|
+
@sleeper << 0
|
91
|
+
@thread&.value
|
48
92
|
end
|
49
93
|
|
50
|
-
def
|
51
|
-
|
52
|
-
initial_wait
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
# Most likely a problem with redis networking.
|
58
|
-
# Punt and try again at the next interval
|
59
|
-
logger.error ex.message
|
60
|
-
logger.error ex.backtrace.first
|
94
|
+
def start
|
95
|
+
@thread ||= safe_thread("scheduler") {
|
96
|
+
initial_wait
|
97
|
+
|
98
|
+
until @done
|
99
|
+
enqueue
|
100
|
+
wait
|
61
101
|
end
|
102
|
+
logger.info("Scheduler exiting...")
|
103
|
+
}
|
104
|
+
end
|
62
105
|
|
63
|
-
|
64
|
-
|
106
|
+
def enqueue
|
107
|
+
@enq.enqueue_jobs
|
108
|
+
rescue => ex
|
109
|
+
# Most likely a problem with redis networking.
|
110
|
+
# Punt and try again at the next interval
|
111
|
+
logger.error ex.message
|
112
|
+
handle_exception(ex)
|
65
113
|
end
|
66
114
|
|
67
115
|
private
|
68
116
|
|
69
|
-
|
117
|
+
def wait
|
118
|
+
@sleeper.pop(random_poll_interval)
|
119
|
+
rescue Timeout::Error
|
120
|
+
# expected
|
121
|
+
rescue => ex
|
122
|
+
# if poll_interval_average hasn't been calculated yet, we can
|
123
|
+
# raise an error trying to reach Redis.
|
124
|
+
logger.error ex.message
|
125
|
+
handle_exception(ex)
|
126
|
+
sleep 5
|
127
|
+
end
|
128
|
+
|
70
129
|
def random_poll_interval
|
71
|
-
|
130
|
+
# We want one Sidekiq process to schedule jobs every N seconds. We have M processes
|
131
|
+
# and **don't** want to coordinate.
|
132
|
+
#
|
133
|
+
# So in N*M second timespan, we want each process to schedule once. The basic loop is:
|
134
|
+
#
|
135
|
+
# * sleep a random amount within that N*M timespan
|
136
|
+
# * wake up and schedule
|
137
|
+
#
|
138
|
+
# We want to avoid one edge case: imagine a set of 2 processes, scheduling every 5 seconds,
|
139
|
+
# so N*M = 10. Each process decides to randomly sleep 8 seconds, now we've failed to meet
|
140
|
+
# that 5 second average. Thankfully each schedule cycle will sleep randomly so the next
|
141
|
+
# iteration could see each process sleep for 1 second, undercutting our average.
|
142
|
+
#
|
143
|
+
# So below 10 processes, we special case and ensure the processes sleep closer to the average.
|
144
|
+
# In the example above, each process should schedule every 10 seconds on average. We special
|
145
|
+
# case smaller clusters to add 50% so they would sleep somewhere between 5 and 15 seconds.
|
146
|
+
# As we run more processes, the scheduling interval average will approach an even spread
|
147
|
+
# between 0 and poll interval so we don't need this artificial boost.
|
148
|
+
#
|
149
|
+
count = process_count
|
150
|
+
interval = poll_interval_average(count)
|
151
|
+
|
152
|
+
if count < 10
|
153
|
+
# For small clusters, calculate a random interval that is ±50% the desired average.
|
154
|
+
interval * rand + interval.to_f / 2
|
155
|
+
else
|
156
|
+
# With 10+ processes, we should have enough randomness to get decent polling
|
157
|
+
# across the entire timespan
|
158
|
+
interval * rand
|
159
|
+
end
|
72
160
|
end
|
73
161
|
|
74
162
|
# We do our best to tune the poll interval to the size of the active Sidekiq
|
@@ -83,33 +171,66 @@ module Sidekiq
|
|
83
171
|
# all your Sidekiq processes at the same time will lead to them all polling at
|
84
172
|
# the same time: the thundering herd problem.
|
85
173
|
#
|
86
|
-
# We only do this if
|
87
|
-
def poll_interval_average
|
88
|
-
|
174
|
+
# We only do this if poll_interval_average is unset (the default).
|
175
|
+
def poll_interval_average(count)
|
176
|
+
@config[:poll_interval_average] || scaled_poll_interval(count)
|
89
177
|
end
|
90
178
|
|
91
179
|
# Calculates an average poll interval based on the number of known Sidekiq processes.
|
92
180
|
# This minimizes a single point of failure by dispersing check-ins but without taxing
|
93
181
|
# Redis if you run many Sidekiq processes.
|
94
|
-
def scaled_poll_interval
|
95
|
-
|
182
|
+
def scaled_poll_interval(process_count)
|
183
|
+
process_count * @config[:average_scheduled_poll_interval]
|
184
|
+
end
|
185
|
+
|
186
|
+
def process_count
|
187
|
+
pcount = Sidekiq.redis { |conn| conn.scard("processes") }
|
96
188
|
pcount = 1 if pcount == 0
|
97
|
-
pcount
|
189
|
+
pcount
|
98
190
|
end
|
99
191
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
192
|
+
# A copy of Sidekiq::ProcessSet#cleanup because server
|
193
|
+
# should never depend on sidekiq/api.
|
194
|
+
def cleanup
|
195
|
+
# dont run cleanup more than once per minute
|
196
|
+
return 0 unless redis { |conn| conn.set("process_cleanup", "1", "NX", "EX", "60") }
|
197
|
+
|
198
|
+
count = 0
|
199
|
+
redis do |conn|
|
200
|
+
procs = conn.sscan("processes").to_a
|
201
|
+
heartbeats = conn.pipelined { |pipeline|
|
202
|
+
procs.each do |key|
|
203
|
+
pipeline.hget(key, "info")
|
204
|
+
end
|
205
|
+
}
|
206
|
+
|
207
|
+
# the hash named key has an expiry of 60 seconds.
|
208
|
+
# if it's not found, that means the process has not reported
|
209
|
+
# in to Redis and probably died.
|
210
|
+
to_prune = procs.select.with_index { |proc, i|
|
211
|
+
heartbeats[i].nil?
|
212
|
+
}
|
213
|
+
count = conn.srem("processes", to_prune) unless to_prune.empty?
|
110
214
|
end
|
215
|
+
count
|
111
216
|
end
|
112
217
|
|
218
|
+
def initial_wait
|
219
|
+
# Have all processes sleep between 5-15 seconds. 10 seconds to give time for
|
220
|
+
# the heartbeat to register (if the poll interval is going to be calculated by the number
|
221
|
+
# of workers), and 5 random seconds to ensure they don't all hit Redis at the same time.
|
222
|
+
total = 0
|
223
|
+
total += INITIAL_WAIT unless @config[:poll_interval_average]
|
224
|
+
total += (5 * rand)
|
225
|
+
|
226
|
+
@sleeper.pop(total)
|
227
|
+
rescue Timeout::Error
|
228
|
+
ensure
|
229
|
+
# periodically clean out the `processes` set in Redis which can collect
|
230
|
+
# references to dead processes over time. The process count affects how
|
231
|
+
# often we scan for scheduled jobs.
|
232
|
+
cleanup
|
233
|
+
end
|
113
234
|
end
|
114
235
|
end
|
115
236
|
end
|