public
Fork of mojombo/god
Description: Ruby process monitor
Homepage: http://god.rubyforge.org
Clone URL: git://github.com/Bertg/god.git
add --log-level to cli tool
mojombo (author)
Fri Jan 04 15:36:55 -0800 2008
commit  d4b77cc7127fe97923092dc0359b1c1ab3ab84b8
tree    d0d1aa0a188b497acf5a29a9db24c76c62c2b199
parent  560fd54a563bba6fbf0a4738b6fa033fd24f12a9
...
9
10
11
12
 
13
14
15
...
71
72
73
 
 
 
 
74
75
76
...
9
10
11
 
12
13
14
15
...
71
72
73
74
75
76
77
78
79
80
0
@@ -9,7 +9,7 @@ require 'optparse'
0
 require 'drb'
0
 
0
 begin
0
- options = {:daemonize => true, :port => 17165}
0
+ options = {:daemonize => true, :port => 17165, :log_level => "info"}
0
 
0
   opts = OptionParser.new do |opts|
0
     opts.banner = <<-EOF
0
@@ -71,6 +71,10 @@ begin
0
     opts.on("-V", "Print extended version and build information") do
0
       options[:info] = true
0
     end
0
+
0
+ opts.on("--log-level LEVEL", "Log level [debug|info|fatal]") do |x|
0
+ options[:log_level] = x.to_sym
0
+ end
0
   end
0
   
0
   opts.parse!
...
138
139
140
 
141
142
143
...
146
147
148
149
 
 
150
151
152
...
167
168
169
 
170
171
172
...
188
189
190
191
 
 
 
 
 
 
 
192
193
194
...
138
139
140
141
142
143
144
...
147
148
149
 
150
151
152
153
154
...
169
170
171
172
173
174
175
...
191
192
193
 
194
195
196
197
198
199
200
201
202
203
0
@@ -138,6 +138,7 @@ module God
0
   PID_FILE_DIRECTORY_DEFAULT = '/var/run/god'
0
   DRB_PORT_DEFAULT = 17165
0
   DRB_ALLOW_DEFAULT = ['127.0.0.1']
0
+ LOG_LEVEL_DEFAULT = :info
0
   
0
   class << self
0
     # user configurable
0
@@ -146,7 +147,8 @@ module God
0
                        :port,
0
                        :allow,
0
                        :log_buffer_size,
0
- :pid_file_directory
0
+ :pid_file_directory,
0
+ :log_level
0
     
0
     # internal
0
     attr_accessor :inited,
0
@@ -167,6 +169,7 @@ module God
0
   self.allow = nil
0
   self.log_buffer_size = nil
0
   self.pid_file_directory = nil
0
+ self.log_level = nil
0
   
0
   # Initialize internal data.
0
   #
0
@@ -188,7 +191,13 @@ module God
0
     self.pid_file_directory ||= PID_FILE_DIRECTORY_DEFAULT
0
     self.port ||= DRB_PORT_DEFAULT
0
     self.allow ||= DRB_ALLOW_DEFAULT
0
- LOG.level = Logger::INFO
0
+ self.log_level ||= LOG_LEVEL_DEFAULT
0
+
0
+ # log level
0
+ log_level_map = {:debug => Logger::DEBUG,
0
+ :info => Logger::INFO,
0
+ :fatal => Logger::FATAL}
0
+ LOG.level = log_level_map[self.log_level]
0
     
0
     # init has been executed
0
     self.inited = true
...
50
51
52
 
 
 
 
 
53
54
55
...
96
97
98
 
 
 
 
 
99
100
101
...
50
51
52
53
54
55
56
57
58
59
60
...
101
102
103
104
105
106
107
108
109
110
111
0
@@ -50,6 +50,11 @@ module God
0
               God.pid = @options[:pid]
0
             end
0
             
0
+ # set log level if requested
0
+ if @options[:log_level]
0
+ God.log_level = @options[:log_level]
0
+ end
0
+
0
             # load config
0
             if @options[:config]
0
               unless File.exist?(@options[:config])
0
@@ -96,6 +101,11 @@ module God
0
           God.port = @options[:port]
0
         end
0
         
0
+ # set log level if requested
0
+ if @options[:log_level]
0
+ God.log_level = @options[:log_level]
0
+ end
0
+
0
         if @options[:config]
0
           unless File.exist?(@options[:config])
0
             abort "File not found: #{@options[:config]}"
...
61
62
63
64
65
 
66
67
68
...
61
62
63
 
 
64
65
66
67
0
@@ -61,8 +61,7 @@ module God
0
       end
0
       
0
       def test
0
- pid = self.watch.pid
0
- process = System::Process.new(pid)
0
+ process = System::Process.new(self.pid)
0
         @timeline.push(process.percent_cpu)
0
         
0
         history = "[" + @timeline.map { |x| "#{x > self.above ? '*' : ''}#{x}%%" }.join(", ") + "]"
...
121
122
123
 
 
124
125
126
...
121
122
123
124
125
126
127
128
0
@@ -121,6 +121,8 @@ module God
0
         end
0
       rescue Errno::ECONNREFUSED
0
         self.code_is ? fail('Refused') : pass('Refused')
0
+ rescue EOFError
0
+ self.code_is ? fail('EOF') : pass('EOF')
0
       rescue Timeout::Error
0
         self.code_is ? fail('Timeout') : pass('Timeout')
