Merge branch 'webpython-hybrid' of ssh://github.com/openHPI/codeocean into webpython-hybrid
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -13,3 +13,4 @@
|
|||||||
/tmp
|
/tmp
|
||||||
/vagrant/
|
/vagrant/
|
||||||
*.sublime-*
|
*.sublime-*
|
||||||
|
/.idea
|
@ -93,6 +93,13 @@ 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])
|
||||||
|
|
||||||
|
if result[:status] == :container_depleted
|
||||||
|
tubesock.send_data JSON.dump({'cmd' => 'container_depleted'})
|
||||||
|
kill_socket(tubesock)
|
||||||
|
end
|
||||||
|
|
||||||
|
if result[:status] == :container_running
|
||||||
socket = result[:socket]
|
socket = result[:socket]
|
||||||
|
|
||||||
socket.on :message do |event|
|
socket.on :message do |event|
|
||||||
@ -106,8 +113,21 @@ class SubmissionsController < ApplicationController
|
|||||||
|
|
||||||
tubesock.onmessage do |data|
|
tubesock.onmessage do |data|
|
||||||
Rails.logger.info("Client sending: " + 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
|
socket.send data
|
||||||
end
|
end
|
||||||
|
rescue JSON::ParserError
|
||||||
|
socket.send data
|
||||||
|
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,14 +170,18 @@ 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
|
||||||
|
|
||||||
|
def kill_container(container)
|
||||||
|
|
||||||
# todo won't this always create a new container?
|
# todo won't this always create a new 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
|
# remove container from pool, then destroy it
|
||||||
(DockerContainerPool.config[:active]) ? DockerContainerPool.remove_from_all_containers(container, @execution_environment) :
|
(DockerContainerPool.config[:active]) ? DockerContainerPool.remove_from_all_containers(container, @execution_environment) :
|
||||||
|
|
||||||
# destroy container
|
#destroy container
|
||||||
self.class.destroy_container(container)
|
self.class.destroy_container(container)
|
||||||
|
|
||||||
# if we recylce containers, we start a fresh one
|
# if we recylce containers, we start a fresh one
|
||||||
@ -187,7 +191,6 @@ class DockerClient
|
|||||||
DockerContainerPool.add_to_all_containers(container, @execution_environment)
|
DockerContainerPool.add_to_all_containers(container, @execution_environment)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def execute_run_command(submission, filename, &block)
|
def execute_run_command(submission, filename, &block)
|
||||||
"""
|
"""
|
||||||
|
Reference in New Issue
Block a user