如何让Ruby每10分钟运行一次任务?

时间:2021-12-17 04:45:41

I would like to do a cron job every 10 minutes, but my system only does 1 hour. So I'm looking for a method to do this. I've seen Timer and sleep but I'm not sure how to do this or even better yet a resource for achieving this.

我想每10分钟做一次cron工作,但我的系统只做1小时。所以我正在寻找一种方法来做到这一点。我已经看过计时器和睡眠,但我不知道如何做到这一点,甚至更好的是实现这一目标的资源。

2 个解决方案

#1


4  

Take a look at http://rufus.rubyforge.org/rufus-scheduler/

看看http://rufus.rubyforge.org/rufus-scheduler/

rufus-scheduler is a Ruby gem for scheduling pieces of code (jobs). It understands running a job AT a certain time, IN a certain time, EVERY x time or simply via a CRON statement. rufus-scheduler is no replacement for cron/at since it runs inside of Ruby.

rufus-scheduler是一个Ruby gem,用于安排代码片段(作业)。它了解在某个时间,某个时间,每次x时间或仅通过CRON语句运行工作。 rufus-scheduler不是cron / at的替代品,因为它在Ruby内部运行。

#2


2  

To do this reliably, invest in a VPS and create the 10-minute cron job as desired. Trying to emulate cron all on your own is very likely to fail in unforeseen ways.

为了可靠地执行此操作,请投资VPS并根据需要创建10分钟的cron作业。试图自己模仿cron很可能会以不可预见的方式失败。

Creating a sleeping process is not the way to go about this; if your server doesn't give you the freedom to make your own cron as you like it, you probably can't create your own background process for this sort of thing, either. You might be able to, on each request, take a look and see how many of the jobs need done (if it was 25 minutes since last request, you might have to do two), and go back and do them retroactively.

创建一个休眠过程不是解决这个问题的方法;如果你的服务器不能让你*地按自己的意愿制作自己的cron,那么你可能也无法为这类事情创建自己的后台进程。您可以在每个请求中查看需要完成的工作数量(如果自上次请求起25分钟后,您可能需要执行两次),然后返回并追溯执行。

But, seriously. You need your own server to do this dependably.

不过实话说。您需要自己的服务器来可靠地执行此操作。

#1


4  

Take a look at http://rufus.rubyforge.org/rufus-scheduler/

看看http://rufus.rubyforge.org/rufus-scheduler/

rufus-scheduler is a Ruby gem for scheduling pieces of code (jobs). It understands running a job AT a certain time, IN a certain time, EVERY x time or simply via a CRON statement. rufus-scheduler is no replacement for cron/at since it runs inside of Ruby.

rufus-scheduler是一个Ruby gem,用于安排代码片段(作业)。它了解在某个时间,某个时间,每次x时间或仅通过CRON语句运行工作。 rufus-scheduler不是cron / at的替代品,因为它在Ruby内部运行。

#2


2  

To do this reliably, invest in a VPS and create the 10-minute cron job as desired. Trying to emulate cron all on your own is very likely to fail in unforeseen ways.

为了可靠地执行此操作,请投资VPS并根据需要创建10分钟的cron作业。试图自己模仿cron很可能会以不可预见的方式失败。

Creating a sleeping process is not the way to go about this; if your server doesn't give you the freedom to make your own cron as you like it, you probably can't create your own background process for this sort of thing, either. You might be able to, on each request, take a look and see how many of the jobs need done (if it was 25 minutes since last request, you might have to do two), and go back and do them retroactively.

创建一个休眠过程不是解决这个问题的方法;如果你的服务器不能让你*地按自己的意愿制作自己的cron,那么你可能也无法为这类事情创建自己的后台进程。您可以在每个请求中查看需要完成的工作数量(如果自上次请求起25分钟后,您可能需要执行两次),然后返回并追溯执行。

But, seriously. You need your own server to do this dependably.

不过实话说。您需要自己的服务器来可靠地执行此操作。