我正在创建一个后台工作,其工作主要是等待用户响应。然后根据他们的回复做一些动作。
class AwaitDriverReply
include Sidekiq::Worker
sidekiq_options retry: false
def perform(order_id)
ActiveRecord::Base.connection_pool.with_connection do
order = Order.find_by(id: order_id)
10.times do
if order.accepted == true
puts "driver confirmed"
break
elsif order.accepted == false
puts "driver has declined"
break
else
puts "awaiting response"
sleep(1)
end
end
end
end
end
我必须睡觉的原因是因为用户可以在不同时间明显回答,并且这个等待≈10秒。现在这是我第一次运行这个工作时的完美工作,然后如果我尝试重复它,那么作业会返回一个错误,我无法调试:
2016-02-22T16:34:52.096Z 1431 TID-ovpfin8kg WARN: NoMethodError: undefined method `accepted' for nil:NilClass
2016-02-22T16:34:52.096Z 1431 TID-ovpfin8kg WARN: /Users/Sebastian/workspace/ex/app/jobs/await_driver_reply.rb:11:in `block (2 levels) in perform'
/Users/Sebastian/workspace/ex/app/jobs/await_driver_reply.rb:10:in `times'
/Users/Sebastian/workspace/ex/app/jobs/await_driver_reply.rb:10:in `block in perform'
/Users/Sebastian/.rvm/gems/ruby-2.3.0-preview2/gems/activerecord-5.0.0.beta2/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `with_connection'
/Users/Sebastian/workspace/ex/app/jobs/await_driver_reply.rb:6:in `perform'
并返回nil接受的未定义方法,这通常意味着订单不存在。但是我添加了puts
来检查订单,它就在那里。