是否可以让一个 Unicorn 子进程处理队列,而其余进程则在 Heroku 单 Dyno 上处理 Web 请求?

发布于 2024-12-27 14:57:30 字数 615 浏览 2 评论 0原文

如果您在 Heroku 上的单个测功机上设置了 Unicorn,例如 3 个工作人员。 是否可以让 2 个子进程处理 Web 请求,并让 1 个 Unicorn 子进程执行后台作业,例如 resque 队列或计划任务?

或者这只是不合适?


现在可以工作了!

好的,所以使用下面的答案我设法让它接受提示,但首先需要进行一些修改。这对我有用。

Procfile

web: bundle exec unicorn_rails -p $PORT -c config/unicorn.rb

unicorn.rb

worker_processes 2
preload_app true
timeout 30

@resque_pid = nil

before_fork do |server, worker|
  @resque_pid ||= spawn("bundle exec rake environment resque:work QUEUE=*")
end

after_fork do |server, worker|
  ActiveRecord::Base.establish_connection
end

If you have Unicorn set up on a single dyno on Heroku, say with 3 workers.
Is it possible to have 2 of the child workers processing web requests, and 1 Unicorn child doing background jobs, such as a resque queue, or scheduled tasks?

Or is that just not appropriate?


Now got it Working!

OK, so using the answer bellow I managed to get it to pick up the cue, but it took a bit of tinkering first. This is what worked for me.

Procfile

web: bundle exec unicorn_rails -p $PORT -c config/unicorn.rb

unicorn.rb

worker_processes 2
preload_app true
timeout 30

@resque_pid = nil

before_fork do |server, worker|
  @resque_pid ||= spawn("bundle exec rake environment resque:work QUEUE=*")
end

after_fork do |server, worker|
  ActiveRecord::Base.establish_connection
end

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

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

发布评论

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

评论(1

乜一 2025-01-03 14:57:30

这当然是可能的 - 请阅读 http://bugsplat。信息/2011-11-27-concurrency-on-heroku-cedar.html。虽然我自己还没有尝试过,但我很快就会尝试。本质上,你最终会得到一个 unicorn.rb,看起来

worker_processes 3
timeout 30

@resque_pid = nil

before_fork do |server, worker|
  @resque_pid ||= spawn("bundle exec rake " + \
  "resque:work QUEUES=scrape,geocode,distance,mailer")
end

我并不完全确定“适当性”,因为这意味着 Heroku 本质上正在损失收入,但他们没有采取任何措施来阻止这种行为(也没有我想他们会的)。

It certainly is possible - take a read of http://bugsplat.info/2011-11-27-concurrency-on-heroku-cedar.html. I've not tried it myself though but I will be soon. Essentially, you'll end up with a unicorn.rb that looks like

worker_processes 3
timeout 30

@resque_pid = nil

before_fork do |server, worker|
  @resque_pid ||= spawn("bundle exec rake " + \
  "resque:work QUEUES=scrape,geocode,distance,mailer")
end

I'm not entirely sure of the 'appropriateness' since it means Heroku is essentially loosing out on revenue but they haven't taken any stops to block this behaviour (nor I think would they).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文