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/client.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "securerandom"
|
4
|
+
require "sidekiq/middleware/chain"
|
5
|
+
require "sidekiq/job_util"
|
3
6
|
|
4
7
|
module Sidekiq
|
5
8
|
class Client
|
9
|
+
include Sidekiq::JobUtil
|
6
10
|
|
7
11
|
##
|
8
12
|
# Define client-side middleware:
|
@@ -11,14 +15,13 @@ module Sidekiq
|
|
11
15
|
# client.middleware do |chain|
|
12
16
|
# chain.use MyClientMiddleware
|
13
17
|
# end
|
14
|
-
# client.push('class' => '
|
18
|
+
# client.push('class' => 'SomeJob', 'args' => [1,2,3])
|
15
19
|
#
|
16
20
|
# All client instances default to the globally-defined
|
17
21
|
# Sidekiq.client_middleware but you can change as necessary.
|
18
22
|
#
|
19
23
|
def middleware(&block)
|
20
|
-
|
21
|
-
if block_given?
|
24
|
+
if block
|
22
25
|
@chain = @chain.dup
|
23
26
|
yield @chain
|
24
27
|
end
|
@@ -27,71 +30,122 @@ module Sidekiq
|
|
27
30
|
|
28
31
|
attr_accessor :redis_pool
|
29
32
|
|
30
|
-
# Sidekiq::Client
|
31
|
-
#
|
32
|
-
# Sidekiq jobs across several Redis instances (for scalability
|
33
|
-
# reasons, e.g.)
|
33
|
+
# Sidekiq::Client is responsible for pushing job payloads to Redis.
|
34
|
+
# Requires the :pool or :config keyword argument.
|
34
35
|
#
|
35
|
-
# Sidekiq::Client.new(
|
36
|
+
# Sidekiq::Client.new(pool: Sidekiq::RedisConnection.create)
|
36
37
|
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
|
43
|
-
|
38
|
+
# Inside the Sidekiq process, you can reuse the configured resources:
|
39
|
+
#
|
40
|
+
# Sidekiq::Client.new(config: config)
|
41
|
+
#
|
42
|
+
# @param pool [ConnectionPool] explicit Redis pool to use
|
43
|
+
# @param config [Sidekiq::Config] use the pool and middleware from the given Sidekiq container
|
44
|
+
# @param chain [Sidekiq::Middleware::Chain] use the given middleware chain
|
45
|
+
def initialize(*args, **kwargs)
|
46
|
+
if args.size == 1 && kwargs.size == 0
|
47
|
+
warn "Sidekiq::Client.new(pool) is deprecated, please use Sidekiq::Client.new(pool: pool), #{caller(0..3)}"
|
48
|
+
# old calling method, accept 1 pool argument
|
49
|
+
@redis_pool = args[0]
|
50
|
+
@chain = Sidekiq.default_configuration.client_middleware
|
51
|
+
@config = Sidekiq.default_configuration
|
52
|
+
else
|
53
|
+
# new calling method: keyword arguments
|
54
|
+
@config = kwargs[:config] || Sidekiq.default_configuration
|
55
|
+
@redis_pool = kwargs[:pool] || Thread.current[:sidekiq_redis_pool] || @config&.redis_pool
|
56
|
+
@chain = kwargs[:chain] || @config&.client_middleware
|
57
|
+
raise ArgumentError, "No Redis pool available for Sidekiq::Client" unless @redis_pool
|
58
|
+
end
|
44
59
|
end
|
45
60
|
|
46
61
|
##
|
47
62
|
# The main method used to push a job to Redis. Accepts a number of options:
|
48
63
|
#
|
49
64
|
# queue - the named queue to use, default 'default'
|
50
|
-
# class - the
|
65
|
+
# class - the job class to call, required
|
51
66
|
# args - an array of simple arguments to the perform method, must be JSON-serializable
|
52
|
-
#
|
67
|
+
# at - timestamp to schedule the job (optional), must be Numeric (e.g. Time.now.to_f)
|
68
|
+
# retry - whether to retry this job if it fails, default true or an integer number of retries
|
69
|
+
# retry_for - relative amount of time to retry this job if it fails, default nil
|
53
70
|
# backtrace - whether to save any error backtrace, default false
|
54
71
|
#
|
55
|
-
#
|
56
|
-
#
|
72
|
+
# If class is set to the class name, the jobs' options will be based on Sidekiq's default
|
73
|
+
# job options. Otherwise, they will be based on the job class's options.
|
74
|
+
#
|
75
|
+
# Any options valid for a job class's sidekiq_options are also available here.
|
76
|
+
#
|
77
|
+
# All keys must be strings, not symbols. NB: because we are serializing to JSON, all
|
78
|
+
# symbols in 'args' will be converted to strings. Note that +backtrace: true+ can take quite a bit of
|
79
|
+
# space in Redis; a large volume of failing jobs can start Redis swapping if you aren't careful.
|
57
80
|
#
|
58
81
|
# Returns a unique Job ID. If middleware stops the job, nil will be returned instead.
|
59
82
|
#
|
60
83
|
# Example:
|
61
|
-
# push('queue' => 'my_queue', 'class' =>
|
84
|
+
# push('queue' => 'my_queue', 'class' => MyJob, 'args' => ['foo', 1, :bat => 'bar'])
|
62
85
|
#
|
63
86
|
def push(item)
|
64
87
|
normed = normalize_item(item)
|
65
|
-
payload =
|
66
|
-
|
88
|
+
payload = middleware.invoke(item["class"], normed, normed["queue"], @redis_pool) do
|
89
|
+
normed
|
90
|
+
end
|
67
91
|
if payload
|
92
|
+
verify_json(payload)
|
68
93
|
raw_push([payload])
|
69
|
-
payload[
|
94
|
+
payload["jid"]
|
70
95
|
end
|
71
96
|
end
|
72
97
|
|
73
98
|
##
|
74
|
-
# Push a large number of jobs to Redis.
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
99
|
+
# Push a large number of jobs to Redis. This method cuts out the redis
|
100
|
+
# network round trip latency. It pushes jobs in batches if more than
|
101
|
+
# `:batch_size` (1000 by default) of jobs are passed. I wouldn't recommend making `:batch_size`
|
102
|
+
# larger than 1000 but YMMV based on network quality, size of job args, etc.
|
103
|
+
# A large number of jobs can cause a bit of Redis command processing latency.
|
78
104
|
#
|
79
105
|
# Takes the same arguments as #push except that args is expected to be
|
80
106
|
# an Array of Arrays. All other keys are duplicated for each job. Each job
|
81
107
|
# is run through the client middleware pipeline and each job gets its own Job ID
|
82
108
|
# as normal.
|
83
109
|
#
|
84
|
-
# Returns an array of the of pushed jobs' jids
|
85
|
-
#
|
110
|
+
# Returns an array of the of pushed jobs' jids, may contain nils if any client middleware
|
111
|
+
# prevented a job push.
|
112
|
+
#
|
113
|
+
# Example (pushing jobs in batches):
|
114
|
+
# push_bulk('class' => MyJob, 'args' => (1..100_000).to_a, batch_size: 1_000)
|
115
|
+
#
|
86
116
|
def push_bulk(items)
|
117
|
+
batch_size = items.delete(:batch_size) || items.delete("batch_size") || 1_000
|
118
|
+
args = items["args"]
|
119
|
+
at = items.delete("at")
|
120
|
+
raise ArgumentError, "Job 'at' must be a Numeric or an Array of Numeric timestamps" if at && (Array(at).empty? || !Array(at).all? { |entry| entry.is_a?(Numeric) })
|
121
|
+
raise ArgumentError, "Job 'at' Array must have same size as 'args' Array" if at.is_a?(Array) && at.size != args.size
|
122
|
+
|
123
|
+
jid = items.delete("jid")
|
124
|
+
raise ArgumentError, "Explicitly passing 'jid' when pushing more than one job is not supported" if jid && args.size > 1
|
125
|
+
|
87
126
|
normed = normalize_item(items)
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
127
|
+
slice_index = 0
|
128
|
+
result = args.each_slice(batch_size).flat_map do |slice|
|
129
|
+
raise ArgumentError, "Bulk arguments must be an Array of Arrays: [[1], [2]]" unless slice.is_a?(Array) && slice.all?(Array)
|
130
|
+
break [] if slice.empty? # no jobs to push
|
131
|
+
|
132
|
+
payloads = slice.map.with_index { |job_args, index|
|
133
|
+
copy = normed.merge("args" => job_args, "jid" => SecureRandom.hex(12))
|
134
|
+
copy["at"] = (at.is_a?(Array) ? at[slice_index + index] : at) if at
|
135
|
+
result = middleware.invoke(items["class"], copy, copy["queue"], @redis_pool) do
|
136
|
+
verify_json(copy)
|
137
|
+
copy
|
138
|
+
end
|
139
|
+
result || nil
|
140
|
+
}
|
141
|
+
slice_index += batch_size
|
142
|
+
|
143
|
+
to_push = payloads.compact
|
144
|
+
raw_push(to_push) unless to_push.empty?
|
145
|
+
payloads.map { |payload| payload&.[]("jid") }
|
146
|
+
end
|
92
147
|
|
93
|
-
|
94
|
-
payloads.collect { |payload| payload['jid'] }
|
148
|
+
result.is_a?(Enumerator::Lazy) ? result.force : result
|
95
149
|
end
|
96
150
|
|
97
151
|
# Allows sharding of jobs across any number of Redis instances. All jobs
|
@@ -99,73 +153,66 @@ module Sidekiq
|
|
99
153
|
#
|
100
154
|
# pool = ConnectionPool.new { Redis.new }
|
101
155
|
# Sidekiq::Client.via(pool) do
|
102
|
-
#
|
103
|
-
#
|
156
|
+
# SomeJob.perform_async(1,2,3)
|
157
|
+
# SomeOtherJob.perform_async(1,2,3)
|
104
158
|
# end
|
105
159
|
#
|
106
160
|
# Generally this is only needed for very large Sidekiq installs processing
|
107
|
-
#
|
108
|
-
# you
|
109
|
-
# Some features, like the API, do not support sharding: they are designed to work
|
110
|
-
# against a single Redis instance.
|
161
|
+
# thousands of jobs per second. I do not recommend sharding unless
|
162
|
+
# you cannot scale any other way (e.g. splitting your app into smaller apps).
|
111
163
|
def self.via(pool)
|
112
164
|
raise ArgumentError, "No pool given" if pool.nil?
|
113
|
-
|
114
|
-
Thread.current[:
|
165
|
+
current_sidekiq_pool = Thread.current[:sidekiq_redis_pool]
|
166
|
+
Thread.current[:sidekiq_redis_pool] = pool
|
115
167
|
yield
|
116
168
|
ensure
|
117
|
-
Thread.current[:
|
169
|
+
Thread.current[:sidekiq_redis_pool] = current_sidekiq_pool
|
118
170
|
end
|
119
171
|
|
120
172
|
class << self
|
121
|
-
|
122
|
-
def default
|
123
|
-
@default ||= new
|
124
|
-
end
|
125
|
-
|
126
173
|
def push(item)
|
127
|
-
|
174
|
+
new.push(item)
|
128
175
|
end
|
129
176
|
|
130
|
-
def push_bulk(
|
131
|
-
|
177
|
+
def push_bulk(...)
|
178
|
+
new.push_bulk(...)
|
132
179
|
end
|
133
180
|
|
134
181
|
# Resque compatibility helpers. Note all helpers
|
135
|
-
# should go through
|
182
|
+
# should go through Sidekiq::Job#client_push.
|
136
183
|
#
|
137
184
|
# Example usage:
|
138
|
-
# Sidekiq::Client.enqueue(
|
185
|
+
# Sidekiq::Client.enqueue(MyJob, 'foo', 1, :bat => 'bar')
|
139
186
|
#
|
140
187
|
# Messages are enqueued to the 'default' queue.
|
141
188
|
#
|
142
189
|
def enqueue(klass, *args)
|
143
|
-
klass.client_push(
|
190
|
+
klass.client_push("class" => klass, "args" => args)
|
144
191
|
end
|
145
192
|
|
146
193
|
# Example usage:
|
147
|
-
# Sidekiq::Client.enqueue_to(:queue_name,
|
194
|
+
# Sidekiq::Client.enqueue_to(:queue_name, MyJob, 'foo', 1, :bat => 'bar')
|
148
195
|
#
|
149
196
|
def enqueue_to(queue, klass, *args)
|
150
|
-
klass.client_push(
|
197
|
+
klass.client_push("queue" => queue, "class" => klass, "args" => args)
|
151
198
|
end
|
152
199
|
|
153
200
|
# Example usage:
|
154
|
-
# Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes,
|
201
|
+
# Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes, MyJob, 'foo', 1, :bat => 'bar')
|
155
202
|
#
|
156
203
|
def enqueue_to_in(queue, interval, klass, *args)
|
157
204
|
int = interval.to_f
|
158
205
|
now = Time.now.to_f
|
159
|
-
ts = (int < 1_000_000_000 ? now + int : int)
|
206
|
+
ts = ((int < 1_000_000_000) ? now + int : int)
|
160
207
|
|
161
|
-
item = {
|
162
|
-
item.delete(
|
208
|
+
item = {"class" => klass, "args" => args, "at" => ts, "queue" => queue}
|
209
|
+
item.delete("at") if ts <= now
|
163
210
|
|
164
211
|
klass.client_push(item)
|
165
212
|
end
|
166
213
|
|
167
214
|
# Example usage:
|
168
|
-
# Sidekiq::Client.enqueue_in(3.minutes,
|
215
|
+
# Sidekiq::Client.enqueue_in(3.minutes, MyJob, 'foo', 1, :bat => 'bar')
|
169
216
|
#
|
170
217
|
def enqueue_in(interval, klass, *args)
|
171
218
|
klass.perform_in(interval, *args)
|
@@ -176,61 +223,45 @@ module Sidekiq
|
|
176
223
|
|
177
224
|
def raw_push(payloads)
|
178
225
|
@redis_pool.with do |conn|
|
179
|
-
|
180
|
-
|
226
|
+
retryable = true
|
227
|
+
begin
|
228
|
+
conn.pipelined do |pipeline|
|
229
|
+
atomic_push(pipeline, payloads)
|
230
|
+
end
|
231
|
+
rescue RedisClient::Error => ex
|
232
|
+
# 2550 Failover can cause the server to become a replica, need
|
233
|
+
# to disconnect and reopen the socket to get back to the primary.
|
234
|
+
# 4495 Use the same logic if we have a "Not enough replicas" error from the primary
|
235
|
+
# 4985 Use the same logic when a blocking command is force-unblocked
|
236
|
+
# The retry logic is copied from sidekiq.rb
|
237
|
+
if retryable && ex.message =~ /READONLY|NOREPLICAS|UNBLOCKED/
|
238
|
+
conn.close
|
239
|
+
retryable = false
|
240
|
+
retry
|
241
|
+
end
|
242
|
+
raise
|
181
243
|
end
|
182
244
|
end
|
183
245
|
true
|
184
246
|
end
|
185
247
|
|
186
248
|
def atomic_push(conn, payloads)
|
187
|
-
if payloads.first
|
188
|
-
conn.zadd(
|
189
|
-
at = hash
|
190
|
-
|
191
|
-
|
249
|
+
if payloads.first.key?("at")
|
250
|
+
conn.zadd("schedule", payloads.flat_map { |hash|
|
251
|
+
at = hash["at"].to_s
|
252
|
+
# ActiveJob sets this but the job has not been enqueued yet
|
253
|
+
hash.delete("enqueued_at")
|
254
|
+
[at, Sidekiq.dump_json(hash.except("at"))]
|
255
|
+
})
|
192
256
|
else
|
193
|
-
|
257
|
+
queue = payloads.first["queue"]
|
194
258
|
now = Time.now.to_f
|
195
|
-
to_push = payloads.map
|
196
|
-
entry[
|
259
|
+
to_push = payloads.map { |entry|
|
260
|
+
entry["enqueued_at"] = now
|
197
261
|
Sidekiq.dump_json(entry)
|
198
|
-
|
199
|
-
conn.sadd(
|
200
|
-
conn.lpush("queue:#{
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
def process_single(worker_class, item)
|
205
|
-
queue = item['queue']
|
206
|
-
|
207
|
-
middleware.invoke(worker_class, item, queue, @redis_pool) do
|
208
|
-
item
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
def normalize_item(item)
|
213
|
-
raise(ArgumentError, "Message must be a Hash of the form: { 'class' => SomeWorker, 'args' => ['bob', 1, :foo => 'bar'] }") unless item.is_a?(Hash)
|
214
|
-
raise(ArgumentError, "Message must include a class and set of arguments: #{item.inspect}") if !item['class'] || !item['args']
|
215
|
-
raise(ArgumentError, "Message args must be an Array") unless item['args'].is_a?(Array)
|
216
|
-
raise(ArgumentError, "Message class must be either a Class or String representation of the class name") unless item['class'].is_a?(Class) || item['class'].is_a?(String)
|
217
|
-
|
218
|
-
normalized_hash(item['class'.freeze])
|
219
|
-
.each{ |key, value| item[key] = value if item[key].nil? }
|
220
|
-
|
221
|
-
item['class'.freeze] = item['class'.freeze].to_s
|
222
|
-
item['queue'.freeze] = item['queue'.freeze].to_s
|
223
|
-
item['jid'.freeze] ||= SecureRandom.hex(12)
|
224
|
-
item['created_at'.freeze] ||= Time.now.to_f
|
225
|
-
item
|
226
|
-
end
|
227
|
-
|
228
|
-
def normalized_hash(item_class)
|
229
|
-
if item_class.is_a?(Class)
|
230
|
-
raise(ArgumentError, "Message must include a Sidekiq::Worker class, not class name: #{item_class.ancestors.inspect}") if !item_class.respond_to?('get_sidekiq_options'.freeze)
|
231
|
-
item_class.get_sidekiq_options
|
232
|
-
else
|
233
|
-
Sidekiq.default_worker_options
|
262
|
+
}
|
263
|
+
conn.sadd("queues", [queue])
|
264
|
+
conn.lpush("queue:#{queue}", to_push)
|
234
265
|
end
|
235
266
|
end
|
236
267
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sidekiq
|
4
|
+
##
|
5
|
+
# Sidekiq::Component assumes a config instance is available at @config
|
6
|
+
module Component # :nodoc:
|
7
|
+
attr_reader :config
|
8
|
+
|
9
|
+
def watchdog(last_words)
|
10
|
+
yield
|
11
|
+
rescue Exception => ex
|
12
|
+
handle_exception(ex, {context: last_words})
|
13
|
+
raise ex
|
14
|
+
end
|
15
|
+
|
16
|
+
def safe_thread(name, &block)
|
17
|
+
Thread.new do
|
18
|
+
Thread.current.name = "sidekiq.#{name}"
|
19
|
+
watchdog(name, &block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def logger
|
24
|
+
config.logger
|
25
|
+
end
|
26
|
+
|
27
|
+
def redis(&block)
|
28
|
+
config.redis(&block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def tid
|
32
|
+
Thread.current["sidekiq_tid"] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
|
33
|
+
end
|
34
|
+
|
35
|
+
def hostname
|
36
|
+
ENV["DYNO"] || Socket.gethostname
|
37
|
+
end
|
38
|
+
|
39
|
+
def process_nonce
|
40
|
+
@@process_nonce ||= SecureRandom.hex(6)
|
41
|
+
end
|
42
|
+
|
43
|
+
def identity
|
44
|
+
@@identity ||= "#{hostname}:#{::Process.pid}:#{process_nonce}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def handle_exception(ex, ctx = {})
|
48
|
+
config.handle_exception(ex, ctx)
|
49
|
+
end
|
50
|
+
|
51
|
+
def fire_event(event, options = {})
|
52
|
+
oneshot = options.fetch(:oneshot, true)
|
53
|
+
reverse = options[:reverse]
|
54
|
+
reraise = options[:reraise]
|
55
|
+
logger.debug("Firing #{event} event") if oneshot
|
56
|
+
|
57
|
+
arr = config[:lifecycle_events][event]
|
58
|
+
arr.reverse! if reverse
|
59
|
+
arr.each do |block|
|
60
|
+
block.call
|
61
|
+
rescue => ex
|
62
|
+
handle_exception(ex, {context: "Exception during Sidekiq lifecycle event.", event: event})
|
63
|
+
raise ex if reraise
|
64
|
+
end
|
65
|
+
arr.clear if oneshot # once we've fired an event, we never fire it again
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|