我的工作人员很少,日志中没有错误。但生产中的相同代码不时显示错误。这并不重要,但我想了解有什么区别以及如何捕获和修复错误。这里有部分日志错误:
2016-01-18T16:34:10.725Z 535 TID-otb5ze3fo TopUpdateWorker JID-73c584ed6fec322784d67796 INFO: start
2016-01-18T16:34:10.725Z 535 TID-otb5ze3fo TopUpdateWorker JID-73c584ed6fec322784d67796 INFO: Things are happening.
2016-01-18T16:34:10.824Z 535 TID-otb5ze3fo TopUpdateWorker JID-73c584ed6fec322784d67796 INFO: fail: 0.099 sec
2016-01-18T16:34:10.824Z 535 TID-otb5ze3fo WARN: {"class"=>"TopUpdateWorker", "args"=>[], "retry"=>3, "queue"=>"default", "jid"=>"73c584ed6fec322784d67796", "created_at"=>1453134850.724368, "enqueued_at"=$
2016-01-18T16:34:10.824Z 535 TID-otb5ze3fo WARN: TypeError: no implicit conversion of nil into Array
2016-01-18T16:34:10.824Z 535 TID-otb5ze3fo WARN: /var/www/vkgazer/code/lib/modules/common_mods.rb:27:in `+'
/var/www/vkgazer/code/lib/modules/common_mods.rb:27:in `get_request'
/var/www/vkgazer/code/app/models/post.rb:170:in `top_post_updater'
/var/www/vkgazer/code/app/workers/top_update_worker.rb:9:in `perform'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:150:in `execute_job'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:132:in `block (2 levels) in process'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:127:in `block in invoke'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/middleware/server/active_record.rb:6:in `call'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/middleware/server/retry_jobs.rb:74:in `call'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/middleware/server/logging.rb:11:in `block in call'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/logging.rb:30:in `with_context'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/middleware/server/logging.rb:7:in `call'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:132:in `call'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:132:in `invoke'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:127:in `block in process'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:166:in `stats'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:126:in `process'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:79:in `process_one'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:67:in `run'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/util.rb:16:in `watchdog'
/var/www/vkgazer/code/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/util.rb:24:in `block in safe_thread'
错误始终在此字符串中。这是代码:
def get_request(code) #
i = 0
result = []
while i < code.size #
part_code = code.slice(i, 25)
if part_code.size >1
str_code = part_code.join(',')
else
str_code = part_code[0]
end
url='https://api.x.com/method/execute'
uri = URI.parse(url)
parameters = {'access_token' => '00',
'code' => "return [#{str_code}];", 'v' => '2'}
response = Net::HTTP.post_form(uri, parameters) #
result = result + JSON.parse(response.body)['response'] #here is error
i += 25
sleep 0.35
end
result #
end
在这里,我向API发送请求,并对结果中的所有响应求和。还有一件事在开发中我的工作持续时间不到10秒,但在生产中它可以是160秒。我的电脑比服务器好吗?
答案 0 :(得分:0)
因此,只要您的response.body
包含“空”回复,例如"{}"
,或者只是错过了'response'
元素,那么JSON.parse(response.body)['response']
就会{{1}所以你的代码试图这样做:
nil
...而且你收到了这个错误。可能最好的解决方法是:
result + nil