Fixed submission-scoring.
Added parsing of exit cmd.
This commit is contained in:
@ -93,20 +93,40 @@ class SubmissionsController < ApplicationController
|
|||||||
Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
|
Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
|
||||||
|
|
||||||
result = @docker_client.execute_run_command(@submission, params[:filename])
|
result = @docker_client.execute_run_command(@submission, params[:filename])
|
||||||
socket = result[:socket]
|
|
||||||
|
|
||||||
socket.on :message do |event|
|
if result[:status] == :container_depleted
|
||||||
Rails.logger.info("Docker sending: " + event.data)
|
tubesock.send_data JSON.dump({'cmd' => 'container_depleted'})
|
||||||
handle_message(event.data, tubesock)
|
|
||||||
end
|
|
||||||
|
|
||||||
socket.on :close do |event|
|
|
||||||
kill_socket(tubesock)
|
kill_socket(tubesock)
|
||||||
end
|
end
|
||||||
|
|
||||||
tubesock.onmessage do |data|
|
if result[:status] == :container_running
|
||||||
Rails.logger.info("Client sending: " + data)
|
socket = result[:socket]
|
||||||
socket.send data
|
|
||||||
|
socket.on :message do |event|
|
||||||
|
Rails.logger.info("Docker sending: " + event.data)
|
||||||
|
handle_message(event.data, tubesock)
|
||||||
|
end
|
||||||
|
|
||||||
|
socket.on :close do |event|
|
||||||
|
kill_socket(tubesock)
|
||||||
|
end
|
||||||
|
|
||||||
|
tubesock.onmessage do |data|
|
||||||
|
Rails.logger.info("Client sending: " + data)
|
||||||
|
# Check wether the client send a JSON command and kill container
|
||||||
|
# if the command is 'exit', send it to docker otherwise.
|
||||||
|
begin
|
||||||
|
parsed = JSON.parse(data)
|
||||||
|
if parsed['cmd'] == 'exit'
|
||||||
|
Rails.logger.info("Client killed container.")
|
||||||
|
@docker_client.kill_container(result[:container])
|
||||||
|
else
|
||||||
|
socket.send data
|
||||||
|
end
|
||||||
|
rescue JSON::ParserError
|
||||||
|
socket.send data
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -134,6 +154,7 @@ class SubmissionsController < ApplicationController
|
|||||||
parsed = JSON.parse(message)
|
parsed = JSON.parse(message)
|
||||||
socket.send_data message
|
socket.send_data message
|
||||||
rescue JSON::ParserError => e
|
rescue JSON::ParserError => e
|
||||||
|
# Check wether the message contains multiple lines, if true try to parse each line
|
||||||
if ((recursive == true) && (message.include? "\n"))
|
if ((recursive == true) && (message.include? "\n"))
|
||||||
for part in message.split("\n")
|
for part in message.split("\n")
|
||||||
self.parse_message(part,output_stream,socket,false)
|
self.parse_message(part,output_stream,socket,false)
|
||||||
|
@ -155,7 +155,7 @@ class DockerClient
|
|||||||
@socket ||= create_socket(@container)
|
@socket ||= create_socket(@container)
|
||||||
# Newline required to flush
|
# Newline required to flush
|
||||||
@socket.send command + "\n"
|
@socket.send command + "\n"
|
||||||
{status: :container_running, socket: @socket}
|
{status: :container_running, socket: @socket, container: @container}
|
||||||
else
|
else
|
||||||
{status: :container_depleted}
|
{status: :container_depleted}
|
||||||
end
|
end
|
||||||
@ -170,22 +170,25 @@ class DockerClient
|
|||||||
timeout = @execution_environment.permitted_execution_time.to_i # seconds
|
timeout = @execution_environment.permitted_execution_time.to_i # seconds
|
||||||
sleep(timeout)
|
sleep(timeout)
|
||||||
Rails.logger.info("Killing container after timeout of " + timeout.to_s + " seconds.")
|
Rails.logger.info("Killing container after timeout of " + timeout.to_s + " seconds.")
|
||||||
# if we use pooling and recylce the containers, put it back. otherwise, destroy it.
|
kill_container(container)
|
||||||
# (DockerContainerPool.config[:active] && RECYCLE_CONTAINERS) ? self.class.return_container(container, @execution_environment) : self.class.destroy_container(container)
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# todo won't this always create a new container?
|
def kill_container(container)
|
||||||
# remove container from pool, then destroy it
|
|
||||||
(DockerContainerPool.config[:active]) ? DockerContainerPool.remove_from_all_containers(container, @execution_environment) :
|
|
||||||
|
|
||||||
# destroy container
|
# todo won't this always create a new container?
|
||||||
self.class.destroy_container(container)
|
# It does, because it's impossible to determine wether a programm is still running or not while using ws to run.
|
||||||
|
# remove container from pool, then destroy it
|
||||||
|
(DockerContainerPool.config[:active]) ? DockerContainerPool.remove_from_all_containers(container, @execution_environment) :
|
||||||
|
|
||||||
# if we recylce containers, we start a fresh one
|
#destroy container
|
||||||
if(DockerContainerPool.config[:active] && RECYCLE_CONTAINERS)
|
self.class.destroy_container(container)
|
||||||
# create new container and add it to @all_containers and @containers.
|
|
||||||
container = self.class.create_container(@execution_environment)
|
# if we recylce containers, we start a fresh one
|
||||||
DockerContainerPool.add_to_all_containers(container, @execution_environment)
|
if(DockerContainerPool.config[:active] && RECYCLE_CONTAINERS)
|
||||||
end
|
# create new container and add it to @all_containers and @containers.
|
||||||
|
container = self.class.create_container(@execution_environment)
|
||||||
|
DockerContainerPool.add_to_all_containers(container, @execution_environment)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -198,7 +201,7 @@ class DockerClient
|
|||||||
execute_websocket_command(command, create_workspace_files, block)
|
execute_websocket_command(command, create_workspace_files, block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute_test_command(subbmission, filename, &block)
|
def execute_test_command(submission, filename, &block)
|
||||||
"""
|
"""
|
||||||
Stick to existing Docker API with exec command.
|
Stick to existing Docker API with exec command.
|
||||||
"""
|
"""
|
||||||
|
@ -5,7 +5,6 @@ ENV LANG en_US.UTF-8
|
|||||||
ADD webpython.py /usr/lib/python3.4/webpython.py
|
ADD webpython.py /usr/lib/python3.4/webpython.py
|
||||||
RUN rm /usr/lib/python3.4/turtle.py
|
RUN rm /usr/lib/python3.4/turtle.py
|
||||||
ADD turtle.py /usr/lib/python3.4/turtle.py
|
ADD turtle.py /usr/lib/python3.4/turtle.py
|
||||||
ADD assess /usr/lib/python3.4/assess
|
|
||||||
RUN adduser --disabled-password --gecos Python python
|
RUN adduser --disabled-password --gecos Python python
|
||||||
USER python
|
USER python
|
||||||
WORKDIR /home/python
|
WORKDIR /home/python
|
||||||
|
Reference in New Issue
Block a user