Heroku、cron、delayed_job 和工人 (Rails 3)

发布于 2024-10-10 13:47:47 字数 663 浏览 3 评论 0 原文

我有两个问题:

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

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

我使用 collectiveideadelayed_job

I have two questions:

  1. How can I add a heroku worker just before running a delayed job and remove it after it finishes?
  2. Is my cron.rake ok?

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

I use collectiveidea delayed_job.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风渺 2024-10-17 13:47:47

我在 HireFire 方面取得了巨大成功。

轻松设置:

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

I've had good success with HireFire.

Easy setup:

  • Add gem 'hirefire' to your Gemfile
  • Create Rails.root/config/initializers/hirefire.rb with the config information.
吻安 2024-10-17 13:47:47

要添加删除/删除工作人员,请在 :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

To add remove/remove workers, hook into your ORM's after :create / after :destroy

With DataMapper on Heroku, I did it like this (You must set the ENV vars yourself)

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
薯片软お妹 2024-10-17 13:47:47
  1. 您也许可以使用“自动缩放”插件,例如 worklessheroku-autoscale.
  2. 关于 cron 我没有看到任何问题......
  1. You maybe can use an "autoscale" plugin like workless or heroku-autoscale.
  2. About the cron I don't see any problem on it...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文