From c1cf28d9dcc63622b5df1caeab3a2baf66811443 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Fri, 19 Aug 2011 10:23:01 +0100 Subject: Added last_state_changed thing for MauveThread. Closes #1836. --- lib/mauve/mauve_thread.rb | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'lib/mauve/mauve_thread.rb') diff --git a/lib/mauve/mauve_thread.rb b/lib/mauve/mauve_thread.rb index 77df95e..7d5dcbe 100644 --- a/lib/mauve/mauve_thread.rb +++ b/lib/mauve/mauve_thread.rb @@ -5,11 +5,10 @@ module Mauve class MauveThread - attr_reader :state, :poll_every + attr_reader :poll_every def initialize @thread = nil - @state = :stopped end def logger @@ -21,9 +20,9 @@ module Mauve # # Set the minimum poll frequency. # - if i.to_f < 0.2 - logger.debug "Increasing thread polling interval to 0.2s from #{i}" - i = 0.2 + if i.to_f < 0 + logger.debug "Increasing thread polling interval to 0s from #{i}" + i = 0 end @poll_every = i @@ -33,6 +32,7 @@ module Mauve # # Good to go. # + @thread = Thread.current self.state = :starting @poll_every ||= interval @@ -76,15 +76,33 @@ module Mauve [:freezing, :stopping].include?(self.state) end + def state + if self.alive? + @thread.key?(:state) ? @thread[:state] : :unknown + else + :stopped + end + end + def state=(s) raise "Bad state for mauve_thread #{s.inspect}" unless [:stopped, :starting, :started, :freezing, :frozen, :stopping, :killing, :killed].include?(s) + raise "Thread not ready yet." unless @thread.is_a?(Thread) - unless @state == s - @state = s + unless @thread[:state] == s + @thread[:state] = s + @thread[:last_state_change] = Time.now logger.debug(s.to_s.capitalize) end - @state + @thread[:state] + end + + def last_state_change + if self.alive? and @thread.key?(:last_state_change) + return @thread[:last_state_change] + else + return nil + end end def freeze @@ -101,13 +119,15 @@ module Mauve def run if self.alive? + # Wake up if we're stopped. if self.stop? @thread.wakeup end else @logger = nil - self.state = :starting - @thread = Thread.new{ self.run_thread { self.main_loop } } + Thread.new do + self.run_thread { self.main_loop } + end end end -- cgit v1.2.1