Sidekiq/Airbrake 仅在重试熄灭时发布异常 [英] Sidekiq/Airbrake only post exception when retries extinguished

查看:62
本文介绍了Sidekiq/Airbrake 仅在重试熄灭时发布异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 Airbrake 仅在重试用完时才收到错误通知,但我似乎想不出实现它的方法...

I would like Airbrake to only be notified of errors when the retries are exhausted, but I can't seem to think of a way to implement it...

我可以添加一个 sidekiq_retries_exhausted 钩子来将错误发送到 AirBrake,但我能想到的捕获实际故障的唯一方法是添加一个吞下错误的中间件,但是,如果有,该作业将被标记为成功没有错误......那么永远不会有任何重试..

I can add a sidekiq_retries_exhausted hook to send the error to AirBrake but the only way I can think of catching the actual failures is to add a middleware that swallows the error, but then, the job will be marked as a success if there is no error... then there will never be any retries..

希望这是有道理的!

推荐答案

我设法通过插入列表开头的 Sidekiq 中间件实现了这一点:

I managed to implement this with a Sidekiq middleware that is inserted at the start of the list:

class RaiseOnRetriesExtinguishedMiddleware
    include Sidekiq::Util

  def call(worker, msg, queue)
    yield
  rescue Exception => e
    bubble_exception(msg, e)
  end

  private

  def bubble_exception(msg, e)
    max_retries = msg['retries'] || Sidekiq::Middleware::Server::RetryJobs::DEFAULT_MAX_RETRY_ATTEMPTS
    retry_count = msg['retry_count'] || 0
    last_try = !msg['retry'] || retry_count == max_retries - 1

    raise e if last_try
  end

  def retry_middleware
    @retry_middleware ||= Sidekiq::Middleware::Server::RetryJobs.new
  end
end

如果是最后一次尝试并抛出异常,它会让它冒泡(到 Airbrake),否则不会.这不会影响失败记录,因为它会在链的后面发生.

If its the last try and its thrown an exception, it'll let it bubble up (to Airbrake) otherwise it won't. This doesn't affect failure recording as that happens later in the chain.

这篇关于Sidekiq/Airbrake 仅在重试熄灭时发布异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