From 8906e12ccb19310d9bc7936e1f33ae7a475f11be Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Sun, 11 Nov 2007 22:06:24 -0800 Subject: [PATCH] add god check to binary --- Manifest.txt | 4 +++- bin/god | 1 + bin/god-check | 48 ------------------------------------------------ lib/god/cli/command.rb | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 52 insertions(+), 50 deletions(-) delete mode 100755 bin/god-check diff --git a/Manifest.txt b/Manifest.txt index 8beb18c..ec4a9ad 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -3,7 +3,6 @@ Manifest.txt README.txt Rakefile bin/god -bin/god-check examples/events.god examples/gravatar.god examples/single.god @@ -15,6 +14,9 @@ lib/god.rb lib/god/behavior.rb lib/god/behaviors/clean_pid_file.rb lib/god/behaviors/notify_when_flapping.rb +lib/god/cli/command.rb +lib/god/cli/run.rb +lib/god/cli/version.rb lib/god/condition.rb lib/god/conditions/always.rb lib/god/conditions/complex.rb diff --git a/bin/god b/bin/god index 72226da..d9b3246 100755 --- a/bin/god +++ b/bin/god @@ -34,6 +34,7 @@ begin status show status of each task quit stop god terminate stop god and all tasks + check run self diagnostic Options: EOF diff --git a/bin/god-check b/bin/god-check deleted file mode 100755 index e25062a..0000000 --- a/bin/god-check +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env ruby - -$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) - -require 'rubygems' -require 'god' - -Thread.new do - begin - event_system = God::EventHandler.event_system - puts "using event system: #{event_system}" - - if God::EventHandler.loaded? - puts "starting event handler" - God::EventHandler.start - else - puts "[fail] event system did not load" - exit(1) - end - - puts 'forking off new process' - - pid = fork do - loop { sleep(1) } - end - - puts "forked process with pid = #{pid}" - - God::EventHandler.register(pid, :proc_exit) do - puts "[ok] process exit event received" - exit(0) - end - - sleep(1) - - puts "killing process" - - Process.kill('KILL', pid) - rescue => e - puts e.message - puts e.backtrace.join("\n") - end -end - -sleep(2) - -puts "[fail] never received process exit event" -exit(1) \ No newline at end of file diff --git a/lib/god/cli/command.rb b/lib/god/cli/command.rb index 55a296c..202b23f 100644 --- a/lib/god/cli/command.rb +++ b/lib/god/cli/command.rb @@ -7,7 +7,6 @@ module God @options = options @args = args - setup dispatch end @@ -27,9 +26,13 @@ module God def dispatch if %w{load status log quit terminate}.include?(@command) + setup send("#{@command}_command") elsif %w{start stop restart monitor unmonitor}.include?(@command) + setup lifecycle_command + elsif @command == 'check' + check_command else puts "Command '#{@command}' is not valid. Run 'god --help' for usage" abort @@ -114,6 +117,50 @@ module God end end + def check_command + Thread.new do + begin + event_system = God::EventHandler.event_system + puts "using event system: #{event_system}" + + if God::EventHandler.loaded? + puts "starting event handler" + God::EventHandler.start + else + puts "[fail] event system did not load" + exit(1) + end + + puts 'forking off new process' + + pid = fork do + loop { sleep(1) } + end + + puts "forked process with pid = #{pid}" + + God::EventHandler.register(pid, :proc_exit) do + puts "[ok] process exit event received" + exit(0) + end + + sleep(1) + + puts "killing process" + + ::Process.kill('KILL', pid) + rescue => e + puts e.message + puts e.backtrace.join("\n") + end + end + + sleep(2) + + puts "[fail] never received process exit event" + exit(1) + end + def lifecycle_command # get the name of the watch/group name = @args[1] -- 2.11.4.GIT