From 943d5554d995cce28b117d95920da840ba850b91 Mon Sep 17 00:00:00 2001 From: Tom Staubitz Date: Sat, 12 Dec 2015 00:41:26 +0800 Subject: [PATCH] Remove @all_containers instead of @containers during cleanup Also added some comments and todos Also fixed debug messages in remove_from_all_containers Also refactored duplicated code with extracted function Also removed some commented lines of code --- .../javascripts/{editor.js => editor.js.erb} | 9 ++++-- lib/docker_container_pool.rb | 29 ++++++++++--------- 2 files changed, 22 insertions(+), 16 deletions(-) rename app/assets/javascripts/{editor.js => editor.js.erb} (98%) diff --git a/app/assets/javascripts/editor.js b/app/assets/javascripts/editor.js.erb similarity index 98% rename from app/assets/javascripts/editor.js rename to app/assets/javascripts/editor.js.erb index 4c9b834e..f4d728be 100644 --- a/app/assets/javascripts/editor.js +++ b/app/assets/javascripts/editor.js.erb @@ -168,7 +168,8 @@ $(function() { } } } - setAnnotations(editors[i], $(editors[i].container).data('id')); + // setAnnotations(editors[i], $(editors[i].container).data('id')); + console.log('location: #{Time.now}' + window.location.port); } // toggle button states (it might be the case that the request for comments button has to be enabled toggleButtonStates(); @@ -419,7 +420,7 @@ $(function() { session.setUseWrapMode(true); var file_id = $(element).data('id'); - setAnnotations(editor, file_id); + //setAnnotations(editor, file_id); session.on('annotationRemoval', handleAnnotationRemoval); session.on('annotationChange', handleAnnotationChange); @@ -1118,7 +1119,9 @@ $(function() { }; var initWebsocketConnection = function(url) { - websocket = new WebSocket('wss://' + window.location.hostname + ':' + window.location.port + url); + //TODO: get the protocol from config file dependent on environment. (dev: ws, prod: wss) + //causes: Puma::HttpParserError: Invalid HTTP format, parsing fails. + websocket = new WebSocket('ws://' + window.location.hostname + ':' + window.location.port + url); websocket.onopen = function(evt) { resetOutputTab(); }; // todo show some kind of indicator for established connection websocket.onclose = function(evt) { /* expected at some point */ }; websocket.onmessage = function(evt) { parseCanvasMessage(evt.data, true); }; diff --git a/lib/docker_container_pool.rb b/lib/docker_container_pool.rb index 1d02876f..90357875 100644 --- a/lib/docker_container_pool.rb +++ b/lib/docker_container_pool.rb @@ -9,12 +9,13 @@ class DockerContainerPool @all_containers = ThreadSafe::Hash[ExecutionEnvironment.all.map { |execution_environment| [execution_environment.id, ThreadSafe::Array.new] }] def self.clean_up @refill_task.try(:shutdown) - @containers.values.each do |containers| + @all_containers.values.each do |containers| DockerClient.destroy_container(containers.shift) until containers.empty? end end def self.config + #TODO: Why erb? @config ||= CodeOcean::Config.new(:docker).read(erb: true)[:pool] end @@ -28,9 +29,11 @@ class DockerContainerPool def self.remove_from_all_containers(container, execution_environment) @all_containers[execution_environment.id]-=[container] + Rails.logger.debug('Removed container ' + container.to_s + ' from all_pool for execution environment ' + execution_environment.to_s + '. Remaining containers in all_pool ' + @all_containers[execution_environment.id].size.to_s) + if(@containers[execution_environment.id].include?(container)) @containers[execution_environment.id]-=[container] - Rails.logger.debug('Removed container ' + container.to_s + ' from all_pool for execution environment ' + execution_environment.to_s + '. Remaining containers in all_pool ' + @all_containers[execution_environment.id].size.to_s) + Rails.logger.debug('Removed container ' + container.to_s + ' from available_pool for execution environment ' + execution_environment.to_s + '. Remaining containers in available_pool ' + @containers[execution_environment.id].size.to_s) end end @@ -75,17 +78,12 @@ class DockerContainerPool Rails.logger.debug('get_container all container count: ' + @all_containers[execution_environment.id].size.to_s) else Rails.logger.error('docker_container_pool.get_container retrieved a container not running. Container will be removed from list: ' + container.to_s) - remove_from_all_containers(container, execution_environment) - Rails.logger.error('Creating a new container and returning that.') - container = create_container(execution_environment) - DockerContainerPool.add_to_all_containers(container, execution_environment) + #TODO: check in which state the container actually is and treat it accordingly (dead,... => destroy?) + container = replace_broken_container(container, execution_environment) end rescue Docker::Error::NotFoundError => error Rails.logger.error('docker_container_pool.get_container rescued from Docker::Error::NotFoundError. Most likely, the container is not there any longer. Removing faulty entry from list: ' + container.to_s) - remove_from_all_containers(container, execution_environment) - Rails.logger.error('Creating a new container and returning that.') - container = create_container(execution_environment) - DockerContainerPool.add_to_all_containers(container, execution_environment) + container = replace_broken_container(container, execution_environment) end end # returning nil is no problem. then the pool is just depleted. @@ -95,6 +93,14 @@ class DockerContainerPool end end + def self.replace_broken_container(container, execution_environment) + remove_from_all_containers(container, execution_environment) + Rails.logger.error('Creating a new container and returning that.') + new_container = create_container(execution_environment) + DockerContainerPool.add_to_all_containers(new_container, execution_environment) + new_container + end + def self.quantities @containers.map { |key, value| [key, value.length] }.to_h end @@ -115,14 +121,11 @@ class DockerContainerPool Rails.logger.info('Adding ' + refill_count.to_s + ' containers for execution_environment ' + execution_environment.name ) c = refill_count.times.map { create_container(execution_environment) } Rails.logger.info('Created containers: ' + c.to_s ) - #c.each { |container| return_container(container, execution_environment) } @containers[execution_environment.id] += c @all_containers[execution_environment.id] += c Rails.logger.debug('@containers for ' + execution_environment.name.to_s + ' (' + @containers.object_id.to_s + ') has the following content: '+ @containers[execution_environment.id].to_s) Rails.logger.debug('@all_containers for ' + execution_environment.name.to_s + ' (' + @all_containers.object_id.to_s + ') has the following content: ' + @all_containers[execution_environment.id].to_s) - #refill_count.times.map { create_container(execution_environment) } end - end def self.start_refill_task