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:

committed by
Sebastian Serth

parent
3cf70a33d8
commit
1546f70818
@ -137,18 +137,10 @@ class SubmissionsController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def run
|
||||
Thread.new do
|
||||
hijack do |tubesock|
|
||||
if @embed_options[:disable_run]
|
||||
kill_socket(tubesock)
|
||||
return
|
||||
end
|
||||
EventMachine.run do
|
||||
container_request_time = Time.zone.now
|
||||
@submission.run(sanitize_filename) do |socket|
|
||||
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
|
||||
@waiting_for_container_time = Time.zone.now - @container_request_time
|
||||
@execution_request_time = Time.zone.now
|
||||
|
||||
socket.on :message do |event|
|
||||
@ -158,11 +150,12 @@ class SubmissionsController < ApplicationController
|
||||
|
||||
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)
|
||||
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
|
||||
@ -182,8 +175,21 @@ class SubmissionsController < ApplicationController
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def run
|
||||
Thread.new do
|
||||
hijack do |tubesock|
|
||||
if @embed_options[:disable_run]
|
||||
kill_socket(tubesock)
|
||||
return
|
||||
end
|
||||
@container_request_time = Time.zone.now
|
||||
@submission.run(sanitize_filename) do |container|
|
||||
handle_websockets(tubesock, container)
|
||||
end
|
||||
end
|
||||
ensure
|
||||
ActiveRecord::Base.connection_pool.release_connection
|
||||
end
|
||||
# unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
|
||||
# Thread.new do
|
||||
@ -396,23 +402,22 @@ class SubmissionsController < ApplicationController
|
||||
def statistics; end
|
||||
|
||||
def test
|
||||
hijack do |tubesock|
|
||||
unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
|
||||
Thread.new do
|
||||
EventMachine.run
|
||||
hijack do |tubesock|
|
||||
if @embed_options[:disable_run]
|
||||
kill_socket(tubesock)
|
||||
return
|
||||
end
|
||||
@container_request_time = Time.now
|
||||
@submission.run_tests(sanitize_filename) do |container|
|
||||
handle_websockets(tubesock, container)
|
||||
end
|
||||
end
|
||||
ensure
|
||||
ActiveRecord::Base.connection_pool.release_connection
|
||||
end
|
||||
end
|
||||
|
||||
output = @docker_client.execute_test_command(@submission, sanitize_filename)
|
||||
|
||||
# tubesock is the socket to the client
|
||||
tubesock.send_data JSON.dump(output)
|
||||
tubesock.send_data JSON.dump('cmd' => 'exit')
|
||||
end
|
||||
end
|
||||
|
||||
def with_server_sent_events
|
||||
response.headers['Content-Type'] = 'text/event-stream'
|
||||
server_sent_event = SSE.new(response.stream)
|
||||
|
@ -136,24 +136,36 @@ class Submission < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def test(file)
|
||||
def score(file)
|
||||
score_command = command_for execution_environment.test_command, file
|
||||
container = run_command_with_self score_command
|
||||
container
|
||||
# Todo receive websocket data and pass it to some score function
|
||||
end
|
||||
|
||||
def run(file)
|
||||
def run(file, &block)
|
||||
run_command = command_for execution_environment.run_command, file
|
||||
container = run_command_with_self run_command
|
||||
container
|
||||
yield(container.socket) if block_given?
|
||||
execute_interactively(run_command, &block)
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
def run_command_with_self(command)
|
||||
container = Container.new(execution_environment, execution_environment.permitted_execution_time)
|
||||
container.copy_submission_files self
|
||||
container.execute_command_interactively(command)
|
||||
container.execute_interactively(command)
|
||||
container
|
||||
end
|
||||
|
||||
|
@ -37,16 +37,19 @@ class Container
|
||||
response
|
||||
end
|
||||
|
||||
def execute_command_interactively(command)
|
||||
def execute_interactively(command)
|
||||
websocket_url = execute_command(command)[:websocket_url]
|
||||
@socket = Faye::WebSocket::Client.new(websocket_url, [], ping: 0.1)
|
||||
# Faye::WebSocket::Client.new(socket_url, [], headers: headers, ping: 0.1)
|
||||
end
|
||||
|
||||
def destroy
|
||||
Faraday.delete container_url
|
||||
end
|
||||
|
||||
def status
|
||||
parse(Faraday.get(container_url))[:status]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def container_url
|
||||
|
Reference in New Issue
Block a user