How do you schedule Resque jobs on Heroku?

 ̄綄美尐妖づ 提交于 2019-12-11 16:34:09

问题


The rescue-scheduler gem installation says to create a separate Rake task for scheduling:

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

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

$ rake resque:scheduler

However my Heroku Procfile already has a worker process to run Resque:

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

I don't want to create yet another worker process just to run scheduled jobs. This old blog posts says you can set it up without using Rake tasks.

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

How do you schedule Resque jobs on Heroku without use 2 worker dynos?

I tried to add multiple dependencies but it gave me a circular dependency error.

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:


This works 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

And to launch it

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


来源:https://stackoverflow.com/questions/51757169/how-do-you-schedule-resque-jobs-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!