【问题标题】:Timeout in a delayed job延迟作业超时
【发布时间】:2014-10-03 21:25:27
【问题描述】:

我有一些代码可能会运行更长的时间。但是,如果它确实我想杀死它,这就是我现在正在做的事情:

def perform
    Timeout.timeout(ENV['JOB_TIMEOUT'].to_i, Exceptions::WorkerTimeout) { do_perform }
  end

私人的 def do_perform ...一些代码... 结束

其中JOB_TIMEOUT 是一个环境变量,其值为10.seconds。我收到报告称,这仍然不能阻止我的工作运行时间超过应有的时间。

有没有更好的方法来做到这一点?

【问题讨论】:

    标签: ruby-on-rails ruby delayed-job


    【解决方案1】:

    我相信delayed_job 会通过多次重试等进行一些异常处理巫术,更不用说我认为 do_perform 将立即返回,并且该作业将在另一个线程中照常继续。我想一个更好的方法是在工作人员内部进行流量控制

    def perform
      # A nil timeout will continue with no timeout, protect against unset ENV
      timeout = (ENV['JOB_TIMEOUT'] || 10).to_i
    
      do_stuff
    
      begin
      Timeout.timeout(timeout) { do_long_running_stuff }
      rescue Timeout::Error
        clean_up_after_self
        notify_business_logic_of_failure
      end
    end
    

    这会奏效。额外的好处是延迟作业不会与您的业务逻辑如此紧密地耦合 - 此代码可以未经修改地移植到任何其他作业排队系统。

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 2011-12-14
      • 2022-11-10
      • 2013-06-10
      • 2012-03-29
      • 1970-01-01
      • 2018-08-05
      • 2013-11-26
      • 2015-10-02
      相关资源
      最近更新 更多