Fix socket handling and add configuration option

Co-authored-by: Felix Auringer <felix.auringer@student.hpi.uni-potsdam.de>
This commit is contained in:
Konrad Hanff
2021-04-01 10:32:47 +02:00
committed by Sebastian Serth
parent 92b249e7b3
commit 6a4e302f4e
4 changed files with 26 additions and 21 deletions

View File

@ -142,7 +142,7 @@ class SubmissionsController < ApplicationController
@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 @execution_request_time = Time.zone.now
socket.on :message do |data| socket.on :output do |data|
Rails.logger.info("#{Time.zone.now.getutc}: Docker sending: #{data}") Rails.logger.info("#{Time.zone.now.getutc}: Docker sending: #{data}")
handle_message(data, tubesock) handle_message(data, tubesock)
end end

View File

@ -146,23 +146,23 @@ class Submission < ApplicationRecord
score_command = command_for execution_environment.test_command, file.name_with_extension score_command = command_for execution_environment.test_command, file.name_with_extension
stdout = "" stdout = ""
stderr = "" stderr = ""
exit_code = 0 exit_code = 1 # default to error
container.execute_interactively(score_command) do |container, socket| container.execute_interactively(score_command) do |container, socket|
socket.on :stderr do socket.on :stderr do |data|
|data| stderr << data stderr << data
end end
socket.on :stdout do socket.on :stdout do |data|
|data| stdout << data stdout << data
end end
socket.on :close do |_exit_code| socket.on :exit do |_exit_code|
exit_code = _exit_code exit_code = _exit_code
EventMachine.stop_event_loop EventMachine.stop_event_loop
end end
end end
output = { output = {
file_role: file.role, file_role: file.role,
waiting_for_container_time: 1.second, # TODO waiting_for_container_time: 1, # TODO
container_execution_time: 1.second, # TODO container_execution_time: 1, # TODO
status: (exit_code == 0) ? :ok : :failed, status: (exit_code == 0) ? :ok : :failed,
stdout: stdout, stdout: stdout,
stderr: stderr, stderr: stderr,

View File

@ -3,7 +3,7 @@
require 'container_connection' require 'container_connection'
class Container class Container
BASE_URL = "http://192.168.178.53:5000" BASE_URL = CodeOcean::Config.new(:code_ocean).read[:container_management][:url]
def initialize(execution_environment, time_limit = nil) def initialize(execution_environment, time_limit = nil)
url = "#{BASE_URL}/execution-environments/#{execution_environment.id}/containers/create" url = "#{BASE_URL}/execution-environments/#{execution_environment.id}/containers/create"
@ -40,7 +40,6 @@ class Container
def execute_interactively(command) def execute_interactively(command)
websocket_url = execute_command(command)[:websocket_url] websocket_url = execute_command(command)[:websocket_url]
EventMachine.run do EventMachine.run do
#socket = Faye::WebSocket::Client.new(websocket_url, [], ping: 0.1)
socket = ContainerConnection.new(websocket_url) socket = ContainerConnection.new(websocket_url)
yield(self, socket) if block_given? yield(self, socket) if block_given?
end end

View File

@ -1,16 +1,18 @@
require 'faye/websocket/client' require 'faye/websocket/client'
class ContainerConnection class ContainerConnection
EVENTS = %i[start message exit stdout stderr].freeze EVENTS = %i[start output exit stdout stderr].freeze
def initialize(url) def initialize(url)
@socket = Faye::WebSocket::Client.new(url, [], ping: 0.1) @socket = Faye::WebSocket::Client.new(url, [], ping: 0.1)
%i[open message error close].each do |event_type| %i[open message error close].each do |event_type|
@socket.on event_type, &:"on_#{event_type}" @socket.on event_type do |event| __send__(:"on_#{event_type}", event) end
end end
EVENTS.each { |event_type| instance_variable_set(:"@#{event_type}_callback", lambda {}) } EVENTS.each { |event_type| instance_variable_set(:"@#{event_type}_callback", lambda {|e|}) }
@start_callback = lambda {}
@exit_code = 0
end end
def on(event, &block) def on(event, &block)
@ -20,26 +22,30 @@ class ContainerConnection
end end
def send(data) def send(data)
@socket.send(data) @socket.send(encode(data))
end end
private private
def parse(event) def decode(event)
JSON.parse(event.data).deep_symbolize_keys JSON.parse(event).deep_symbolize_keys
end
def encode(data)
data
end end
def on_message(event) def on_message(event)
event = parse(event) event = decode(event.data)
case event[:type] case event[:type].to_sym
when :exit_code when :exit_code
@exit_code = event[:data] @exit_code = event[:data]
when :stderr when :stderr
@stderr_callback.call event[:data] @stderr_callback.call event[:data]
@message_callback.call event[:data] @output_callback.call event[:data]
when :stdout when :stdout
@stdout_callback.call event[:data] @stdout_callback.call event[:data]
@message_callback.call event[:data] @output_callback.call event[:data]
else else
:error :error
end end