Remove 'previous' DockerContainerPool implementation

This commit is contained in:
Sebastian Serth
2021-10-29 20:05:28 +02:00
parent 9d833e37b3
commit 2551ea709b
4 changed files with 12 additions and 121 deletions

View File

@ -119,13 +119,11 @@ class DockerClient
container.start_time = Time.zone.now
container.status = :created
container.execution_environment = execution_environment
container.re_use = true
container.docker_client = new(execution_environment: execution_environment)
Thread.new do
timeout = Random.rand(MINIMUM_CONTAINER_LIFETIME..MAXIMUM_CONTAINER_LIFETIME) # seconds
sleep(timeout)
container.re_use = false
if container.status == :executing
Thread.new do
timeout = SELF_DESTROY_GRACE_PERIOD.to_i
@ -227,7 +225,7 @@ class DockerClient
Rails.logger.info("destroying container #{container}")
# Checks only if container assignment is not nil and not whether the container itself is still present.
if container && !DockerContainerPool.active?
if container
container.kill
container.port_bindings.each_value {|port| PortPool.release(port) }
begin
@ -240,8 +238,6 @@ class DockerClient
# Checks only if container assignment is not nil and not whether the container itself is still present.
container&.delete(force: true, v: true)
elsif container
DockerContainerPool.destroy_container(container)
end
rescue Docker::Error::NotFoundError => e
Rails.logger.error("destroy_container: Rescued from Docker::Error::NotFoundError: #{e}")
@ -261,7 +257,7 @@ class DockerClient
def execute_command(command, before_execution_block, output_consuming_block)
# tries ||= 0
container_request_time = Time.zone.now
@container = DockerContainerPool.get_container(@execution_environment)
@container = self.class.create_container(@execution_environment)
waiting_for_container_time = Time.zone.now - container_request_time
if @container
@container.status = :executing
@ -285,7 +281,7 @@ container_execution_time: nil}
# called when the user clicks the "Run" button
def open_websocket_connection(command, before_execution_block, _output_consuming_block)
@container = DockerContainerPool.get_container(@execution_environment)
@container = self.class.create_container(@execution_environment)
if @container
@container.status = :executing
# do not use try here, directly call the passed proc and rescue from the error in order to log the problem.
@ -351,13 +347,7 @@ container_execution_time: nil}
# exit the timeout thread if it is still alive
exit_thread_if_alive
@socket.close
# if we use pooling and recylce the containers, put it back. otherwise, destroy it.
if DockerContainerPool.active? && RECYCLE_CONTAINERS
self.class.return_container(container,
@execution_environment)
else
self.class.destroy_container(container)
end
self.class.destroy_container(container)
end
def kill_container(container)
@ -413,7 +403,6 @@ container_execution_time: nil}
end
def self.initialize_environment
# TODO: Move to DockerContainerPool
raise Error.new('Docker configuration missing!') unless config[:connection_timeout] && config[:workspace_root]
Docker.url = config[:host] if config[:host]
@ -455,21 +444,6 @@ container_execution_time: nil}
`docker pull #{docker_image}` if docker_image
end
def self.return_container(container, execution_environment)
Rails.logger.debug { "returning container #{container}" }
begin
clean_container_workspace(container)
rescue Docker::Error::NotFoundError => e
# FIXME: Create new container?
Rails.logger.info("return_container: Rescued from Docker::Error::NotFoundError: #{e}")
Rails.logger.info('Nothing is done here additionally. The container will be exchanged upon its next retrieval.')
end
DockerContainerPool.return_container(container, execution_environment)
container.status = :available
end
# private :return_container
def send_command(command, container)
result = {status: :failed, stdout: '', stderr: ''}
output = nil
@ -489,12 +463,7 @@ container_execution_time: nil}
result = {status: (output[2])&.zero? ? :ok : :failed, stdout: output[0].join.force_encoding('utf-8'), stderr: output[1].join.force_encoding('utf-8')}
end
# if we use pooling and recylce the containers, put it back. otherwise, destroy it.
if DockerContainerPool.active? && RECYCLE_CONTAINERS
self.class.return_container(container, @execution_environment)
else
self.class.destroy_container(container)
end
self.class.destroy_container(container)
result
rescue Timeout::Error
Rails.logger.info("got timeout error for container #{container}")