如何使用 cron 运行 rake 任务并在每次运行之间设置时间间隔?

发布于 2024-10-22 00:41:02 字数 512 浏览 4 评论 0原文

有没有办法让 rake 任务的每次开始到结束运行之间有 2 分钟的间隙?

例如:

1:00 PM: Run a rake task
         Task will usually take 30 seconds to complete, but could take 5 minutes.
         The task took 3 minutes to complete in this first run.
1:02 PM: cron has been scheduled to run task every 2 minutes but the previous 
         task has not completed yet, so cron should not run this task right now
1:03 PM: task has completed
         cron should wait 2 minutes
1:05 PM  cron should re-run the task

Is there some way to have a gap of 2 minutes between each start-to-finish run of a rake task?

For example:

1:00 PM: Run a rake task
         Task will usually take 30 seconds to complete, but could take 5 minutes.
         The task took 3 minutes to complete in this first run.
1:02 PM: cron has been scheduled to run task every 2 minutes but the previous 
         task has not completed yet, so cron should not run this task right now
1:03 PM: task has completed
         cron should wait 2 minutes
1:05 PM  cron should re-run the task

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

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

发布评论

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

评论(3

月竹挽风 2024-10-29 00:41:03

看看delayed_jobs 或resque-scheduler gems。

Take a look at delayed_jobs or resque-scheduler gems.

八巷 2024-10-29 00:41:03

您可以使用无限循环(Ruby 程序)来代替 cron-tab 来运行 rake 任务。
任务完成后只需使用 sleep 方法停止 2 分钟。
并且有一个救援块,可以在出现任何错误时再次运行您的 rake 任务。

Instead of cron-tab you can have an infinite loop (a ruby program) for running your rake task.
After completion of task just use sleep method to stop it for 2 minutes.
And have a rescue block which will run your rake task again in case of any error.

So尛奶瓶 2024-10-29 00:41:03

我建议让任务的结束在运行结束时自行重新安排。

编辑

虽然我对 cron 的(有限)经验源于 10 多年前,但我认为你可以让 cron 不重新启动仍在运行的作业吗?
因此,您可以保持“每 x 分钟一次,但如果它仍在运行”的调度,但最后任务将作业重置为“y 分钟后”。

然后,如果任务碰巧无法重新安排自己,cron 的正常设施将“很快”重新启动它。

I suggest letting the end of the task reschedule itself at the end of it's run.

EDIT

Though my (limited) experience with cron stems from over 10 years ago I assume you can make cron NOT restart a job if it's still running?
So you can keep the scheduling of "every x minutes, but not if it's still running", but at the end the task resets the job to "y minutes later".

Then if the task happens to NOT be able to reschedule itself cron's normal facilities will restart it anyway "soon".

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