Integrate new API with websocket (run only)
Co-authored-by: Felix Auringer <felix.auringer@student.hpi.uni-potsdam.de>
This commit is contained in:

committed by
Sebastian Serth

parent
2e2cd1855e
commit
3cf70a33d8
@ -138,79 +138,60 @@ class SubmissionsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
# TODO: reimplement SSEs with websocket commands
|
Thread.new do
|
||||||
# with_server_sent_events do |server_sent_event|
|
hijack do |tubesock|
|
||||||
# output = @docker_client.execute_run_command(@submission, sanitize_filename)
|
if @embed_options[:disable_run]
|
||||||
|
|
||||||
# server_sent_event.write({stdout: output[:stdout]}, event: 'output') if output[:stdout]
|
|
||||||
# server_sent_event.write({stderr: output[:stderr]}, event: 'output') if output[:stderr]
|
|
||||||
# end
|
|
||||||
|
|
||||||
hijack do |tubesock|
|
|
||||||
if @embed_options[:disable_run]
|
|
||||||
kill_socket(tubesock)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
# probably add:
|
|
||||||
# ensure
|
|
||||||
# #guarantee that the thread is releasing the DB connection after it is done
|
|
||||||
# ApplicationRecord.connectionpool.releaseconnection
|
|
||||||
# end
|
|
||||||
unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
|
|
||||||
Thread.new do
|
|
||||||
EventMachine.run
|
|
||||||
ensure
|
|
||||||
ActiveRecord::Base.connection_pool.release_connection
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# socket is the socket into the container, tubesock is the socket to the client
|
|
||||||
|
|
||||||
# give the docker_client the tubesock object, so that it can send messages (timeout)
|
|
||||||
# @docker_client.tubesock = tubesock
|
|
||||||
|
|
||||||
container_request_time = Time.zone.now
|
|
||||||
# result = @docker_client.execute_run_command(@submission, sanitize_filename)
|
|
||||||
container = @submission.run(sanitize_filename)
|
|
||||||
tubesock.send_data JSON.dump({'cmd' => 'status', 'status' => :container_running})
|
|
||||||
@waiting_for_container_time = Time.zone.now - container_request_time
|
|
||||||
|
|
||||||
socket = container.socket
|
|
||||||
socket.on :message do |event|
|
|
||||||
Rails.logger.info("#{Time.zone.now.getutc}: Docker sending: #{event.data}")
|
|
||||||
handle_message(event.data, tubesock, container)
|
|
||||||
end
|
|
||||||
|
|
||||||
socket.on :close do |_event|
|
|
||||||
kill_socket(tubesock)
|
kill_socket(tubesock)
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
EventMachine.run do
|
||||||
|
container_request_time = Time.zone.now
|
||||||
|
@submission.run(sanitize_filename) do |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
|
||||||
|
|
||||||
tubesock.onmessage do |data|
|
socket.on :message do |event|
|
||||||
Rails.logger.info("#{Time.zone.now.getutc}: Client sending: #{data}")
|
Rails.logger.info("#{Time.zone.now.getutc}: Docker sending: #{event.data}")
|
||||||
# Check whether the client send a JSON command and kill container
|
handle_message(event.data, tubesock)
|
||||||
# if the command is 'client_kill', send it to docker otherwise.
|
end
|
||||||
begin
|
|
||||||
|
|
||||||
parsed = JSON.parse(data) unless data == "\n"
|
socket.on :close do |_event|
|
||||||
if parsed.instance_of?(Hash) && parsed['cmd'] == 'client_kill'
|
EventMachine.stop_event_loop
|
||||||
Rails.logger.debug("Client exited container.")
|
kill_socket(tubesock)
|
||||||
container.destroy
|
end
|
||||||
else
|
|
||||||
socket.send data
|
tubesock.onmessage do |data|
|
||||||
Rails.logger.debug { "Sent the received client data to docker:#{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
|
||||||
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
|
||||||
|
|
||||||
# Send command after all listeners are attached.
|
|
||||||
# Newline required to flush
|
|
||||||
@execution_request_time = Time.zone.now
|
|
||||||
end
|
end
|
||||||
|
# unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
|
||||||
|
# Thread.new do
|
||||||
|
# EventMachine.run
|
||||||
|
# ensure
|
||||||
|
# ActiveRecord::Base.connection_pool.release_connection
|
||||||
|
# end
|
||||||
|
# end
|
||||||
end
|
end
|
||||||
|
|
||||||
def kill_socket(tubesock)
|
def kill_socket(tubesock)
|
||||||
@ -235,7 +216,7 @@ class SubmissionsController < ApplicationController
|
|||||||
tubesock.close
|
tubesock.close
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_message(message, tubesock, container)
|
def handle_message(message, tubesock)
|
||||||
@raw_output ||= ''
|
@raw_output ||= ''
|
||||||
@run_output ||= ''
|
@run_output ||= ''
|
||||||
# Handle special commands first
|
# Handle special commands first
|
||||||
@ -245,7 +226,7 @@ class SubmissionsController < ApplicationController
|
|||||||
# Do not call kill_socket for the websocket to the client here.
|
# Do not call kill_socket for the websocket to the client here.
|
||||||
# @docker_client.exit_container closes the socket to the container,
|
# @docker_client.exit_container closes the socket to the container,
|
||||||
# kill_socket is called in the "on close handler" of the websocket to the container
|
# kill_socket is called in the "on close handler" of the websocket to the container
|
||||||
container.destroy
|
# container.destroy
|
||||||
when /^#timeout/
|
when /^#timeout/
|
||||||
@run_output = "timeout: #{@run_output}" # add information that this run timed out to the buffer
|
@run_output = "timeout: #{@run_output}" # add information that this run timed out to the buffer
|
||||||
else
|
else
|
||||||
@ -257,7 +238,7 @@ class SubmissionsController < ApplicationController
|
|||||||
test_command = run_command
|
test_command = run_command
|
||||||
end
|
end
|
||||||
unless %r{root@|:/workspace|#{run_command}|#{test_command}|bash: cmd:canvasevent: command not found}.match?(message)
|
unless %r{root@|:/workspace|#{run_command}|#{test_command}|bash: cmd:canvasevent: command not found}.match?(message)
|
||||||
parse_message(message, 'stdout', tubesock, container)
|
parse_message(message, 'stdout', tubesock)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -269,7 +250,7 @@ class SubmissionsController < ApplicationController
|
|||||||
if parsed.instance_of?(Hash) && parsed.key?('cmd')
|
if parsed.instance_of?(Hash) && parsed.key?('cmd')
|
||||||
socket.send_data message
|
socket.send_data message
|
||||||
Rails.logger.info("parse_message sent: #{message}")
|
Rails.logger.info("parse_message sent: #{message}")
|
||||||
container.destroy if container && parsed['cmd'] == 'exit'
|
# container.destroy if container && parsed['cmd'] == 'exit'
|
||||||
else
|
else
|
||||||
parsed = {'cmd' => 'write', 'stream' => output_stream, 'data' => message}
|
parsed = {'cmd' => 'write', 'stream' => output_stream, 'data' => message}
|
||||||
socket.send_data JSON.dump(parsed)
|
socket.send_data JSON.dump(parsed)
|
||||||
|
@ -146,10 +146,12 @@ class Submission < ApplicationRecord
|
|||||||
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
|
container = run_command_with_self run_command
|
||||||
container
|
container
|
||||||
|
yield(container.socket) if block_given?
|
||||||
|
container.destroy
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_command_with_self(command)
|
def run_command_with_self(command)
|
||||||
container = Container.new(execution_environment)
|
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_command_interactively(command)
|
||||||
container
|
container
|
||||||
|
@ -5,17 +5,21 @@ class Container
|
|||||||
|
|
||||||
attr_accessor :socket
|
attr_accessor :socket
|
||||||
|
|
||||||
def initialize(execution_environment)
|
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"
|
||||||
response = Faraday.post url
|
body = {}
|
||||||
|
if time_limit
|
||||||
|
body[:time_limit] = time_limit
|
||||||
|
end
|
||||||
|
response = Faraday.post(url, body.to_json, "Content-Type" => "application/json")
|
||||||
response = parse response
|
response = parse response
|
||||||
@container_id = response[:id]
|
@container_id = response[:id]
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_files(files)
|
def copy_files(files)
|
||||||
url = container_url + "/files"
|
url = container_url + "/files"
|
||||||
payload = files.map{ |filename, content| { filename => content } }
|
body = files.map{ |filename, content| { filename => content } }
|
||||||
Faraday.post(url, payload.to_json)
|
Faraday.post(url, body.to_json, "Content-Type" => "application/json")
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_submission_files(submission)
|
def copy_submission_files(submission)
|
||||||
@ -35,7 +39,8 @@ class Container
|
|||||||
|
|
||||||
def execute_command_interactively(command)
|
def execute_command_interactively(command)
|
||||||
websocket_url = execute_command(command)[:websocket_url]
|
websocket_url = execute_command(command)[:websocket_url]
|
||||||
@socket = Faye::WebSocket::Client.new websocket_url
|
@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
|
||||||
|
Reference in New Issue
Block a user