1

I have a class that I am monitoring using god. This is it's structure:

lib/my_class.rb

#!/usr/bin/env ruby

class MyClass
  def start(config)
    loop do
      EventMachine::run do             
    end
  end
end

begin   
  config = {"foo" => "bar"}  
  my_class = MyClass.new
  my_class.start(config)  
rescue Exception => ex
  puts "Exception in MyClass: #{ex.message} at #{ex.backtrace.join("\n")}"
end

And this is how I'm running it with god:

config/my_config.god

rails_root = File.dirname(File.dirname(__FILE__))
God.pid_file_directory =  File.join(rails_root, 'tmp/pids/')

# myclass 
God.watch do |w| 
  w.name = "myclass" 
  w.interval = 30.seconds 
  w.start = "#{rails_root}/script/rails runner #{rails_root}/lib/my_class.rb" 
  # w.stop = "do nothing?" 
  # w.restart = "#{w.stop} && #{w.start}"
  w.start_grace = 10.seconds 
  w.restart_grace = 10.seconds 
  w.log = "#{rails_root}/log/my_class.log"

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

end

Since I didn't specify a stop (I don't know how), god will send a SIGTERM and then a SIGKILL to stop the process if that fails. Is this how I'm supposed to handle non-daemonized processes?

David
  • 7,310
  • 6
  • 41
  • 63
  • *"...terminate non-daemonized processes with god?"* - This is a programming forum, we tend to avoid religious discussion here =) – Ed S. Jul 04 '11 at 06:00
  • haha, I thought about titling it: "What is the proper way to have god daemonize and slay processes?" ^_^ – David Jul 04 '11 at 06:19

1 Answers1

0

Somewhat unrelated but you are not supposed to EM::run() in a loop... EM is started just once, and then it just reacts to whatever inputs it listens too...

On the same topic, it is usually a good idea to protect your EM process with

Signal.trap("INT") do 
  EM::stop()
end

That will ensure you are cleanly finishing all operations before shutting down.

Gregory Mostizky
  • 7,231
  • 1
  • 26
  • 29