sidekiq 5.1.1 → 7.1.2

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.

Files changed (149) hide show
  1. checksums.yaml +5 -5
  2. data/Changes.md +627 -8
  3. data/LICENSE.txt +9 -0
  4. data/README.md +47 -50
  5. data/bin/sidekiq +22 -3
  6. data/bin/sidekiqload +213 -115
  7. data/bin/sidekiqmon +11 -0
  8. data/lib/generators/sidekiq/job_generator.rb +57 -0
  9. data/lib/generators/sidekiq/templates/{worker.rb.erb → job.rb.erb} +2 -2
  10. data/lib/generators/sidekiq/templates/{worker_spec.rb.erb → job_spec.rb.erb} +1 -1
  11. data/lib/generators/sidekiq/templates/{worker_test.rb.erb → job_test.rb.erb} +1 -1
  12. data/lib/sidekiq/api.rb +566 -329
  13. data/lib/sidekiq/capsule.rb +127 -0
  14. data/lib/sidekiq/cli.rb +241 -256
  15. data/lib/sidekiq/client.rb +125 -102
  16. data/lib/sidekiq/component.rb +68 -0
  17. data/lib/sidekiq/config.rb +278 -0
  18. data/lib/sidekiq/deploy.rb +62 -0
  19. data/lib/sidekiq/embedded.rb +61 -0
  20. data/lib/sidekiq/fetch.rb +49 -42
  21. data/lib/sidekiq/job.rb +374 -0
  22. data/lib/sidekiq/job_logger.rb +36 -9
  23. data/lib/sidekiq/job_retry.rb +147 -98
  24. data/lib/sidekiq/job_util.rb +105 -0
  25. data/lib/sidekiq/launcher.rb +207 -103
  26. data/lib/sidekiq/logger.rb +131 -0
  27. data/lib/sidekiq/manager.rb +43 -47
  28. data/lib/sidekiq/metrics/query.rb +153 -0
  29. data/lib/sidekiq/metrics/shared.rb +95 -0
  30. data/lib/sidekiq/metrics/tracking.rb +136 -0
  31. data/lib/sidekiq/middleware/chain.rb +113 -56
  32. data/lib/sidekiq/middleware/current_attributes.rb +95 -0
  33. data/lib/sidekiq/middleware/i18n.rb +7 -7
  34. data/lib/sidekiq/middleware/modules.rb +21 -0
  35. data/lib/sidekiq/monitor.rb +146 -0
  36. data/lib/sidekiq/paginator.rb +28 -16
  37. data/lib/sidekiq/processor.rb +159 -107
  38. data/lib/sidekiq/rails.rb +54 -43
  39. data/lib/sidekiq/redis_client_adapter.rb +96 -0
  40. data/lib/sidekiq/redis_connection.rb +39 -81
  41. data/lib/sidekiq/ring_buffer.rb +29 -0
  42. data/lib/sidekiq/scheduled.rb +139 -48
  43. data/lib/sidekiq/sd_notify.rb +149 -0
  44. data/lib/sidekiq/systemd.rb +24 -0
  45. data/lib/sidekiq/testing/inline.rb +6 -5
  46. data/lib/sidekiq/testing.rb +70 -88
  47. data/lib/sidekiq/transaction_aware_client.rb +44 -0
  48. data/lib/sidekiq/version.rb +3 -1
  49. data/lib/sidekiq/web/action.rb +15 -11
  50. data/lib/sidekiq/web/application.rb +143 -77
  51. data/lib/sidekiq/web/csrf_protection.rb +180 -0
  52. data/lib/sidekiq/web/helpers.rb +144 -106
  53. data/lib/sidekiq/web/router.rb +23 -19
  54. data/lib/sidekiq/web.rb +60 -111
  55. data/lib/sidekiq/worker_compatibility_alias.rb +13 -0
  56. data/lib/sidekiq.rb +94 -183
  57. data/sidekiq.gemspec +25 -23
  58. data/web/assets/images/apple-touch-icon.png +0 -0
  59. data/web/assets/javascripts/application.js +130 -61
  60. data/web/assets/javascripts/base-charts.js +106 -0
  61. data/web/assets/javascripts/chart.min.js +13 -0
  62. data/web/assets/javascripts/chartjs-plugin-annotation.min.js +7 -0
  63. data/web/assets/javascripts/dashboard-charts.js +166 -0
  64. data/web/assets/javascripts/dashboard.js +36 -282
  65. data/web/assets/javascripts/metrics.js +264 -0
  66. data/web/assets/stylesheets/application-dark.css +147 -0
  67. data/web/assets/stylesheets/application-rtl.css +2 -95
  68. data/web/assets/stylesheets/application.css +134 -521
  69. data/web/assets/stylesheets/bootstrap.css +2 -2
  70. data/web/locales/ar.yml +71 -64
  71. data/web/locales/cs.yml +62 -62
  72. data/web/locales/da.yml +60 -53
  73. data/web/locales/de.yml +65 -53
  74. data/web/locales/el.yml +43 -24
  75. data/web/locales/en.yml +84 -65
  76. data/web/locales/es.yml +70 -54
  77. data/web/locales/fa.yml +65 -65
  78. data/web/locales/fr.yml +83 -62
  79. data/web/locales/gd.yml +99 -0
  80. data/web/locales/he.yml +65 -64
  81. data/web/locales/hi.yml +59 -59
  82. data/web/locales/it.yml +53 -53
  83. data/web/locales/ja.yml +75 -64
  84. data/web/locales/ko.yml +52 -52
  85. data/web/locales/lt.yml +83 -0
  86. data/web/locales/nb.yml +61 -61
  87. data/web/locales/nl.yml +52 -52
  88. data/web/locales/pl.yml +45 -45
  89. data/web/locales/pt-br.yml +63 -55
  90. data/web/locales/pt.yml +51 -51
  91. data/web/locales/ru.yml +68 -63
  92. data/web/locales/sv.yml +53 -53
  93. data/web/locales/ta.yml +60 -60
  94. data/web/locales/uk.yml +62 -61
  95. data/web/locales/ur.yml +64 -64
  96. data/web/locales/vi.yml +83 -0
  97. data/web/locales/zh-cn.yml +43 -16
  98. data/web/locales/zh-tw.yml +42 -8
  99. data/web/views/_footer.erb +8 -2
  100. data/web/views/_job_info.erb +21 -4
  101. data/web/views/_metrics_period_select.erb +12 -0
  102. data/web/views/_nav.erb +4 -18
  103. data/web/views/_paging.erb +2 -0
  104. data/web/views/_poll_link.erb +3 -6
  105. data/web/views/_summary.erb +7 -7
  106. data/web/views/busy.erb +75 -25
  107. data/web/views/dashboard.erb +58 -18
  108. data/web/views/dead.erb +3 -3
  109. data/web/views/layout.erb +4 -2
  110. data/web/views/metrics.erb +82 -0
  111. data/web/views/metrics_for_job.erb +68 -0
  112. data/web/views/morgue.erb +14 -15
  113. data/web/views/queue.erb +33 -23
  114. data/web/views/queues.erb +14 -4
  115. data/web/views/retries.erb +19 -16
  116. data/web/views/retry.erb +3 -3
  117. data/web/views/scheduled.erb +17 -15
  118. metadata +71 -140
  119. data/.github/contributing.md +0 -32
  120. data/.github/issue_template.md +0 -11
  121. data/.gitignore +0 -13
  122. data/.travis.yml +0 -14
  123. data/3.0-Upgrade.md +0 -70
  124. data/4.0-Upgrade.md +0 -53
  125. data/5.0-Upgrade.md +0 -56
  126. data/COMM-LICENSE +0 -95
  127. data/Ent-Changes.md +0 -210
  128. data/Gemfile +0 -8
  129. data/LICENSE +0 -9
  130. data/Pro-2.0-Upgrade.md +0 -138
  131. data/Pro-3.0-Upgrade.md +0 -44
  132. data/Pro-4.0-Upgrade.md +0 -35
  133. data/Pro-Changes.md +0 -716
  134. data/Rakefile +0 -8
  135. data/bin/sidekiqctl +0 -99
  136. data/code_of_conduct.md +0 -50
  137. data/lib/generators/sidekiq/worker_generator.rb +0 -49
  138. data/lib/sidekiq/core_ext.rb +0 -1
  139. data/lib/sidekiq/delay.rb +0 -41
  140. data/lib/sidekiq/exception_handler.rb +0 -29
  141. data/lib/sidekiq/extensions/action_mailer.rb +0 -57
  142. data/lib/sidekiq/extensions/active_record.rb +0 -40
  143. data/lib/sidekiq/extensions/class_methods.rb +0 -40
  144. data/lib/sidekiq/extensions/generic_proxy.rb +0 -31
  145. data/lib/sidekiq/logging.rb +0 -122
  146. data/lib/sidekiq/middleware/server/active_record.rb +0 -22
  147. data/lib/sidekiq/middleware/server/active_record_cache.rb +0 -11
  148. data/lib/sidekiq/util.rb +0 -66
  149. data/lib/sidekiq/worker.rb +0 -204
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
- require 'securerandom'
3
- require 'sidekiq/middleware/chain'
2
+
3
+ require "securerandom"
4
+ require "sidekiq/middleware/chain"
5
+ require "sidekiq/job_util"
4
6
 
