Class: CarrotRpc::ServerRunner::Signal

Inherits:
Object
  • Object
show all
Defined in:
lib/carrot_rpc/server_runner/signal.rb

Overview

Stores a signal and allows for it to be waited on

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSignal

Returns a new instance of Signal.



9
10
11
# File 'lib/carrot_rpc/server_runner/signal.rb', line 9

def initialize
  self.reader, self.writer = IO.pipe
end

Instance Attribute Details

#nameObject

Attributes



7
8
9
# File 'lib/carrot_rpc/server_runner/signal.rb', line 7

def name
  @name
end

Instance Method Details

#trapvoid

This method returns an undefined value.

Traps all CarrotRpc::ServerRunner::Signals.



20
21
22
23
24
25
26
# File 'lib/carrot_rpc/server_runner/signal.rb', line 20

def trap
  CarrotRpc::ServerRunner::Signals.trap do |name|
    # @note can't log from a trap context: since Ruby 2.0 traps don't allow mutexes as it could lead to a dead lock,
    #   so `logger.info` here would return "log writing failed. can't be called from trap context"
    receive(name)
  end
end

#waitString

Waits for a signal trapped by #trap to be received.

Returns:

  • (String)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/carrot_rpc/server_runner/signal.rb', line 31

def wait
  loop do
    # Wait {#receive}.  IO.select can wake up
    read_ready, _write_ready, _error_ready = IO.select([reader])

    next unless read_ready.include? reader

    reader.read_nonblock(1)

    break name
  end
end