Class: Async::Container::Notify::Pipe

Inherits:
Client
  • Object
show all
Defined in:
lib/async/container/notify/pipe.rb

Direct Known Subclasses

Process::Instance, Thread::Instance

Constant Summary collapse

NOTIFY_PIPE =
'NOTIFY_PIPE'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

#error!, #status!, #stopping!

Constructor Details

#initialize(io) ⇒ Pipe

Returns a new instance of Pipe.



44
45
46
# File 'lib/async/container/notify/pipe.rb', line 44

def initialize(io)
  @io = io
end

Class Method Details

.open!(environment = ENV) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/async/container/notify/pipe.rb', line 34

def self.open!(environment = ENV)
  if descriptor = environment.delete(NOTIFY_PIPE)
    self.new(::IO.for_fd(descriptor.to_i))
  end
rescue Errno::EBADF => error
  Async.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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/async/container/notify/pipe.rb', line 50

def before_spawn(arguments, options)
  environment = environment_for(arguments)
  
  # Use `notify_pipe` option if specified:
  if notify_pipe = options.delete(:notify_pipe)
    options[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 !options.key?(3)
    options[3] = @io
    environment[NOTIFY_PIPE] = "3"
  
  # Otherwise, give up!
  else
    raise ArgumentError, "Please specify valid file descriptor for notify_pipe!"
  end
end

#ready!(**message) ⇒ Object



82
83
84
# File 'lib/async/container/notify/pipe.rb', line 82

def ready!(**message)
  send(ready: true, **message)
end

#reloading!(**message) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/async/container/notify/pipe.rb', line 94

def reloading!(**message)
  message[:ready] = false
  message[:reloading] = true
  message[:status] ||= "Reloading..."
  
  send(**message)
end

#restarting!(**message) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/async/container/notify/pipe.rb', line 86

def restarting!(**message)
  message[:ready] = false
  message[:reloading] = true
  message[:status] ||= "Restarting..."
  
  send(**message)
end

#send(**message) ⇒ Object



75
76
77
78
79
80
# File 'lib/async/container/notify/pipe.rb', line 75

def send(**message)
  data = ::JSON.dump(message)
  
  @io.puts(data)
  @io.flush
end