From b46055eca3a27788681eb6bf974a39e34a47c9e2 Mon Sep 17 00:00:00 2001 From: Hauke Klement Date: Tue, 14 Apr 2015 19:59:11 +0200 Subject: [PATCH] added option to recycle Docker containers --- lib/docker_client.rb | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/lib/docker_client.rb b/lib/docker_client.rb index 62e84032..cae78534 100644 --- a/lib/docker_client.rb +++ b/lib/docker_client.rb @@ -5,6 +5,7 @@ class DockerClient DEFAULT_MEMORY_LIMIT = 256 LOCAL_WORKSPACE_ROOT = Rails.root.join('tmp', 'files', Rails.env) MINIMUM_MEMORY_LIMIT = 4 + RECYCLE_CONTAINERS = true RETRY_COUNT = 2 attr_reader :container @@ -155,19 +156,19 @@ class DockerClient `docker pull #{docker_image}` if docker_image end - #this sends the command to run whatever is defined in the backend + def return_container(container) + local_workspace_path = self.class.local_workspace_path(container) + FileUtils.rm_rf(local_workspace_path) if local_workspace_path + FileUtils.mkdir(local_workspace_path) + DockerContainerPool.return_container(container, @execution_environment) + end + private :return_container + def send_command(command, container, &block) Timeout.timeout(@execution_environment.permitted_execution_time.to_i) do stderr = [] stdout = [] - # map command in a shell call, maybe add -c - command = ['bash', '-c', command] - #command.join!(' ') - # lets call the command, but we do not want the container to stop afterwards - # thats why we use exec. If its ok do stop the container this could be assign instead - container.exec(command) do |stream, chunk| - #container.attach(stdin: StringIO.new(command)) do |stream, chunk| - + container.exec(['bash', '-c', command]) do |stream, chunk| block.call(stream, chunk) if block_given? if stream == :stderr stderr.push(chunk) @@ -180,16 +181,7 @@ class DockerClient rescue Timeout::Error {status: :timeout} ensure - Concurrent::Future.execute { - # If you do not want to reuse running container you could use: - #self.class.destroy_container(container) - # This could be moved to an execution environment specific setting - - # we may need to stop the exec call here..!!! - FileUtils.rm_rf(local_workspace_path(container)) if local_workspace_path(container) - FileUtils.mkdir(local_workspace_path) - DockerContainerPool.return_container(container, @execution_environment) - } + RECYCLE_CONTAINERS ? return_container(container) : self.class.destroy_container(container) end private :send_command