sidekiq 5.2.10 → 6.5.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/Changes.md +422 -1
  3. data/LICENSE +3 -3
  4. data/README.md +24 -35
  5. data/bin/sidekiq +27 -3
  6. data/bin/sidekiqload +79 -67
  7. data/bin/sidekiqmon +8 -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 +527 -310
  13. data/lib/sidekiq/cli.rb +204 -208
  14. data/lib/sidekiq/client.rb +78 -82
  15. data/lib/sidekiq/component.rb +65 -0
  16. data/lib/sidekiq/delay.rb +8 -7
  17. data/lib/sidekiq/extensions/action_mailer.rb +13 -22
  18. data/lib/sidekiq/extensions/active_record.rb +13 -10
  19. data/lib/sidekiq/extensions/class_methods.rb +14 -11
  20. data/lib/sidekiq/extensions/generic_proxy.rb +7 -5
  21. data/lib/sidekiq/fetch.rb +50 -40
  22. data/lib/sidekiq/job.rb +13 -0
  23. data/lib/sidekiq/job_logger.rb +33 -7
  24. data/lib/sidekiq/job_retry.rb +126 -106
  25. data/lib/sidekiq/job_util.rb +71 -0
  26. data/lib/sidekiq/launcher.rb +177 -83
  27. data/lib/sidekiq/logger.rb +156 -0
  28. data/lib/sidekiq/manager.rb +40 -41
  29. data/lib/sidekiq/metrics/deploy.rb +47 -0
  30. data/lib/sidekiq/metrics/query.rb +153 -0
  31. data/lib/sidekiq/metrics/shared.rb +94 -0
  32. data/lib/sidekiq/metrics/tracking.rb +134 -0
  33. data/lib/sidekiq/middleware/chain.rb +102 -46
  34. data/lib/sidekiq/middleware/current_attributes.rb +63 -0
  35. data/lib/sidekiq/middleware/i18n.rb +7 -7
  36. data/lib/sidekiq/middleware/modules.rb +21 -0
  37. data/lib/sidekiq/monitor.rb +133 -0
  38. data/lib/sidekiq/paginator.rb +28 -16
  39. data/lib/sidekiq/processor.rb +104 -97
  40. data/lib/sidekiq/rails.rb +46 -37
  41. data/lib/sidekiq/redis_client_adapter.rb +154 -0
  42. data/lib/sidekiq/redis_connection.rb +108 -77
  43. data/lib/sidekiq/ring_buffer.rb +29 -0
  44. data/lib/sidekiq/scheduled.rb +105 -42
  45. data/lib/sidekiq/sd_notify.rb +149 -0
  46. data/lib/sidekiq/systemd.rb +24 -0
  47. data/lib/sidekiq/testing/inline.rb +6 -5
  48. data/lib/sidekiq/testing.rb +68 -58
  49. data/lib/sidekiq/transaction_aware_client.rb +45 -0
  50. data/lib/sidekiq/version.rb +2 -1
  51. data/lib/sidekiq/web/action.rb +15 -11
  52. data/lib/sidekiq/web/application.rb +103 -77
  53. data/lib/sidekiq/web/csrf_protection.rb +180 -0
  54. data/lib/sidekiq/web/helpers.rb +125 -95
  55. data/lib/sidekiq/web/router.rb +23 -19
  56. data/lib/sidekiq/web.rb +65 -105
  57. data/lib/sidekiq/worker.rb +259 -109
  58. data/lib/sidekiq.rb +170 -62
  59. data/sidekiq.gemspec +23 -16
  60. data/web/assets/images/apple-touch-icon.png +0 -0
  61. data/web/assets/javascripts/application.js +113 -61
  62. data/web/assets/javascripts/chart.min.js +13 -0
  63. data/web/assets/javascripts/chartjs-plugin-annotation.min.js +7 -0
  64. data/web/assets/javascripts/dashboard.js +53 -89
  65. data/web/assets/javascripts/graph.js +16 -0
  66. data/web/assets/javascripts/metrics.js +262 -0
  67. data/web/assets/stylesheets/application-dark.css +143 -0
  68. data/web/assets/stylesheets/application-rtl.css +0 -4
  69. data/web/assets/stylesheets/application.css +88 -233
  70. data/web/locales/ar.yml +8 -2
  71. data/web/locales/de.yml +14 -2
  72. data/web/locales/el.yml +43 -19
  73. data/web/locales/en.yml +13 -1
  74. data/web/locales/es.yml +18 -2
  75. data/web/locales/fr.yml +10 -3
  76. data/web/locales/ja.yml +14 -1
  77. data/web/locales/lt.yml +83 -0
  78. data/web/locales/pl.yml +4 -4
  79. data/web/locales/pt-br.yml +27 -9
  80. data/web/locales/ru.yml +4 -0
  81. data/web/locales/vi.yml +83 -0
  82. data/web/locales/zh-cn.yml +36 -11
  83. data/web/locales/zh-tw.yml +32 -7
  84. data/web/views/_footer.erb +1 -1
  85. data/web/views/_job_info.erb +3 -2
  86. data/web/views/_nav.erb +1 -1
  87. data/web/views/_poll_link.erb +2 -5
  88. data/web/views/_summary.erb +7 -7
  89. data/web/views/busy.erb +61 -22
  90. data/web/views/dashboard.erb +23 -14
  91. data/web/views/dead.erb +3 -3
  92. data/web/views/layout.erb +3 -1
  93. data/web/views/metrics.erb +69 -0
  94. data/web/views/metrics_for_job.erb +87 -0
  95. data/web/views/morgue.erb +9 -6
  96. data/web/views/queue.erb +23 -10
  97. data/web/views/queues.erb +10 -2
  98. data/web/views/retries.erb +11 -8
  99. data/web/views/retry.erb +3 -3
  100. data/web/views/scheduled.erb +5 -2
  101. metadata +58 -63
  102. data/.circleci/config.yml +0 -61
  103. data/.github/contributing.md +0 -32
  104. data/.github/issue_template.md +0 -11
  105. data/.gitignore +0 -15
  106. data/.travis.yml +0 -11
  107. data/3.0-Upgrade.md +0 -70
  108. data/4.0-Upgrade.md +0 -53
  109. data/5.0-Upgrade.md +0 -56
  110. data/COMM-LICENSE +0 -97
  111. data/Ent-Changes.md +0 -238
  112. data/Gemfile +0 -19
  113. data/Pro-2.0-Upgrade.md +0 -138
  114. data/Pro-3.0-Upgrade.md +0 -44
  115. data/Pro-4.0-Upgrade.md +0 -35
  116. data/Pro-Changes.md +0 -759
  117. data/Rakefile +0 -9
  118. data/bin/sidekiqctl +0 -20
  119. data/code_of_conduct.md +0 -50
  120. data/lib/generators/sidekiq/worker_generator.rb +0 -49
  121. data/lib/sidekiq/core_ext.rb +0 -1
  122. data/lib/sidekiq/ctl.rb +0 -221
  123. data/lib/sidekiq/exception_handler.rb +0 -29
  124. data/lib/sidekiq/logging.rb +0 -122
  125. data/lib/sidekiq/middleware/server/active_record.rb +0 -23
  126. data/lib/sidekiq/util.rb +0 -66
