如何为特定作业设置max_run_time? [英] How to set max_run_time for a specific job?

查看:80
本文介绍了如何为特定作业设置max_run_time?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为特定工作设置 Delayed :: Worker.max_run_time = 1.hour ,我知道这需要一段时间。但是,这是在 initializers / delayed_job_config.rb 中设置为全局配置的。结果,此更改将使我的所有作业的最长运行时间为1小时。有没有一种方法可以只更改一个特定的作业而不创建自定义作业?

I want to set Delayed::Worker.max_run_time = 1.hour for a specific job that I know will take a while. However, this is set as a global configuration in initializers/delayed_job_config.rb. As a result, this change will make ALL of my jobs have a max run time of 1 hour. Is there a way to just change it for one specific job without creating a custom job?

推荐答案

查看 Worker 类在GitHub上

def run(job)
  job_say job, 'RUNNING'
  runtime =  Benchmark.realtime do
    Timeout.timeout(self.class.max_run_time.to_i, WorkerTimeout) { job.invoke_job }
    job.destroy
  end
  job_say job, 'COMPLETED after %.4f' % runtime
  return true  # did work
rescue DeserializationError => error
  job.last_error = "#{error.message}\n#{error.backtrace.join("\n")}"
  failed(job)
rescue Exception => error
  self.class.lifecycle.run_callbacks(:error, self, job){ handle_failed_job(job, error) }
  return false  # work failed
end

似乎您不能设置每个职位的最高工资。但我认为您可以在工作中暂停自己的超时时间。假设Timeout类允许嵌套!值得一试。

It doesn't appear that you can set a per-job max. But I would think you could roll your own timeout, in your job. Assuming the Timeout class allows nesting! Worth a try.

class MyLongJobClass

  def perform
    Timeout.timeout(1.hour.to_i, WorkerTimeout) { do_perform }
  end

  private

  def do_perform
    # ... real perform work
  end

end

这篇关于如何为特定作业设置max_run_time?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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