Class: TurboTest::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_test/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration, endpoint = nil) ⇒ Server

Returns a new instance of Server.



36
37
38
39
40
41
42
# File 'lib/turbo_test/server.rb', line 36

def initialize(configuration, endpoint = nil)
	@configuration = configuration
	@endpoint = endpoint || Async::IO::Endpoint.unix("turbo_test-#{Process.pid}.ipc")
	@wrapper = Wrapper.new
	
	@container = Async::Container.new
end

Instance Method Details

#host(queue) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/turbo_test/server.rb', line 44

def host(queue)
	input, output = IO.pipe
	
	@container.spawn(name: "#{self.class} Host") do |instance|
		connected = 0
		progress = Console.logger.progress("Queue", queue.size)
		failures = []
		
		statistics = {
			succeeded: 0,
			failed: 0,
		}
		
		Async do |task|
			bound_endpoint = Sync do
				Async::IO::SharedEndpoint.bound(@endpoint)
			end
			
			instance.ready!
			
			bound_endpoint.accept do |peer|
				# Console.logger.info(self) {"Incoming connection from #{peer}..."}
				
				packer = @wrapper.packer(peer)
				unpacker = @wrapper.unpacker(peer)
				
				packer.write([:connected, connected])
				connected += 1
				
				unpacker.each do |message|
					command, *arguments = message
					
					case command
					when :ready
						Console.logger.debug("Child Ready") {arguments}
						
						if job = queue.pop
							packer.write([:job, job])
							packer.flush
						else
							Console.logger.debug("Child Closed")
							peer.close_write
							connected -= 1
							
							if connected.zero?
								print_summary(failures)
								task.stop
							end
						end
					when :finished
						Console.logger.debug("Job Finished") {arguments}
					when :failed
						Console.logger.debug("Job Failed") {arguments}
						failures << arguments
						statistics[:failed] += 1
					when :count
						Console.logger.debug("Job Count") {arguments}
						statistics[:succeeded] += arguments.first
					when :result
						Console.logger.debug("Job Result") {arguments}
						progress.increment
					when :error
						Console.logger.error("Job Error") {arguments}
					else
						Console.logger.warn(self) {"Unhandled command: #{command}#{arguments.inspect}"}
					end
				end
			end
			
			bound_endpoint.close
		end
	ensure
		Console.logger.info("Writing results")
		@wrapper.packer(output).write(statistics).flush
	end
	
	output.close
	
	return @wrapper.unpacker(input)
end


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/turbo_test/server.rb', line 125

def print_summary(failures, command = $0)
	return unless failures.any?
	
	failures.sort_by!{|(failure)| failure[:location]}
	
	$stderr.puts nil, "Failures:", nil
	
	failures.each do |(failure)|
		$stderr.puts failure[:report]
		$stderr.puts
	end
	
	$stderr.puts nil, "Summary:", nil
	
	failures.each do |(failure)|
		$stderr.puts "#{command} #{failure[:location]} \# #{failure[:description]}"
	end
	
	$stderr.puts
end

#run(queue) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/turbo_test/server.rb', line 194

def run(queue)
	# Start the host:
	results = self.host(queue)
	
	# Wait until the host is ready:
	@container.wait_until_ready
	
	# Start the workers:
	self.workers
	
	# Wait for the container to finish:
	@container.wait
	
	# Read the results from the host:
	return results.read
ensure
	if path = @endpoint.path and File.exist?(path)
		File.unlink(path)
	end
end

#workersObject



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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/turbo_test/server.rb', line 146

def workers
	@container.run(name: "#{self.class} Worker") do |instance|
		Async do |task|
			sleep(rand)
			@endpoint.connect do |peer|
				threads = Async::IO::Threads.new
				
				packer = @wrapper.packer(peer)
				unpacker = @wrapper.unpacker(peer)
				
				packer.write([:ready])
				packer.flush
				
				instance.ready!
				
				unpacker.each do |message|
					command, tail = message
					
					case command
					when :connected
						@configuration&.worker&.call(*tail)
					when :job
						klass, *arguments = *tail
						
						begin
							# We run this in a separate thread to keep it isolated from the worker loop:
							result = threads.async do
								klass.new(*arguments).call(packer: packer)
							end.wait
							
							packer.write([:result, result])
						rescue Exception => exception
							packer.write([:error, exception.class, exception.backtrace])
						end
						
						packer.write([:ready])
						packer.flush
					else
						Console.logger.warn(self) {"Unhandled command: #{command}#{arguments.inspect}"}
					end
				end
			end
		rescue Errno::ECONNREFUSED
			# Host is finished already.
		end
	end
end