Begin to refactor websocket handling and implement test

Co-authored-by: Felix Auringer <felix.auringer@student.hpi.uni-potsdam.de>
This commit is contained in:
Konrad Hanff
2021-03-30 16:10:19 +02:00
committed by Sebastian Serth
parent 3cf70a33d8
commit 1546f70818
3 changed files with 78 additions and 58 deletions

View File

@ -137,6 +137,45 @@ class SubmissionsController < ApplicationController
end end
end end
def handle_websockets(tubesock, container)
socket = container.socket
tubesock.send_data JSON.dump({'cmd' => 'status', 'status' => :container_running})
@waiting_for_container_time = Time.zone.now - @container_request_time
@execution_request_time = Time.zone.now
socket.on :message do |event|
Rails.logger.info("#{Time.zone.now.getutc}: Docker sending: #{event.data}")
handle_message(event.data, tubesock)
end
socket.on :close do |_event|
EventMachine.stop_event_loop
tubesock.send_data JSON.dump({'cmd' => 'timeout'}) if container.status == 'timeouted'
kill_socket(tubesock)
end
tubesock.onmessage do |data|
Rails.logger.info("#{Time.now.getutc.to_s}: Client sending: #{data}")
# Check whether the client send a JSON command and kill container
# if the command is 'client_kill', send it to docker otherwise.
begin
parsed = JSON.parse(data) unless data == "\n"
if parsed.instance_of?(Hash) && parsed['cmd'] == 'client_kill'
Rails.logger.debug("Client exited container.")
container.destroy
else
socket.send data
Rails.logger.debug { "Sent the received client data to docker:#{data}" }
end
rescue JSON::ParserError => error
socket.send data
Rails.logger.debug { "Rescued parsing error, sent the received client data to docker:#{data}" }
Sentry.set_extras(data: data)
end
end
end
def run def run
Thread.new do Thread.new do
hijack do |tubesock| hijack do |tubesock|
@ -144,46 +183,13 @@ class SubmissionsController < ApplicationController
kill_socket(tubesock) kill_socket(tubesock)
return return
end end
EventMachine.run do @container_request_time = Time.zone.now
container_request_time = Time.zone.now @submission.run(sanitize_filename) do |container|
@submission.run(sanitize_filename) do |socket| handle_websockets(tubesock, container)
tubesock.send_data JSON.dump({'cmd' => 'status', 'status' => :container_running})
@waiting_for_container_time = Time.zone.now - container_request_time
@execution_request_time = Time.zone.now
socket.on :message do |event|
Rails.logger.info("#{Time.zone.now.getutc}: Docker sending: #{event.data}")
handle_message(event.data, tubesock)
end
socket.on :close do |_event|
EventMachine.stop_event_loop
kill_socket(tubesock)
end
tubesock.onmessage do |data|
Rails.logger.info(Time.now.getutc.to_s + ": Client sending: " + data)
# Check whether the client send a JSON command and kill container
# if the command is 'client_kill', send it to docker otherwise.
begin
parsed = JSON.parse(data) unless data == "\n"
if parsed.instance_of?(Hash) && parsed['cmd'] == 'client_kill'
Rails.logger.debug("Client exited container.")
container.destroy
else
socket.send data
Rails.logger.debug { "Sent the received client data to docker:#{data}" }
end
rescue JSON::ParserError => error
socket.send data
Rails.logger.debug { "Rescued parsing error, sent the received client data to docker:#{data}" }
Sentry.set_extras(data: data)
end
end
end
end end
end end
ensure
ActiveRecord::Base.connection_pool.release_connection
end end
# unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive? # unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
# Thread.new do # Thread.new do
@ -396,20 +402,19 @@ class SubmissionsController < ApplicationController
def statistics; end def statistics; end
def test def test
hijack do |tubesock| Thread.new do
unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive? hijack do |tubesock|
Thread.new do if @embed_options[:disable_run]
EventMachine.run kill_socket(tubesock)
ensure return
ActiveRecord::Base.connection_pool.release_connection end
@container_request_time = Time.now
@submission.run_tests(sanitize_filename) do |container|
handle_websockets(tubesock, container)
end end
end end
ensure
output = @docker_client.execute_test_command(@submission, sanitize_filename) ActiveRecord::Base.connection_pool.release_connection
# tubesock is the socket to the client
tubesock.send_data JSON.dump(output)
tubesock.send_data JSON.dump('cmd' => 'exit')
end end
end end

View File

@ -136,24 +136,36 @@ class Submission < ApplicationRecord
end end
end end
def test(file) def score(file)
score_command = command_for execution_environment.test_command, file score_command = command_for execution_environment.test_command, file
container = run_command_with_self score_command container = run_command_with_self score_command
container container
# Todo receive websocket data and pass it to some score function
end end
def run(file) def run(file, &block)
run_command = command_for execution_environment.run_command, file run_command = command_for execution_environment.run_command, file
container = run_command_with_self run_command execute_interactively(run_command, &block)
container end
yield(container.socket) if block_given?
def run_tests(file, &block)
test_command = command_for execution_environment.test_command, file
execute_interactively(test_command, &block)
end
def execute_interactively(command)
container = nil
EventMachine.run do
container = run_command_with_self command
yield(container) if block_given?
end
container.destroy container.destroy
end end
def run_command_with_self(command) def run_command_with_self(command)
container = Container.new(execution_environment, execution_environment.permitted_execution_time) container = Container.new(execution_environment, execution_environment.permitted_execution_time)
container.copy_submission_files self container.copy_submission_files self
container.execute_command_interactively(command) container.execute_interactively(command)
container container
end end

View File

@ -37,16 +37,19 @@ class Container
response response
end end
def execute_command_interactively(command) def execute_interactively(command)
websocket_url = execute_command(command)[:websocket_url] websocket_url = execute_command(command)[:websocket_url]
@socket = Faye::WebSocket::Client.new(websocket_url, [], ping: 0.1) @socket = Faye::WebSocket::Client.new(websocket_url, [], ping: 0.1)
# Faye::WebSocket::Client.new(socket_url, [], headers: headers, ping: 0.1)
end end
def destroy def destroy
Faraday.delete container_url Faraday.delete container_url
end end
def status
parse(Faraday.get(container_url))[:status]
end
private private
def container_url def container_url