Fix container pooling with websockets
This commit is contained in:
@ -93,6 +93,7 @@ class DockerClient
|
|||||||
FileUtils.mkdir(local_workspace_path)
|
FileUtils.mkdir(local_workspace_path)
|
||||||
container.start(container_start_options(execution_environment, local_workspace_path))
|
container.start(container_start_options(execution_environment, local_workspace_path))
|
||||||
container.start_time = Time.now
|
container.start_time = Time.now
|
||||||
|
container.status = :created
|
||||||
container
|
container
|
||||||
rescue Docker::Error::NotFoundError => error
|
rescue Docker::Error::NotFoundError => error
|
||||||
destroy_container(container)
|
destroy_container(container)
|
||||||
@ -135,6 +136,7 @@ class DockerClient
|
|||||||
def execute_command(command, before_execution_block, output_consuming_block)
|
def execute_command(command, before_execution_block, output_consuming_block)
|
||||||
#tries ||= 0
|
#tries ||= 0
|
||||||
@container = DockerContainerPool.get_container(@execution_environment)
|
@container = DockerContainerPool.get_container(@execution_environment)
|
||||||
|
@container.status = :executing
|
||||||
if @container
|
if @container
|
||||||
before_execution_block.try(:call)
|
before_execution_block.try(:call)
|
||||||
send_command(command, @container, &output_consuming_block)
|
send_command(command, @container, &output_consuming_block)
|
||||||
@ -149,6 +151,7 @@ class DockerClient
|
|||||||
|
|
||||||
def execute_websocket_command(command, before_execution_block, output_consuming_block)
|
def execute_websocket_command(command, before_execution_block, output_consuming_block)
|
||||||
@container = DockerContainerPool.get_container(@execution_environment)
|
@container = DockerContainerPool.get_container(@execution_environment)
|
||||||
|
@container.status = :executing
|
||||||
if @container
|
if @container
|
||||||
before_execution_block.try(:call)
|
before_execution_block.try(:call)
|
||||||
# todo catch exception if socket could not be created
|
# todo catch exception if socket could not be created
|
||||||
@ -169,21 +172,24 @@ class DockerClient
|
|||||||
Thread.new do
|
Thread.new do
|
||||||
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.")
|
if container.status != :returned
|
||||||
kill_container(container)
|
Rails.logger.info("Killing container after timeout of " + timeout.to_s + " seconds.")
|
||||||
|
kill_container(container)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def kill_container(container)
|
def exit_container(container)
|
||||||
"""
|
# if we use pooling and recylce the containers, put it back. otherwise, destroy it.
|
||||||
Please note that we cannot properly recycle containers when using
|
(DockerContainerPool.config[:active] && RECYCLE_CONTAINERS) ? self.class.return_container(container, @execution_environment) : self.class.destroy_container(container)
|
||||||
websockets because it is impossible to determine whether a program
|
end
|
||||||
is still running.
|
|
||||||
"""
|
def kill_container(container)
|
||||||
# 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) :
|
if (DockerContainerPool.config[:active])
|
||||||
|
DockerContainerPool.remove_from_all_containers(container, @execution_environment)
|
||||||
|
end
|
||||||
|
|
||||||
#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
|
||||||
@ -267,6 +273,7 @@ class DockerClient
|
|||||||
def self.return_container(container, execution_environment)
|
def self.return_container(container, execution_environment)
|
||||||
clean_container_workspace(container)
|
clean_container_workspace(container)
|
||||||
DockerContainerPool.return_container(container, execution_environment)
|
DockerContainerPool.return_container(container, execution_environment)
|
||||||
|
container.status = :returned
|
||||||
end
|
end
|
||||||
#private :return_container
|
#private :return_container
|
||||||
|
|
||||||
@ -285,7 +292,9 @@ class DockerClient
|
|||||||
Rails.logger.info('got timeout error for container ' + container.to_s)
|
Rails.logger.info('got timeout error for container ' + container.to_s)
|
||||||
|
|
||||||
# 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) :
|
if (DockerContainerPool.config[:active])
|
||||||
|
DockerContainerPool.remove_from_all_containers(container, @execution_environment)
|
||||||
|
end
|
||||||
|
|
||||||
# destroy container
|
# destroy container
|
||||||
self.class.destroy_container(container)
|
self.class.destroy_container(container)
|
||||||
|
Reference in New Issue
Block a user