开发者

How to run a rake task using cron with a time-gap between each run?

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 secon开发者_高级运维ds 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


Take a look at delayed_jobs or resque-scheduler gems.


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.


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