如何使用 cron 运行 rake 任务并在每次运行之间设置时间间隔?
有没有办法让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看delayed_jobs 或resque-scheduler gems。
Take a look at delayed_jobs or resque-scheduler gems.
您可以使用无限循环(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.
我建议让任务的结束在运行结束时自行重新安排。
编辑
虽然我对 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".