@@ -1,40 +1,48 @@
1
1
  # frozen_string_literal: true
2
- require 'uri'
3
- require 'set'
4
- require 'yaml'
5
- require 'cgi'
2
+
3
+ require "uri"
4
+ require "set"
5
+ require "yaml"
6
+ require "cgi"
6
7
 
7
8
  module Sidekiq
8
9
  # This is not a public API
9
10
  module WebHelpers
10
11
  def strings(lang)
11
- @@strings ||= {}
12
- @@strings[lang] ||= begin
13
- # Allow sidekiq-web extensions to add locale paths
14
- # so extensions can be localized
15
- settings.locales.each_with_object({}) do |path, global|
16
- find_locale_files(lang).each do |file|
17
- strs = YAML.load(File.open(file))
18
- global.merge!(strs[lang])
19
- end
12
+ @strings ||= {}
13
+
14
+ # Allow sidekiq-web extensions to add locale paths
15
+ # so extensions can be localized
16
+ @strings[lang] ||= settings.locales.each_with_object({}) do |path, global|
17
+ find_locale_files(lang).each do |file|
18
+ strs = YAML.safe_load(File.open(file))
19
+ global.merge!(strs[lang])
20
20
  end
21
21
  end
22
22
  end
23
23
 
24
+ def singularize(str, count)
25
+ if count == 1 && str.respond_to?(:singularize) # rails
26
+ str.singularize
27
+ else
28
+ str
29
+ end
30
+ end
31
+
24
32
  def clear_caches
25
- @@strings = nil
26
- @@locale_files = nil
27
- @@available_locales = nil
33
+ @strings = nil
34
+ @locale_files = nil
35
+ @available_locales = nil
28
36
  end
