使用 Resque Scheduler 设置 rake 任务 - Rails 4

Setting up a rake task with Resque Scheduler - Rails 4

我在 Rails 4 使用 Resque Scheduler gem。

我也在使用 sitemap generator gem 来动态生成我的站点地图。

我无法找出使用 resque 调度程序安排 rake 任务的最佳方法。站点地图生成器推荐 whenever,但我假设 resque scheduler 可以完成同样的事情(如果不需要,不想安装另一个 gem)。

有人知道如何设置吗?

我想每 5 小时 运行 rake sitemap:refresh:no_ping

我想我会安排一个后台工作,然后 运行 从那里开始:

# resque_schedule.yml

update_sitemap:
  every: 5h
  class: "SitemapUpdater"
  description: "This job refreshes the sitemap"



# sitemap_updater.rb

class SitemapUpdater

  @queue = :sitemap_queue

  def self.perform
    # run rake task here
  end

end

...但是,我不确定这是否是一个好习惯。任何建议将不胜感激。

我没有发现你的方法有问题,你必须知道调度程序在每次部署期间都会重置,所以如果你频繁部署,你的计划作业可能 运行 更晚甚至根本不是 运行,as documented:

IMPORTANT: Rufus every syntax will calculate jobs scheduling time starting from the moment of deploy, resulting in resetting schedule time on every deploy, so it's probably a good idea to use it only for frequent jobs (like every 10-30 minutes), otherwise - when you use something like every 20h and deploy once-twice per day - it will schedule the job for 20 hours from deploy, resulting in a job to never be run.

您还可以 运行 来自系统 cron 本身的 rake ,这是一个更轻量级的解决方案,因为它根本不需要调度程序 gem,只需要 rake 任务, 并将及时可靠地安排。

参见例如this answer for setting up the "every 5 hours" frequency in crontab and you might also need to study RVM wrappers 如果您为 ruby 项目使用 RVM(在这种情况下,您必须使用 RVM 包装器调用 rake,例如调用 /home/deploy/.rvm/wrappers/ruby-2.3.0@mygemset/rake 而不仅仅是 rake)。