【问题标题】:stop sidekiq worker perform method停止 sidekiq 工作者执行方法
【发布时间】:2015-04-08 23:48:48
【问题描述】:

我的工人的 perform 方法中有一个 begin rescue 块,就像这样

begin
  HTTParty.get(url)
rescue
  ## call failed for some reason, log and stop performing
  break
end
## do more stuff here with the result of the call if it didn't fail
## this can fail too so a further
  begin
    ##would be cumbersome
  rescue
  end

但是我得到了Invalid break (SyntaxError)

还有其他方法可以告诉 sidekiq 这项工作基本上完成了吗?我不想让它重试,而是完全退出。

【问题讨论】:

    标签: ruby-on-rails ruby sidekiq


    【解决方案1】:

    返回:

    begin
      HTTParty.get(url)
    rescue => e
      logger.warn(e.message)
      return
    end
    

    【讨论】:

      【解决方案2】:

      你不需要任何休息。如果 begin 中的代码失败,将停止执行并执行 rescue 块中的内容。要默默忽略并返回,只需将救援内容留空即可。

      def perform
        begin
          HTTParty.get(url)
        rescue
          ## call failed for some reason, log and stop performing
        end
      end
      

      你也可以缩短方法

      def perform
        HTTParty.get(url)
      rescue
        ## call failed for some reason, log and stop performing
      end
      

      您可能只想明确地挽救某些异常。我不确定你想要拯救什么(这取决于 HTTParty 可以筹集到什么)。

      def perform
        HTTParty.get(url)
      rescue WhateverError
        ## call failed for some reason, log and stop performing
      end
      

      这是一个例子

      def perform
        HTTParty.get(url)
      rescue WhateverError => e
        Rails.logger.error "Kaboom! #{e.message}"
      end
      

      【讨论】:

      • 好吧,我有很多代码在 HTTParty 调用之后执行(其中一些也可能失败)。我不希望有许多开始救援结束块的级联代码层,而只是将容易失败的代码包装在开始和救援中并退出执行
      • 如果你在最后使用一个救援语句,就像我展示给你的那样,没有任何异常类型,所有(标准)异常都将被救援。
      猜你喜欢
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 2018-04-29
      • 2017-04-05
      相关资源
      最近更新 更多