added option to recycle Docker containers

This commit is contained in:
Hauke Klement
2015-04-14 19:59:11 +02:00
parent 91ac7e8e37
commit b46055eca3

View File

@ -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