From 8a7e5236a07eacae99f996641720f6554864001d Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Fri, 10 Aug 2007 23:02:52 -0700 Subject: [PATCH] fix god binary daemonization, create some sample setups --- bin/god | 27 ++++++++++++++++++++++----- lib/god/hub.rb | 4 +--- test/configs/events/events.god | 29 +++++++++++++++++++++++++++++ test/configs/events/simple_server.rb | 3 +++ test/configs/{pid.rb => pid/pid.god} | 2 +- test/configs/pid/simple_server.rb | 3 +++ 6 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 test/configs/events/events.god create mode 100755 test/configs/events/simple_server.rb rename test/configs/{pid.rb => pid/pid.god} (73%) create mode 100755 test/configs/pid/simple_server.rb diff --git a/bin/god b/bin/god index 9488da6..2603a48 100755 --- a/bin/god +++ b/bin/god @@ -7,7 +7,6 @@ require 'optparse' require 'drb' require 'daemons' -require 'god' options = {:daemonize => true, :port => 17165} @@ -32,6 +31,14 @@ EOF options[:port] = x end + opts.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x| + options[:pid] = x + end + + opts.on("-lFILE", "--log FILE", "Where to write the log file") do |x| + options[:log] = x + end + opts.on("-D", "--no-daemonize", "Don't daemonize") do options[:daemonize] = false end @@ -42,10 +49,14 @@ EOF end.parse! if options[:version] + require 'god' + # print version puts "Version #{God::VERSION}" exit! elsif command = ARGV[0] + require 'god' + # a command was specified # disable at_exit @@ -77,13 +88,19 @@ elsif command = ARGV[0] else # start god if !options[:daemonize] + require 'god' load File.expand_path(options[:config]) else pid = fork do begin + require 'god' + # Dir.chdir "/" + + log_file = options[:log] || "/dev/null" + STDIN.reopen "/dev/null" - STDOUT.reopen('god.log', "a") + STDOUT.reopen(log_file, "a") STDERR.reopen STDOUT puts "Starting god" @@ -112,11 +129,11 @@ else end end - File.open("god.pid", 'w') { |f| f.write pid } + pid_file = options[:pid] || "god.pid" + File.open(pid_file, 'w') { |f| f.write pid } ::Process.detach pid - # disable at_exit - module God; def self.at_exit; end; end + exit! end end diff --git a/lib/god/hub.rb b/lib/god/hub.rb index c9010a6..b1c74f2 100644 --- a/lib/god/hub.rb +++ b/lib/god/hub.rb @@ -77,12 +77,10 @@ module God watch = metric.watch watch.mutex.synchronize do - msg = watch.name + ' ' + condition.class.name + " [true]" + msg = watch.name + ' ' + condition.class.name + " [true] " + metric.destination.inspect Syslog.debug(msg) puts msg - p metric.destination - dest = metric.destination[true] watch.move(dest) end diff --git a/test/configs/events/events.god b/test/configs/events/events.god new file mode 100644 index 0000000..278f2d0 --- /dev/null +++ b/test/configs/events/events.god @@ -0,0 +1,29 @@ +God.watch do |w| + w.name = "local-3000" + w.interval = 5 # seconds + w.start = File.join(File.dirname(__FILE__), *%w[simple_server.rb]) + w.stop = "" + + # determine the state on startup + w.transition(:init, { true => :up, false => :start }) do |on| + on.condition(:process_running) do |c| + c.running = true + end + end + + # determine when process has finished starting + w.transition(:start, :up) do |on| + on.condition(:process_running) do |c| + c.running = true + end + end + + # start if process is not running + w.transition(:up, :start) do |on| + on.condition(:process_exits) + + on.condition(:process_running) do |c| + c.running = false + end + end +end \ No newline at end of file diff --git a/test/configs/events/simple_server.rb b/test/configs/events/simple_server.rb new file mode 100755 index 0000000..91b7a25 --- /dev/null +++ b/test/configs/events/simple_server.rb @@ -0,0 +1,3 @@ +#! /usr/bin/env ruby + +loop { puts 'server'; sleep 1 } \ No newline at end of file diff --git a/test/configs/pid.rb b/test/configs/pid/pid.god similarity index 73% rename from test/configs/pid.rb rename to test/configs/pid/pid.god index 885c0e8..7b93c3d 100644 --- a/test/configs/pid.rb +++ b/test/configs/pid/pid.god @@ -1,6 +1,6 @@ God.watch do |w| w.name = 'simple_server' - w.start = '/Users/tom/Desktop/simple_server.rb' + w.start = File.join(File.dirname(__FILE__), *%w[simple_server.rb]) w.stop = '' w.interval = 5 w.grace = 2 diff --git a/test/configs/pid/simple_server.rb b/test/configs/pid/simple_server.rb new file mode 100755 index 0000000..91b7a25 --- /dev/null +++ b/test/configs/pid/simple_server.rb @@ -0,0 +1,3 @@ +#! /usr/bin/env ruby + +loop { puts 'server'; sleep 1 } \ No newline at end of file -- 2.11.4.GIT