7

我正在使用ResqueRedis来处理后台作业。我想每小时调用一次方法 Resque.enqueue(MyModel) 来执行后台任务。

例如:假设我有一个拥有 10,000 个用户的网站。我想每天只调用这个方法 24 次;不是 10,000 * 24。示例方法如下:

Resque.enqueue(MyModel)

提前谢谢你的帮助。我还应该提到我更喜欢坚持使用 Resque,而不是转向延迟工作。谢谢你。

4

3 回答 3

7

您应该为此类任务使用 cron 作业。

我建议您使用Whenever gem。

在此处查看 railscast:http ://railscasts.com/episodes/164-cron-in-ruby

于 2011-05-22T23:05:20.917 回答
3

Since you are already using Resque, I would recommend to use one of the many available Resque plugins: resque-scheduler. Plugs into the Resque UI very nicely as well. Simple setup as explained in the README. Also adds a lot of the missing DelayedJob stuff (delayed execution).

Why I switched from whenever to resque-scheduler:

  • Stays in the app-folder and doesn't mess with your cron file.
  • To stop all 'crons' just disable the Resque workers.
  • Exceptions are logged to the Resque UI.
  • Manual rescheduling via the Resque UI possible.
  • In combination with resque-loner you prevent double-execution should a job take longer than the span between two executions.
  • Still obeys to the priority-system of Resque.
  • No additional boot-time (could take up to 60s if your app gets bigger) since it uses Resque's worker-pool.
  • One less "tech" used.
  • Simple switch: Configuration is done in a cron-like manner.
于 2011-05-22T23:12:34.110 回答
0

你可以很容易地使用无论何时,它都能满足你的要求,而且还有更多,它有一个很好的文档。

您可以使用如下语法:

every 3.hours do
    runner "MyModel.some_process"
    rake "my:rake:task"
    command "/usr/bin/my_great_command"
end

使用以下方式安装它:

$ gem install whenever

或将其添加到您的 GemFile:

gem 'whenever', :require => false

然后运行:

$ cd /apps/my-great-project
$ wheneverize .

希望这对你有帮助!

于 2015-08-24T09:13:41.270 回答