ruby-on-rails - Sidekiq 作业错误处理并在父作业中重试?
问题描述
我有一个工作,它会产生其他工作并且内联做一些事情。
abc_job.rb
class AbcJob
def perform
post_events
end
private
def post_events
foo
bar
done
end
def foo
OneJob.perform_later({})
end
def bar
TwoJob.perform_later({})
end
def done
ThreeJob.perform_later({})
end
end
需要处理错误并为里面的所有作业添加重试abc_job
。
一种方法是在post_events
所有方法中添加开始救援。
请建议最好的方法,并且还需要为子作业添加重试。
解决方案
您可以做的一种方法是使用元编程。您可以创建一个外部模块说ExceptionHandler
->您可以rescue
在这里处理错误时处理错误->然后将此模块包含在您的AbcJob
文件中。
前任:
创建一个模块文件:
module ExceptionHandler extend ActiveSupport::Concern # Define custom error subclasses - rescue catches `StandardErrors` class InvalidToken < StandardError; end class MissingToken < StandardError; end included do # Define in-built handlers rescue_from ActiveRecord::RecordNotFound, with: :four_not_four rescue_from ActiveRecord::RecordInvalid, with: :four_not_four # Define custom handlers rescue_from ExceptionHandler::MissingToken, with: :unauthorized_request rescue_from ExceptionHandler::InvalidToken, with: :unauthorized_request end private ## Action on what to do when error occurs def four_not_four redirect_to root_path, alert: "This page was not found" end def unauthorized_request redirect_to root_path, alert: "You're not authorized to perform this request" end end
然后在您的
Abcjob
文件中,只需包含此模块:class AbcJob include ExceptionHandler def perform post_events end ... end
现在,每当作业抛出处理程序时,这将触发您在中提到的方法
ExceptionHandler
并运行它们。
推荐阅读
- oracle - Oracle 平均数
- rust - 缓存 sqlx 池导致文件句柄溢出
- ruby-on-rails - Ruby on Rails - Dhalang PDF 无法使用 localhost URL
- azure-sql-database - Azure函数将csv转换为excel文件
- javascript - React.createContext 给出数组的长度而不是给出原始数组
- java - 如何将它与 java 中的 regionmatches 循环一起使用
- angular - @ngxs-logger-plugin 没有显示带有某些 Observable 的 nextState
- python - 奇怪的 numpy 数组元素检查行为
- deployment - 部署 WebApp 并始终保持运行
- r - 我怎样才能使函数通过向量并识别其上数字的符号?