5
7
  module Sidekiq
6
8
  class Client
9
+ include Sidekiq::JobUtil
7
10
 
8
11
  ##
9
12
  # Define client-side middleware:
@@ -12,14 +15,13 @@ module Sidekiq
12
15
  # client.middleware do |chain|
13
16
  # chain.use MyClientMiddleware
14
17
  # end
15
- # client.push('class' => 'SomeWorker', 'args' => [1,2,3])
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
- if block_given?
24
+ if block
23
25
  @chain = @chain.dup
24
26
  yield @chain
25
27
  end
@@ -28,34 +30,48 @@ module Sidekiq
28
30
 
29
31
  attr_accessor :redis_pool
30
32
 
31
- # Sidekiq::Client normally uses the default Redis pool but you may
32
- # pass a custom ConnectionPool if you want to shard your
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(ConnectionPool.new { Redis.new })
36
+ # Sidekiq::Client.new(pool: Sidekiq::RedisConnection.create)
37
37
  #
38
- # Generally this is only needed for very large Sidekiq installs processing
39
- # thousands of jobs per second. I don't recommend sharding unless you
40
- # cannot scale any other way (e.g. splitting your app into smaller apps).
41
- def initialize(redis_pool=nil)
42
- @redis_pool = redis_pool || Thread.current[:sidekiq_via_pool] || Sidekiq.redis_pool
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 worker class to call, required
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
53
69
  # backtrace - whether to save any error backtrace, default false
