Class: Process::Daemon::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/process/daemon/notification.rb

Overview

This is a one shot cross-process notification mechanism using pipes. It can also be used in the same process if required, e.g. the self-pipe trick.

Instance Method Summary collapse

Constructor Details

#initializeNotification



25
26
27
28
29
# File 'lib/process/daemon/notification.rb', line 25

def initialize
  @output, @input = IO.pipe
  
  @signalled = false
end

Instance Method Details

#signalObject

Signal the notification.



32
33
34
35
36
# File 'lib/process/daemon/notification.rb', line 32

def signal
  @signalled = true
  
  @input.puts
end

#signalled?Boolean

Was this notification signalled?



39
40
41
# File 'lib/process/daemon/notification.rb', line 39

def signalled?
  @signalled
end

#wait(timeout: nil) ⇒ Object

Wait/block until a signal is received. Optional timeout.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/process/daemon/notification.rb', line 45

def wait(timeout: nil)
  if timeout
    read_ready, _, _ = IO.select([@output], [], [], timeout)
    
    return false unless read_ready and read_ready.any?
  end
  
  @signalled or @output.read(1)
  
  # Just in case that this was split across multiple processes.
  @signalled = true
end