Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have one Unicorn child process a queue, whilst the rest process web requests on Heroku single Dyno?

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
like image 453
nverba Avatar asked Nov 14 '22 11:11

nverba


1 Answers

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).

like image 77
John Beynon Avatar answered Dec 20 '22 19:12

John Beynon