X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Ftimer.rb;h=10a6f318e919bdb976a084d680681adcfc819269;hb=7205060ebc35daf26a22ff6453b4faef477aaca7;hp=dd30ab685c9976d25ac6d70529d529678aca2701;hpb=9f836a1ed8d0ef118ae538594d0909b68100a133;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/timer.rb b/lib/rbot/timer.rb index dd30ab68..10a6f318 100644 --- a/lib/rbot/timer.rb +++ b/lib/rbot/timer.rb @@ -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?