Class: Async::Container::Controller

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

Overview

Manages the life-cycle of one or more containers in order to support a persistent system. e.g. a web server, job server or some other long running system.

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!, container_class: Container, graceful_stop: true) ⇒ Controller

Initialize the controller.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/async/container/controller.rb', line 26

def initialize(notify: Notify.open!, container_class: Container, graceful_stop: true)
	@notify = notify
	@container_class = container_class
	@graceful_stop = graceful_stop
	
	@container = nil
	@signals = {}
	
	self.trap(SIGHUP) do
		self.restart
	end
end

Instance Attribute Details

#containerObject (readonly)

The current container being managed by the controller.



49
50
51
# File 'lib/async/container/controller.rb', line 49

def container
  @container
end

#container_classObject (readonly)

The container class used by the controller.



43
44
45
# File 'lib/async/container/controller.rb', line 43

def container_class
  @container_class
end

#graceful_stopObject (readonly)

The graceful stop flag used by the controller.



46
47
48
# File 'lib/async/container/controller.rb', line 46

def graceful_stop
  @graceful_stop
end

#notifyObject (readonly)

The notify client used by the controller.



40
41
42
# File 'lib/async/container/controller.rb', line 40

def notify
  @notify
end

Instance Method Details

#create_containerObject

Create a container for the controller. Can be overridden by a sub-class.



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

def create_container
	@container_class.new(policy: self.make_policy)
end

#make_policyObject

Create a policy for managing child lifecycle events. Can be overridden by a sub-class to provide a custom policy.



77
78
79
# File 'lib/async/container/controller.rb', line 77

def make_policy
	Policy::DEFAULT
end

#reloadObject

Reload the existing container. Children instances will be reloaded using ‘SIGHUP`.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/async/container/controller.rb', line 178

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

#restartObject

Restart the container. A new container is created, and if successful, any old container is terminated gracefully. This is equivalent to a blue-green deployment.



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/async/container/controller.rb', line 126

def restart
	if @container
		@notify&.restarting!
		
		Console.info(self, "Restarting container...")
	else
		Console.info(self, "Starting container...")
	end
	
	container = self.create_container
	
	begin
		self.setup(container)
	rescue => error
		@notify&.error!(error.to_s)
		
		raise SetupError, container
	end
	
	# Wait for all child processes to enter the ready state.
	Console.info(self, "Waiting for startup...")
	container.wait_until_ready
	Console.info(self, "Finished startup.")
	
	if container.failed?
		@notify&.error!("Container failed to start!")
		
		raise SetupError, container
	end
	
	# The following swap should be atomic:
	old_container = @container
	@container = container
	container = nil
	
	if old_container
		Console.info(self, "Stopping old container...")
		old_container&.stop(@graceful_stop)
	end
	
	@notify&.ready!(size: @container.size, status: "Running with #{@container.size} children.")
rescue => error
	raise
ensure
	# If we are leaving this function with an exception, kill the container:
	if container
		Console.warn(self, "Stopping failed container...", exception: error)
		container.stop(false)
	end
end

#runObject

Enter the controller run loop, trapping ‘SIGINT` and `SIGTERM`.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/async/container/controller.rb', line 204

def run
	@notify&.status!("Initializing controller...")
	
	with_signal_handlers do
		self.start
		
		while @container&.running?
			begin
				@container.wait
			rescue SignalException => exception
				if handler = @signals[exception.signo]
					begin
						handler.call
					rescue SetupError => error
						Console.error(self, error)
					end
				else
					raise
				end
			end
		end
	end
rescue Interrupt
	self.stop
rescue Terminate
	self.stop(false)
ensure
	self.stop(false)
end

#running?Boolean

Whether the controller has a running container.

Returns:

  • (Boolean)


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

def running?
	!!@container
end

#setup(container) ⇒ Object

Spawn container instances into the given container. Should be overridden by a sub-class.



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

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

Start the container unless it’s already running.



108
109
110
111
112
113
114
115
# File 'lib/async/container/controller.rb', line 108

def start
	unless @container
		Console.info(self, "Controller starting...")
		self.restart
	end
	
	Console.info(self, "Controller started.")
end

#state_stringObject

The state of the controller.



53
54
55
56
57
58
59
# File 'lib/async/container/controller.rb', line 53

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

#stop(graceful = @graceful_stop) ⇒ Object

Stop the container if it’s running.



119
120
121
122
# File 'lib/async/container/controller.rb', line 119

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

#to_sObject

A human readable representation of the controller.



63
64
65
# File 'lib/async/container/controller.rb', line 63

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

#trap(signal, &block) ⇒ Object

Trap the specified signal.



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

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

#waitObject

Wait for the underlying container to start.



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

def wait
	@container&.wait
end