Run method periodically in manualy created interval in Rails

时间:2016-04-15 11:12:52

标签: ruby-on-rails ruby cron

I have a form when user can set day in week and time period (for example Monday from 02:00 to 03:00). Then I want to run specific method (periodically, for example every 30 seconds) but only during given time interval (it should be performed on background every Monday during 02:00 and 03:00, every 30 seconds). Is it possible to do this when time interval is created dynamically (by user)?

1 个答案:

答案 0 :(得分:1)

你可以定义一个运行rake任务的cron作业(参考这个语法糖:https://github.com/javan/whenever)。 您的rake任务可以每X秒运行一次,并在数据库中查询新的作业。

但是,通过cron调用rake任务每次都会启动rails应用程序,因此更加苛刻的方法是将sidekiq(http://sidekiq.org/)与sidekiq-scheduler(https://github.com/moove-it/sidekiq-scheduler)结合使用。 / p>