sidekiq 6.3.1 → 7.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changes.md +285 -11
- data/LICENSE.txt +9 -0
- data/README.md +47 -34
- data/bin/sidekiq +4 -9
- 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 +333 -190
- data/lib/sidekiq/capsule.rb +127 -0
- data/lib/sidekiq/cli.rb +86 -80
- data/lib/sidekiq/client.rb +104 -95
- data/lib/sidekiq/{util.rb → component.rb} +14 -41
- data/lib/sidekiq/config.rb +282 -0
- data/lib/sidekiq/deploy.rb +62 -0
- data/lib/sidekiq/embedded.rb +61 -0
- data/lib/sidekiq/fetch.rb +23 -24
- data/lib/sidekiq/job.rb +371 -10
- data/lib/sidekiq/job_logger.rb +16 -28
- data/lib/sidekiq/job_retry.rb +99 -58
- data/lib/sidekiq/job_util.rb +107 -0
- data/lib/sidekiq/launcher.rb +103 -95
- data/lib/sidekiq/logger.rb +9 -44
- data/lib/sidekiq/manager.rb +40 -41
- data/lib/sidekiq/metrics/query.rb +153 -0
- data/lib/sidekiq/metrics/shared.rb +95 -0
- data/lib/sidekiq/metrics/tracking.rb +136 -0
- data/lib/sidekiq/middleware/chain.rb +96 -51
- data/lib/sidekiq/middleware/current_attributes.rb +59 -16
- data/lib/sidekiq/middleware/i18n.rb +6 -4
- data/lib/sidekiq/middleware/modules.rb +21 -0
- data/lib/sidekiq/monitor.rb +17 -4
- data/lib/sidekiq/paginator.rb +17 -9
- data/lib/sidekiq/processor.rb +81 -80
- data/lib/sidekiq/rails.rb +21 -14
- data/lib/sidekiq/redis_client_adapter.rb +95 -0
- data/lib/sidekiq/redis_connection.rb +14 -82
- data/lib/sidekiq/ring_buffer.rb +29 -0
- data/lib/sidekiq/scheduled.rb +76 -38
- data/lib/sidekiq/testing/inline.rb +4 -4
- data/lib/sidekiq/testing.rb +42 -69
- 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 +95 -11
- data/lib/sidekiq/web/csrf_protection.rb +4 -4
- data/lib/sidekiq/web/helpers.rb +58 -30
- data/lib/sidekiq/web.rb +22 -17
- data/lib/sidekiq/worker_compatibility_alias.rb +13 -0
- data/lib/sidekiq.rb +85 -202
- data/sidekiq.gemspec +12 -10
- data/web/assets/javascripts/application.js +77 -26
- 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 +168 -0
- data/web/assets/javascripts/dashboard.js +3 -240
- data/web/assets/javascripts/metrics.js +264 -0
- data/web/assets/stylesheets/application-dark.css +17 -17
- data/web/assets/stylesheets/application-rtl.css +2 -91
- data/web/assets/stylesheets/application.css +69 -302
- data/web/locales/ar.yml +70 -70
- 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 +84 -69
- data/web/locales/es.yml +68 -68
- data/web/locales/fa.yml +65 -65
- data/web/locales/fr.yml +81 -67
- 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 +73 -68
- data/web/locales/ko.yml +52 -52
- data/web/locales/lt.yml +66 -66
- 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 +67 -66
- 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 +67 -67
- data/web/locales/zh-cn.yml +43 -16
- data/web/locales/zh-tw.yml +42 -8
- data/web/views/_footer.erb +5 -2
- data/web/views/_job_info.erb +18 -2
- 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 +1 -1
- data/web/views/_summary.erb +1 -1
- data/web/views/busy.erb +44 -28
- data/web/views/dashboard.erb +36 -4
- data/web/views/filtering.erb +7 -0
- data/web/views/metrics.erb +82 -0
- data/web/views/metrics_for_job.erb +68 -0
- data/web/views/morgue.erb +5 -9
- data/web/views/queue.erb +15 -15
- data/web/views/queues.erb +3 -1
- data/web/views/retries.erb +5 -9
- data/web/views/scheduled.erb +12 -13
- metadata +62 -31
- 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/worker.rb +0 -311
data/lib/sidekiq/job_retry.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "sidekiq/scheduled"
|
4
|
-
require "sidekiq/api"
|
5
|
-
|
6
3
|
require "zlib"
|
7
4
|
require "base64"
|
5
|
+
require "sidekiq/component"
|
8
6
|
|
9
7
|
module Sidekiq
|
10
8
|
##
|
@@ -25,18 +23,19 @@ module Sidekiq
|
|
25
23
|
#
|
26
24
|
# A job looks like:
|
27
25
|
#
|
28
|
-
# { 'class' => '
|
26
|
+
# { 'class' => 'HardJob', 'args' => [1, 2, 'foo'], 'retry' => true }
|
29
27
|
#
|
30
28
|
# The 'retry' option also accepts a number (in place of 'true'):
|
31
29
|
#
|
32
|
-
# { 'class' => '
|
30
|
+
# { 'class' => 'HardJob', 'args' => [1, 2, 'foo'], 'retry' => 5 }
|
33
31
|
#
|
34
32
|
# The job will be retried this number of times before giving up. (If simply
|
35
33
|
# 'true', Sidekiq retries 25 times)
|
36
34
|
#
|
37
|
-
#
|
35
|
+
# Relevant options for job retries:
|
38
36
|
#
|
39
|
-
# * 'queue' - the queue
|
37
|
+
# * 'queue' - the queue for the initial job
|
38
|
+
# * 'retry_queue' - if job retries should be pushed to a different (e.g. lower priority) queue
|
40
39
|
# * 'retry_count' - number of times we've retried so far.
|
41
40
|
# * 'error_message' - the message from the exception
|
42
41
|
# * 'error_class' - the exception class
|
@@ -50,13 +49,14 @@ module Sidekiq
|
|
50
49
|
# The default number of retries is 25 which works out to about 3 weeks
|
51
50
|
# You can change the default maximum number of retries in your initializer:
|
52
51
|
#
|
53
|
-
# Sidekiq.
|
52
|
+
# Sidekiq.default_configuration[:max_retries] = 7
|
54
53
|
#
|
55
|
-
# or limit the number of retries for a particular
|
54
|
+
# or limit the number of retries for a particular job and send retries to
|
55
|
+
# a low priority queue with:
|
56
56
|
#
|
57
|
-
# class
|
58
|
-
# include Sidekiq::
|
59
|
-
# sidekiq_options :
|
57
|
+
# class MyJob
|
58
|
+
# include Sidekiq::Job
|
59
|
+
# sidekiq_options retry: 10, retry_queue: 'low'
|
60
60
|
# end
|
61
61
|
#
|
62
62
|
class JobRetry
|
@@ -64,17 +64,19 @@ module Sidekiq
|
|
64
64
|
|
65
65
|
class Skip < Handled; end
|
66
66
|
|
67
|
-
include Sidekiq::
|
67
|
+
include Sidekiq::Component
|
68
68
|
|
69
69
|
DEFAULT_MAX_RETRY_ATTEMPTS = 25
|
70
70
|
|
71
|
-
def initialize(
|
72
|
-
@
|
71
|
+
def initialize(capsule)
|
72
|
+
@config = @capsule = capsule
|
73
|
+
@max_retries = Sidekiq.default_configuration[:max_retries] || DEFAULT_MAX_RETRY_ATTEMPTS
|
74
|
+
@backtrace_cleaner = Sidekiq.default_configuration[:backtrace_cleaner]
|
73
75
|
end
|
74
76
|
|
75
77
|
# The global retry handler requires only the barest of data.
|
76
78
|
# We want to be able to retry as much as possible so we don't
|
77
|
-
# require the
|
79
|
+
# require the job to be instantiated.
|
78
80
|
def global(jobstr, queue)
|
79
81
|
yield
|
80
82
|
rescue Handled => ex
|
@@ -88,9 +90,9 @@ module Sidekiq
|
|
88
90
|
|
89
91
|
msg = Sidekiq.load_json(jobstr)
|
90
92
|
if msg["retry"]
|
91
|
-
|
93
|
+
process_retry(nil, msg, queue, e)
|
92
94
|
else
|
93
|
-
|
95
|
+
@capsule.config.death_handlers.each do |handler|
|
94
96
|
handler.call(msg, e)
|
95
97
|
rescue => handler_ex
|
96
98
|
handle_exception(handler_ex, {context: "Error calling death handler", job: msg})
|
@@ -101,14 +103,14 @@ module Sidekiq
|
|
101
103
|
end
|
102
104
|
|
103
105
|
# The local retry support means that any errors that occur within
|
104
|
-
# this block can be associated with the given
|
106
|
+
# this block can be associated with the given job instance.
|
105
107
|
# This is required to support the `sidekiq_retries_exhausted` block.
|
106
108
|
#
|
107
109
|
# Note that any exception from the block is wrapped in the Skip
|
108
110
|
# exception so the global block does not reprocess the error. The
|
109
111
|
# Skip exception is unwrapped within Sidekiq::Processor#process before
|
110
112
|
# calling the handle_exception handlers.
|
111
|
-
def local(
|
113
|
+
def local(jobinst, jobstr, queue)
|
112
114
|
yield
|
113
115
|
rescue Handled => ex
|
114
116
|
raise ex
|
@@ -121,11 +123,11 @@ module Sidekiq
|
|
121
123
|
|
122
124
|
msg = Sidekiq.load_json(jobstr)
|
123
125
|
if msg["retry"].nil?
|
124
|
-
msg["retry"] =
|
126
|
+
msg["retry"] = jobinst.class.get_sidekiq_options["retry"]
|
125
127
|
end
|
126
128
|
|
127
129
|
raise e unless msg["retry"]
|
128
|
-
|
130
|
+
process_retry(jobinst, msg, queue, e)
|
129
131
|
# We've handled this error associated with this job, don't
|
130
132
|
# need to handle it at the global level
|
131
133
|
raise Skip
|
@@ -133,10 +135,10 @@ module Sidekiq
|
|
133
135
|
|
134
136
|
private
|
135
137
|
|
136
|
-
# Note that +
|
137
|
-
# instantiate the
|
138
|
+
# Note that +jobinst+ can be nil here if an error is raised before we can
|
139
|
+
# instantiate the job instance. All access must be guarded and
|
138
140
|
# best effort.
|
139
|
-
def
|
141
|
+
def process_retry(jobinst, msg, queue, exception)
|
140
142
|
max_retry_attempts = retry_attempts_from(msg["retry"], @max_retries)
|
141
143
|
|
142
144
|
msg["queue"] = (msg["retry_queue"] || queue)
|
@@ -158,33 +160,80 @@ module Sidekiq
|
|
158
160
|
end
|
159
161
|
|
160
162
|
if msg["backtrace"]
|
163
|
+
backtrace = @backtrace_cleaner.call(exception.backtrace)
|
161
164
|
lines = if msg["backtrace"] == true
|
162
|
-
|
165
|
+
backtrace
|
163
166
|
else
|
164
|
-
|
167
|
+
backtrace[0...msg["backtrace"].to_i]
|
165
168
|
end
|
166
169
|
|
167
170
|
msg["error_backtrace"] = compress_backtrace(lines)
|
168
171
|
end
|
169
172
|
|
170
|
-
if count
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
173
|
+
return retries_exhausted(jobinst, msg, exception) if count >= max_retry_attempts
|
174
|
+
|
175
|
+
rf = msg["retry_for"]
|
176
|
+
return retries_exhausted(jobinst, msg, exception) if rf && ((msg["failed_at"] + rf) < Time.now.to_f)
|
177
|
+
|
178
|
+
strategy, delay = delay_for(jobinst, count, exception, msg)
|
179
|
+
case strategy
|
180
|
+
when :discard
|
181
|
+
return # poof!
|
182
|
+
when :kill
|
183
|
+
return retries_exhausted(jobinst, msg, exception)
|
184
|
+
end
|
185
|
+
|
186
|
+
# Logging here can break retries if the logging device raises ENOSPC #3979
|
187
|
+
# logger.debug { "Failure! Retry #{count} in #{delay} seconds" }
|
188
|
+
jitter = rand(10) * (count + 1)
|
189
|
+
retry_at = Time.now.to_f + delay + jitter
|
190
|
+
payload = Sidekiq.dump_json(msg)
|
191
|
+
redis do |conn|
|
192
|
+
conn.zadd("retry", retry_at.to_s, payload)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
# returns (strategy, seconds)
|
197
|
+
def delay_for(jobinst, count, exception, msg)
|
198
|
+
rv = begin
|
199
|
+
# sidekiq_retry_in can return two different things:
|
200
|
+
# 1. When to retry next, as an integer of seconds
|
201
|
+
# 2. A symbol which re-routes the job elsewhere, e.g. :discard, :kill, :default
|
202
|
+
block = jobinst&.sidekiq_retry_in_block
|
203
|
+
|
204
|
+
# the sidekiq_retry_in_block can be defined in a wrapped class (ActiveJob for instance)
|
205
|
+
unless msg["wrapped"].nil?
|
206
|
+
wrapped = Object.const_get(msg["wrapped"])
|
207
|
+
block = wrapped.respond_to?(:sidekiq_retry_in_block) ? wrapped.sidekiq_retry_in_block : nil
|
178
208
|
end
|
179
|
-
|
180
|
-
|
181
|
-
|
209
|
+
block&.call(count, exception, msg)
|
210
|
+
rescue Exception => e
|
211
|
+
handle_exception(e, {context: "Failure scheduling retry using the defined `sidekiq_retry_in` in #{jobinst.class.name}, falling back to default"})
|
212
|
+
nil
|
213
|
+
end
|
214
|
+
|
215
|
+
rv = rv.to_i if rv.respond_to?(:to_i)
|
216
|
+
delay = (count**4) + 15
|
217
|
+
if Integer === rv && rv > 0
|
218
|
+
delay = rv
|
219
|
+
elsif rv == :discard
|
220
|
+
return [:discard, nil] # do nothing, job goes poof
|
221
|
+
elsif rv == :kill
|
222
|
+
return [:kill, nil]
|
182
223
|
end
|
224
|
+
|
225
|
+
[:default, delay]
|
183
226
|
end
|
184
227
|
|
185
|
-
def retries_exhausted(
|
228
|
+
def retries_exhausted(jobinst, msg, exception)
|
186
229
|
begin
|
187
|
-
block =
|
230
|
+
block = jobinst&.sidekiq_retries_exhausted_block
|
231
|
+
|
232
|
+
# the sidekiq_retries_exhausted_block can be defined in a wrapped class (ActiveJob for instance)
|
233
|
+
unless msg["wrapped"].nil?
|
234
|
+
wrapped = Object.const_get(msg["wrapped"])
|
235
|
+
block = wrapped.respond_to?(:sidekiq_retries_exhausted_block) ? wrapped.sidekiq_retries_exhausted_block : nil
|
236
|
+
end
|
188
237
|
block&.call(msg, exception)
|
189
238
|
rescue => e
|
190
239
|
handle_exception(e, {context: "Error calling retries_exhausted", job: msg})
|
@@ -192,7 +241,7 @@ module Sidekiq
|
|
192
241
|
|
193
242
|
send_to_morgue(msg) unless msg["dead"] == false
|
194
243
|
|
195
|
-
|
244
|
+
@capsule.config.death_handlers.each do |handler|
|
196
245
|
handler.call(msg, exception)
|
197
246
|
rescue => e
|
198
247
|
handle_exception(e, {context: "Error calling death handler", job: msg})
|
@@ -202,7 +251,15 @@ module Sidekiq
|
|
202
251
|
def send_to_morgue(msg)
|
203
252
|
logger.info { "Adding dead #{msg["class"]} job #{msg["jid"]}" }
|
204
253
|
payload = Sidekiq.dump_json(msg)
|
205
|
-
|
254
|
+
now = Time.now.to_f
|
255
|
+
|
256
|
+
redis do |conn|
|
257
|
+
conn.multi do |xa|
|
258
|
+
xa.zadd("dead", now.to_s, payload)
|
259
|
+
xa.zremrangebyscore("dead", "-inf", now - @capsule.config[:dead_timeout_in_seconds])
|
260
|
+
xa.zremrangebyrank("dead", 0, - @capsule.config[:dead_max_jobs])
|
261
|
+
end
|
262
|
+
end
|
206
263
|
end
|
207
264
|
|
208
265
|
def retry_attempts_from(msg_retry, default)
|
@@ -213,22 +270,6 @@ module Sidekiq
|
|
213
270
|
end
|
214
271
|
end
|
215
272
|
|
216
|
-
def delay_for(worker, count, exception)
|
217
|
-
jitter = rand(10) * (count + 1)
|
218
|
-
if worker&.sidekiq_retry_in_block
|
219
|
-
custom_retry_in = retry_in(worker, count, exception).to_i
|
220
|
-
return custom_retry_in + jitter if custom_retry_in > 0
|
221
|
-
end
|
222
|
-
(count**4) + 15 + jitter
|
223
|
-
end
|
224
|
-
|
225
|
-
def retry_in(worker, count, exception)
|
226
|
-
worker.sidekiq_retry_in_block.call(count, exception)
|
227
|
-
rescue Exception => e
|
228
|
-
handle_exception(e, {context: "Failure scheduling retry using the defined `sidekiq_retry_in` in #{worker.class.name}, falling back to default"})
|
229
|
-
nil
|
230
|
-
end
|
231
|
-
|
232
273
|
def exception_caused_by_shutdown?(e, checked_causes = [])
|
233
274
|
return false unless e.cause
|
234
275
|
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require "securerandom"
|
2
|
+
require "time"
|
3
|
+
|
4
|
+
module Sidekiq
|
5
|
+
module JobUtil
|
6
|
+
# These functions encapsulate various job utilities.
|
7
|
+
|
8
|
+
TRANSIENT_ATTRIBUTES = %w[]
|
9
|
+
|
10
|
+
def validate(item)
|
11
|
+
raise(ArgumentError, "Job must be a Hash with 'class' and 'args' keys: `#{item}`") unless item.is_a?(Hash) && item.key?("class") && item.key?("args")
|
12
|
+
raise(ArgumentError, "Job args must be an Array: `#{item}`") unless item["args"].is_a?(Array) || item["args"].is_a?(Enumerator::Lazy)
|
13
|
+
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)
|
14
|
+
raise(ArgumentError, "Job 'at' must be a Numeric timestamp: `#{item}`") if item.key?("at") && !item["at"].is_a?(Numeric)
|
15
|
+
raise(ArgumentError, "Job tags must be an Array: `#{item}`") if item["tags"] && !item["tags"].is_a?(Array)
|
16
|
+
raise(ArgumentError, "retry_for must be a relative amount of time, e.g. 48.hours `#{item}`") if item["retry_for"] && item["retry_for"] > 1_000_000_000
|
17
|
+
end
|
18
|
+
|
19
|
+
def verify_json(item)
|
20
|
+
job_class = item["wrapped"] || item["class"]
|
21
|
+
args = item["args"]
|
22
|
+
mode = Sidekiq::Config::DEFAULTS[:on_complex_arguments]
|
23
|
+
|
24
|
+
if mode == :raise || mode == :warn
|
25
|
+
if (unsafe_item = json_unsafe?(args))
|
26
|
+
msg = <<~EOM
|
27
|
+
Job arguments to #{job_class} must be native JSON types, but #{unsafe_item.inspect} is a #{unsafe_item.class}.
|
28
|
+
See https://github.com/sidekiq/sidekiq/wiki/Best-Practices
|
29
|
+
To disable this error, add `Sidekiq.strict_args!(false)` to your initializer.
|
30
|
+
EOM
|
31
|
+
|
32
|
+
if mode == :raise
|
33
|
+
raise(ArgumentError, msg)
|
34
|
+
else
|
35
|
+
warn(msg)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def normalize_item(item)
|
42
|
+
validate(item)
|
43
|
+
|
44
|
+
# merge in the default sidekiq_options for the item's class and/or wrapped element
|
45
|
+
# this allows ActiveJobs to control sidekiq_options too.
|
46
|
+
defaults = normalized_hash(item["class"])
|
47
|
+
defaults = defaults.merge(item["wrapped"].get_sidekiq_options) if item["wrapped"].respond_to?(:get_sidekiq_options)
|
48
|
+
item = defaults.merge(item)
|
49
|
+
|
50
|
+
raise(ArgumentError, "Job must include a valid queue name") if item["queue"].nil? || item["queue"] == ""
|
51
|
+
|
52
|
+
# remove job attributes which aren't necessary to persist into Redis
|
53
|
+
TRANSIENT_ATTRIBUTES.each { |key| item.delete(key) }
|
54
|
+
|
55
|
+
item["jid"] ||= SecureRandom.hex(12)
|
56
|
+
item["class"] = item["class"].to_s
|
57
|
+
item["queue"] = item["queue"].to_s
|
58
|
+
item["retry_for"] = item["retry_for"].to_i if item["retry_for"]
|
59
|
+
item["created_at"] ||= Time.now.to_f
|
60
|
+
item
|
61
|
+
end
|
62
|
+
|
63
|
+
def normalized_hash(item_class)
|
64
|
+
if item_class.is_a?(Class)
|
65
|
+
raise(ArgumentError, "Message must include a Sidekiq::Job class, not class name: #{item_class.ancestors.inspect}") unless item_class.respond_to?(:get_sidekiq_options)
|
66
|
+
item_class.get_sidekiq_options
|
67
|
+
else
|
68
|
+
Sidekiq.default_job_options
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
RECURSIVE_JSON_UNSAFE = {
|
75
|
+
Integer => ->(val) {},
|
76
|
+
Float => ->(val) {},
|
77
|
+
TrueClass => ->(val) {},
|
78
|
+
FalseClass => ->(val) {},
|
79
|
+
NilClass => ->(val) {},
|
80
|
+
String => ->(val) {},
|
81
|
+
Array => ->(val) {
|
82
|
+
val.each do |e|
|
83
|
+
unsafe_item = RECURSIVE_JSON_UNSAFE[e.class].call(e)
|
84
|
+
return unsafe_item unless unsafe_item.nil?
|
85
|
+
end
|
86
|
+
nil
|
87
|
+
},
|
88
|
+
Hash => ->(val) {
|
89
|
+
val.each do |k, v|
|
90
|
+
return k unless String === k
|
91
|
+
|
92
|
+
unsafe_item = RECURSIVE_JSON_UNSAFE[v.class].call(v)
|
93
|
+
return unsafe_item unless unsafe_item.nil?
|
94
|
+
end
|
95
|
+
nil
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
RECURSIVE_JSON_UNSAFE.default = ->(val) { val }
|
100
|
+
RECURSIVE_JSON_UNSAFE.compare_by_identity
|
101
|
+
private_constant :RECURSIVE_JSON_UNSAFE
|
102
|
+
|
103
|
+
def json_unsafe?(item)
|
104
|
+
RECURSIVE_JSON_UNSAFE[item.class].call(item)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|