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

编程入门 行业动态 更新时间:2024-10-27 07:27:19
本文介绍了Rails 后台工作者总是第一次失败,第二次工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个在保存对象后触发的 sidekiq 工作器:

I have a sidekiq worker that fires after an object is saved:

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

worker 如下所示(它还没有完成……只是在测试):

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

奇怪的是,每次 worker 触发时,它最初都会失败并显示以下错误:

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.

推荐答案

确保 process always true - 这是需要的一部分回调链.

Ensure that process always true - which is needed as part of the callback chain.

所以改写成这样:

def process ProcessWorker.perform_async(id, key) if key.present? true end

当执行 ActiveRecord 回调链时,任何返回布尔值的方法都会影响该链 - false 将取消回调链.

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.

更多推荐

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

本文发布于:2023-11-23 04:28:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620094.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作者   后台   工作   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!