public
Fork of mojombo/god
Description: Ruby process monitor
Homepage: http://god.rubyforge.org
Clone URL: git://github.com/Bertg/god.git
non-leaking logger
mojombo (author)
Tue Jan 15 18:36:21 -0800 2008
commit  f652f62c370aaf3c5eb75e449ef07f663e5e3390
tree    c566fe69aa5553f4ddcf5f83db32e6e0cfee5737
parent  890833241226b54b636f55f11c319c6de0bf18ac
...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
132
133
...
88
89
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
0
@@ -88,46 +88,48 @@ module God
0
           watch.mutex.synchronize do
0
             # ensure this condition is still active when we finally get the mutex
0
             if self.directory[condition] && phase == watch.phase
0
- # run the test
0
- result = condition.test
0
-
0
- # log
0
- messages = self.log(watch, metric, condition, result)
0
-
0
- # notify
0
- if condition.notify && self.trigger?(metric, result)
0
- self.notify(condition, messages.last)
0
- end
0
-
0
- # after-condition
0
- condition.after
0
-
0
- # get the destination
0
- dest =
0
- if result && condition.transition
0
- # condition override
0
- condition.transition
0
- else
0
- # regular
0
- metric.destination && metric.destination[result]
0
- end
0
-
0
- # transition or reschedule
0
- if dest
0
- # transition
0
- begin
0
- watch.move(dest)
0
- rescue EventRegistrationFailedError
0
- msg = watch.name + ' Event registration failed, moving back to previous state'
0
- applog(watch, :info, msg)
0
-
0
- dest = watch.state
0
- retry
0
- end
0
- else
0
- # reschedule
0
- Timer.get.schedule(condition)
0
- end
0
+ # # run the test
0
+ # result = condition.test
0
+ #
0
+ # # log
0
+ # messages = self.log(watch, metric, condition, result)
0
+ #
0
+ # # notify
0
+ # if condition.notify && self.trigger?(metric, result)
0
+ # self.notify(condition, messages.last)
0
+ # end
0
+ #
0
+ # # after-condition
0
+ # condition.after
0
+ #
0
+ # # get the destination
0
+ # dest =
0
+ # if result && condition.transition
0
+ # # condition override
0
+ # condition.transition
0
+ # else
0
+ # # regular
0
+ # metric.destination && metric.destination[result]
0
+ # end
0
+ #
0
+ # # transition or reschedule
0
+ # if dest
0
+ # # transition
0
+ # begin
0
+ # watch.move(dest)
0
+ # rescue EventRegistrationFailedError
0
+ # msg = watch.name + ' Event registration failed, moving back to previous state'
0
+ # applog(watch, :info, msg)
0
+ #
0
+ # dest = watch.state
0
+ # retry
0
+ # end
0
+ # else
0
+ # # reschedule
0
+ # Timer.get.schedule(condition)
0
+ # end
0
+ puts 'reschedule'
0
+ Timer.get.schedule(condition)
0
             end
0
           end
0
         rescue Exception => e
...
21
22
23
 
 
 
24
25
26
...
52
53
54
 
 
55
56
57
58
59
60
61
62
 
 
 
63
64
65
 
 
66
67
68
69
70
...
21
22
23
24
25
26
27
28
29
...
55
56
57
58
59
60
61
62
63
 
 
 
 
64
65
66
67
 
 
68
69
70
 
71
72
73
0
@@ -21,6 +21,9 @@ module God
0
       self.logs = {}
0
       @mutex = Mutex.new
0
       @capture = nil
0
+ @templogio = StringIO.new
0
+ @templog = ::Logger.new(@templogio)
0
+ @templog.level = Logger::INFO
0
       load_syslog
0
     end
0
     
0
@@ -52,19 +55,19 @@ module God
0
     #
0
     # Returns nothing
0
     def log(watch, level, text)
0
+ puts 'log'
0
+
0
       # initialize watch log if necessary
0
       self.logs[watch.name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT) if watch
0
       
0
       # push onto capture and timeline for the given watch
0
- buf = StringIO.new
0
- templog = ::Logger.new(buf)
0
- templog.level = Logger::INFO
0
- templog.send(level, text % [])
0
+ @templogio.truncate(0)
0
+ @templogio.rewind
0
+ @templog.send(level, text % [])
0
       @mutex.synchronize do
0
- @capture.puts(buf.string) if @capture
0
- self.logs[watch.name] << [Time.now, buf.string] if watch
0
+ @capture.puts(@templogio.string) if @capture
0
+ self.logs[watch.name] << [Time.now, @templogio.string] if watch
0
       end
0
- templog.close
0
       
0
       # send to regular logger
0
       self.send(level, text % [])
...
20
21
22
23
 
24
25
26
...
45
46
47
48
49
50
51
...
80
81
82
83
84
85
86
...
105
106
107
108
109
110
111
112
 
 
113
114
115
...
119
120
121
122
123
124
125
...
20
21
22
 
23
24
25
26
...
45
46
47
 
48
49
50
...
79
80
81
 
82
83
84
...
103
104
105
 
 
 
 
 
106
107
108
109
110
...
114
115
116
 
117
118
119
0
@@ -20,7 +20,7 @@ module God
0
   class Timer
0
     INTERVAL = 0.25
0
     
0
- attr_reader :events, :pending_events, :conditions, :timer
0
+ attr_reader :events, :pending_events, :timer
0
     
0
     @@timer = nil
0
     
0
@@ -45,7 +45,6 @@ module God
0
     def initialize
0
       @events = []
0
       @pending_events = []
0
- @conditions = []
0
       @pending_mutex = Mutex.new
0
       
0
       @timer = Thread.new do
0
@@ -80,7 +79,6 @@ module God
0
             
0
             # remove all triggered events
0
             triggered.each do |event|
0
- @conditions.delete(event.condition)
0
               @events.delete(event)
0
             end
0
           rescue Exception => e
0
@@ -105,11 +103,8 @@ module God
0
     # Returns nothing
0
     def schedule(condition, delay = condition.interval)
0
       applog(nil, :debug, "timer schedule #{condition} in #{delay} seconds")
0
- unless @conditions.include?(condition)
0
- @pending_mutex.synchronize do
0
- @pending_events << TimerEvent.new(condition, delay)
0
- end
0
- @conditions << condition
0
+ @pending_mutex.synchronize do
0
+ @pending_events << TimerEvent.new(condition, delay)
0
       end
0
     end
0
     
0
@@ -119,7 +114,6 @@ module God
0
     # Returns nothing
0
     def unschedule(condition)
0
       applog(nil, :debug, "timer unschedule #{condition}")
0
- @conditions.delete(condition)
0
     end
0
     
0
     # Trigger the event's condition to be evaluated
...
1
 
2
3
4
...
 
1
2
3
4
0
@@ -1,4 +1,4 @@
0
-('01'..'20').each do |i|
0
+('01'..'50').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])

Comments

    No one has commented yet.