aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/mauve_thread.rb
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2011-08-19 10:23:01 +0100
committerPatrick J Cherry <patrick@bytemark.co.uk>2011-08-19 10:23:01 +0100
commitc1cf28d9dcc63622b5df1caeab3a2baf66811443 (patch)
tree1bde7a1aac62472f73740657bfe77a01184e534c /lib/mauve/mauve_thread.rb
parent3455b9a9f195e273e574989a0845ad8d2065e82f (diff)
Added last_state_changed thing for MauveThread. Closes #1836.
Diffstat (limited to 'lib/mauve/mauve_thread.rb')
-rw-r--r--lib/mauve/mauve_thread.rb40
1 files changed, 30 insertions, 10 deletions
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