Heroku,cron,delayed_job和worker(Rails 3)

时间:2011-01-05 09:36:32

标签: ruby-on-rails heroku delayed-job

我有两个问题:

  1. 如何在运行延迟作业之前添加一个heroku worker并在完成后将其删除?
  2. 我的cron.rake好吗?
  3. cron.rake:

    desc "This task is called by the Heroku cron add-on"
    task :cron => :environment do
    
      puts "requesting homepage to refresh cache"
      uri = URI.parse('http://something.com')
      Net::HTTP.get(uri)
      puts "end requesting homepage"
    
      puts "start sending daily mail"
      User.notified_today.each do |user|
        Delayed::Job.enqueue UserMailer.daily_mail(user).deliver
      end
      puts "end sending daily mail"
    
    end
    

    我使用collectiveidea delayed_job

3 个答案:

答案 0 :(得分:4)

我在HireFire取得了很大的成功。

简易设置:

  • gem 'hirefire'添加到您的Gemfile
  • 使用配置信息创建Rails.root/config/initializers/hirefire.rb

答案 1 :(得分:2)

要添加删除/删除工作程序,请在:create / after:destroy

之后挂钩您的ORM

使用Heroku上的DataMapper,我这样做了(你必须自己设置ENV变量)

MAX_CONCURRENT_WORKERS = 5
if ENV["HEROKU_APP"]  
  Delayed::Job.after :create do
    workers_needed = [Delayed::Job.count, MAX_CONCURRENT_WORKERS].min
    client = Heroku::Client.new(ENV['HEROKU_USERNAME'], ENV['HEROKU_PASSWORD'])
    client.set_workers(ENV['HEROKU_APP'], workers_needed)        
    puts "- Initialized Heroku workers for ZipDecoder"           
  end

  Delayed::Job.after :destroy do
    workers_needed = [Delayed::Job.count, MAX_CONCURRENT_WORKERS].min
    client = Heroku::Client.new(ENV['HEROKU_USERNAME'], ENV['HEROKU_PASSWORD'])
    client.set_workers(ENV['HEROKU_APP'], workers_needed)        
    puts "- Cleaned Up a Delayed Job for ZipDecoder ---------------------------------"
  end
end

答案 2 :(得分:1)

  1. 您可以使用worklessheroku-autoscale等“自动缩放”插件。
  2. 关于cron我没有看到任何问题......