sidekiq 6.2.2 → 8.1.5
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 +726 -11
- data/LICENSE.txt +9 -0
- data/README.md +70 -39
- data/bin/kiq +17 -0
- data/bin/lint-herb +13 -0
- data/bin/multi_queue_bench +271 -0
- data/bin/sidekiq +4 -9
- data/bin/sidekiqload +214 -115
- data/bin/sidekiqmon +4 -1
- data/bin/webload +69 -0
- data/lib/active_job/queue_adapters/sidekiq_adapter.rb +124 -0
- data/lib/generators/sidekiq/job_generator.rb +71 -0
- data/lib/generators/sidekiq/templates/{worker.rb.erb → job.rb.erb} +3 -3
- data/lib/generators/sidekiq/templates/{worker_spec.rb.erb → job_spec.rb.erb} +1 -1
- data/lib/generators/sidekiq/templates/{worker_test.rb.erb → job_test.rb.erb} +1 -1
- data/lib/sidekiq/api.rb +729 -264
- data/lib/sidekiq/capsule.rb +135 -0
- data/lib/sidekiq/cli.rb +124 -100
- data/lib/sidekiq/client.rb +153 -106
- data/lib/sidekiq/component.rb +132 -0
- data/lib/sidekiq/config.rb +320 -0
- data/lib/sidekiq/deploy.rb +64 -0
- data/lib/sidekiq/embedded.rb +64 -0
- data/lib/sidekiq/fetch.rb +27 -26
- 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 -5
- data/lib/sidekiq/job_logger.rb +23 -32
- data/lib/sidekiq/job_retry.rb +141 -68
- data/lib/sidekiq/job_util.rb +113 -0
- data/lib/sidekiq/launcher.rb +122 -98
- data/lib/sidekiq/loader.rb +57 -0
- data/lib/sidekiq/logger.rb +27 -106
- data/lib/sidekiq/manager.rb +41 -43
- data/lib/sidekiq/metrics/query.rb +184 -0
- data/lib/sidekiq/metrics/shared.rb +109 -0
- data/lib/sidekiq/metrics/tracking.rb +153 -0
- data/lib/sidekiq/middleware/chain.rb +96 -51
- data/lib/sidekiq/middleware/current_attributes.rb +120 -0
- data/lib/sidekiq/middleware/i18n.rb +8 -4
- data/lib/sidekiq/middleware/modules.rb +23 -0
- data/lib/sidekiq/monitor.rb +16 -6
- data/lib/sidekiq/paginator.rb +37 -10
- data/lib/sidekiq/processor.rb +105 -87
- data/lib/sidekiq/profiler.rb +73 -0
- data/lib/sidekiq/rails.rb +49 -36
- data/lib/sidekiq/redis_client_adapter.rb +117 -0
- data/lib/sidekiq/redis_connection.rb +55 -86
- data/lib/sidekiq/ring_buffer.rb +32 -0
- data/lib/sidekiq/scheduled.rb +106 -50
- 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 +59 -0
- data/lib/sidekiq/tui/controls.rb +53 -0
- data/lib/sidekiq/tui/filtering.rb +53 -0
- data/lib/sidekiq/tui/tabs/base_tab.rb +204 -0
- data/lib/sidekiq/tui/tabs/busy.rb +118 -0
- data/lib/sidekiq/tui/tabs/dead.rb +19 -0
- data/lib/sidekiq/tui/tabs/home.rb +144 -0
- data/lib/sidekiq/tui/tabs/metrics.rb +131 -0
- data/lib/sidekiq/tui/tabs/queues.rb +95 -0
- data/lib/sidekiq/tui/tabs/retries.rb +19 -0
- data/lib/sidekiq/tui/tabs/scheduled.rb +19 -0
- data/lib/sidekiq/tui/tabs/set_tab.rb +96 -0
- data/lib/sidekiq/tui/tabs.rb +15 -0
- data/lib/sidekiq/tui.rb +382 -0
- data/lib/sidekiq/version.rb +6 -1
- data/lib/sidekiq/web/action.rb +149 -64
- data/lib/sidekiq/web/application.rb +376 -268
- data/lib/sidekiq/web/config.rb +117 -0
- data/lib/sidekiq/web/helpers.rb +213 -87
- data/lib/sidekiq/web/router.rb +61 -74
- data/lib/sidekiq/web.rb +71 -100
- data/lib/sidekiq/worker_compatibility_alias.rb +13 -0
- data/lib/sidekiq.rb +95 -196
- data/sidekiq.gemspec +14 -11
- data/web/assets/images/logo.png +0 -0
- data/web/assets/images/status.png +0 -0
- data/web/assets/javascripts/application.js +171 -57
- data/web/assets/javascripts/base-charts.js +120 -0
- data/web/assets/javascripts/chart.min.js +13 -0
- data/web/assets/javascripts/chartjs-adapter-date-fns.min.js +7 -0
- data/web/assets/javascripts/chartjs-plugin-annotation.min.js +7 -0
- data/web/assets/javascripts/dashboard-charts.js +194 -0
- data/web/assets/javascripts/dashboard.js +41 -274
- data/web/assets/javascripts/metrics.js +280 -0
- 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 +45 -24
- data/web/locales/en.yml +93 -69
- 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 +61 -59
- data/web/locales/it.yml +94 -54
- data/web/locales/ja.yml +74 -68
- 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} +85 -56
- 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 +62 -60
- 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} +44 -9
- 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 +151 -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 +130 -61
- data/LICENSE +0 -9
- data/lib/generators/sidekiq/worker_generator.rb +0 -57
- data/lib/sidekiq/delay.rb +0 -41
- data/lib/sidekiq/exception_handler.rb +0 -27
- 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/util.rb +0 -95
- data/lib/sidekiq/web/csrf_protection.rb +0 -180
- data/lib/sidekiq/worker.rb +0 -244
- data/web/assets/stylesheets/application-dark.css +0 -147
- data/web/assets/stylesheets/application-rtl.css +0 -246
- data/web/assets/stylesheets/application.css +0 -1053
- 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 -68
- 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 -7
- data/web/views/_status.erb +0 -4
- data/web/views/_summary.erb +0 -40
- data/web/views/busy.erb +0 -132
- data/web/views/dashboard.erb +0 -83
- data/web/views/dead.erb +0 -34
- data/web/views/layout.erb +0 -42
- data/web/views/morgue.erb +0 -78
- data/web/views/queue.erb +0 -55
- 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/lib/sidekiq/client.rb
CHANGED
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
require "securerandom"
|
|
4
4
|
require "sidekiq/middleware/chain"
|
|
5
|
+
require "sidekiq/job_util"
|
|
5
6
|
|
|
6
7
|
module Sidekiq
|
|
7
8
|
class Client
|
|
9
|
+
include Sidekiq::JobUtil
|
|
10
|
+
|
|
8
11
|
##
|
|
9
12
|
# Define client-side middleware:
|
|
10
13
|
#
|
|
@@ -12,13 +15,12 @@ module Sidekiq
|
|
|
12
15
|
# client.middleware do |chain|
|
|
13
16
|
# chain.use MyClientMiddleware
|
|
14
17
|
# end
|
|
15
|
-
# client.push('class' => '
|
|
18
|
+
# client.push('class' => 'SomeJob', 'args' => [1,2,3])
|
|
16
19
|
#
|
|
17
20
|
# All client instances default to the globally-defined
|
|
18
21
|
# Sidekiq.client_middleware but you can change as necessary.
|
|
19
22
|
#
|
|
20
23
|
def middleware(&block)
|
|
21
|
-
@chain ||= Sidekiq.client_middleware
|
|
22
24
|
if block
|
|
23
25
|
@chain = @chain.dup
|
|
24
26
|
yield @chain
|
|
@@ -28,49 +30,81 @@ module Sidekiq
|
|
|
28
30
|
|
|
29
31
|
attr_accessor :redis_pool
|
|
30
32
|
|
|
31
|
-
# Sidekiq::Client
|
|
32
|
-
#
|
|
33
|
-
# Sidekiq jobs across several Redis instances (for scalability
|
|
34
|
-
# reasons, e.g.)
|
|
33
|
+
# Sidekiq::Client is responsible for pushing job payloads to Redis.
|
|
34
|
+
# Requires the :pool or :config keyword argument.
|
|
35
35
|
#
|
|
36
|
-
# Sidekiq::Client.new(
|
|
36
|
+
# Sidekiq::Client.new(pool: Sidekiq::RedisConnection.create)
|
|
37
37
|
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
#
|
|
41
|
-
|
|
42
|
-
|
|
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
|
|
43
74
|
end
|
|
44
75
|
|
|
45
76
|
##
|
|
46
77
|
# The main method used to push a job to Redis. Accepts a number of options:
|
|
47
78
|
#
|
|
48
79
|
# queue - the named queue to use, default 'default'
|
|
49
|
-
# class - the
|
|
80
|
+
# class - the job class to call, required
|
|
50
81
|
# args - an array of simple arguments to the perform method, must be JSON-serializable
|
|
51
82
|
# at - timestamp to schedule the job (optional), must be Numeric (e.g. Time.now.to_f)
|
|
52
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
|
|
53
85
|
# backtrace - whether to save any error backtrace, default false
|
|
54
86
|
#
|
|
55
87
|
# If class is set to the class name, the jobs' options will be based on Sidekiq's default
|
|
56
|
-
#
|
|
88
|
+
# job options. Otherwise, they will be based on the job class's options.
|
|
57
89
|
#
|
|
58
|
-
# Any options valid for a
|
|
90
|
+
# Any options valid for a job class's sidekiq_options are also available here.
|
|
59
91
|
#
|
|
60
|
-
# All
|
|
92
|
+
# All keys must be strings, not symbols. NB: because we are serializing to JSON, all
|
|
61
93
|
# symbols in 'args' will be converted to strings. Note that +backtrace: true+ can take quite a bit of
|
|
62
94
|
# space in Redis; a large volume of failing jobs can start Redis swapping if you aren't careful.
|
|
63
95
|
#
|
|
64
96
|
# Returns a unique Job ID. If middleware stops the job, nil will be returned instead.
|
|
65
97
|
#
|
|
66
98
|
# Example:
|
|
67
|
-
# push('queue' => 'my_queue', 'class' =>
|
|
99
|
+
# push('queue' => 'my_queue', 'class' => MyJob, 'args' => ['foo', 1, :bat => 'bar'])
|
|
68
100
|
#
|
|
69
101
|
def push(item)
|
|
70
102
|
normed = normalize_item(item)
|
|
71
|
-
payload =
|
|
72
|
-
|
|
103
|
+
payload = middleware.invoke(item["class"], normed, normed["queue"], @redis_pool) do
|
|
104
|
+
normed
|
|
105
|
+
end
|
|
73
106
|
if payload
|
|
107
|
+
verify_json(payload)
|
|
74
108
|
raw_push([payload])
|
|
75
109
|
payload["jid"]
|
|
76
110
|
end
|
|
@@ -78,37 +112,76 @@ module Sidekiq
|
|
|
78
112
|
|
|
79
113
|
##
|
|
80
114
|
# Push a large number of jobs to Redis. This method cuts out the redis
|
|
81
|
-
# network round trip latency.
|
|
82
|
-
# 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.
|
|
83
118
|
# A large number of jobs can cause a bit of Redis command processing latency.
|
|
84
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
|
+
#
|
|
85
128
|
# Takes the same arguments as #push except that args is expected to be
|
|
86
129
|
# an Array of Arrays. All other keys are duplicated for each job. Each job
|
|
87
130
|
# is run through the client middleware pipeline and each job gets its own Job ID
|
|
88
131
|
# as normal.
|
|
89
132
|
#
|
|
90
|
-
# Returns an array of the of pushed jobs' jids
|
|
91
|
-
#
|
|
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
|
+
#
|
|
92
139
|
def push_bulk(items)
|
|
93
140
|
args = items["args"]
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
at = items.delete("at")
|
|
98
|
-
raise ArgumentError, "Job 'at' must be a Numeric or an Array of Numeric timestamps" if at && (Array(at).empty? || !Array(at).all?(Numeric))
|
|
141
|
+
at = items.delete("at") || items.delete(:at)
|
|
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) })
|
|
99
143
|
raise ArgumentError, "Job 'at' Array must have same size as 'args' Array" if at.is_a?(Array) && at.size != args.size
|
|
100
144
|
|
|
145
|
+
# Use a smaller batch size by default for scheduled jobs since adding to sorted sets is more costly.
|
|
146
|
+
batch_size = items.delete(:batch_size) || items.delete("batch_size") || (at ? 100 : 1_000)
|
|
147
|
+
|
|
148
|
+
jid = items.delete("jid")
|
|
149
|
+
raise ArgumentError, "Explicitly passing 'jid' when pushing more than one job is not supported" if jid && args.size > 1
|
|
150
|
+
|
|
151
|
+
spread_interval = items.delete(:spread_interval) || items.delete("spread_interval")
|
|
152
|
+
raise ArgumentError, "Jobs 'spread_interval' must be a positive Numeric" if spread_interval && (!spread_interval.is_a?(Numeric) || spread_interval <= 0)
|
|
153
|
+
raise ArgumentError, "Only one of 'at' or 'spread_interval' can be provided" if at && spread_interval
|
|
154
|
+
|
|
155
|
+
if !at && spread_interval
|
|
156
|
+
# Do not use spread interval smaller than pooling interval.
|
|
157
|
+
spread_interval = [spread_interval, 5].max
|
|
158
|
+
now = Time.now.to_f
|
|
159
|
+
at = args.map { now + rand * spread_interval }
|
|
160
|
+
end
|
|
161
|
+
|
|
101
162
|
normed = normalize_item(items)
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
|
105
178
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
179
|
+
to_push = payloads.compact
|
|
180
|
+
raw_push(to_push) unless to_push.empty?
|
|
181
|
+
payloads.map { |payload| payload&.[]("jid") }
|
|
182
|
+
end
|
|
109
183
|
|
|
110
|
-
|
|
111
|
-
payloads.collect { |payload| payload["jid"] }
|
|
184
|
+
result.is_a?(Enumerator::Lazy) ? result.force : result
|
|
112
185
|
end
|
|
113
186
|
|
|
114
187
|
# Allows sharding of jobs across any number of Redis instances. All jobs
|
|
@@ -116,8 +189,8 @@ module Sidekiq
|
|
|
116
189
|
#
|
|
117
190
|
# pool = ConnectionPool.new { Redis.new }
|
|
118
191
|
# Sidekiq::Client.via(pool) do
|
|
119
|
-
#
|
|
120
|
-
#
|
|
192
|
+
# SomeJob.perform_async(1,2,3)
|
|
193
|
+
# SomeOtherJob.perform_async(1,2,3)
|
|
121
194
|
# end
|
|
122
195
|
#
|
|
123
196
|
# Generally this is only needed for very large Sidekiq installs processing
|
|
@@ -125,11 +198,11 @@ module Sidekiq
|
|
|
125
198
|
# you cannot scale any other way (e.g. splitting your app into smaller apps).
|
|
126
199
|
def self.via(pool)
|
|
127
200
|
raise ArgumentError, "No pool given" if pool.nil?
|
|
128
|
-
current_sidekiq_pool = Thread.current[:
|
|
129
|
-
Thread.current[:
|
|
201
|
+
current_sidekiq_pool = Thread.current[:sidekiq_redis_pool]
|
|
202
|
+
Thread.current[:sidekiq_redis_pool] = pool
|
|
130
203
|
yield
|
|
131
204
|
ensure
|
|
132
|
-
Thread.current[:
|
|
205
|
+
Thread.current[:sidekiq_redis_pool] = current_sidekiq_pool
|
|
133
206
|
end
|
|
134
207
|
|
|
135
208
|
class << self
|
|
@@ -137,15 +210,15 @@ module Sidekiq
|
|
|
137
210
|
new.push(item)
|
|
138
211
|
end
|
|
139
212
|
|
|
140
|
-
def push_bulk(
|
|
141
|
-
new.push_bulk(
|
|
213
|
+
def push_bulk(...)
|
|
214
|
+
new.push_bulk(...)
|
|
142
215
|
end
|
|
143
216
|
|
|
144
217
|
# Resque compatibility helpers. Note all helpers
|
|
145
|
-
# should go through
|
|
218
|
+
# should go through Sidekiq::Job#client_push.
|
|
146
219
|
#
|
|
147
220
|
# Example usage:
|
|
148
|
-
# Sidekiq::Client.enqueue(
|
|
221
|
+
# Sidekiq::Client.enqueue(MyJob, 'foo', 1, :bat => 'bar')
|
|
149
222
|
#
|
|
150
223
|
# Messages are enqueued to the 'default' queue.
|
|
151
224
|
#
|
|
@@ -154,19 +227,19 @@ module Sidekiq
|
|
|
154
227
|
end
|
|
155
228
|
|
|
156
229
|
# Example usage:
|
|
157
|
-
# Sidekiq::Client.enqueue_to(:queue_name,
|
|
230
|
+
# Sidekiq::Client.enqueue_to(:queue_name, MyJob, 'foo', 1, :bat => 'bar')
|
|
158
231
|
#
|
|
159
232
|
def enqueue_to(queue, klass, *args)
|
|
160
233
|
klass.client_push("queue" => queue, "class" => klass, "args" => args)
|
|
161
234
|
end
|
|
162
235
|
|
|
163
236
|
# Example usage:
|
|
164
|
-
# Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes,
|
|
237
|
+
# Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes, MyJob, 'foo', 1, :bat => 'bar')
|
|
165
238
|
#
|
|
166
239
|
def enqueue_to_in(queue, interval, klass, *args)
|
|
167
240
|
int = interval.to_f
|
|
168
241
|
now = Time.now.to_f
|
|
169
|
-
ts = (int < 1_000_000_000 ? now + int : int)
|
|
242
|
+
ts = ((int < 1_000_000_000) ? now + int : int)
|
|
170
243
|
|
|
171
244
|
item = {"class" => klass, "args" => args, "at" => ts, "queue" => queue}
|
|
172
245
|
item.delete("at") if ts <= now
|
|
@@ -175,7 +248,7 @@ module Sidekiq
|
|
|
175
248
|
end
|
|
176
249
|
|
|
177
250
|
# Example usage:
|
|
178
|
-
# Sidekiq::Client.enqueue_in(3.minutes,
|
|
251
|
+
# Sidekiq::Client.enqueue_in(3.minutes, MyJob, 'foo', 1, :bat => 'bar')
|
|
179
252
|
#
|
|
180
253
|
def enqueue_in(interval, klass, *args)
|
|
181
254
|
klass.perform_in(interval, *args)
|
|
@@ -186,8 +259,23 @@ module Sidekiq
|
|
|
186
259
|
|
|
187
260
|
def raw_push(payloads)
|
|
188
261
|
@redis_pool.with do |conn|
|
|
189
|
-
|
|
190
|
-
|
|
262
|
+
retryable = true
|
|
263
|
+
begin
|
|
264
|
+
conn.pipelined do |pipeline|
|
|
265
|
+
atomic_push(pipeline, payloads)
|
|
266
|
+
end
|
|
267
|
+
rescue RedisClient::Error => ex
|
|
268
|
+
# 2550 Failover can cause the server to become a replica, need
|
|
269
|
+
# to disconnect and reopen the socket to get back to the primary.
|
|
270
|
+
# 4495 Use the same logic if we have a "Not enough replicas" error from the primary
|
|
271
|
+
# 4985 Use the same logic when a blocking command is force-unblocked
|
|
272
|
+
# The retry logic is copied from sidekiq.rb
|
|
273
|
+
if retryable && ex.message =~ /READONLY|NOREPLICAS|UNBLOCKED/
|
|
274
|
+
conn.close
|
|
275
|
+
retryable = false
|
|
276
|
+
retry
|
|
277
|
+
end
|
|
278
|
+
raise
|
|
191
279
|
end
|
|
192
280
|
end
|
|
193
281
|
true
|
|
@@ -195,64 +283,23 @@ module Sidekiq
|
|
|
195
283
|
|
|
196
284
|
def atomic_push(conn, payloads)
|
|
197
285
|
if payloads.first.key?("at")
|
|
198
|
-
conn.zadd("schedule", payloads.
|
|
199
|
-
at = hash
|
|
286
|
+
conn.zadd("schedule", payloads.flat_map { |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")
|
|
200
290
|
[at, Sidekiq.dump_json(hash)]
|
|
201
291
|
})
|
|
202
292
|
else
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
def process_single(worker_class, item)
|
|
215
|
-
queue = item["queue"]
|
|
216
|
-
|
|
217
|
-
middleware.invoke(worker_class, item, queue, @redis_pool) do
|
|
218
|
-
item
|
|
219
|
-
end
|
|
220
|
-
end
|
|
221
|
-
|
|
222
|
-
def validate(item)
|
|
223
|
-
raise(ArgumentError, "Job must be a Hash with 'class' and 'args' keys: `#{item}`") unless item.is_a?(Hash) && item.key?("class") && item.key?("args")
|
|
224
|
-
raise(ArgumentError, "Job args must be an Array: `#{item}`") unless item["args"].is_a?(Array)
|
|
225
|
-
raise(ArgumentError, "Job class must be either a Class or String representation of the class name: `#{item}`") unless item["class"].is_a?(Class) || item["class"].is_a?(String)
|
|
226
|
-
raise(ArgumentError, "Job 'at' must be a Numeric timestamp: `#{item}`") if item.key?("at") && !item["at"].is_a?(Numeric)
|
|
227
|
-
raise(ArgumentError, "Job tags must be an Array: `#{item}`") if item["tags"] && !item["tags"].is_a?(Array)
|
|
228
|
-
end
|
|
229
|
-
|
|
230
|
-
def normalize_item(item)
|
|
231
|
-
validate(item)
|
|
232
|
-
# raise(ArgumentError, "Arguments must be native JSON types, see https://github.com/mperham/sidekiq/wiki/Best-Practices") unless JSON.load(JSON.dump(item['args'])) == item['args']
|
|
233
|
-
|
|
234
|
-
# merge in the default sidekiq_options for the item's class and/or wrapped element
|
|
235
|
-
# this allows ActiveJobs to control sidekiq_options too.
|
|
236
|
-
defaults = normalized_hash(item["class"])
|
|
237
|
-
defaults = defaults.merge(item["wrapped"].get_sidekiq_options) if item["wrapped"].respond_to?("get_sidekiq_options")
|
|
238
|
-
item = defaults.merge(item)
|
|
239
|
-
|
|
240
|
-
raise(ArgumentError, "Job must include a valid queue name") if item["queue"].nil? || item["queue"] == ""
|
|
241
|
-
|
|
242
|
-
item["class"] = item["class"].to_s
|
|
243
|
-
item["queue"] = item["queue"].to_s
|
|
244
|
-
item["jid"] ||= SecureRandom.hex(12)
|
|
245
|
-
item["created_at"] ||= Time.now.to_f
|
|
246
|
-
|
|
247
|
-
item
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
def normalized_hash(item_class)
|
|
251
|
-
if item_class.is_a?(Class)
|
|
252
|
-
raise(ArgumentError, "Message must include a Sidekiq::Worker class, not class name: #{item_class.ancestors.inspect}") unless item_class.respond_to?("get_sidekiq_options")
|
|
253
|
-
item_class.get_sidekiq_options
|
|
254
|
-
else
|
|
255
|
-
Sidekiq.default_worker_options
|
|
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
|
|
256
303
|
end
|
|
257
304
|
end
|
|
258
305
|
end
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
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
|
+
|
|
21
|
+
##
|
|
22
|
+
# Sidekiq::Component provides a set of utility methods depending only
|
|
23
|
+
# on Sidekiq::Config. It assumes a config instance is available at @config.
|
|
24
|
+
module Component # :nodoc:
|
|
25
|
+
attr_reader :config
|
|
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
|
+
|
|
37
|
+
def watchdog(last_words)
|
|
38
|
+
yield
|
|
39
|
+
rescue Exception => ex
|
|
40
|
+
handle_exception(ex, {context: last_words})
|
|
41
|
+
raise ex
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def safe_thread(name, priority: nil, &block)
|
|
45
|
+
Thread.new do
|
|
46
|
+
Thread.current.name = "sidekiq.#{name}"
|
|
47
|
+
watchdog(name, &block)
|
|
48
|
+
end.tap { |t| t.priority = (priority || config.thread_priority || DEFAULT_THREAD_PRIORITY) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def logger
|
|
52
|
+
config.logger
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def redis(&block)
|
|
56
|
+
config.redis(&block)
|
|
57
|
+
end
|
|
58
|
+
|
|
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
|
|
63
|
+
Thread.current["sidekiq_tid"] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def hostname
|
|
67
|
+
ENV["DYNO"] || Socket.gethostname
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def process_nonce
|
|
71
|
+
@@process_nonce ||= SecureRandom.hex(6)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def identity
|
|
75
|
+
@@identity ||= "#{hostname}:#{::Process.pid}:#{process_nonce}"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def handle_exception(ex, ctx = {})
|
|
79
|
+
config.handle_exception(ex, ctx)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def fire_event(event, options = {})
|
|
83
|
+
oneshot = options.fetch(:oneshot, true)
|
|
84
|
+
reverse = options[:reverse]
|
|
85
|
+
reraise = options[:reraise]
|
|
86
|
+
logger.debug("Firing #{event} event") if oneshot
|
|
87
|
+
|
|
88
|
+
arr = config[:lifecycle_events][event]
|
|
89
|
+
arr.reverse! if reverse
|
|
90
|
+
arr.each do |block|
|
|
91
|
+
block.call
|
|
92
|
+
rescue => ex
|
|
93
|
+
handle_exception(ex, {context: "Exception during Sidekiq lifecycle event.", event: event})
|
|
94
|
+
raise ex if reraise
|
|
95
|
+
end
|
|
96
|
+
arr.clear if oneshot # once we've fired an event, we never fire it again
|
|
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
|
|
131
|
+
end
|
|
132
|
+
end
|