您如何在Heroku上安排Resque工作?

时间:2018-08-09 00:01:08

标签: ruby-on-rails resque resque-scheduler

rescue-scheduler gem安装要求创建一个单独的Rake任务以进行调度:

https://github.com/resque/resque-scheduler#rake-integration

namespace :resque do
  task :setup_schedule => :setup do
    ...
  task :scheduler => :setup_schedule

$ rake resque:scheduler

但是我的Heroku Procfile已经有一个运行Resque的工作进程:

resque: env TERM_CHILD=1 RESQUE_TERM_TIMEOUT=7 QUEUE=* bundle exec rake resque:work COUNT=1

我不想创建另一个工作进程只是为了运行计划的作业。这篇旧的博客文章说,您可以在不使用Rake任务的情况下进行设置。

https://www.perfectline.co/blog/2011/07/cron-tasks-for-your-rails-application-with-resque-2/

您如何在不使用2个工人dynos的情况下在Heroku上安排Resque作业?

我尝试添加多个依赖项,但这给了我一个循环依赖项错误。

namespace :resque do
  task :setup => [:environment, :scheduler]
Console
>env TERM_CHILD=1 RESQUE_TERM_TIMEOUT=7 QUEUE=* bundle exec rake resque:work COUNT=1
rake aborted!
Circular dependency detected: TOP => resque:work => resque:preload => resque:setup => resque:scheduler => resque:setup_schedule => resque:setup

Tasks: TOP => resque:work => resque:preload => resque:setup => resque:scheduler => resque:setup_schedule

1 个答案:

答案 0 :(得分:0)

这有效 https://grosser.it/2012/04/14/resque-scheduler-on-heroku-without-extra-workers/

require 'resque/tasks'
require 'resque/scheduler/tasks'

namespace :resque do

  desc "schedule and work, so we only need 1 dyno"
  task :schedule_and_work do
    if Process.respond_to? :fork
      if Process.fork
        sh "rake environment resque:work"
      else
        sh "rake resque:scheduler"
        Process.wait
      end
    else # windows
      pid = Process.spawn "rake environment resque:work"
      Rake::Task["resque:scheduler"].invoke
      Process.wait pid
    end
  end

  task :setup => :environment

  task :setup_schedule => :setup do
    require 'resque-scheduler'

    # The schedule doesn't need to be stored in a YAML, it just needs to
    # be a hash.  YAML is usually the easiest.
    # Resque.schedule = YAML.load_file('your_resque_schedule.yml')
    Resque.schedule = {merit_rules: {every: '1m', class: 'Merit::ReputationChangeObserver', queue: :badge_queue,
      description: 'This job runs Merit\'s rules to award badges.'}}

  end

  task :scheduler => :setup_schedule

end

并启动它

env TERM_CHILD=1 RESQUE_TERM_TIMEOUT=7 QUEUE=* bundle exec rake resque:schedule_and_work COUNT=1