是否可以重置 Rails 中延迟作业的超时?

发布于 2025-01-09 17:19:50 字数 445 浏览 0 评论 0原文

我有一项延迟的工作,需要运行几个步骤。我想在步骤完成时重置超时。
是否可以?

例如

# config/initializers/delayed_job_config.rb
Delayed::Worker.max_run_time = 5.minutes
# app/jobs/my_process_job.rb
class MyProcessJob < ApplicationJob
  def perform
    `sleep 4m` # step 1
    # reset timeout
    `sleep 4m` # step 2
    puts 'done'
  end
end

I have a delayed job that run a few steps. I would like to reset the timeout whenever a step is completed.
Is it possible?

For example

# config/initializers/delayed_job_config.rb
Delayed::Worker.max_run_time = 5.minutes
# app/jobs/my_process_job.rb
class MyProcessJob < ApplicationJob
  def perform
    `sleep 4m` # step 1
    # reset timeout
    `sleep 4m` # step 2
    puts 'done'
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2025-01-16 17:19:50

了解延迟作业,不修改是无法重置超时的延迟工作。但是,您可以执行以下操作

# config/initializers/delayed_job_config.rb
Delayed::Worker.max_run_time = 10.minutes
# app/jobs/my_process_job.rb
class MyProcessJob < ApplicationJob
  def perform
    Timeout.timeout(5.minutes.to_i) {
      `sleep 4m` # step 1
    }

    Timeout.timeout(5.minutes.to_i) {
      `sleep 4m` # step 2
    }
    puts 'done'
  end
end

Looking at how timeouts are implemented in Delayed Job, it's not possible to reset the timeout without modifying Delayed Job. However, you could do the following instead

# config/initializers/delayed_job_config.rb
Delayed::Worker.max_run_time = 10.minutes
# app/jobs/my_process_job.rb
class MyProcessJob < ApplicationJob
  def perform
    Timeout.timeout(5.minutes.to_i) {
      `sleep 4m` # step 1
    }

    Timeout.timeout(5.minutes.to_i) {
      `sleep 4m` # step 2
    }
    puts 'done'
  end
end

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文