54
70
  #
55
71
  # If class is set to the class name, the jobs' options will be based on Sidekiq's default
56
- # worker options. Otherwise, they will be based on the job class's options.
72
+ # job options. Otherwise, they will be based on the job class's options.
57
73
  #
58
- # Any options valid for a worker class's sidekiq_options are also available here.
74
+ # Any options valid for a job class's sidekiq_options are also available here.
59
75
  #
60
76
  # All options must be strings, not symbols. NB: because we are serializing to JSON, all
61
77
  # symbols in 'args' will be converted to strings. Note that +backtrace: true+ can take quite a bit of
@@ -64,44 +80,69 @@ module Sidekiq
64
80
  # Returns a unique Job ID. If middleware stops the job, nil will be returned instead.
65
81
  #
66
82
  # Example:
67
- # push('queue' => 'my_queue', 'class' => MyWorker, 'args' => ['foo', 1, :bat => 'bar'])
83
+ # push('queue' => 'my_queue', 'class' => MyJob, 'args' => ['foo', 1, :bat => 'bar'])
68
84
  #
69
85
  def push(item)
70
86
  normed = normalize_item(item)
71
- payload = process_single(item['class'.freeze], normed)
72
-
87
+ payload = middleware.invoke(item["class"], normed, normed["queue"], @redis_pool) do
88
+ normed
89
+ end
73
90
  if payload
