CodeOverload
CodeOverload

Reputation: 48505

Correct way to monitor ruby background processes (like Resque) in production

I've been struggling with this for a while, What is the correct approach to start background processes like resque and resque scheduler? Is using God an overkill?

Currently I'm trying to get God to work, but I'm not sure if the *.god conf files should be in the app's directory or somewhere else.

This is what I use:

config
 |- app.god
 |- God
   |- resque.god
   |- resque_scheduler.god

# config/god/resque.god
rails_env   = ENV['RAILS_ENV']  || raise(ArgumentError, "RAILS_ENV not defined")
rails_root  = ENV['RAILS_ROOT'] || File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
num_workers = rails_env == 'production' ? 5 : 2

num_workers.times do |num|
  God.watch do |w|
    w.dir      = "#{rails_root}"
    w.name     = "resque-#{num}"
    w.group    = 'resque'
    w.interval = 30.seconds
    w.env      = {"QUEUE"=>"*", "RAILS_ENV"=>rails_env, "BUNDLE_GEMFILE"=>"#{rails_root}/Gemfile"}
    w.start    = "/usr/bin/rake -f #{rails_root}/Rakefile environment resque:work"
    w.log      = "#{rails_root}/log/resque-scheduler.log"

    ... start/stop methods  ...
  end
end

There's a root user and a MyApp user. MyApp user has an app located in: /home/myapp/apps/myapp_production/current

The God capistrano recipe I use is:

# config/deploy.rb
after "deploy:restart", "god:restart"

namespace :god do
  def try_killing_resque_workers
    run "pkill -3 -f resque"
  rescue
    nil
  end

  desc "Restart God gracefully"
  task "restart", :roles => :app do
    god_config_path = File.join(release_path, 'config', 'app.god')
    begin
      # Throws an exception if god is not running.
      run "cd #{release_path}; bundle exec god status && RAILS_ENV=#{rails_env} RAILS_ROOT=#{release_path} bundle exec god load #{god_config_path} && bundle exec god start resque"

      # Kill resque processes and have god restart them with the newly loaded config.
      try_killing_resque_workers
    rescue => ex
      # god is dead, workers should be as well, but who knows.
      try_killing_resque_workers

      # Start god.
      run "cd #{release_path}; RAILS_ENV=#{rails_env} bundle exec god -c #{god_config_path}"
    end
  end
end

When I deploy, I get "The server is not available (or you do not have permissions to access it)"

What's weird is when I even login as a root and run god status it returns nothing but if i run god --version it returns the version.

Anyone knows why?

Upvotes: 0

Views: 589

Answers (1)

mestachs
mestachs

Reputation: 1889

Did you setup an init.d script ? Do you have god running ?

/etc/init.d/god status

You can try to launch it by hand

/usr/bin/god -c /etc/god/conf.god -D

and check the logs

Upvotes: 1

Related Questions