Class: Kraps::Interval

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/kraps/interval.rb

Instance Method Summary collapse

Constructor Details

#initialize(timeout, &block) ⇒ Interval

Returns a new instance of Interval.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kraps/interval.rb', line 5

def initialize(timeout, &block)
  super()

  @thread_queue = TimeoutQueue.new
  @main_queue = TimeoutQueue.new
  @stopped = false

  @thread = Thread.new do
    until @stopped
      item = @thread_queue.deq(timeout: timeout)

      block.call unless @stopped

      @main_queue.enq(1) if item
    end
  end
end

Instance Method Details

#fire(timeout:) ⇒ Object



23
24
25
26
# File 'lib/kraps/interval.rb', line 23

def fire(timeout:)
  @thread_queue.enq(1)
  @main_queue.deq(timeout: timeout)
end

#stopObject



28
29
30
31
32
# File 'lib/kraps/interval.rb', line 28

def stop
  @stopped = true
  @thread_queue.enq(nil)
  @thread.join
end