91
+ verify_json(payload)
74
92
  raw_push([payload])
75
- payload['jid'.freeze]
93
+ payload["jid"]
76
94
  end
77
95
  end
78
96
 
79
97
  ##
80
- # Push a large number of jobs to Redis. In practice this method is only
81
- # useful if you are pushing thousands of jobs or more. This method
82
- # cuts out the redis network round trip latency.
98
+ # Push a large number of jobs to Redis. This method cuts out the redis
99
+ # network round trip latency. It pushes jobs in batches if more than
100
+ # `:batch_size` (1000 by default) of jobs are passed. I wouldn't recommend making `:batch_size`
101
+ # larger than 1000 but YMMV based on network quality, size of job args, etc.
102
+ # A large number of jobs can cause a bit of Redis command processing latency.
83
103
  #
84
104
  # Takes the same arguments as #push except that args is expected to be
85
105
  # an Array of Arrays. All other keys are duplicated for each job. Each job
86
106
  # is run through the client middleware pipeline and each job gets its own Job ID
87
107
  # as normal.
88
108
  #
89
- # Returns an array of the of pushed jobs' jids. The number of jobs pushed can be less
90
- # than the number given if the middleware stopped processing for one or more jobs.
109
+ # Returns an array of the of pushed jobs' jids, may contain nils if any client middleware
110
+ # prevented a job push.
111
+ #
112
+ # Example (pushing jobs in batches):
113
+ # push_bulk('class' => 'MyJob', 'args' => (1..100_000).to_a, batch_size: 1_000)
114
+ #
91
115
  def push_bulk(items)
92
- arg = items['args'.freeze].first
93
- return [] unless arg # no jobs to push
94
- raise ArgumentError, "Bulk arguments must be an Array of Arrays: [[1], [2]]" if !arg.is_a?(Array)
116
+ batch_size = items.delete(:batch_size) || items.delete("batch_size") || 1_000
117
+ args = items["args"]
118
+ at = items.delete("at")
119
+ 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) })
120
+ raise ArgumentError, "Job 'at' Array must have same size as 'args' Array" if at.is_a?(Array) && at.size != args.size
121
+
122
+ jid = items.delete("jid")
123
+ raise ArgumentError, "Explicitly passing 'jid' when pushing more than one job is not supported" if jid && args.size > 1
95
124
 
96
125
  normed = normalize_item(items)
97
- payloads = items['args'.freeze].map do |args|
98
- copy = normed.merge('args'.freeze => args, 'jid'.freeze => SecureRandom.hex(12), 'enqueued_at'.freeze => Time.now.to_f)
99
- result = process_single(items['class'.freeze], copy)
100
- result ? result : nil
101
- end.compact
102
-
103
- raw_push(payloads) if !payloads.empty?
104
- payloads.collect { |payload| payload['jid'.freeze] }
126
+ result = args.each_slice(batch_size).flat_map do |slice|
127
+ raise ArgumentError, "Bulk arguments must be an Array of Arrays: [[1], [2]]" unless slice.is_a?(Array) && slice.all?(Array)
128
+ break [] if slice.empty? # no jobs to push
129
+
130
+ payloads = slice.map.with_index { |job_args, index|
131
+ copy = normed.merge("args" => job_args, "jid" => SecureRandom.hex(12))
132
+ copy["at"] = (at.is_a?(Array) ? at[index] : at) if at
133
+ result = middleware.invoke(items["class"], copy, copy["queue"], @redis_pool) do
134
+ verify_json(copy)
135
+ copy
136
+ end
137
+ result || nil
138
+ }
139
+
140
+ to_push = payloads.compact
141
+ raw_push(to_push) unless to_push.empty?
142
+ payloads.map { |payload| payload&.[]("jid") }
143
+ end
144
+
145
+ result.is_a?(Enumerator::Lazy) ? result.force : result
105
146
  end
106
147
 
107
148
  # Allows sharding of jobs across any number of Redis instances. All jobs
