]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/timer.rb
* move 'version' help to the module that actually supports the command
[user/henk/code/ruby/rbot.git] / lib / rbot / timer.rb
index dd30ab685c9976d25ac6d70529d529678aca2701..10a6f318e919bdb976a084d680681adcfc819269 100644 (file)
@@ -108,6 +108,7 @@ class Timer
       rescue Exception => e
         error "Timer action #{self.inspect}: block #{@block.inspect} failed!"
         error e.pretty_inspect
+        debug e.backtrace.join("\n")
       end
 
       if @repeat && @period > 0
@@ -197,18 +198,31 @@ class Timer
     self.configure(aid, :period => period, &block)
   end
 
-  protected
-
   def start
-    raise 'double-started timer' if @thread
+    raise 'already started' if @thread
+    @stopping = false
+    debug "starting timer #{self}"
     @thread = Thread.new do
       loop do
         tmout = self.run_actions
+        break if tmout and tmout < 0
         self.synchronize { @tick.wait(tmout) }
       end
     end
   end
 
+  def stop
+    raise 'already stopped' unless @thread
+    debug "stopping timer #{self}..."
+    @stopping = true
+    self.synchronize { @tick.signal }
+    @thread.join(60) or @thread.kill
+    debug "timer #{self} stopped"
+    @thread = nil
+  end
+
+  protected
+
   def [](aid)
     aid ||= @current
     raise "no current action" unless aid
@@ -219,6 +233,7 @@ class Timer
   def run_actions(now = Time.now)
     nxt = nil
     @actions.keys.each do |k|
+      return -1 if @stopping
       a = @actions[k]
       next if (!a) or a.blocked?