29
37
 
30
38
  def locale_files
31
- @@locale_files ||= settings.locales.flat_map do |path|
39
+ @locale_files ||= settings.locales.flat_map { |path|
32
40
  Dir["#{path}/*.yml"]
33
- end
41
+ }
34
42
  end
35
43
 
36
44
  def available_locales
37
- @@available_locales ||= locale_files.map { |path| File.basename(path, '.yml') }.uniq
45
+ @available_locales ||= locale_files.map { |path| File.basename(path, ".yml") }.uniq
38
46
  end
39
47
 
40
48
  def find_locale_files(lang)
@@ -62,33 +70,25 @@ module Sidekiq
62
70
  @head_html.join if defined?(@head_html)
63
71
  end
64
72
 
65
- def poll_path
66
- if current_path != '' && params['poll']
67
- root_path + current_path
68
- else
69
- ""
70
- end
71
- end
72
-
73
73
  def text_direction
74
- get_locale['TextDirection'] || 'ltr'
74
+ get_locale["TextDirection"] || "ltr"
75
75
  end
76
76
 
77
77
  def rtl?
78
- text_direction == 'rtl'
78
+ text_direction == "rtl"
79
79
  end
80
80
 
81
81
  # See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
82
82
  def user_preferred_languages
83
- languages = env['HTTP_ACCEPT_LANGUAGE']
84
- languages.to_s.downcase.gsub(/\s+/, '').split(',').map do |language|
85
- locale, quality = language.split(';q=', 2)
86
- locale = nil if locale == '*' # Ignore wildcards
83
+ languages = env["HTTP_ACCEPT_LANGUAGE"]
84
+ languages.to_s.downcase.gsub(/\s+/, "").split(",").map { |language|
85
+ locale, quality = language.split(";q=", 2)
86
+ locale = nil if locale == "*" # Ignore wildcards
87
87
  quality = quality ? quality.to_f : 1.0
88
88
  [locale, quality]
89
- end.sort do |(_, left), (_, right)|
89
+ }.sort { |(_, left), (_, right)|
90
90
  right <=> left
91
- end.map(&:first).compact
91
+ }.map(&:first).compact
92
92
  end
93
93
 
94
94
  # Given an Accept-Language header like "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2"
@@ -97,31 +97,38 @@ module Sidekiq
97
97
  # Inspiration taken from https://github.com/iain/http_accept_language/blob/master/lib/http_accept_language/parser.rb
98
98
  def locale
99
99
  @locale ||= begin
100
- matched_locale = user_preferred_languages.map do |preferred|
101
- preferred_language = preferred.split('-', 2).first
100
+ matched_locale = user_preferred_languages.map { |preferred|
101
+ preferred_language = preferred.split("-", 2).first
102
102
 
103
- lang_group = available_locales.select do |available|
104
- preferred_language == available.split('-', 2).first
105
- end
103
+ lang_group = available_locales.select { |available|
104
+ preferred_language == available.split("-", 2).first
105
+ }
106
106
 
107
107
  lang_group.find { |lang| lang == preferred } || lang_group.min_by(&:length)
108
- end.compact.first
108
+ }.compact.first
109
109
 
110
- matched_locale || 'en'
110
+ matched_locale || "en"
111
111
  end
112
112
  end
113
113
 
114
+ # within is used by Sidekiq Pro
115
+ def display_tags(job, within = nil)
116
+ job.tags.map { |tag|
117
+ "<span class='label label-info jobtag'>#{::Rack::Utils.escape_html(tag)}</span>"
118
+ }.join(" ")
119
+ end
120
+
114
121
  # mperham/sidekiq#3243
115
122
  def unfiltered?
116
- yield unless env['PATH_INFO'].start_with?("/filter/")
123
+ yield unless env["PATH_INFO"].start_with?("/filter/")
117
124
  end
118
125
 
119
126
  def get_locale
120
127
  strings(locale)
121
128
  end
122
129
 
123
- def t(msg, options={})
124
- string = get_locale[msg] || strings('en')[msg] || msg
130
+ def t(msg, options = {})
131
+ string = get_locale[msg] || strings("en")[msg] || msg
125
132
  if options.empty?
126
133
  string
127
134
  else
@@ -129,28 +136,38 @@ module Sidekiq
129
136
  end
130
137
  end
131
138
 
