sidekiq 6.5.10 → 8.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Changes.md +568 -18
- data/README.md +66 -38
- data/bin/kiq +17 -0
- data/bin/lint-herb +13 -0
- data/bin/multi_queue_bench +271 -0
- data/bin/sidekiq +3 -8
- data/bin/sidekiqload +215 -118
- data/bin/sidekiqmon +3 -0
- data/bin/webload +69 -0
- data/lib/active_job/queue_adapters/sidekiq_adapter.rb +124 -0
- data/lib/generators/sidekiq/job_generator.rb +17 -3
- data/lib/generators/sidekiq/templates/job.rb.erb +1 -1
- data/lib/sidekiq/api.rb +526 -218
- data/lib/sidekiq/capsule.rb +135 -0
- data/lib/sidekiq/cli.rb +88 -96
- data/lib/sidekiq/client.rb +114 -47
- data/lib/sidekiq/component.rb +71 -4
- data/lib/sidekiq/config.rb +316 -0
- data/lib/sidekiq/deploy.rb +64 -0
- data/lib/sidekiq/embedded.rb +64 -0
- data/lib/sidekiq/fetch.rb +12 -14
- data/lib/sidekiq/iterable_job.rb +56 -0
- data/lib/sidekiq/job/interrupt_handler.rb +24 -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 +322 -0
- data/lib/sidekiq/job.rb +397 -10
- data/lib/sidekiq/job_logger.rb +14 -11
- data/lib/sidekiq/job_retry.rb +78 -28
- data/lib/sidekiq/job_util.rb +58 -16
- data/lib/sidekiq/launcher.rb +86 -70
- data/lib/sidekiq/loader.rb +57 -0
- data/lib/sidekiq/logger.rb +25 -94
- data/lib/sidekiq/manager.rb +11 -13
- data/lib/sidekiq/metrics/query.rb +84 -48
- data/lib/sidekiq/metrics/shared.rb +29 -14
- data/lib/sidekiq/metrics/tracking.rb +49 -30
- data/lib/sidekiq/middleware/chain.rb +19 -18
- data/lib/sidekiq/middleware/current_attributes.rb +78 -21
- data/lib/sidekiq/middleware/i18n.rb +2 -0
- data/lib/sidekiq/middleware/modules.rb +2 -0
- data/lib/sidekiq/monitor.rb +15 -5
- data/lib/sidekiq/paginator.rb +26 -4
- data/lib/sidekiq/processor.rb +78 -65
- data/lib/sidekiq/profiler.rb +73 -0
- data/lib/sidekiq/rails.rb +45 -60
- data/lib/sidekiq/redis_client_adapter.rb +35 -72
- data/lib/sidekiq/redis_connection.rb +54 -114
- data/lib/sidekiq/ring_buffer.rb +3 -0
- data/lib/sidekiq/scheduled.rb +28 -30
- data/lib/sidekiq/systemd.rb +2 -0
- data/lib/sidekiq/test_api.rb +331 -0
- data/lib/sidekiq/testing/inline.rb +2 -30
- data/lib/sidekiq/testing.rb +2 -342
- data/lib/sidekiq/transaction_aware_client.rb +23 -9
- data/lib/sidekiq/tui/controls.rb +53 -0
- data/lib/sidekiq/tui/filtering.rb +53 -0
- data/lib/sidekiq/tui/tabs/base_tab.rb +230 -0
- data/lib/sidekiq/tui/tabs/busy.rb +104 -0
- data/lib/sidekiq/tui/tabs/dead.rb +14 -0
- data/lib/sidekiq/tui/tabs/home.rb +115 -0
- data/lib/sidekiq/tui/tabs/metrics.rb +114 -0
- data/lib/sidekiq/tui/tabs/queues.rb +83 -0
- data/lib/sidekiq/tui/tabs/retries.rb +14 -0
- data/lib/sidekiq/tui/tabs/scheduled.rb +14 -0
- data/lib/sidekiq/tui/tabs/set_tab.rb +91 -0
- data/lib/sidekiq/tui/tabs.rb +15 -0
- data/lib/sidekiq/tui.rb +390 -0
- data/lib/sidekiq/version.rb +6 -1
- data/lib/sidekiq/web/action.rb +149 -64
- data/lib/sidekiq/web/application.rb +379 -281
- data/lib/sidekiq/web/config.rb +117 -0
- data/lib/sidekiq/web/helpers.rb +190 -71
- data/lib/sidekiq/web/router.rb +62 -75
- data/lib/sidekiq/web.rb +68 -101
- data/lib/sidekiq/worker_compatibility_alias.rb +13 -0
- data/lib/sidekiq.rb +85 -269
- data/sidekiq.gemspec +15 -11
- data/web/assets/images/logo.png +0 -0
- data/web/assets/images/status.png +0 -0
- data/web/assets/javascripts/application.js +84 -24
- data/web/assets/javascripts/base-charts.js +120 -0
- data/web/assets/javascripts/chartjs-adapter-date-fns.min.js +7 -0
- data/web/assets/javascripts/dashboard-charts.js +194 -0
- data/web/assets/javascripts/dashboard.js +18 -234
- data/web/assets/javascripts/metrics.js +138 -120
- data/web/assets/stylesheets/style.css +776 -0
- data/web/locales/ar.yml +72 -70
- data/web/locales/cs.yml +64 -62
- data/web/locales/da.yml +62 -53
- data/web/locales/de.yml +67 -65
- data/web/locales/el.yml +4 -7
- data/web/locales/en.yml +89 -71
- data/web/locales/es.yml +91 -68
- data/web/locales/fa.yml +67 -65
- data/web/locales/fr.yml +82 -67
- data/web/locales/gd.yml +110 -0
- data/web/locales/he.yml +67 -64
- data/web/locales/hi.yml +95 -59
- data/web/locales/it.yml +94 -54
- data/web/locales/ja.yml +69 -70
- data/web/locales/ko.yml +54 -52
- data/web/locales/lt.yml +68 -66
- data/web/locales/nb.yml +63 -61
- data/web/locales/nl.yml +54 -52
- data/web/locales/pl.yml +47 -45
- data/web/locales/{pt-br.yml → pt-BR.yml} +81 -70
- data/web/locales/pt.yml +53 -51
- data/web/locales/ru.yml +69 -66
- data/web/locales/sv.yml +55 -53
- data/web/locales/ta.yml +100 -64
- data/web/locales/tr.yml +102 -0
- data/web/locales/uk.yml +87 -61
- data/web/locales/ur.yml +66 -64
- data/web/locales/vi.yml +69 -67
- data/web/locales/zh-CN.yml +107 -0
- data/web/locales/{zh-tw.yml → zh-TW.yml} +13 -3
- data/web/views/_footer.html.erb +32 -0
- data/web/views/_job_info.html.erb +115 -0
- data/web/views/_metrics_period_select.html.erb +15 -0
- data/web/views/_nav.html.erb +45 -0
- data/web/views/_paging.html.erb +26 -0
- data/web/views/_poll_link.html.erb +4 -0
- data/web/views/_summary.html.erb +40 -0
- data/web/views/busy.html.erb +155 -0
- data/web/views/dashboard.html.erb +104 -0
- data/web/views/dead.html.erb +38 -0
- data/web/views/filtering.html.erb +6 -0
- data/web/views/layout.html.erb +26 -0
- data/web/views/metrics.html.erb +85 -0
- data/web/views/metrics_for_job.html.erb +58 -0
- data/web/views/morgue.html.erb +69 -0
- data/web/views/profiles.html.erb +43 -0
- data/web/views/queue.html.erb +57 -0
- data/web/views/queues.html.erb +46 -0
- data/web/views/retries.html.erb +77 -0
- data/web/views/retry.html.erb +39 -0
- data/web/views/scheduled.html.erb +64 -0
- data/web/views/{scheduled_job_info.erb → scheduled_job_info.html.erb} +3 -3
- metadata +113 -70
- data/lib/sidekiq/delay.rb +0 -43
- data/lib/sidekiq/extensions/action_mailer.rb +0 -48
- data/lib/sidekiq/extensions/active_record.rb +0 -43
- data/lib/sidekiq/extensions/class_methods.rb +0 -43
- data/lib/sidekiq/extensions/generic_proxy.rb +0 -33
- data/lib/sidekiq/metrics/deploy.rb +0 -47
- data/lib/sidekiq/web/csrf_protection.rb +0 -180
- data/lib/sidekiq/worker.rb +0 -370
- data/web/assets/javascripts/graph.js +0 -16
- data/web/assets/stylesheets/application-dark.css +0 -143
- data/web/assets/stylesheets/application-rtl.css +0 -242
- data/web/assets/stylesheets/application.css +0 -999
- data/web/assets/stylesheets/bootstrap-rtl.min.css +0 -9
- data/web/assets/stylesheets/bootstrap.css +0 -5
- data/web/locales/zh-cn.yml +0 -93
- data/web/views/_footer.erb +0 -20
- data/web/views/_job_info.erb +0 -89
- data/web/views/_nav.erb +0 -52
- data/web/views/_paging.erb +0 -23
- data/web/views/_poll_link.erb +0 -4
- data/web/views/_status.erb +0 -4
- data/web/views/_summary.erb +0 -40
- data/web/views/busy.erb +0 -137
- data/web/views/dashboard.erb +0 -84
- data/web/views/dead.erb +0 -34
- data/web/views/layout.erb +0 -42
- data/web/views/metrics.erb +0 -69
- data/web/views/metrics_for_job.erb +0 -87
- data/web/views/morgue.erb +0 -78
- data/web/views/queue.erb +0 -59
- data/web/views/queues.erb +0 -38
- data/web/views/retries.erb +0 -83
- data/web/views/retry.erb +0 -34
- data/web/views/scheduled.erb +0 -57
- /data/{LICENSE → LICENSE.txt} +0 -0
data/lib/sidekiq/client.rb
CHANGED
|
@@ -21,7 +21,6 @@ module Sidekiq
|
|
|
21
21
|
# Sidekiq.client_middleware but you can change as necessary.
|
|
22
22
|
#
|
|
23
23
|
def middleware(&block)
|
|
24
|
-
@chain ||= Sidekiq.client_middleware
|
|
25
24
|
if block
|
|
26
25
|
@chain = @chain.dup
|
|
27
26
|
yield @chain
|
|
@@ -31,18 +30,47 @@ module Sidekiq
|
|
|
31
30
|
|
|
32
31
|
attr_accessor :redis_pool
|
|
33
32
|
|
|
34
|
-
# Sidekiq::Client
|
|
35
|
-
#
|
|
36
|
-
# Sidekiq jobs across several Redis instances (for scalability
|
|
37
|
-
# reasons, e.g.)
|
|
33
|
+
# Sidekiq::Client is responsible for pushing job payloads to Redis.
|
|
34
|
+
# Requires the :pool or :config keyword argument.
|
|
38
35
|
#
|
|
39
|
-
# Sidekiq::Client.new(
|
|
36
|
+
# Sidekiq::Client.new(pool: Sidekiq::RedisConnection.create)
|
|
40
37
|
#
|
|
41
|
-
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
|
|
45
|
-
|
|
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
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Cancel the IterableJob with the given JID.
|
|
62
|
+
# **NB: Cancellation is asynchronous.** Iteration checks every
|
|
63
|
+
# five seconds so this will not immediately stop the given job.
|
|
64
|
+
def cancel!(jid)
|
|
65
|
+
key = "it-#{jid}"
|
|
66
|
+
_, result, _ = Sidekiq.redis do |c|
|
|
67
|
+
c.pipelined do |p|
|
|
68
|
+
p.hsetnx(key, "cancelled", Time.now.to_i)
|
|
69
|
+
p.hget(key, "cancelled")
|
|
70
|
+
p.expire(key, Sidekiq::Job::Iterable::STATE_TTL, "nx")
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
result.to_i
|
|
46
74
|
end
|
|
47
75
|
|
|
48
76
|
##
|
|
@@ -53,6 +81,7 @@ module Sidekiq
|
|
|
53
81
|
# args - an array of simple arguments to the perform method, must be JSON-serializable
|
|
54
82
|
# at - timestamp to schedule the job (optional), must be Numeric (e.g. Time.now.to_f)
|
|
55
83
|
# retry - whether to retry this job if it fails, default true or an integer number of retries
|
|
84
|
+
# retry_for - relative amount of time to retry this job if it fails, default nil
|
|
56
85
|
# backtrace - whether to save any error backtrace, default false
|
|
57
86
|
#
|
|
58
87
|
# If class is set to the class name, the jobs' options will be based on Sidekiq's default
|
|
@@ -60,7 +89,7 @@ module Sidekiq
|
|
|
60
89
|
#
|
|
61
90
|
# Any options valid for a job class's sidekiq_options are also available here.
|
|
62
91
|
#
|
|
63
|
-
# All
|
|
92
|
+
# All keys must be strings, not symbols. NB: because we are serializing to JSON, all
|
|
64
93
|
# symbols in 'args' will be converted to strings. Note that +backtrace: true+ can take quite a bit of
|
|
65
94
|
# space in Redis; a large volume of failing jobs can start Redis swapping if you aren't careful.
|
|
66
95
|
#
|
|
@@ -83,42 +112,76 @@ module Sidekiq
|
|
|
83
112
|
|
|
84
113
|
##
|
|
85
114
|
# Push a large number of jobs to Redis. This method cuts out the redis
|
|
86
|
-
# network round trip latency.
|
|
87
|
-
# 1000
|
|
115
|
+
# network round trip latency. It pushes jobs in batches if more than
|
|
116
|
+
# `:batch_size` (1000 by default) of jobs are passed. I wouldn't recommend making `:batch_size`
|
|
117
|
+
# larger than 1000 but YMMV based on network quality, size of job args, etc.
|
|
88
118
|
# A large number of jobs can cause a bit of Redis command processing latency.
|
|
89
119
|
#
|
|
120
|
+
# Accepts an `:at` option to schedule the jobs for future execution. It
|
|
121
|
+
# accepts either a single Numeric timestamp (or seconds-from-now) applied
|
|
122
|
+
# to every job, or an Array of Numeric values with the same size as `args`
|
|
123
|
+
# to schedule each job at its corresponding time.
|
|
124
|
+
#
|
|
125
|
+
# Accepts an additional `:spread_interval` option (in seconds) to randomly spread
|
|
126
|
+
# the jobs schedule times over the specified interval.
|
|
127
|
+
#
|
|
90
128
|
# Takes the same arguments as #push except that args is expected to be
|
|
91
129
|
# an Array of Arrays. All other keys are duplicated for each job. Each job
|
|
92
130
|
# is run through the client middleware pipeline and each job gets its own Job ID
|
|
93
131
|
# as normal.
|
|
94
132
|
#
|
|
95
|
-
# Returns an array of the of pushed jobs' jids
|
|
96
|
-
#
|
|
133
|
+
# Returns an array of the of pushed jobs' jids, may contain nils if any client middleware
|
|
134
|
+
# prevented a job push.
|
|
135
|
+
#
|
|
136
|
+
# Example (pushing jobs in batches):
|
|
137
|
+
# push_bulk('class' => MyJob, 'args' => (1..100_000).to_a, batch_size: 1_000)
|
|
138
|
+
#
|
|
97
139
|
def push_bulk(items)
|
|
98
140
|
args = items["args"]
|
|
99
|
-
|
|
100
|
-
return [] if args.empty? # no jobs to push
|
|
101
|
-
|
|
102
|
-
at = items.delete("at")
|
|
141
|
+
at = items.delete("at") || items.delete(:at)
|
|
103
142
|
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) })
|
|
104
143
|
raise ArgumentError, "Job 'at' Array must have same size as 'args' Array" if at.is_a?(Array) && at.size != args.size
|
|
105
144
|
|
|
106
145
|
jid = items.delete("jid")
|
|
107
146
|
raise ArgumentError, "Explicitly passing 'jid' when pushing more than one job is not supported" if jid && args.size > 1
|
|
108
147
|
|
|
148
|
+
spread_interval = items.delete(:spread_interval) || items.delete("spread_interval")
|
|
149
|
+
raise ArgumentError, "Jobs 'spread_interval' must be a positive Numeric" if spread_interval && (!spread_interval.is_a?(Numeric) || spread_interval <= 0)
|
|
150
|
+
raise ArgumentError, "Only one of 'at' or 'spread_interval' can be provided" if at && spread_interval
|
|
151
|
+
|
|
152
|
+
if !at && spread_interval
|
|
153
|
+
# Do not use spread interval smaller than pooling interval.
|
|
154
|
+
spread_interval = [spread_interval, 5].max
|
|
155
|
+
now = Time.now.to_f
|
|
156
|
+
at = args.map { now + rand * spread_interval }
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Use a smaller batch size by default for scheduled jobs since adding to sorted sets is more costly.
|
|
160
|
+
batch_size = items.delete(:batch_size) || items.delete("batch_size") || (at ? 100 : 1_000)
|
|
161
|
+
|
|
109
162
|
normed = normalize_item(items)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
163
|
+
slice_index = 0
|
|
164
|
+
result = args.each_slice(batch_size).flat_map do |slice|
|
|
165
|
+
raise ArgumentError, "Bulk arguments must be an Array of Arrays: [[1], [2]]" unless slice.is_a?(Array) && slice.all?(Array)
|
|
166
|
+
break [] if slice.empty? # no jobs to push
|
|
167
|
+
|
|
168
|
+
payloads = slice.map.with_index { |job_args, index|
|
|
169
|
+
copy = normed.merge("args" => job_args, "jid" => SecureRandom.hex(12))
|
|
170
|
+
copy["at"] = (at.is_a?(Array) ? at[slice_index + index] : at) if at
|
|
171
|
+
result = middleware.invoke(items["class"], copy, copy["queue"], @redis_pool) do
|
|
172
|
+
verify_json(copy)
|
|
173
|
+
copy
|
|
174
|
+
end
|
|
175
|
+
result || nil
|
|
176
|
+
}
|
|
177
|
+
slice_index += batch_size
|
|
178
|
+
|
|
179
|
+
to_push = payloads.compact
|
|
180
|
+
raw_push(to_push) unless to_push.empty?
|
|
181
|
+
payloads.map { |payload| payload&.[]("jid") }
|
|
182
|
+
end
|
|
119
183
|
|
|
120
|
-
|
|
121
|
-
payloads.collect { |payload| payload["jid"] }
|
|
184
|
+
result.is_a?(Enumerator::Lazy) ? result.force : result
|
|
122
185
|
end
|
|
123
186
|
|
|
124
187
|
# Allows sharding of jobs across any number of Redis instances. All jobs
|
|
@@ -135,11 +198,11 @@ module Sidekiq
|
|
|
135
198
|
# you cannot scale any other way (e.g. splitting your app into smaller apps).
|
|
136
199
|
def self.via(pool)
|
|
137
200
|
raise ArgumentError, "No pool given" if pool.nil?
|
|
138
|
-
current_sidekiq_pool = Thread.current[:
|
|
139
|
-
Thread.current[:
|
|
201
|
+
current_sidekiq_pool = Thread.current[:sidekiq_redis_pool]
|
|
202
|
+
Thread.current[:sidekiq_redis_pool] = pool
|
|
140
203
|
yield
|
|
141
204
|
ensure
|
|
142
|
-
Thread.current[:
|
|
205
|
+
Thread.current[:sidekiq_redis_pool] = current_sidekiq_pool
|
|
143
206
|
end
|
|
144
207
|
|
|
145
208
|
class << self
|
|
@@ -147,8 +210,8 @@ module Sidekiq
|
|
|
147
210
|
new.push(item)
|
|
148
211
|
end
|
|
149
212
|
|
|
150
|
-
def push_bulk(
|
|
151
|
-
new.push_bulk(
|
|
213
|
+
def push_bulk(...)
|
|
214
|
+
new.push_bulk(...)
|
|
152
215
|
end
|
|
153
216
|
|
|
154
217
|
# Resque compatibility helpers. Note all helpers
|
|
@@ -201,14 +264,14 @@ module Sidekiq
|
|
|
201
264
|
conn.pipelined do |pipeline|
|
|
202
265
|
atomic_push(pipeline, payloads)
|
|
203
266
|
end
|
|
204
|
-
rescue
|
|
267
|
+
rescue RedisClient::Error => ex
|
|
205
268
|
# 2550 Failover can cause the server to become a replica, need
|
|
206
269
|
# to disconnect and reopen the socket to get back to the primary.
|
|
207
270
|
# 4495 Use the same logic if we have a "Not enough replicas" error from the primary
|
|
208
271
|
# 4985 Use the same logic when a blocking command is force-unblocked
|
|
209
272
|
# The retry logic is copied from sidekiq.rb
|
|
210
273
|
if retryable && ex.message =~ /READONLY|NOREPLICAS|UNBLOCKED/
|
|
211
|
-
conn.
|
|
274
|
+
conn.close
|
|
212
275
|
retryable = false
|
|
213
276
|
retry
|
|
214
277
|
end
|
|
@@ -221,18 +284,22 @@ module Sidekiq
|
|
|
221
284
|
def atomic_push(conn, payloads)
|
|
222
285
|
if payloads.first.key?("at")
|
|
223
286
|
conn.zadd("schedule", payloads.flat_map { |hash|
|
|
224
|
-
at = hash
|
|
287
|
+
at = hash["at"].to_s
|
|
288
|
+
# ActiveJob sets enqueued_at but the job has not been enqueued yet
|
|
289
|
+
hash = hash.except("enqueued_at", "at")
|
|
225
290
|
[at, Sidekiq.dump_json(hash)]
|
|
226
291
|
})
|
|
227
292
|
else
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
293
|
+
now = ::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond) # milliseconds since the epoch
|
|
294
|
+
grouped_queues = payloads.group_by { |job| job["queue"] }
|
|
295
|
+
conn.sadd("queues", grouped_queues.keys)
|
|
296
|
+
grouped_queues.each do |queue, grouped_payloads|
|
|
297
|
+
to_push = grouped_payloads.map { |entry|
|
|
298
|
+
entry["enqueued_at"] = now
|
|
299
|
+
Sidekiq.dump_json(entry)
|
|
300
|
+
}
|
|
301
|
+
conn.lpush("queue:#{queue}", to_push)
|
|
302
|
+
end
|
|
236
303
|
end
|
|
237
304
|
end
|
|
238
305
|
end
|
data/lib/sidekiq/component.rb
CHANGED
|
@@ -1,9 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Sidekiq
|
|
4
|
+
# Ruby's default thread priority is 0, which uses 100ms time slices.
|
|
5
|
+
# This can lead to some surprising thread starvation; if using a lot of
|
|
6
|
+
# CPU-heavy concurrency, it may take several seconds before a Thread gets
|
|
7
|
+
# on the CPU.
|
|
8
|
+
#
|
|
9
|
+
# Negative priorities lower the timeslice by half, so -1 = 50ms, -2 = 25ms, etc.
|
|
10
|
+
# With more frequent timeslices, we reduce the risk of unintentional timeouts
|
|
11
|
+
# and starvation.
|
|
12
|
+
#
|
|
13
|
+
# Customize like so:
|
|
14
|
+
#
|
|
15
|
+
# Sidekiq.configure_server do |cfg|
|
|
16
|
+
# cfg.thread_priority = 0
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
DEFAULT_THREAD_PRIORITY = -1
|
|
20
|
+
|
|
2
21
|
##
|
|
3
|
-
# Sidekiq::Component
|
|
22
|
+
# Sidekiq::Component provides a set of utility methods depending only
|
|
23
|
+
# on Sidekiq::Config. It assumes a config instance is available at @config.
|
|
4
24
|
module Component # :nodoc:
|
|
5
25
|
attr_reader :config
|
|
6
26
|
|
|
27
|
+
# This is epoch milliseconds, appropriate for persistence
|
|
28
|
+
def real_ms
|
|
29
|
+
::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# used for time difference and relative comparisons, not persistence.
|
|
33
|
+
def mono_ms
|
|
34
|
+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :millisecond)
|
|
35
|
+
end
|
|
36
|
+
|
|
7
37
|
def watchdog(last_words)
|
|
8
38
|
yield
|
|
9
39
|
rescue Exception => ex
|
|
@@ -11,11 +41,11 @@ module Sidekiq
|
|
|
11
41
|
raise ex
|
|
12
42
|
end
|
|
13
43
|
|
|
14
|
-
def safe_thread(name, &block)
|
|
44
|
+
def safe_thread(name, priority: nil, &block)
|
|
15
45
|
Thread.new do
|
|
16
|
-
Thread.current.name = name
|
|
46
|
+
Thread.current.name = "sidekiq.#{name}"
|
|
17
47
|
watchdog(name, &block)
|
|
18
|
-
end
|
|
48
|
+
end.tap { |t| t.priority = (priority || config.thread_priority || DEFAULT_THREAD_PRIORITY) }
|
|
19
49
|
end
|
|
20
50
|
|
|
21
51
|
def logger
|
|
@@ -27,6 +57,9 @@ module Sidekiq
|
|
|
27
57
|
end
|
|
28
58
|
|
|
29
59
|
def tid
|
|
60
|
+
# We XOR with PID to ensure Thread IDs changes after fork.
|
|
61
|
+
# I'm unclear why we don't multiply the two values to better guarantee
|
|
62
|
+
# a unique value but it's been this way for quite a while now. #3685
|
|
30
63
|
Thread.current["sidekiq_tid"] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
|
|
31
64
|
end
|
|
32
65
|
|
|
@@ -50,6 +83,7 @@ module Sidekiq
|
|
|
50
83
|
oneshot = options.fetch(:oneshot, true)
|
|
51
84
|
reverse = options[:reverse]
|
|
52
85
|
reraise = options[:reraise]
|
|
86
|
+
logger.debug("Firing #{event} event") if oneshot
|
|
53
87
|
|
|
54
88
|
arr = config[:lifecycle_events][event]
|
|
55
89
|
arr.reverse! if reverse
|
|
@@ -61,5 +95,38 @@ module Sidekiq
|
|
|
61
95
|
end
|
|
62
96
|
arr.clear if oneshot # once we've fired an event, we never fire it again
|
|
63
97
|
end
|
|
98
|
+
|
|
99
|
+
# When you have a large tree of components, the `inspect` output
|
|
100
|
+
# can get out of hand, especially with lots of Sidekiq::Config
|
|
101
|
+
# references everywhere. We avoid calling `inspect` on more complex
|
|
102
|
+
# state and use `to_s` instead to keep output manageable, #6553
|
|
103
|
+
def inspect
|
|
104
|
+
"#<#{self.class.name} #{
|
|
105
|
+
instance_variables.map do |name|
|
|
106
|
+
value = instance_variable_get(name)
|
|
107
|
+
case value
|
|
108
|
+
when Proc
|
|
109
|
+
"#{name}=#{value}"
|
|
110
|
+
when Sidekiq::Config
|
|
111
|
+
"#{name}=#{value}"
|
|
112
|
+
when Sidekiq::Component
|
|
113
|
+
"#{name}=#{value}"
|
|
114
|
+
else
|
|
115
|
+
"#{name}=#{value.inspect}"
|
|
116
|
+
end
|
|
117
|
+
end.join(", ")
|
|
118
|
+
}>"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def default_tag(dir = Dir.pwd)
|
|
122
|
+
name = File.basename(dir)
|
|
123
|
+
prevdir = File.dirname(dir) # Capistrano release directory?
|
|
124
|
+
if name.to_i != 0 && prevdir
|
|
125
|
+
if File.basename(prevdir) == "releases"
|
|
126
|
+
return File.basename(File.dirname(prevdir))
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
name
|
|
130
|
+
end
|
|
64
131
|
end
|
|
65
132
|
end
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "forwardable"
|
|
4
|
+
require "sidekiq/redis_connection"
|
|
5
|
+
|
|
6
|
+
module Sidekiq
|
|
7
|
+
# Sidekiq::Config represents the global configuration for an instance of Sidekiq.
|
|
8
|
+
class Config
|
|
9
|
+
extend Forwardable
|
|
10
|
+
|
|
11
|
+
DEFAULTS = {
|
|
12
|
+
labels: Set.new,
|
|
13
|
+
require: ".",
|
|
14
|
+
environment: nil,
|
|
15
|
+
concurrency: 5,
|
|
16
|
+
timeout: 25,
|
|
17
|
+
poll_interval_average: nil,
|
|
18
|
+
average_scheduled_poll_interval: 5,
|
|
19
|
+
on_complex_arguments: :raise,
|
|
20
|
+
# if the Iterable job runs longer than this value (in seconds), then the job
|
|
21
|
+
# will be interrupted after the current iteration and re-enqueued at the back of the queue
|
|
22
|
+
max_iteration_runtime: nil,
|
|
23
|
+
error_handlers: [],
|
|
24
|
+
death_handlers: [],
|
|
25
|
+
lifecycle_events: {
|
|
26
|
+
startup: [],
|
|
27
|
+
quiet: [],
|
|
28
|
+
shutdown: [],
|
|
29
|
+
exit: [],
|
|
30
|
+
# triggers when we fire the first heartbeat on startup OR repairing a network partition
|
|
31
|
+
heartbeat: [],
|
|
32
|
+
# triggers on EVERY heartbeat call, every 10 seconds
|
|
33
|
+
beat: []
|
|
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
|
+
backtrace_cleaner: ->(backtrace) { backtrace },
|
|
39
|
+
logged_job_attributes: ["bid", "tags"],
|
|
40
|
+
redis_idle_timeout: nil
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
ERROR_HANDLER = ->(ex, ctx, cfg = Sidekiq.default_configuration) {
|
|
44
|
+
Sidekiq::Context.with(ctx) do
|
|
45
|
+
dev = cfg[:environment] == "development"
|
|
46
|
+
fancy = dev && $stdout.tty? # 🎩
|
|
47
|
+
# Weird logic here but we want to show the backtrace in local
|
|
48
|
+
# development or if verbose logging is enabled.
|
|
49
|
+
#
|
|
50
|
+
# `full_message` contains the error class, message and backtrace
|
|
51
|
+
# `detailed_message` contains the error class and message
|
|
52
|
+
#
|
|
53
|
+
# Absolutely terrible API names. Not useful at all to have two
|
|
54
|
+
# methods with similar but obscure names.
|
|
55
|
+
if dev || cfg.logger.debug?
|
|
56
|
+
cfg.logger.info { ex.full_message(highlight: fancy) }
|
|
57
|
+
else
|
|
58
|
+
cfg.logger.info { ex.detailed_message(highlight: fancy) }
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
def initialize(options = {})
|
|
64
|
+
@options = DEFAULTS.merge(options)
|
|
65
|
+
@options[:error_handlers] << ERROR_HANDLER if @options[:error_handlers].empty?
|
|
66
|
+
@directory = {}
|
|
67
|
+
@redis_config = {}
|
|
68
|
+
@capsules = {}
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def_delegators :@options, :[], :[]=, :fetch, :key?, :has_key?, :merge!, :dig
|
|
72
|
+
attr_reader :capsules
|
|
73
|
+
attr_accessor :thread_priority
|
|
74
|
+
|
|
75
|
+
def inspect
|
|
76
|
+
"#<#{self.class.name} @options=#{
|
|
77
|
+
@options.except(:lifecycle_events, :reloader, :death_handlers, :error_handlers).inspect
|
|
78
|
+
}>"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def to_json(*)
|
|
82
|
+
Sidekiq.dump_json(@options)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# LEGACY: edits the default capsule
|
|
86
|
+
# config.concurrency = 5
|
|
87
|
+
def concurrency=(val)
|
|
88
|
+
default_capsule.concurrency = Integer(val)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def concurrency
|
|
92
|
+
default_capsule.concurrency
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def total_concurrency
|
|
96
|
+
capsules.each_value.sum(&:concurrency)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Edit the default capsule.
|
|
100
|
+
# config.queues = %w( high default low ) # strict
|
|
101
|
+
# config.queues = %w( high,3 default,2 low,1 ) # weighted
|
|
102
|
+
# config.queues = %w( feature1,1 feature2,1 feature3,1 ) # random
|
|
103
|
+
#
|
|
104
|
+
# With weighted priority, queue will be checked first (weight / total) of the time.
|
|
105
|
+
# high will be checked first (3/6) or 50% of the time.
|
|
106
|
+
# I'd recommend setting weights between 1-10. Weights in the hundreds or thousands
|
|
107
|
+
# are ridiculous and unnecessarily expensive. You can get random queue ordering
|
|
108
|
+
# by explicitly setting all weights to 1.
|
|
109
|
+
def queues=(val)
|
|
110
|
+
default_capsule.queues = val
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def queues
|
|
114
|
+
default_capsule.queues
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def client_middleware
|
|
118
|
+
@client_chain ||= Sidekiq::Middleware::Chain.new(self)
|
|
119
|
+
yield @client_chain if block_given?
|
|
120
|
+
@client_chain
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def server_middleware
|
|
124
|
+
@server_chain ||= Sidekiq::Middleware::Chain.new(self)
|
|
125
|
+
yield @server_chain if block_given?
|
|
126
|
+
@server_chain
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def default_capsule(&block)
|
|
130
|
+
capsule("default", &block)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# register a new queue processing subsystem
|
|
134
|
+
def capsule(name)
|
|
135
|
+
nm = name.to_s
|
|
136
|
+
cap = @capsules.fetch(nm) do
|
|
137
|
+
cap = Sidekiq::Capsule.new(nm, self)
|
|
138
|
+
@capsules[nm] = cap
|
|
139
|
+
end
|
|
140
|
+
yield cap if block_given?
|
|
141
|
+
cap
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# All capsules must use the same Redis configuration
|
|
145
|
+
def redis=(hash)
|
|
146
|
+
@redis_config = @redis_config.merge(hash)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def reap_idle_redis_connections(timeout = 60)
|
|
150
|
+
self[:redis_idle_timeout] = timeout
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def redis_pool
|
|
154
|
+
Thread.current[:sidekiq_redis_pool] || Thread.current[:sidekiq_capsule]&.redis_pool || local_redis_pool
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def local_redis_pool
|
|
158
|
+
# this is our internal client/housekeeping pool. each capsule has its
|
|
159
|
+
# own pool for executing threads.
|
|
160
|
+
@redis ||= new_redis_pool(10, "internal")
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def new_redis_pool(size, name = "unset")
|
|
164
|
+
# connection pool is lazy, it will not create connections unless you actually need them
|
|
165
|
+
# so don't be skimpy!
|
|
166
|
+
RedisConnection.create({size: size, logger: logger, pool_name: name}.merge(@redis_config))
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def redis_info
|
|
170
|
+
redis do |conn|
|
|
171
|
+
conn.call("INFO") { |i| i.lines(chomp: true).map { |l| l.split(":", 2) }.select { |l| l.size == 2 }.to_h }
|
|
172
|
+
rescue RedisClientAdapter::CommandError => ex
|
|
173
|
+
# 2850 return fake version when INFO command has (probably) been renamed
|
|
174
|
+
raise unless /unknown command/.match?(ex.message)
|
|
175
|
+
{
|
|
176
|
+
"redis_version" => "9.9.9",
|
|
177
|
+
"uptime_in_days" => "9999",
|
|
178
|
+
"connected_clients" => "9999",
|
|
179
|
+
"used_memory_human" => "9P",
|
|
180
|
+
"used_memory_peak_human" => "9P"
|
|
181
|
+
}.freeze
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def redis
|
|
186
|
+
raise ArgumentError, "requires a block" unless block_given?
|
|
187
|
+
redis_pool.with do |conn|
|
|
188
|
+
retryable = true
|
|
189
|
+
begin
|
|
190
|
+
yield conn
|
|
191
|
+
rescue RedisClientAdapter::BaseError => ex
|
|
192
|
+
# 2550 Failover can cause the server to become a replica, need
|
|
193
|
+
# to disconnect and reopen the socket to get back to the primary.
|
|
194
|
+
# 4495 Use the same logic if we have a "Not enough replicas" error from the primary
|
|
195
|
+
# 4985 Use the same logic when a blocking command is force-unblocked
|
|
196
|
+
# The same retry logic is also used in client.rb
|
|
197
|
+
if retryable && ex.message =~ /READONLY|NOREPLICAS|UNBLOCKED/
|
|
198
|
+
conn.close
|
|
199
|
+
retryable = false
|
|
200
|
+
retry
|
|
201
|
+
end
|
|
202
|
+
raise
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# register global singletons which can be accessed elsewhere
|
|
208
|
+
def register(name, instance)
|
|
209
|
+
# logger.debug("register[#{name}] = #{instance}")
|
|
210
|
+
# Sidekiq Enterprise lazy registers a few services so we
|
|
211
|
+
# can't lock down this hash completely.
|
|
212
|
+
hash = @directory.dup
|
|
213
|
+
hash[name] = instance
|
|
214
|
+
@directory = hash.freeze
|
|
215
|
+
instance
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# find a singleton
|
|
219
|
+
def lookup(name, default_class = nil)
|
|
220
|
+
# JNDI is just a fancy name for a hash lookup
|
|
221
|
+
@directory.fetch(name) do |key|
|
|
222
|
+
return nil unless default_class
|
|
223
|
+
register(key, default_class.new(self))
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def freeze!
|
|
228
|
+
@directory.freeze
|
|
229
|
+
@options.freeze
|
|
230
|
+
true
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
##
|
|
234
|
+
# Death handlers are called when all retries for a job have been exhausted and
|
|
235
|
+
# the job dies. It's the notification to your application
|
|
236
|
+
# that this job will not succeed without manual intervention.
|
|
237
|
+
#
|
|
238
|
+
# Sidekiq.configure_server do |config|
|
|
239
|
+
# config.death_handlers << ->(job, ex) do
|
|
240
|
+
# end
|
|
241
|
+
# end
|
|
242
|
+
def death_handlers
|
|
243
|
+
@options[:death_handlers]
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# How frequently Redis should be checked by a random Sidekiq process for
|
|
247
|
+
# scheduled and retriable jobs. Each individual process will take turns by
|
|
248
|
+
# waiting some multiple of this value.
|
|
249
|
+
#
|
|
250
|
+
# See sidekiq/scheduled.rb for an in-depth explanation of this value
|
|
251
|
+
def average_scheduled_poll_interval=(interval)
|
|
252
|
+
@options[:average_scheduled_poll_interval] = interval
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Register a proc to handle any error which occurs within the Sidekiq process.
|
|
256
|
+
#
|
|
257
|
+
# Sidekiq.configure_server do |config|
|
|
258
|
+
# config.error_handlers << proc {|ex,ctx_hash,config| MyErrorService.notify(ex, ctx_hash) }
|
|
259
|
+
# end
|
|
260
|
+
#
|
|
261
|
+
# The default error handler logs errors to @logger.
|
|
262
|
+
def error_handlers
|
|
263
|
+
@options[:error_handlers]
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# Register a block to run at a point in the Sidekiq lifecycle.
|
|
267
|
+
# :startup, :quiet, :shutdown, or :exit are valid events.
|
|
268
|
+
#
|
|
269
|
+
# Sidekiq.configure_server do |config|
|
|
270
|
+
# config.on(:shutdown) do
|
|
271
|
+
# puts "Goodbye cruel world!"
|
|
272
|
+
# end
|
|
273
|
+
# end
|
|
274
|
+
def on(event, &block)
|
|
275
|
+
raise ArgumentError, "Symbols only please: #{event}" unless event.is_a?(Symbol)
|
|
276
|
+
raise ArgumentError, "Invalid event name: #{event}" unless @options[:lifecycle_events].key?(event)
|
|
277
|
+
@options[:lifecycle_events][event] << block
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def logger
|
|
281
|
+
@logger ||= Sidekiq::Logger.new($stdout, level: :info).tap do |log|
|
|
282
|
+
log.level = Logger::INFO
|
|
283
|
+
log.formatter = if ENV["DYNO"]
|
|
284
|
+
Sidekiq::Logger::Formatters::WithoutTimestamp.new
|
|
285
|
+
else
|
|
286
|
+
Sidekiq::Logger::Formatters::Pretty.new
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def logger=(logger)
|
|
292
|
+
if logger.nil?
|
|
293
|
+
self.logger.level = Logger::FATAL
|
|
294
|
+
return
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
@logger = logger
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
# INTERNAL USE ONLY
|
|
301
|
+
def handle_exception(ex, ctx = {})
|
|
302
|
+
if @options[:error_handlers].empty?
|
|
303
|
+
logger.error { "No error handlers configured, logging exception directly" }
|
|
304
|
+
logger.error { ex }
|
|
305
|
+
end
|
|
306
|
+
@options[:error_handlers].each do |handler|
|
|
307
|
+
handler.call(ex, ctx, self)
|
|
308
|
+
rescue Exception => e
|
|
309
|
+
l = logger
|
|
310
|
+
l.error "!!! ERROR HANDLER THREW AN ERROR !!!"
|
|
311
|
+
l.error e
|
|
312
|
+
l.error e.backtrace.join("\n") unless e.backtrace.nil?
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
end
|