Class: Async::Container::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/async/container/controller.rb

Overview

Manages the life-cycle of a container.

Constant Summary collapse

SIGHUP =
Signal.list["HUP"]
SIGINT =
Signal.list["INT"]
SIGTERM =
Signal.list["TERM"]
SIGUSR1 =
Signal.list["USR1"]
SIGUSR2 =
Signal.list["USR2"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notify: Notify.open!) ⇒ Controller

Returns a new instance of Controller.



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/async/container/controller.rb', line 49

def initialize(notify: Notify.open!)
  @container = nil
  
  if @notify = notify
    @notify.status!("Initializing...")
  end
  
  @signals = {}
  
  trap(SIGHUP) do
    self.restart
  end
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



79
80
81
# File 'lib/async/container/controller.rb', line 79

def container
  @container
end

Instance Method Details

#create_containerObject



81
82
83
# File 'lib/async/container/controller.rb', line 81

def create_container
  Container.new
end

#reloadObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/async/container/controller.rb', line 153

def reload
  @notify&.reloading!
  
  Async.logger.info(self) {"Reloading container: #{@container}..."}
  
  begin
    self.setup(@container)
  rescue
    raise InitializationError, container
  end
  
  # Wait for all child processes to enter the ready state.
  Async.logger.debug(self, "Waiting for startup...")
  @container.wait_until_ready
  Async.logger.debug(self, "Finished startup.")
  
  if @container.failed?
    @notify.error!("Container failed!")
    
    raise InitializationError, @container
  else
    @notify&.ready!
  end
end

#restartObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/async/container/controller.rb', line 107

def restart
  if @container
    @notify&.restarting!
    
    Async.logger.debug(self) {"Restarting container..."}
  else
    Async.logger.debug(self) {"Starting container..."}
  end
  
  container = self.create_container
  
  begin
    self.setup(container)
  rescue
    @notify&.error!($!.to_s)
    
    raise InitializationError, container
  end
  
  # Wait for all child processes to enter the ready state.
  Async.logger.debug(self, "Waiting for startup...")
  container.wait_until_ready
  Async.logger.debug(self, "Finished startup.")
  
  if container.failed?
    @notify&.error!($!.to_s)
    
    container.stop
    
    raise InitializationError, container
  end
  
  # Make this swap as atomic as possible:
  old_container = @container
  @container = container
  
  Async.logger.debug(self, "Stopping old container...")
  old_container&.stop
  @notify&.ready!
rescue
  # If we are leaving this function with an exception, try to kill the container:
  container&.stop(false)
  
  raise
end

#runObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/async/container/controller.rb', line 178

def run
  # I thought this was the default... but it doesn't always raise an exception unless you do this explicitly.
  interrupt_action = Signal.trap(:INT) do
    raise Interrupt
  end
  
  terminate_action = Signal.trap(:TERM) do
    raise Terminate
  end
  
  self.start
  
  while @container&.running?
    begin
      @container.wait
    rescue SignalException => exception
      if handler = @signals[exception.signo]
        begin
          handler.call
        rescue InitializationError => error
          Async.logger.error(self) {error}
        end
      else
        raise
      end
    end
  end
rescue Interrupt
  self.stop(true)
rescue Terminate
  self.stop(false)
ensure
  self.stop(true)
  
  # Restore the interrupt handler:
  Signal.trap(:INT, interrupt_action)
  Signal.trap(:TERM, terminate_action)
end

#running?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/async/container/controller.rb', line 85

def running?
  !!@container
end

#setup(container) ⇒ Object



93
94
95
96
# File 'lib/async/container/controller.rb', line 93

def setup(container)
  # Don't do this, otherwise calling super is risky for sub-classes:
  # raise NotImplementedError, "Container setup is must be implemented in derived class!"
end

#startObject



98
99
100
# File 'lib/async/container/controller.rb', line 98

def start
  self.restart unless @container
end

#state_stringObject



63
64
65
66
67
68
69
# File 'lib/async/container/controller.rb', line 63

def state_string
  if running?
    "running"
  else
    "stopped"
  end
end

#stop(graceful = true) ⇒ Object



102
103
104
105
# File 'lib/async/container/controller.rb', line 102

def stop(graceful = true)
  @container&.stop(graceful)
  @container = nil
end

#to_sObject



71
72
73
# File 'lib/async/container/controller.rb', line 71

def to_s
  "#{self.class} #{state_string}"
end

#trap(signal, &block) ⇒ Object



75
76
77
# File 'lib/async/container/controller.rb', line 75

def trap(signal, &block)
  @signals[signal] = block
end

#waitObject



89
90
91
# File 'lib/async/container/controller.rb', line 89

def wait
  @container&.wait
end