@@ -109,8 +150,8 @@ module Sidekiq
109
150
  #
110
151
  # pool = ConnectionPool.new { Redis.new }
111
152
  # Sidekiq::Client.via(pool) do
112
- # SomeWorker.perform_async(1,2,3)
113
- # SomeOtherWorker.perform_async(1,2,3)
153
+ # SomeJob.perform_async(1,2,3)
154
+ # SomeOtherJob.perform_async(1,2,3)
114
155
  # end
115
156
  #
116
157
  # Generally this is only needed for very large Sidekiq installs processing
@@ -118,58 +159,57 @@ module Sidekiq
118
159
  # you cannot scale any other way (e.g. splitting your app into smaller apps).
119
160
  def self.via(pool)
120
161
  raise ArgumentError, "No pool given" if pool.nil?
121
- current_sidekiq_pool = Thread.current[:sidekiq_via_pool]
122
- Thread.current[:sidekiq_via_pool] = pool
162
+ current_sidekiq_pool = Thread.current[:sidekiq_redis_pool]
163
+ Thread.current[:sidekiq_redis_pool] = pool
123
164
  yield
124
165
  ensure
125
- Thread.current[:sidekiq_via_pool] = current_sidekiq_pool
166
+ Thread.current[:sidekiq_redis_pool] = current_sidekiq_pool
126
167
  end
127
168
 
128
169
  class << self
129
-
130
170
  def push(item)
131
171
  new.push(item)
132
172
  end
133
173
 
134
- def push_bulk(items)
135
- new.push_bulk(items)
174
+ def push_bulk(...)
175
+ new.push_bulk(...)
136
176
  end
137
177
 
138
178
  # Resque compatibility helpers. Note all helpers
139
- # should go through Worker#client_push.
179
+ # should go through Sidekiq::Job#client_push.
140
180
  #
141
181
  # Example usage:
142
- # Sidekiq::Client.enqueue(MyWorker, 'foo', 1, :bat => 'bar')
182
+ # Sidekiq::Client.enqueue(MyJob, 'foo', 1, :bat => 'bar')
143
183
  #
144
184
  # Messages are enqueued to the 'default' queue.
145
185
  #
146
186
  def enqueue(klass, *args)
147
- klass.client_push('class'.freeze => klass, 'args'.freeze => args)
187
+ klass.client_push("class" => klass, "args" => args)
148
188
  end
149
189
 
150
190
  # Example usage:
151
- # Sidekiq::Client.enqueue_to(:queue_name, MyWorker, 'foo', 1, :bat => 'bar')
191
+ # Sidekiq::Client.enqueue_to(:queue_name, MyJob, 'foo', 1, :bat => 'bar')
152
192
  #
153
193
  def enqueue_to(queue, klass, *args)
154
- klass.client_push('queue'.freeze => queue, 'class'.freeze => klass, 'args'.freeze => args)
194
+ klass.client_push("queue" => queue, "class" => klass, "args" => args)
155
195
  end
156
196
 
157
197
  # Example usage:
158
- # Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes, MyWorker, 'foo', 1, :bat => 'bar')
198
+ # Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes, MyJob, 'foo', 1, :bat => 'bar')
159
199
  #
160
200
  def enqueue_to_in(queue, interval, klass, *args)
161
201
  int = interval.to_f
162
202
  now = Time.now.to_f
163
- ts = (int < 1_000_000_000 ? now + int : int)
203
+ ts = ((int < 1_000_000_000) ? now + int : int)
164
204
 
165
- item = { 'class'.freeze => klass, 'args'.freeze => args, 'at'.freeze => ts, 'queue'.freeze => queue }
166
- item.delete('at'.freeze) if ts <= now
205
+ item = {"class" => klass, "args" => args, "at" => ts, "queue" => queue}
206
+ item.delete("at") if ts <= now
167
207
 
168
208
  klass.client_push(item)
169
209
  end
170
210
 
171
211
  # Example usage:
172
- # Sidekiq::Client.enqueue_in(3.minutes, MyWorker, 'foo', 1, :bat => 'bar')
212
+ # Sidekiq::Client.enqueue_in(3.minutes, MyJob, 'foo', 1, :bat => 'bar')
173
213
  #
