在Rails环境中运行计划任务的最佳方法是什么?脚本/跑步吗?耙?我想每隔几分钟运行一次任务。


当前回答

你可以使用resque和resque- scheduling gem来创建cron,这很容易做到。

https://github.com/resque/resque

https://github.com/resque/resque-scheduler

其他回答

你可以使用resque和resque- scheduling gem来创建cron,这很容易做到。

https://github.com/resque/resque

https://github.com/resque/resque-scheduler

我用了发条宝石,它对我来说工作得很好。还有一个clockwork gem允许脚本作为守护进程运行。

我不太确定,我猜这取决于任务:多长时间运行一次,有多复杂,需要与rails项目进行多少直接交流等等。我想如果做一件事只有“一种最好的方法”,就不会有那么多不同的方法了。

在我在Rails项目中的上一份工作中,我们需要制作一个批量邀请邮件(调查邀请,而不是垃圾邮件),它应该在服务器有时间的时候发送计划好的邮件。我认为我们将使用守护进程工具来运行我创建的rake任务。

不幸的是,我们公司遇到了一些资金问题,被主要竞争对手“收购”了,所以这个项目一直没有完成,所以我不知道我们最终会用什么。

有意思的是没人提到Sidetiq。 如果你已经在使用Sidekiq,这是一个很好的补充。

Sidetiq提供了一个简单的API,用于为 Sidekiq。

Job看起来是这样的:

class MyWorker
  include Sidekiq::Worker
  include Sidetiq::Schedulable

  recurrence { hourly.minute_of_hour(15, 45) }

  def perform
    # do stuff ...
  end
end

我使用脚本运行cron,这是运行cron的最好方式。 这里有一些cron的例子,

打开CronTab - > sudo CronTab -e

并粘贴波纹线:

00 00 * * * wget https://your_host/some_API_end_point

这里有一些cron格式,对你有帮助

::CRON FORMAT::

Examples Of crontab Entries
15 6 2 1 * /home/melissa/backup.sh
Run the shell script /home/melissa/backup.sh on January 2 at 6:15 A.M.

15 06 02 Jan * /home/melissa/backup.sh
Same as the above entry. Zeroes can be added at the beginning of a number for legibility, without changing their value.

0 9-18 * * * /home/carl/hourly-archive.sh
Run /home/carl/hourly-archive.sh every hour, on the hour, from 9 A.M. through 6 P.M., every day.

0 9,18 * * Mon /home/wendy/script.sh
Run /home/wendy/script.sh every Monday, at 9 A.M. and 6 P.M.

30 22 * * Mon,Tue,Wed,Thu,Fri /usr/local/bin/backup
Run /usr/local/bin/backup at 10:30 P.M., every weekday. 

希望这对你有帮助:)