public
Fork of mojombo/god
Description: Ruby process monitor
Homepage: http://god.rubyforge.org
Clone URL: git://github.com/Bertg/god.git
fix array push mem leak. damn you ruby
mojombo (author)
Fri Jan 18 17:14:30 -0800 2008
commit  2b8efe7a0b34b8305e287781802e32989a15dc1a
tree    10d11807c0569e6ce0b8fef85edded8d4eee702b
parent  4892e52bf354fd0d074918adbe4c3bf7f253f2e0
...
14
15
16
17
 
18
19
20
21
22
23
24
25
26
27
28
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31
32
...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 
 
 
 
 
 
 
 
 
 
 
 
91
92
93
...
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
 
167
168
169
170
171
 
 
 
172
173
 
174
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
177
178
...
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
...
89
90
91
 
 
 
 
 
 
 
 
 
 
 
 
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
...
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
0
@@ -14,19 +14,32 @@ module God
0
     
0
     self.directory = {}
0
     self.queue = Queue.new
0
- self.mutex = Mutex.new
0
+ self.mutex = Monitor.new
0
     
0
     def self.start
0
       5.times do
0
         Thread.new do
0
           loop do
0
- puts 'looping'
0
- item = self.queue.pop rescue nil
0
- if item
0
- puts 'reschedule'
0
- Timer.queue << TimerEvent.new(item.condition, item.condition.interval)
0
-
0
- sleep 0.25 if self.queue.empty?
0
+ begin
0
+ event = self.queue.pop rescue nil
0
+ if event
0
+ self.mutex.synchronize do
0
+ case event.condition
0
+ when PollCondition
0
+ self.handle_poll(event.condition, event.phase)
0
+ when EventCondition, TriggerCondition
0
+ self.handle_event(event.condition)
0
+ end
0
+ end
0
+
0
+ # Timer.queue << TimerEvent.new(item.condition, item.condition.interval)
0
+
0
+ sleep 0.25 if self.queue.empty?
0
+ end
0
+ rescue Exception => e
0
+ message = format("Unhandled exception (%s): %s\n%s",
0
+ e.class, e.message, e.backtrace.join("\n"))
0
+ applog(nil, :fatal, message)
0
             end
0
           end
0
         end
0
@@ -76,18 +89,18 @@ module God
0
     # +phase+ is the phase of the Watch at the time the condition was scheduled
0
     #
0
     # Returns nothing
0
- def self.trigger(condition, phase = nil)
0
- self.queue << [condition, phase]
0
-
0
- # self.mutex.synchronize do
0
- # case condition
0
- # when PollCondition
0
- # self.handle_poll(condition, phase)
0
- # when EventCondition, TriggerCondition
0
- # self.handle_event(condition)
0
- # end
0
- # end
0
- end
0
+ # def self.trigger(condition, phase = nil)
0
+ # self.queue << [condition, phase]
0
+ #
0
+ # self.mutex.synchronize do
0
+ # case condition
0
+ # when PollCondition
0
+ # self.handle_poll(condition, phase)
0
+ # when EventCondition, TriggerCondition
0
+ # self.handle_event(condition)
0
+ # end
0
+ # end
0
+ # end
0
     
0
     # private
0
     
0
@@ -98,81 +111,59 @@ module God
0
     #
0
     # Returns nothing
0
     def self.handle_poll(condition, phase)
0
- # # puts 'reschedule'
0
- # # Timer.get.schedule(condition)
0
- #
0
- # metric = self.directory[condition]
0
- #
0
- # # it's possible that the timer will trigger an event before it can be cleared
0
- # # by an exiting metric, in which case it should be ignored
0
- # return if metric.nil?
0
- #
0
- # Thread.new do
0
- # begin
0
- # watch = metric.watch
0
- #
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
- # puts 'reschedule'
0
- # Timer.get.schedule(condition)
0
- # end
0
- # # end
0
- # rescue Exception => e
0
- # message = format("Unhandled exception (%s): %s\n%s",
0
- # e.class, e.message, e.backtrace.join("\n"))
0
- # applog(nil, :fatal, message)
0
- # end
0
- # end
0
+ metric = self.directory[condition]
0
       
0
- # Thread.new do
0
- # puts 'reschedule'
0
- # Timer.get.schedule(condition)
0
- # end
0
+ # it's possible that the timer will trigger an event before it can be cleared
0
+ # by an exiting metric, in which case it should be ignored
0
+ return if metric.nil?
0
       
0
- # Timer.get.schedule(condition)
0
+ watch = metric.watch
0
       
0
- # Thread.list.each {|t| p t}
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.queue << TimerEvent.new(condition, condition.interval)
0
+ # end
0
+ end
0
+ end
0
     end
0
     
0
     # Asynchronously evaluate and handle the given event condition. Handles logging
...
64
65
66
67
 
68
69
70
...
64
65
66
 
67
68
69
70
0
@@ -64,7 +64,7 @@ module God
0
       @templog.send(level, text % [])
0
       @mutex.synchronize do
0
         @capture.puts(@templogio.string) if @capture
0
- self.logs[watch.name] << [Time.now, @templogio.string] if watch
0
+ self.logs[watch.name] << [] if watch # [Time.now, @templogio.string] if watch
0
       end
0
       
0
       # send to regular logger
...
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
...
1
2
 
3
4
5
6
7
8
 
9
10
11
12
13
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
 
 
 
 
 
16
17
18
19
20
0
@@ -1,38 +1,20 @@
0
 module God
0
   
0
- class Timeline < Array
0
+ class Timeline
0
     # Instantiate a new Timeline
0
     # +max_size+ is the maximum size to which the timeline should grow
0
     #
0
     # Returns Timeline
0
     def initialize(max_size)
0
- super()
0
+ @storage = []
0
       @max_size = max_size
0
     end
0
     
0
     # Push a value onto the Timeline
0
     # +val+ is the value to push
0
- #
0
- # Implementation explanation:
0
- # A performance optimization appears here to speed up the push time.
0
- # In essence, the code does this:
0
- #
0
- # def push(val)
0
- # super(val)
0
- # shift if size > @max_size
0
- # end
0
- #
0
- # But that's super slow due to the shift, so we resort to reverse! and pop
0
- # which gives us a 2x speedup with 100 elements and a 6x speedup with 1000
0
- #
0
- # Returns Timeline
0
     def push(val)
0
- if (size + 1) > @max_size
0
- reverse!
0
- pop
0
- reverse!
0
- end
0
- super(val)
0
+ @storage.concat(val)
0
+ @storage.shift if @storage.size > @max_size
0
     end
0
     
0
     # Push a value onto the timeline

Comments

    No one has commented yet.