132
- def workers
133
- @workers ||= Sidekiq::Workers.new
139
+ def sort_direction_label
140
+ (params[:direction] == "asc") ? "&uarr;" : "&darr;"
141
+ end
142
+
143
+ def workset
144
+ @work ||= Sidekiq::WorkSet.new
134
145
  end
135
146
 
136
147
  def processes
137
148
  @processes ||= Sidekiq::ProcessSet.new
138
149
  end
139
150
 
140
- def stats
141
- @stats ||= Sidekiq::Stats.new
151
+ # Sorts processes by hostname following the natural sort order
152
+ def sorted_processes
153
+ @sorted_processes ||= begin
154
+ return processes unless processes.all? { |p| p["hostname"] }
155
+
156
+ processes.to_a.sort_by do |process|
157
+ # Kudos to `shurikk` on StackOverflow
158
+ # https://stackoverflow.com/a/15170063/575547
159
+ process["hostname"].split(/(\d+)/).map { |a| /\d+/.match?(a) ? a.to_i : a }
160
+ end
161
+ end
142
162
  end
143
163
 
144
- def retries_with_score(score)
145
- Sidekiq.redis do |conn|
146
- conn.zrangebyscore('retry', score, score)
147
- end.map { |msg| Sidekiq.load_json(msg) }
164
+ def stats
165
+ @stats ||= Sidekiq::Stats.new
148
166
  end
149
167
 
150
168
  def redis_connection
151
169
  Sidekiq.redis do |conn|
152
- c = conn.connection
153
- "redis://#{c[:location]}/#{c[:db]}"
170
+ conn.connection[:id]
154
171
  end
155
172
  end
156
173
 
@@ -163,24 +180,24 @@ module Sidekiq
163
180
  end
164
181
 
165
182
  def root_path
166
- "#{env['SCRIPT_NAME']}/"
183
+ "#{env["SCRIPT_NAME"]}/"
167
184
  end
168
185
 
169
186
  def current_path