174
214
  def enqueue_in(interval, klass, *args)
175
215
  klass.perform_in(interval, *args)
@@ -180,62 +220,45 @@ module Sidekiq
180
220
 
181
221
  def raw_push(payloads)
182
222
  @redis_pool.with do |conn|
183
- conn.multi do
184
- atomic_push(conn, payloads)
223
+ retryable = true
224
+ begin
225
+ conn.pipelined do |pipeline|
226
+ atomic_push(pipeline, payloads)
227
+ end
228
+ rescue RedisClient::Error => ex
229
+ # 2550 Failover can cause the server to become a replica, need
230
+ # to disconnect and reopen the socket to get back to the primary.
231
+ # 4495 Use the same logic if we have a "Not enough replicas" error from the primary
232
+ # 4985 Use the same logic when a blocking command is force-unblocked
233
+ # The retry logic is copied from sidekiq.rb
234
+ if retryable && ex.message =~ /READONLY|NOREPLICAS|UNBLOCKED/
235
+ conn.close
236
+ retryable = false
237
+ retry
238
+ end
239
+ raise
185
240
  end
186
241
  end
187
242
  true
188
243
  end
189
244
 
190
245
  def atomic_push(conn, payloads)
191
- if payloads.first['at'.freeze]
192
- conn.zadd('schedule'.freeze, payloads.map do |hash|
193
- at = hash.delete('at'.freeze).to_s
246
+ if payloads.first.key?("at")
247
+ conn.zadd("schedule", payloads.flat_map { |hash|
248
+ at = hash.delete("at").to_s
249
+ # ActiveJob sets this but the job has not been enqueued yet
250
+ hash.delete("enqueued_at")
194
251
  [at, Sidekiq.dump_json(hash)]
195
- end)
252
+ })
196
253
  else
197
- q = payloads.first['queue'.freeze]
254
+ queue = payloads.first["queue"]
198
255
  now = Time.now.to_f
199
- to_push = payloads.map do |entry|
200
- entry['enqueued_at'.freeze] = now
256
+ to_push = payloads.map { |entry|
257
+ entry["enqueued_at"] = now
201
258
  Sidekiq.dump_json(entry)
202
- end
203
- conn.sadd('queues'.freeze, q)
204
- conn.lpush("queue:#{q}", to_push)
205
- end
206
- end
207
-
208
- def process_single(worker_class, item)
209
- queue = item['queue'.freeze]
210
-
211
- middleware.invoke(worker_class, item, queue, @redis_pool) do
212
- item
213
- end
214
- end
215
-
216
- def normalize_item(item)
217
- 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.has_key?('class'.freeze) && item.has_key?('args'.freeze)
218
- raise(ArgumentError, "Job args must be an Array") unless item['args'].is_a?(Array)
219
- raise(ArgumentError, "Job class must be either a Class or String representation of the class name") unless item['class'.freeze].is_a?(Class) || item['class'.freeze].is_a?(String)
220
- raise(ArgumentError, "Job 'at' must be a Numeric timestamp") if item.has_key?('at'.freeze) && !item['at'].is_a?(Numeric)
221
- #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']
222
-
223
- normalized_hash(item['class'.freeze])
224
- .each{ |key, value| item[key] = value if item[key].nil? }
225
-
226
- item['class'.freeze] = item['class'.freeze].to_s
227
- item['queue'.freeze] = item['queue'.freeze].to_s
228
- item['jid'.freeze] ||= SecureRandom.hex(12)
229
- item['created_at'.freeze] ||= Time.now.to_f
230
- item
231
- end
232
-
233
- def normalized_hash(item_class)
234
- if item_class.is_a?(Class)
235
- 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)
236
- item_class.get_sidekiq_options
237
- else
238
- Sidekiq.default_worker_options
259
+ }
260
+ conn.sadd("queues", [queue])
261
+ conn.lpush("queue:#{queue}", to_push)
239
262
  end
240
263
  end
241
264
  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