Method: Thread::Every#initialize
- Defined in:
- lib/thread/every.rb
#initialize(every, &block) ⇒ Every
Create an every with the given seconds and block.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/thread/every.rb', line 21 def initialize (every, &block) raise ArgumentError, 'no block given' unless block @every = every @old = true @mutex = Mutex.new @thread = Thread.new { loop do begin value = block.call @mutex.synchronize { @at = Time.now @value = value @old = false @exception = nil } rescue Restart next rescue Exception => e @mutex.synchronize { @at = Time.now @exception = e } break if e.is_a? Cancel end cond.broadcast if cond? begin sleep @every rescue Restart next rescue Cancel => e @mutex.synchronize { @at = Time.now @exception = e } break end end } ObjectSpace.define_finalizer self, self.class.finalizer(@thread) end |