lib/bluepill/process.rb in bluepill-0.0.1 vs lib/bluepill/process.rb in bluepill-0.0.2
- old
+ new
@@ -80,22 +80,26 @@
# Let state_machine do its initialization stuff
super()
end
def add_watch(name, options = {})
- self.watches << ConditionWatch.new(name, options.merge(:logger => self.watch_logger))
+ self.watches << ConditionWatch.new(name, options.merge(:logger => self.logger))
end
def daemonize?
!!self.daemonize
end
def dispatch!(event)
- logger.info "Got stop"
self.send("#{event}!")
end
+ def logger=(logger)
+ @logger = logger
+ self.watches.each {|w| w.logger = logger }
+ end
+
def process_running?(force = false)
@process_running = nil if force
@process_running ||= signal_process(0)
end
@@ -156,26 +160,31 @@
def run_watches
now = Time.now.to_i
threads = self.watches.collect do |watch|
- Thread.new { Thread.current[:events] = watch.run(self.actual_pid, now) }
+ [watch, Thread.new { Thread.current[:events] = watch.run(self.actual_pid, now) }]
end
@transitioned = false
- threads.inject([]) do |events, thread|
+ threads.inject([]) do |events, (watch, thread)|
thread.join
- events << thread[:events]
+ if thread[:events].size > 0
+ logger.info "#{watch.name} dispatched: #{thread[:events].join(',')}"
+ events << thread[:events]
+ end
+ events
end.flatten.uniq.each do |event|
break if @transitioned
self.dispatch!(event)
end
end
def record_transition(state_name)
@transitioned = true
+ self.watches.each { |w| w.clear_history! }
# do other stuff here?
end
def signal_process(code)
::Process.kill(code, actual_pid)
@@ -189,13 +198,9 @@
end
def clear_pid
@actual_pid = nil
File.unlink(pid_file) if File.exists?(pid_file)
- end
-
- def watch_logger
- @watch_logger ||= Logger.new(self.logger, "#{self.name}:") if self.logger
end
end
end
\ No newline at end of file