Merge pull request #1079 from openHPI/sync_execution_environments

Sync execution environments
This commit is contained in:
Sebastian Serth
2021-11-09 18:44:35 +01:00
committed by GitHub
11 changed files with 262 additions and 77 deletions

View File

@ -9,10 +9,18 @@ class Runner::Strategy
raise NotImplementedError
end
def self.environments
raise NotImplementedError
end
def self.sync_environment(_environment)
raise NotImplementedError
end
def self.remove_environment(_environment)
raise NotImplementedError
end
def self.request_from_management(_environment)
raise NotImplementedError
end

View File

@ -14,17 +14,40 @@ class Runner::Strategy::DockerContainerPool < Runner::Strategy
FileUtils.mkdir_p(File.expand_path(config[:workspace_root]))
end
def self.sync_environment(environment)
# There is no dedicated sync mechanism yet. However, we need to emit a warning when the pool was previously
# empty for this execution environment. In this case the validation command probably was not executed.
return true unless environment.pool_size_previously_changed?
def self.environments
pool_size.keys.map {|key| {id: key} }
end
case environment.pool_size_previously_was
when nil, 0
false
else
true
def self.sync_environment(environment)
# Force a database commit and start a new transaction.
if environment.class.connection.transaction_open?
environment.class.connection.commit_db_transaction
environment.class.connection.begin_db_transaction
end
url = "#{config[:url]}/docker_container_pool/refill_environment/#{environment.id}"
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Refilling execution environment at #{url}" }
response = Faraday.post(url)
return true if response.success?
raise Runner::Error::UnexpectedResponse.new("Could not refill execution environment in DockerContainerPool, got response: #{response.as_json}")
rescue Faraday::Error => e
raise Runner::Error::FaradayError.new("Request to DockerContainerPool failed: #{e.inspect}")
ensure
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Finished refilling environment" }
end
def self.remove_environment(environment)
url = "#{config[:url]}/docker_container_pool/purge_environment/#{environment.id}"
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Purging execution environment at #{url}" }
response = Faraday.delete(url)
return true if response.success?
raise Runner::Error::UnexpectedResponse.new("Could not delete execution environment in DockerContainerPool, got response: #{response.as_json}")
rescue Faraday::Error => e
raise Runner::Error::FaradayError.new("Request to DockerContainerPool failed: #{e.inspect}")
ensure
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Finished purging environment" }
end
def self.request_from_management(environment)
@ -49,7 +72,10 @@ class Runner::Strategy::DockerContainerPool < Runner::Strategy
def destroy_at_management
url = "#{self.class.config[:url]}/docker_container_pool/destroy_container/#{container.id}"
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Destroying runner at #{url}" }
Faraday.delete(url)
response = Faraday.delete(url)
return true if response.success?
raise Runner::Error::UnexpectedResponse.new("Could not delete execution environment in DockerContainerPool, got response: #{response.as_json}")
rescue Faraday::Error => e
raise Runner::Error::FaradayError.new("Request to DockerContainerPool failed: #{e.inspect}")
ensure

View File

@ -5,7 +5,17 @@
class Runner::Strategy::Null < Runner::Strategy
def self.initialize_environment; end
def self.sync_environment(_environment); end
def self.environments
raise Runner::Error.new
end
def self.sync_environment(_environment)
raise Runner::Error.new
end
def self.remove_environment(_environment)
raise Runner::Error.new
end
def self.request_from_management(_environment)
SecureRandom.uuid

View File

@ -19,16 +19,51 @@ class Runner::Strategy::Poseidon < Runner::Strategy
nil
end
def self.environments
url = "#{config[:url]}/execution-environments"
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Getting list of execution environments at #{url}" }
response = http_connection.get url
case response.status
when 200
response_body = parse response
execution_environments = response_body[:executionEnvironments]
execution_environments.presence || raise(Runner::Error::UnexpectedResponse.new("Could not get the list of execution environments in Poseidon, got response: #{response.as_json}"))
when 404
raise Runner::Error::EnvironmentNotFound.new
else
handle_error response
end
rescue Faraday::Error => e
raise Runner::Error::FaradayError.new("Could not get the list of execution environments because of Faraday error: #{e.inspect}")
ensure
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Finished getting the list of execution environments" }
end
def self.sync_environment(environment)
url = "#{config[:url]}/execution-environments/#{environment.id}"
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Synchronizing execution environment at #{url}" }
response = http_connection.put url, environment.to_json
return true if [201, 204].include? response.status
Rails.logger.warn("Could not create execution environment in Poseidon, got response: #{response.as_json}")
false
raise Runner::Error::UnexpectedResponse.new("Could not synchronize execution environment in Poseidon, got response: #{response.as_json}")
rescue Faraday::Error => e
Rails.logger.warn("Could not create execution environment because of Faraday error: #{e.inspect}")
false
raise Runner::Error::FaradayError.new("Could not synchronize execution environment because of Faraday error: #{e.inspect}")
ensure
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Finished synchronizing execution environment" }
end
def self.remove_environment(environment)
url = "#{config[:url]}/execution-environments/#{environment.id}"
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Deleting execution environment at #{url}" }
response = http_connection.delete url
return true if response.status == 204
raise Runner::Error::UnexpectedResponse.new("Could not delete execution environment in Poseidon, got response: #{response.as_json}")
rescue Faraday::Error => e
raise Runner::Error::FaradayError.new("Could not delete execution environment because of Faraday error: #{e.inspect}")
ensure
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Finished deleting execution environment" }
end
def self.request_from_management(environment)