sidekiq 6.0.4 → 7.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sidekiq might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Changes.md +491 -10
- data/LICENSE.txt +9 -0
- data/README.md +47 -38
- data/bin/sidekiq +22 -3
- data/bin/sidekiqload +207 -117
- data/bin/sidekiqmon +4 -1
- 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/{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 +418 -233
- data/lib/sidekiq/capsule.rb +127 -0
- data/lib/sidekiq/cli.rb +122 -86
- data/lib/sidekiq/client.rb +109 -97
- data/lib/sidekiq/{util.rb → component.rb} +14 -13
- data/lib/sidekiq/config.rb +287 -0
- data/lib/sidekiq/deploy.rb +62 -0
- data/lib/sidekiq/embedded.rb +61 -0
- data/lib/sidekiq/fetch.rb +43 -35
- data/lib/sidekiq/{worker.rb → job.rb} +161 -34
- data/lib/sidekiq/job_logger.rb +18 -30
- data/lib/sidekiq/job_retry.rb +102 -63
- data/lib/sidekiq/job_util.rb +107 -0
- data/lib/sidekiq/launcher.rb +180 -88
- data/lib/sidekiq/logger.rb +13 -47
- data/lib/sidekiq/manager.rb +40 -41
- data/lib/sidekiq/metrics/query.rb +155 -0
- data/lib/sidekiq/metrics/shared.rb +95 -0
- data/lib/sidekiq/metrics/tracking.rb +136 -0
- data/lib/sidekiq/middleware/chain.rb +99 -52
- data/lib/sidekiq/middleware/current_attributes.rb +95 -0
- data/lib/sidekiq/middleware/i18n.rb +6 -4
- data/lib/sidekiq/middleware/modules.rb +21 -0
- data/lib/sidekiq/monitor.rb +18 -5
- data/lib/sidekiq/paginator.rb +17 -9
- data/lib/sidekiq/processor.rb +81 -80
- data/lib/sidekiq/rails.rb +37 -21
- data/lib/sidekiq/redis_client_adapter.rb +111 -0
- data/lib/sidekiq/redis_connection.rb +22 -87
- data/lib/sidekiq/ring_buffer.rb +29 -0
- data/lib/sidekiq/scheduled.rb +102 -39
- data/lib/sidekiq/sd_notify.rb +149 -0
- data/lib/sidekiq/systemd.rb +24 -0
- data/lib/sidekiq/testing/inline.rb +4 -4
- data/lib/sidekiq/testing.rb +68 -78
- data/lib/sidekiq/transaction_aware_client.rb +44 -0
- data/lib/sidekiq/version.rb +2 -1
- data/lib/sidekiq/web/action.rb +3 -3
- data/lib/sidekiq/web/application.rb +132 -28
- data/lib/sidekiq/web/csrf_protection.rb +180 -0
- data/lib/sidekiq/web/helpers.rb +93 -65
- data/lib/sidekiq/web/router.rb +6 -5
- data/lib/sidekiq/web.rb +43 -74
- data/lib/sidekiq/worker_compatibility_alias.rb +13 -0
- data/lib/sidekiq.rb +86 -199
- data/sidekiq.gemspec +17 -8
- data/web/assets/images/apple-touch-icon.png +0 -0
- data/web/assets/javascripts/application.js +146 -61
- 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 +182 -0
- data/web/assets/javascripts/dashboard.js +35 -274
- data/web/assets/javascripts/metrics.js +298 -0
- data/web/assets/stylesheets/application-dark.css +146 -124
- data/web/assets/stylesheets/application-rtl.css +2 -95
- data/web/assets/stylesheets/application.css +109 -529
- data/web/locales/ar.yml +71 -65
- data/web/locales/cs.yml +62 -62
- data/web/locales/da.yml +60 -53
- data/web/locales/de.yml +65 -65
- data/web/locales/el.yml +43 -24
- data/web/locales/en.yml +85 -67
- data/web/locales/es.yml +70 -54
- data/web/locales/fa.yml +65 -65
- data/web/locales/fr.yml +83 -62
- data/web/locales/gd.yml +99 -0
- data/web/locales/he.yml +65 -64
- data/web/locales/hi.yml +59 -59
- data/web/locales/it.yml +53 -53
- data/web/locales/ja.yml +75 -65
- data/web/locales/ko.yml +52 -52
- data/web/locales/lt.yml +83 -0
- data/web/locales/nb.yml +61 -61
- 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 -63
- data/web/locales/sv.yml +53 -53
- data/web/locales/ta.yml +60 -60
- data/web/locales/uk.yml +62 -61
- data/web/locales/ur.yml +64 -64
- 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 +6 -3
- data/web/views/_job_info.erb +19 -3
- data/web/views/_metrics_period_select.erb +12 -0
- data/web/views/_nav.erb +1 -1
- data/web/views/_paging.erb +2 -0
- data/web/views/_poll_link.erb +3 -6
- data/web/views/_summary.erb +7 -7
- data/web/views/busy.erb +73 -26
- data/web/views/dashboard.erb +48 -18
- data/web/views/dead.erb +1 -1
- data/web/views/filtering.erb +7 -0
- data/web/views/layout.erb +3 -2
- data/web/views/metrics.erb +91 -0
- data/web/views/metrics_for_job.erb +59 -0
- data/web/views/morgue.erb +11 -15
- data/web/views/queue.erb +25 -25
- data/web/views/queues.erb +13 -7
- data/web/views/retries.erb +12 -16
- data/web/views/retry.erb +1 -1
- data/web/views/scheduled.erb +13 -14
- metadata +65 -56
- data/.circleci/config.yml +0 -82
- data/.github/contributing.md +0 -32
- data/.github/issue_template.md +0 -11
- data/.gitignore +0 -13
- data/.standard.yml +0 -20
- data/3.0-Upgrade.md +0 -70
- data/4.0-Upgrade.md +0 -53
- data/5.0-Upgrade.md +0 -56
- data/6.0-Upgrade.md +0 -72
- data/COMM-LICENSE +0 -97
- data/Ent-2.0-Upgrade.md +0 -37
- data/Ent-Changes.md +0 -256
- data/Gemfile +0 -24
- data/Gemfile.lock +0 -199
- data/LICENSE +0 -9
- data/Pro-2.0-Upgrade.md +0 -138
- data/Pro-3.0-Upgrade.md +0 -44
- data/Pro-4.0-Upgrade.md +0 -35
- data/Pro-5.0-Upgrade.md +0 -25
- data/Pro-Changes.md +0 -776
- data/Rakefile +0 -10
- data/code_of_conduct.md +0 -50
- 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 -47
- data/lib/sidekiq/extensions/active_record.rb +0 -42
- data/lib/sidekiq/extensions/class_methods.rb +0 -42
- data/lib/sidekiq/extensions/generic_proxy.rb +0 -31
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,14 +15,13 @@ 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
|
-
|
22
|
-
if block_given?
|
24
|
+
if block
|
23
25
|
@chain = @chain.dup
|
24
26
|
yield @chain
|
25
27
|
end
|
@@ -28,49 +30,66 @@ 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
|
43
59
|
end
|
44
60
|
|
45
61
|
##
|
46
62
|
# The main method used to push a job to Redis. Accepts a number of options:
|
47
63
|
#
|
48
64
|
# queue - the named queue to use, default 'default'
|
49
|
-
# class - the
|
65
|
+
# class - the job class to call, required
|
50
66
|
# args - an array of simple arguments to the perform method, must be JSON-serializable
|
51
67
|
# at - timestamp to schedule the job (optional), must be Numeric (e.g. Time.now.to_f)
|
52
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
72
|
# If class is set to the class name, the jobs' options will be based on Sidekiq's default
|
56
|
-
#
|
73
|
+
# job options. Otherwise, they will be based on the job class's options.
|
57
74
|
#
|
58
|
-
# Any options valid for a
|
75
|
+
# Any options valid for a job class's sidekiq_options are also available here.
|
59
76
|
#
|
60
|
-
# All
|
77
|
+
# All keys must be strings, not symbols. NB: because we are serializing to JSON, all
|
61
78
|
# symbols in 'args' will be converted to strings. Note that +backtrace: true+ can take quite a bit of
|
62
79
|
# space in Redis; a large volume of failing jobs can start Redis swapping if you aren't careful.
|
63
80
|
#
|
64
81
|
# Returns a unique Job ID. If middleware stops the job, nil will be returned instead.
|
65
82
|
#
|
66
83
|
# Example:
|
67
|
-
# push('queue' => 'my_queue', 'class' =>
|
84
|
+
# push('queue' => 'my_queue', 'class' => MyJob, 'args' => ['foo', 1, :bat => 'bar'])
|
68
85
|
#
|
69
86
|
def push(item)
|
70
87
|
normed = normalize_item(item)
|
71
|
-
payload =
|
72
|
-
|
88
|
+
payload = middleware.invoke(item["class"], normed, normed["queue"], @redis_pool) do
|
89
|
+
normed
|
90
|
+
end
|
73
91
|
if payload
|
92
|
+
verify_json(payload)
|
74
93
|
raw_push([payload])
|
75
94
|
payload["jid"]
|
76
95
|
end
|
@@ -78,8 +97,9 @@ module Sidekiq
|
|
78
97
|
|
79
98
|
##
|
80
99
|
# Push a large number of jobs to Redis. This method cuts out the redis
|
81
|
-
# network round trip latency.
|
82
|
-
# 1000
|
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.
|
83
103
|
# A large number of jobs can cause a bit of Redis command processing latency.
|
84
104
|
#
|
85
105
|
# Takes the same arguments as #push except that args is expected to be
|
@@ -87,27 +107,45 @@ module Sidekiq
|
|
87
107
|
# is run through the client middleware pipeline and each job gets its own Job ID
|
88
108
|
# as normal.
|
89
109
|
#
|
90
|
-
# Returns an array of the of pushed jobs' jids
|
91
|
-
#
|
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
|
+
#
|
92
116
|
def push_bulk(items)
|
93
|
-
|
94
|
-
|
95
|
-
raise ArgumentError, "Bulk arguments must be an Array of Arrays: [[1], [2]]" unless arg.is_a?(Array)
|
96
|
-
|
117
|
+
batch_size = items.delete(:batch_size) || items.delete("batch_size") || 1_000
|
118
|
+
args = items["args"]
|
97
119
|
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))
|
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
|
99
125
|
|
100
126
|
normed = normalize_item(items)
|
101
|
-
|
102
|
-
|
103
|
-
|
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
|
104
131
|
|
105
|
-
|
106
|
-
|
107
|
-
|
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
|
108
147
|
|
109
|
-
|
110
|
-
payloads.collect { |payload| payload["jid"] }
|
148
|
+
result.is_a?(Enumerator::Lazy) ? result.force : result
|
111
149
|
end
|
112
150
|
|
113
151
|
# Allows sharding of jobs across any number of Redis instances. All jobs
|
@@ -115,8 +153,8 @@ module Sidekiq
|
|
115
153
|
#
|
116
154
|
# pool = ConnectionPool.new { Redis.new }
|
117
155
|
# Sidekiq::Client.via(pool) do
|
118
|
-
#
|
119
|
-
#
|
156
|
+
# SomeJob.perform_async(1,2,3)
|
157
|
+
# SomeOtherJob.perform_async(1,2,3)
|
120
158
|
# end
|
121
159
|
#
|
122
160
|
# Generally this is only needed for very large Sidekiq installs processing
|
@@ -124,11 +162,11 @@ module Sidekiq
|
|
124
162
|
# you cannot scale any other way (e.g. splitting your app into smaller apps).
|
125
163
|
def self.via(pool)
|
126
164
|
raise ArgumentError, "No pool given" if pool.nil?
|
127
|
-
current_sidekiq_pool = Thread.current[:
|
128
|
-
Thread.current[:
|
165
|
+
current_sidekiq_pool = Thread.current[:sidekiq_redis_pool]
|
166
|
+
Thread.current[:sidekiq_redis_pool] = pool
|
129
167
|
yield
|
130
168
|
ensure
|
131
|
-
Thread.current[:
|
169
|
+
Thread.current[:sidekiq_redis_pool] = current_sidekiq_pool
|
132
170
|
end
|
133
171
|
|
134
172
|
class << self
|
@@ -136,15 +174,15 @@ module Sidekiq
|
|
136
174
|
new.push(item)
|
137
175
|
end
|
138
176
|
|
139
|
-
def push_bulk(
|
140
|
-
new.push_bulk(
|
177
|
+
def push_bulk(...)
|
178
|
+
new.push_bulk(...)
|
141
179
|
end
|
142
180
|
|
143
181
|
# Resque compatibility helpers. Note all helpers
|
144
|
-
# should go through
|
182
|
+
# should go through Sidekiq::Job#client_push.
|
145
183
|
#
|
146
184
|
# Example usage:
|
147
|
-
# Sidekiq::Client.enqueue(
|
185
|
+
# Sidekiq::Client.enqueue(MyJob, 'foo', 1, :bat => 'bar')
|
148
186
|
#
|
149
187
|
# Messages are enqueued to the 'default' queue.
|
150
188
|
#
|
@@ -153,19 +191,19 @@ module Sidekiq
|
|
153
191
|
end
|
154
192
|
|
155
193
|
# Example usage:
|
156
|
-
# Sidekiq::Client.enqueue_to(:queue_name,
|
194
|
+
# Sidekiq::Client.enqueue_to(:queue_name, MyJob, 'foo', 1, :bat => 'bar')
|
157
195
|
#
|
158
196
|
def enqueue_to(queue, klass, *args)
|
159
197
|
klass.client_push("queue" => queue, "class" => klass, "args" => args)
|
160
198
|
end
|
161
199
|
|
162
200
|
# Example usage:
|
163
|
-
# Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes,
|
201
|
+
# Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes, MyJob, 'foo', 1, :bat => 'bar')
|
164
202
|
#
|
165
203
|
def enqueue_to_in(queue, interval, klass, *args)
|
166
204
|
int = interval.to_f
|
167
205
|
now = Time.now.to_f
|
168
|
-
ts = (int < 1_000_000_000 ? now + int : int)
|
206
|
+
ts = ((int < 1_000_000_000) ? now + int : int)
|
169
207
|
|
170
208
|
item = {"class" => klass, "args" => args, "at" => ts, "queue" => queue}
|
171
209
|
item.delete("at") if ts <= now
|
@@ -174,7 +212,7 @@ module Sidekiq
|
|
174
212
|
end
|
175
213
|
|
176
214
|
# Example usage:
|
177
|
-
# Sidekiq::Client.enqueue_in(3.minutes,
|
215
|
+
# Sidekiq::Client.enqueue_in(3.minutes, MyJob, 'foo', 1, :bat => 'bar')
|
178
216
|
#
|
179
217
|
def enqueue_in(interval, klass, *args)
|
180
218
|
klass.perform_in(interval, *args)
|
@@ -185,8 +223,23 @@ module Sidekiq
|
|
185
223
|
|
186
224
|
def raw_push(payloads)
|
187
225
|
@redis_pool.with do |conn|
|
188
|
-
|
189
|
-
|
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
|
190
243
|
end
|
191
244
|
end
|
192
245
|
true
|
@@ -194,8 +247,10 @@ module Sidekiq
|
|
194
247
|
|
195
248
|
def atomic_push(conn, payloads)
|
196
249
|
if payloads.first.key?("at")
|
197
|
-
conn.zadd("schedule", payloads.
|
250
|
+
conn.zadd("schedule", payloads.flat_map { |hash|
|
198
251
|
at = hash.delete("at").to_s
|
252
|
+
# ActiveJob sets this but the job has not been enqueued yet
|
253
|
+
hash.delete("enqueued_at")
|
199
254
|
[at, Sidekiq.dump_json(hash)]
|
200
255
|
})
|
201
256
|
else
|
@@ -205,52 +260,9 @@ module Sidekiq
|
|
205
260
|
entry["enqueued_at"] = now
|
206
261
|
Sidekiq.dump_json(entry)
|
207
262
|
}
|
208
|
-
conn.sadd("queues", queue)
|
263
|
+
conn.sadd("queues", [queue])
|
209
264
|
conn.lpush("queue:#{queue}", to_push)
|
210
265
|
end
|
211
266
|
end
|
212
|
-
|
213
|
-
def process_single(worker_class, item)
|
214
|
-
queue = item["queue"]
|
215
|
-
|
216
|
-
middleware.invoke(worker_class, item, queue, @redis_pool) do
|
217
|
-
item
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
def normalize_item(item)
|
222
|
-
# 6.0.0 push_bulk bug, #4321
|
223
|
-
# TODO Remove after a while...
|
224
|
-
item.delete("at") if item.key?("at") && item["at"].nil?
|
225
|
-
|
226
|
-
raise(ArgumentError, "Job must be a Hash with 'class' and 'args' keys: { 'class' => SomeWorker, 'args' => ['bob', 1, :foo => 'bar'] }") unless item.is_a?(Hash) && item.key?("class") && item.key?("args")
|
227
|
-
raise(ArgumentError, "Job args must be an Array") unless item["args"].is_a?(Array)
|
228
|
-
raise(ArgumentError, "Job class must be either a Class or String representation of the class name") unless item["class"].is_a?(Class) || item["class"].is_a?(String)
|
229
|
-
raise(ArgumentError, "Job 'at' must be a Numeric timestamp") if item.key?("at") && !item["at"].is_a?(Numeric)
|
230
|
-
raise(ArgumentError, "Job tags must be an Array") if item["tags"] && !item["tags"].is_a?(Array)
|
231
|
-
# 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']
|
232
|
-
|
233
|
-
# merge in the default sidekiq_options for the item's class and/or wrapped element
|
234
|
-
# this allows ActiveJobs to control sidekiq_options too.
|
235
|
-
defaults = normalized_hash(item["class"])
|
236
|
-
defaults = defaults.merge(item["wrapped"].get_sidekiq_options) if item["wrapped"].respond_to?("get_sidekiq_options")
|
237
|
-
item = defaults.merge(item)
|
238
|
-
|
239
|
-
item["class"] = item["class"].to_s
|
240
|
-
item["queue"] = item["queue"].to_s
|
241
|
-
item["jid"] ||= SecureRandom.hex(12)
|
242
|
-
item["created_at"] ||= Time.now.to_f
|
243
|
-
|
244
|
-
item
|
245
|
-
end
|
246
|
-
|
247
|
-
def normalized_hash(item_class)
|
248
|
-
if item_class.is_a?(Class)
|
249
|
-
raise(ArgumentError, "Message must include a Sidekiq::Worker class, not class name: #{item_class.ancestors.inspect}") unless item_class.respond_to?("get_sidekiq_options")
|
250
|
-
item_class.get_sidekiq_options
|
251
|
-
else
|
252
|
-
Sidekiq.default_worker_options
|
253
|
-
end
|
254
|
-
end
|
255
267
|
end
|
256
268
|
end
|
@@ -1,15 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "socket"
|
4
|
-
require "securerandom"
|
5
|
-
require "sidekiq/exception_handler"
|
6
|
-
|
7
3
|
module Sidekiq
|
8
4
|
##
|
9
|
-
#
|
10
|
-
#
|
11
|
-
|
12
|
-
include ExceptionHandler
|
5
|
+
# Sidekiq::Component assumes a config instance is available at @config
|
6
|
+
module Component # :nodoc:
|
7
|
+
attr_reader :config
|
13
8
|
|
14
9
|
def watchdog(last_words)
|
15
10
|
yield
|
@@ -20,17 +15,17 @@ module Sidekiq
|
|
20
15
|
|
21
16
|
def safe_thread(name, &block)
|
22
17
|
Thread.new do
|
23
|
-
Thread.current.name = name
|
18
|
+
Thread.current.name = "sidekiq.#{name}"
|
24
19
|
watchdog(name, &block)
|
25
20
|
end
|
26
21
|
end
|
27
22
|
|
28
23
|
def logger
|
29
|
-
|
24
|
+
config.logger
|
30
25
|
end
|
31
26
|
|
32
27
|
def redis(&block)
|
33
|
-
|
28
|
+
config.redis(&block)
|
34
29
|
end
|
35
30
|
|
36
31
|
def tid
|
@@ -49,11 +44,17 @@ module Sidekiq
|
|
49
44
|
@@identity ||= "#{hostname}:#{::Process.pid}:#{process_nonce}"
|
50
45
|
end
|
51
46
|
|
47
|
+
def handle_exception(ex, ctx = {})
|
48
|
+
config.handle_exception(ex, ctx)
|
49
|
+
end
|
50
|
+
|
52
51
|
def fire_event(event, options = {})
|
52
|
+
oneshot = options.fetch(:oneshot, true)
|
53
53
|
reverse = options[:reverse]
|
54
54
|
reraise = options[:reraise]
|
55
|
+
logger.debug("Firing #{event} event") if oneshot
|
55
56
|
|
56
|
-
arr =
|
57
|
+
arr = config[:lifecycle_events][event]
|
57
58
|
arr.reverse! if reverse
|
58
59
|
arr.each do |block|
|
59
60
|
block.call
|
@@ -61,7 +62,7 @@ module Sidekiq
|
|
61
62
|
handle_exception(ex, {context: "Exception during Sidekiq lifecycle event.", event: event})
|
62
63
|
raise ex if reraise
|
63
64
|
end
|
64
|
-
arr.clear
|
65
|
+
arr.clear if oneshot # once we've fired an event, we never fire it again
|
65
66
|
end
|
66
67
|
end
|
67
68
|
end
|