god doesn't stop unicorn
Asked Answered
V

1

8

I have this file

rails_env = ENV['RAILS_ENV'] || 'development'
rails_root = ENV['RAILS_ROOT'] || "/home/luiz/rails_dev/api"

God.watch do |w|
  w.name = "unicorn"
  w.interval = 30.seconds # default

  # unicorn needs to be run from the rails root
  w.start = "cd #{rails_root} && unicorn_rails -c config/unicorn.rb -E #{rails_env}"

  # QUIT gracefully shuts down workers
  w.stop = "kill -QUIT `cat #{rails_root}/tmp/pids/unicorn.pid`"

  # USR2 causes the master to re-create itself and spawn a new worker pool
  w.restart = "kill -USR2 `cat #{rails_root}/tmp/pids/unicorn.pid`"

  w.start_grace = 10.seconds
  w.restart_grace = 10.seconds
  w.pid_file = "#{rails_root}/tmp/pids/unicorn.pid"

  w.behavior(:clean_pid_file)

  w.start_if do |start|
    start.condition(:process_running) do |c|
      c.interval = 5.seconds
      c.running = false
    end
  end

  w.restart_if do |restart|
    restart.condition(:memory_usage) do |c|
      c.above = 300.megabytes
      c.times = [3, 5] # 3 out of 5 intervals
    end

    restart.condition(:cpu_usage) do |c|
      c.above = 50.percent
      c.times = 5
    end
  end

  # lifecycle
  w.lifecycle do |on|
    on.condition(:flapping) do |c|
      c.to_state = [:start, :restart]
      c.times = 5
      c.within = 5.minute
      c.transition = :unmonitored
      c.retry_in = 10.minutes
      c.retry_times = 5
      c.retry_within = 2.hours
    end
  end
end

I start unicorn with god -c unicorn.god -D -p 8081 and my workers are setup fine. but, sometimes I need do stop unicorn (god stop unicorn -p 8081 in another console) but the server keep up and running.
what am I missing?

Edit

We're moving from unicorn to puma (not because this question, it's a performance thing), and not going to use god anymore...thanks everybody for your help

Vidovic answered 25/5, 2012 at 17:7 Comment(7)
is the pidfile being created?Bigamy
What is the output from original god? From the one you used to start your god script. Also, could there be any conflict with port numbers (already running some other service on 8081)?Jihad
Can you add your config/unicorn.rbPostdiluvian
@LuizE. any chance you could elaborate on the performance considerations that went into switching to puma? Are you running MRI Ruby? Thanks!Cetane
I'm sorry...I don't blog or something...all I can say is we've been using unicorn, and using puma we saw a drastically decrease on memory usage, we could handle more requisitions. also, puma is easier to configureVidovic
I had the same question, I found that using "god terminate" kills unicorn, where as "god stop" and "god quit" do not.Delirium
@Delirium what a great comment. What a funny language. Why do i not use RubeBolanger
V
1

Now we are using foreman and puma right now...this is our initializer:
web: bundle exec puma -q -d -e production -b 'unix:///home/api/shared/web.socket' -S /home/api/shared/web.state --control 'unix:///home/api/shared/web.ctl'

and deploying with capistrano, so that we can stop and restart the server like this

config/deploy.rb

# Puma commands
_cset(:puma_cmd) { "#{fetch(:bundle_cmd, 'bundle')} exec puma" }
_cset(:pumactl_cmd) { "#{fetch(:bundle_cmd, 'bundle')} exec pumactl" }
_cset(:puma_state) { "#{shared_path}/puma.state" }
_cset(:puma_role) { :app }

# Puma
namespace :puma do

  desc 'Start puma'
  task :start do
    run "cd #{current_path} ; bundle exec foreman start web"
  end

  desc 'Stop puma'
  task :stop, :roles => lambda { fetch(:puma_role) }, :on_no_matching_servers => :continue do
    run "cd #{current_path} && #{fetch(:pumactl_cmd)} -S #{fetch(:puma_state)} stop"
  end

  desc 'Restart puma'
  task :restart, :roles => lambda { fetch(:puma_role) }, :on_no_matching_servers => :continue do
    run "cd #{current_path} && #{fetch(:pumactl_cmd)} -S #{fetch(:puma_state)} restart"
  end
end
Vidovic answered 14/8, 2013 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.