Fix socket handling and add configuration option
Co-authored-by: Felix Auringer <felix.auringer@student.hpi.uni-potsdam.de>
This commit is contained in:

committed by
Sebastian Serth

parent
92b249e7b3
commit
6a4e302f4e
@ -142,7 +142,7 @@ class SubmissionsController < ApplicationController
|
||||
@waiting_for_container_time = Time.zone.now - @container_request_time
|
||||
@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}")
|
||||
handle_message(data, tubesock)
|
||||
end
|
||||
|
@ -146,23 +146,23 @@ class Submission < ApplicationRecord
|
||||
score_command = command_for execution_environment.test_command, file.name_with_extension
|
||||
stdout = ""
|
||||
stderr = ""
|
||||
exit_code = 0
|
||||
exit_code = 1 # default to error
|
||||
container.execute_interactively(score_command) do |container, socket|
|
||||
socket.on :stderr do
|
||||
|data| stderr << data
|
||||
socket.on :stderr do |data|
|
||||
stderr << data
|
||||
end
|
||||
socket.on :stdout do
|
||||
|data| stdout << data
|
||||
socket.on :stdout do |data|
|
||||
stdout << data
|
||||
end
|
||||
socket.on :close do |_exit_code|
|
||||
socket.on :exit do |_exit_code|
|
||||
exit_code = _exit_code
|
||||
EventMachine.stop_event_loop
|
||||
end
|
||||
end
|
||||
output = {
|
||||
file_role: file.role,
|
||||
waiting_for_container_time: 1.second, # TODO
|
||||
container_execution_time: 1.second, # TODO
|
||||
waiting_for_container_time: 1, # TODO
|
||||
container_execution_time: 1, # TODO
|
||||
status: (exit_code == 0) ? :ok : :failed,
|
||||
stdout: stdout,
|
||||
stderr: stderr,
|
||||
|
@ -3,7 +3,7 @@
|
||||
require 'container_connection'
|
||||
|
||||
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)
|
||||
url = "#{BASE_URL}/execution-environments/#{execution_environment.id}/containers/create"
|
||||
@ -40,7 +40,6 @@ class Container
|
||||
def execute_interactively(command)
|
||||
websocket_url = execute_command(command)[:websocket_url]
|
||||
EventMachine.run do
|
||||
#socket = Faye::WebSocket::Client.new(websocket_url, [], ping: 0.1)
|
||||
socket = ContainerConnection.new(websocket_url)
|
||||
yield(self, socket) if block_given?
|
||||
end
|
||||
|
@ -1,16 +1,18 @@
|
||||
require 'faye/websocket/client'
|
||||
|
||||
class ContainerConnection
|
||||
EVENTS = %i[start message exit stdout stderr].freeze
|
||||
EVENTS = %i[start output exit stdout stderr].freeze
|
||||
|
||||
def initialize(url)
|
||||
@socket = Faye::WebSocket::Client.new(url, [], ping: 0.1)
|
||||
|
||||
%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
|
||||
|
||||
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
|
||||
|
||||
def on(event, &block)
|
||||
@ -20,26 +22,30 @@ class ContainerConnection
|
||||
end
|
||||
|
||||
def send(data)
|
||||
@socket.send(data)
|
||||
@socket.send(encode(data))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse(event)
|
||||
JSON.parse(event.data).deep_symbolize_keys
|
||||
def decode(event)
|
||||
JSON.parse(event).deep_symbolize_keys
|
||||
end
|
||||
|
||||
def encode(data)
|
||||
data
|
||||
end
|
||||
|
||||
def on_message(event)
|
||||
event = parse(event)
|
||||
case event[:type]
|
||||
event = decode(event.data)
|
||||
case event[:type].to_sym
|
||||
when :exit_code
|
||||
@exit_code = event[:data]
|
||||
when :stderr
|
||||
@stderr_callback.call event[:data]
|
||||
@message_callback.call event[:data]
|
||||
@output_callback.call event[:data]
|
||||
when :stdout
|
||||
@stdout_callback.call event[:data]
|
||||
@message_callback.call event[:data]
|
||||
@output_callback.call event[:data]
|
||||
else
|
||||
:error
|
||||
end
|
||||
|
Reference in New Issue
Block a user