170
- @current_path ||= request.path_info.gsub(/^\//,'')
187
+ @current_path ||= request.path_info.gsub(/^\//, "")
171
188
  end
172
189
 
173
190
  def current_status
174
- workers.size == 0 ? 'idle' : 'active'
191
+ (workset.size == 0) ? "idle" : "active"
175
192
  end
176
193
 
177
194
  def relative_time(time)
178
195
  stamp = time.getutc.iso8601
179
- %{<time class="ltr" dir="ltr" title="#{stamp}" datetime="#{stamp}">#{time}</time>}
196
+ %(<time class="ltr" dir="ltr" title="#{stamp}" datetime="#{stamp}">#{time}</time>)
180
197
  end
181
198
 
182
199
  def job_params(job, score)
183
- "#{score}-#{job['jid']}"
200
+ "#{score}-#{job["jid"]}"
184
201
  end
185
202
 
186
203
  def parse_params(params)
@@ -188,59 +205,58 @@ module Sidekiq
188
205
  [score.to_f, jid]
189
206
  end
190
207
 
191
- SAFE_QPARAMS = %w(page poll)
208
+ SAFE_QPARAMS = %w[page direction]
192
209
 
193
210
  # Merge options with current params, filter safe params, and stringify to query string
194
211
  def qparams(options)
195
- # stringify
196
- options.keys.each do |key|
197
- options[key.to_s] = options.delete(key)
198
- end
212
+ stringified_options = options.transform_keys(&:to_s)
213
+
214
+ to_query_string(params.merge(stringified_options))
215
+ end
199
216
 
200
- params.merge(options).map do |key, value|
217
+ def to_query_string(params)
218
+ params.map { |key, value|
201
219
  SAFE_QPARAMS.include?(key) ? "#{key}=#{CGI.escape(value.to_s)}" : next
202
- end.compact.join("&")
220
+ }.compact.join("&")
203
221
  end
204
222
 
205
223
  def truncate(text, truncate_after_chars = 2000)
206
- truncate_after_chars && text.size > truncate_after_chars ? "#{text[0..truncate_after_chars]}..." : text
224
+ (truncate_after_chars && text.size > truncate_after_chars) ? "#{text[0..truncate_after_chars]}..." : text
207
225
  end
208
226
 
209
227
  def display_args(args, truncate_after_chars = 2000)
210
- return "Invalid job payload, args is nil" if args == nil
211
- return "Invalid job payload, args must be an Array, not #{args.class.name}" if !args.is_a?(Array)
228
+ return "Invalid job payload, args is nil" if args.nil?
229
+ return "Invalid job payload, args must be an Array, not #{args.class.name}" unless args.is_a?(Array)
212
230
 
213
231
  begin
214
- args.map do |arg|
232
+ args.map { |arg|
215
233
  h(truncate(to_display(arg), truncate_after_chars))
216
- end.join(", ")
234
+ }.join(", ")
217
235
  rescue
218
236
  "Illegal job arguments: #{h args.inspect}"
219
237
  end
220
238
  end
221
239
 
222
240
  def csrf_tag
223
- "<input type='hidden' name='authenticity_token' value='#{session[:csrf]}'/>"
241
+ "<input type='hidden' name='authenticity_token' value='#{env[:csrf_token]}'/>"
224
242
  end
225
243
 
226
244
  def to_display(arg)
245
+ arg.inspect
246
+ rescue
227
247
  begin
228
- arg.inspect
229
- rescue
230
- begin
231
- arg.to_s
232
- rescue => ex
233
- "Cannot display argument: [#{ex.class.name}] #{ex.message}"
234
- end
248
+ arg.to_s
249
+ rescue => ex
250
+ "Cannot display argument: [#{ex.class.name}] #{ex.message}"
235
251
  end
236
252
  end
237
253
 
238
- RETRY_JOB_KEYS = Set.new(%w(
254
+ RETRY_JOB_KEYS = Set.new(%w[
239
255
  queue class args retry_count retried_at failed_at
240
256
  jid error_message error_class backtrace
241
257
  error_backtrace enqueued_at retry wrapped
242
- created_at
243
- ))
258
+ created_at tags display_class
259
+ ])
244
260
 
245
261
  def retry_extra_items(retry_job)
246
262
  @retry_extra_items ||= {}.tap do |extra|
@@ -250,15 +266,29 @@ module Sidekiq
250
266
  end
251
267
  end
252
268
 
269
+ def format_memory(rss_kb)
270
+ return "0" if rss_kb.nil? || rss_kb == 0
271
+
272
+ if rss_kb < 100_000
273
+ "#{number_with_delimiter(rss_kb)} KB"
274
+ elsif rss_kb < 10_000_000
275
+ "#{number_with_delimiter((rss_kb / 1024.0).to_i)} MB"
276
+ else
277
+ "#{number_with_delimiter((rss_kb / (1024.0 * 1024.0)).round(1))} GB"
278
+ end
279
+ end
280
+
253
281
  def number_with_delimiter(number)
282
+ return "" if number.nil?
283
+
254
284
  begin
255
285
  Float(number)
256
286
  rescue ArgumentError, TypeError
257
287
  return number
258
288
  end
259
289
 
260
- options = {delimiter: ',', separator: '.'}
261
- parts = number.to_s.to_str.split('.')
290
+ options = {delimiter: ",", separator: "."}
291
+ parts = number.to_s.to_str.split(".")
262
292
  parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
263
293
  parts.join(options[:separator])
264
294
  end
@@ -266,8 +296,8 @@ module Sidekiq
266
296
  def h(text)
267
297
  ::Rack::Utils.escape_html(text)
268
298
  rescue ArgumentError => e
269
- raise unless e.message.eql?('invalid byte sequence in UTF-8')
270
- text.encode!('UTF-16', 'UTF-8', invalid: :replace, replace: '').encode!('UTF-8', 'UTF-16')
299
+ raise unless e.message.eql?("invalid byte sequence in UTF-8")
300
+ text.encode!("UTF-16", "UTF-8", invalid: :replace, replace: "").encode!("UTF-8", "UTF-16")
271
301
  retry
272
302
  end
273
303
 
@@ -284,7 +314,7 @@ module Sidekiq
284
314
  end
285
315
 
286
316
  def environment_title_prefix
287
- environment = Sidekiq.options[:environment] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
317
+ environment = Sidekiq[:environment] || ENV["APP_ENV"] || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
288
318
 
289
319
  "[#{environment.upcase}] " unless environment == "production"
290
320
  end
@@ -294,30 +324,30 @@ module Sidekiq
294
324
  end
295
325
 
296
326
  def server_utc_time
297
- Time.now.utc.strftime('%H:%M:%S UTC')
327
+ Time.now.utc.strftime("%H:%M:%S UTC")
298
328
  end
299
329
 
300
330
  def redis_connection_and_namespace
301
331
  @redis_connection_and_namespace ||= begin
302
- namespace_suffix = namespace == nil ? '' : "##{namespace}"
332
+ namespace_suffix = namespace.nil? ? "" : "##{namespace}"
303
333
  "#{redis_connection}#{namespace_suffix}"
304
334
  end
305
335
  end
306
336
 
307
337
  def retry_or_delete_or_kill(job, params)
308
- if params['retry']
338
+ if params["retry"]
309
339
  job.retry
310
- elsif params['delete']
340
+ elsif params["delete"]
311
341
  job.delete
312
- elsif params['kill']
342
+ elsif params["kill"]
313
343
  job.kill
314
344
  end
315
345
  end
316
346
 
317
347
  def delete_or_add_queue(job, params)
318
- if params['delete']
348
+ if params["delete"]
319
349
  job.delete
320
- elsif params['add_to_queue']
350
+ elsif params["add_to_queue"]
321
351
  job.add_to_queue
322
352
  end
323
353
  end
@@ -1,18 +1,23 @@
1
1
  # frozen_string_literal: true
2
- require 'rack'
2
+
3
+ require "rack"
3
4
 
4
5
  module Sidekiq
5
6
  module WebRouter
6
- GET = 'GET'
7
- DELETE = 'DELETE'
8
- POST = 'POST'
9
- PUT = 'PUT'
10
- PATCH = 'PATCH'
11
- HEAD = 'HEAD'
12
-
13
- ROUTE_PARAMS = 'rack.route_params'
14
- REQUEST_METHOD = 'REQUEST_METHOD'
15
- PATH_INFO = 'PATH_INFO'
7
+ GET = "GET"
8
+ DELETE = "DELETE"
9
+ POST = "POST"
10
+ PUT = "PUT"
11
+ PATCH = "PATCH"
12
+ HEAD = "HEAD"
13
+
14
+ ROUTE_PARAMS = "rack.route_params"
15
+ REQUEST_METHOD = "REQUEST_METHOD"
16
+ PATH_INFO = "PATH_INFO"
17
+
18
+ def head(path, &block)
19
+ route(HEAD, path, &block)
20
+ end
16
21
 
17
22
  def get(path, &block)
18
23
  route(GET, path, &block)
@@ -35,10 +40,9 @@ module Sidekiq
35
40
  end
36
41
 
37
42
  def route(method, path, &block)
38
- @routes ||= { GET => [], POST => [], PUT => [], PATCH => [], DELETE => [], HEAD => [] }
43
+ @routes ||= {GET => [], POST => [], PUT => [], PATCH => [], DELETE => [], HEAD => []}
39
44
 
40
45
  @routes[method] << WebRoute.new(method, path, block)
41
- @routes[HEAD] << WebRoute.new(method, path, block) if method == GET
42
46
  end
43
47
 
44
48
  def match(env)
@@ -50,7 +54,8 @@ module Sidekiq
50
54
  path_info = "/" if path_info == ""
51
55
 
52
56
  @routes[request_method].each do |route|
53
- if params = route.match(request_method, path_info)
57
+ params = route.match(request_method, path_info)
58
+ if params
54
59
  env[ROUTE_PARAMS] = params
55
60
 
56
61
  return WebAction.new(env, route.block)
@@ -64,7 +69,7 @@ module Sidekiq
64
69
  class WebRoute
65
70
  attr_accessor :request_method, :pattern, :block, :name
66
71
 
67
- NAMED_SEGMENTS_PATTERN = /\/([^\/]*):([^\.:$\/]+)/
72
+ NAMED_SEGMENTS_PATTERN = /\/([^\/]*):([^.:$\/]+)/
68
73
 
69
74
  def initialize(request_method, pattern, block)
70
75
  @request_method = request_method
@@ -77,7 +82,7 @@ module Sidekiq
77
82
  end
78
83
 
79
84
  def compile
80
- if pattern.match(NAMED_SEGMENTS_PATTERN)
85
+ if pattern.match?(NAMED_SEGMENTS_PATTERN)
81
86
  p = pattern.gsub(NAMED_SEGMENTS_PATTERN, '/\1(?<\2>[^$/]+)')
82
87
 
83
88
  Regexp.new("\\A#{p}\\Z")
@@ -91,9 +96,8 @@ module Sidekiq
91
96
  when String
92
97
  {} if path == matcher
93
98
  else
94
- if path_match = path.match(matcher)
95
- Hash[path_match.names.map(&:to_sym).zip(path_match.captures)]
96
- end
99
+ path_match = path.match(matcher)
100
+ path_match&.named_captures&.transform_keys(&:to_sym)
97
101
  end
98
102
  end
99
103
  end