0
       end
...
63
64
65
66
67
 
68
69
70
...
63
64
65
 
 
66
67
68
69
0
@@ -63,8 +63,7 @@ module God
0
       end
0
       
0
       def test
0
- pid = self.pid
0
- process = System::Process.new(pid)
0
+ process = System::Process.new(self.pid)
0
         @timeline.push(process.memory)
0
         
0
         history = "[" + @timeline.map { |x| "#{x > self.above ? '*' : ''}#{x}kb" }.join(", ") + "]"
...
125
126
127
 
 
 
 
 
128
129
130
...
125
126
127
128
129
130
131
132
133
134
135
0
@@ -125,6 +125,11 @@ module God
0
       @pid_file ||= default_pid_file
0
     end
0
     
0
+ # Fetch the PID from pid_file. If the pid_file does not
0
+ # exist, then use the PID from the last time it was read.
0
+ # If it has never been read, then return nil.
0
+ #
0
+ # Returns Integer(pid) or nil
0
     def pid
0
       contents = File.read(self.pid_file).strip rescue ''
0
       real_pid = contents =~ /^\d+$/ ? contents.to_i : nil
...
94
95
96
 
97
98
99
...
117
118
119
 
120
121
122
...
94
95
96
97
98
99
100
...
118
119
120
121
122
123
124
0
@@ -94,6 +94,7 @@ module God
0
     def schedule(condition, delay = condition.interval)
0
       @mutex.synchronize do
0
         unless @conditions.include?(condition)
0
+ applog(nil, :debug, "timer schedule #{condition} in #{delay}")
0
           @events << TimerEvent.new(condition, delay)
0
           @conditions << condition
0
           @events.sort! { |x, y| x.at <=> y.at }
0
@@ -117,6 +118,7 @@ module God
0
     #
0
     # Returns nothing
0
     def trigger(event)
0
+ applog(nil, :debug, "timer trigger #{event}")
0
       Hub.trigger(event.condition, event.phase)
0
     end
0
     
...
1
 
2
3
4
...
 
1
2
3
4
0
@@ -1,4 +1,4 @@
0
-('01'..'20').each do |i|
0
+('01'..'08').each do |i|
0
   God.watch do |w|
0
     w.name = "stress-#{i}"
0
     w.start = "ruby " + File.join(File.dirname(__FILE__), *%w[simple_server.rb])
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
20
21
22
 
23
24
 
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
...
73
74
75
 
 
 
 
 
 
 
 
 
76
77
78
...
 
 
 
 
1
2
3
4
 
 
 
 
 
 
 
 
 
 
 
5
6
7
 
8
9
 
10
11
12
 
 
 
 
 
13
14
15
16
17
18
 
 
 
 
 
 
19
20
21
...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
0
@@ -1,46 +1,21 @@
0
-if $0 == __FILE__
0
- require File.join(File.dirname(__FILE__), *%w[.. .. lib god])
0
-end
0
-
0
 ENV['GOD_TEST_RAILS_ROOT'] || abort("Set a rails root for testing in an environment variable called GOD_TEST_RAILS_ROOT")
0
 
0
 RAILS_ROOT = ENV['GOD_TEST_RAILS_ROOT']
0
 
0
-God.init do |g|
0
- # g.host =
0
- # g.port = 7777
0
- # g.pid_file_directory =
0
-end
0
-
0
-class SimpleNotifier
0
- def self.notify(str)
0
- puts "Notifying: #{str}"
0
- end
0
-end
0
+port = 5000
0
 
0
 God.watch do |w|
0
- w.name = "local-3000"
0
+ w.name = "local-#{port}"
0
   w.interval = 5.seconds
0
- w.start = "mongrel_rails start -P ./log/mongrel.pid -c #{RAILS_ROOT} -p 3001 -d"
0
+ w.start = "mongrel_rails start -P ./log/mongrel.pid -c #{RAILS_ROOT} -p #{port} -d"
0
   w.restart = "mongrel_rails restart -P ./log/mongrel.pid -c #{RAILS_ROOT}"
0
   w.stop = "mongrel_rails stop -P ./log/mongrel.pid -c #{RAILS_ROOT}"
0
- w.restart_grace = 5.seconds
0
- w.stop_grace = 5.seconds
0
- w.autostart = true
0
- w.uid = 'kev'
0
- w.gid = 'kev'
0
   w.group = 'mongrels'
0
   w.pid_file = File.join(RAILS_ROOT, "log/mongrel.pid")
0
   
0
   # clean pid files before start if necessary
0
   w.behavior(:clean_pid_file)
0
   
0
- w.behavior(:notify_when_flapping) do |b|
0
- b.failures = 5
0
- b.seconds = 60.seconds
0
- b.notifier = SimpleNotifier
0
- end
0
-
0
   # determine the state on startup
0
   w.transition(:init, { true => :up, false => :start }) do |on|
0
     on.condition(:process_running) do |c|
0
@@ -73,5 +48,14 @@ God.watch do |w|
0
       c.above = 10.percent
0
       c.times = [3, 5]
0
     end
0
+
0
+ on.condition(:http_response_code) do |c|
0
+ c.host = 'localhost'
0
+ c.port = port
0
+ c.path = '/'
0
+ c.code_is_not = 200
0
+ c.timeout = 10.seconds
0
+ c.times = [3, 5]
0
+ end
0
   end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.