RailsCasts Pro episodes are now free!

Learn more or hide this

Ritchie Young's Profile

GitHub User: ritchiey

Comments by Ritchie Young

Avatar

Sorry, the problem with the above solution is that both the parent and child processes will write out their pid to the worker's pid file. We just want the child so change it to:

ruby
after_fork do |server, worker|
  if Process.ppid > 1 # Not the daemon
    child_pid = server.config[:pid].sub('.pid', ".#{worker.nr}.pid")
    File.open(child_pid, "wb") {|f| f << Process.pid }
  end
end
Avatar

Add this to the bottom of your unicorn.rb

ruby
after_fork do |server, worker|
  child_pid = server.config[:pid].sub('.pid', ".#{worker.nr}.pid")
  system("echo #{Process.pid} > #{child_pid}")
end

so that each unicorn worker creates its own PID. See this post for more info.

Avatar

Very useful. Thanks, Ryan.

I did have to change a few things before it worked in Rails 3.0 though. I had to remove the html hash in the delete form.

delete.html.erb
<%= form_tag request.url, method: :delete do %>

And I needed to add a delete method to the application_controller:

application_controller.rb
  def delete
    render 'application/delete'
  end

After that it worked beautifully.

Avatar

Hi Ryan,

It looks like the caboo.se domain name has lapsed and has been taken over by a squatter.

Your pastie link to the original code will work if you update it to:

http://pastie.org/172566

Thanks so much for your screencasts. I refer back to them frequently.