Class: Rpush::Daemon::InterruptibleSleep

Inherits:
Object
  • Object
show all
Defined in:
lib/rpush/daemon/interruptible_sleep.rb

Instance Method Summary collapse

Constructor Details

#initialize(duration) ⇒ InterruptibleSleep

Returns a new instance of InterruptibleSleep.



6
7
8
9
10
11
12
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 6

def initialize(duration)
  @duration = duration
  @obj = Object.new
  @obj.extend(MonitorMixin)
  @condition = @obj.new_cond
  @stop = false
end

Instance Method Details

#sleepObject



14
15
16
17
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 14

def sleep
  return if @stop
  @obj.synchronize { @condition.wait(100_000) }
end

#startObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 19

def start
  @stop = false

  @thread = Thread.new do
    loop do
      break if @stop
      Kernel.sleep(@duration)
      wakeup
    end
  end
end

#stopObject



31
32
33
34
35
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 31

def stop
  @stop = true
  wakeup
  @thread.kill if @thread
end

#wakeupObject



37
38
39
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 37

def wakeup
  @obj.synchronize { @condition.signal }
end