简体   繁体   English

Rails后台工作者总是第一次失败,第二次工作

[英]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.

相关问题 第一次更新失败,第二次成功 - Update fails first time, succeeds second time Rails:脚手架适用于第一张桌子,但不适用于第二张桌子 - Rails : Scaffold works for the first, but not for the second table 尝试抢救块中的第一个错误后,Rails 4.0.0 to_time总是失败 - Rails 4.0.0 to_time always fails after first error in try rescue block Rails会话第一次失败并通过重新加载 - Rails sessions fails first time and passes on reload 在第一个工作期间如何运行第二个工作? - How running second worker, during the first worker? Ruby on Rails,背景图片第一次不起作用 - Ruby on rails, background image not working the first time Rails 4:SASS背景图像在本地工作,在AWS中失败 - Rails 4: SASS background image works locally, fails in AWS 在Mongodb的Rails中的关联很多时间无法找到数据,但有时可以正常工作 - Association in Mongodb in rails many time fails to find data but sometime works rails + dokku bundle在第一次尝试部署时失败 - rails + dokku bundle fails when trying to deploy for the first time Sidekiq-Unique-jobs Lock 仅适用于第一个 Worker,然后所有待处理的作业同时执行? - Sidekiq-Unique-jobs Lock works only for first Worker, then all pending jobs get executed at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM