[英]Rails background worker always fails first time, works second
I have a sidekiq worker that fires after an object is saved: 我有一个sidekiq worker,在保存对象后将其触发:
class Post < ActiveRecord::Base
attr_accessible :description,
:name,
:key
after_save :process
def process
ProcessWorker.perform_async(id, key) if key.present?
end
def secure_url
key.match(/(.*\/)+(.*$)/)[1]
end
def nonsecure_url
key.gsub('https', 'http')
end
end
The worker looks like the following (it's not complete yet... just testing): 工作程序如下所示(尚未完成,只是测试):
class ProcessWorker
include Sidekiq::Worker
def perform(id, key)
post = Post.find(id)
puts post.nonsecure_url
end
end
Oddly enough, every time the worker fires, it initially fails with the following error: 奇怪的是,每次工人解雇时,它最初都会失败并出现以下错误:
undefined method `gsub' for nil:NilClass
But then, when the worker retries a bit later – it always succeeds. 但是,当工人稍后重试时,它总是成功。
It really feels as though something isn't initializing when it should... but I can't seem to track it down. 确实感觉好像什么时候没有初始化……但是我似乎无法追踪。
Ensure that process
always true
- which is needed as part of the callback chain. 确保该process
始终为 true
这是回调链的一部分。
So rewrite it like this: 所以像这样重写它:
def process
ProcessWorker.perform_async(id, key) if key.present?
true
end
When the ActiveRecord
callback chain is being executed any methods that return a boolean will affect the chain - a false
will cancel the callback chain. 执行ActiveRecord
回调链时,任何返回布尔值的方法都会影响该链-如果为false
则将取消该回调链。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.