From 6ff3d3680957d233e9de45bc5a3226a58898c2fc Mon Sep 17 00:00:00 2001 From: Alexander Kastius Date: Fri, 9 Sep 2016 17:06:30 +0200 Subject: [PATCH] Fixed container-ending on exit. --- app/controllers/submissions_controller.rb | 5 +++-- lib/docker_client.rb | 12 +++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index 7ff99437..c272b177 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -129,7 +129,7 @@ class SubmissionsController < ApplicationController socket.on :message do |event| Rails.logger.info( Time.now.getutc.to_s + ": Docker sending: " + event.data) - handle_message(event.data, tubesock) + handle_message(event.data, tubesock, result[:container]) end socket.on :close do |event| @@ -171,10 +171,11 @@ class SubmissionsController < ApplicationController tubesock.close end - def handle_message(message, tubesock) + def handle_message(message, tubesock, container) # Handle special commands first if (/^exit/.match(message)) kill_socket(tubesock) + @docker_client.exit_container(container) else # Filter out information about run_command, test_command, user or working directory run_command = @submission.execution_environment.run_command % command_substitutions(params[:filename]) diff --git a/lib/docker_client.rb b/lib/docker_client.rb index f993c37c..6bcf7392 100644 --- a/lib/docker_client.rb +++ b/lib/docker_client.rb @@ -261,16 +261,21 @@ class DockerClient end end - def exit_container(container) - Rails.logger.debug('exiting container ' + container.to_s) - # exit the timeout thread if it is still alive + def exit_thread_if_alive if(@thread && @thread.alive?) @thread.exit end + end + + def exit_container(container) + Rails.logger.debug('exiting container ' + container.to_s) + # exit the timeout thread if it is still alive + exit_thread_if_alive # if we use pooling and recylce the containers, put it back. otherwise, destroy it. (DockerContainerPool.config[:active] && RECYCLE_CONTAINERS) ? self.class.return_container(container, @execution_environment) : self.class.destroy_container(container) end + def kill_container(container) Rails.logger.info('killing container ' + container.to_s) # remove container from pool, then destroy it @@ -286,6 +291,7 @@ class DockerClient container = self.class.create_container(@execution_environment) DockerContainerPool.add_to_all_containers(container, @execution_environment) end + exit_thread_if_alive end def execute_run_command(submission, filename, &block)