Class: Async::Container::Notify::Pipe
- Defined in:
- lib/async/container/notify/pipe.rb
Overview
Implements a process readiness protocol using an inherited pipe file descriptor.
Direct Known Subclasses
Constant Summary collapse
- NOTIFY_PIPE =
The environment variable key which contains the pipe file descriptor.
'NOTIFY_PIPE'
Class Method Summary collapse
-
.open!(environment = ENV) ⇒ Object
Open a notification client attached to the current NOTIFY_PIPE if possible.
Instance Method Summary collapse
-
#before_spawn(arguments, options) ⇒ Object
Inserts or duplicates the environment given an argument array.
-
#initialize(io) ⇒ Pipe
constructor
Initialize the notification client.
-
#send(**message) ⇒ Object
Formats the message using JSON and sends it to the parent controller.
Methods inherited from Client
#error!, #ready!, #reloading!, #restarting!, #status!, #stopping!
Constructor Details
#initialize(io) ⇒ Pipe
Initialize the notification client.
48 49 50 |
# File 'lib/async/container/notify/pipe.rb', line 48 def initialize(io) @io = io end |
Class Method Details
.open!(environment = ENV) ⇒ Object
Open a notification client attached to the current NOTIFY_PIPE if possible.
36 37 38 39 40 41 42 43 44 |
# File 'lib/async/container/notify/pipe.rb', line 36 def self.open!(environment = ENV) if descriptor = environment.delete(NOTIFY_PIPE) self.new(::IO.for_fd(descriptor.to_i)) end rescue Errno::EBADF => error Console.logger.error(self) {error} return nil end |
Instance Method Details
#before_spawn(arguments, options) ⇒ Object
Inserts or duplicates the environment given an argument array. Sets or clears it in a way that is suitable for Process.spawn.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/async/container/notify/pipe.rb', line 54 def before_spawn(arguments, ) environment = environment_for(arguments) # Use `notify_pipe` option if specified: if notify_pipe = .delete(:notify_pipe) [notify_pipe] = @io environment[NOTIFY_PIPE] = notify_pipe.to_s # Use stdout if it's not redirected: # This can cause issues if the user expects stdout to be connected to a terminal. # elsif !options.key?(:out) # options[:out] = @io # environment[NOTIFY_PIPE] = "1" # Use fileno 3 if it's available: elsif !.key?(3) [3] = @io environment[NOTIFY_PIPE] = "3" # Otherwise, give up! else raise ArgumentError, "Please specify valid file descriptor for notify_pipe!" end end |
#send(**message) ⇒ Object
Formats the message using JSON and sends it to the parent controller. This is suitable for use with Channel.
81 82 83 84 85 86 |
# File 'lib/async/container/notify/pipe.rb', line 81 def send(**) data = ::JSON.dump() @io.puts(data) @io.flush end |