Refactor sync_environment methods

* Add delete_environment method
* Change return value to allow raising an exception
This commit is contained in:
Sebastian Serth
2021-11-04 01:03:12 +01:00
parent 38e1f5b486
commit ecf470bddd
10 changed files with 106 additions and 31 deletions

View File

@ -177,6 +177,8 @@ class ExecutionEnvironmentsController < ApplicationController
success = ExecutionEnvironment.all.map do |execution_environment|
Runner.strategy_class.sync_environment(execution_environment)
rescue Runner::Error
false
end
if success.all?
redirect_to ExecutionEnvironment, notice: t('execution_environments.index.synchronize_all.success')

View File

@ -33,10 +33,11 @@ class ExecutionEnvironment < ApplicationRecord
before_validation :clean_exposed_ports
validates :exposed_ports, array: {numericality: {greater_than_or_equal_to: 0, less_than: 65_536, only_integer: true}}
def set_default_values
set_default_values_if_present(permitted_execution_time: 60, pool_size: 0)
end
private :set_default_values
after_destroy :delete_runner_environment
after_save :working_docker_image?, if: :validate_docker_image?
after_rollback :delete_runner_environment, on: :create
after_rollback :sync_runner_environment, on: %i[update destroy]
def to_s
name
@ -86,5 +87,23 @@ class ExecutionEnvironment < ApplicationRecord
rescue Runner::Error => e
errors.add(:docker_image, "error: #{e}")
end
private :working_docker_image?
def delete_runner_environment
Runner.strategy_class.remove_environment(self)
rescue Runner::Error => e
unless errors.include?(:docker_image)
errors.add(:docker_image, "error: #{e}")
raise ActiveRecord::RecordInvalid.new(self)
end
end
def sync_runner_environment
previous_saved_environment = self.class.find(id)
Runner.strategy_class.sync_environment(previous_saved_environment)
rescue Runner::Error => e
unless errors.include?(:docker_image)
errors.add(:docker_image, "error: #{e}")
raise ActiveRecord::RecordInvalid.new(self)
end
end
end

View File

@ -140,16 +140,20 @@ class Runner < ApplicationRecord
rescue Runner::Error::EnvironmentNotFound
# Whenever the environment could not be found by the runner management, we
# try to synchronize it and then forward a more specific error to our callee.
if strategy_class.sync_environment(execution_environment)
begin
strategy_class.sync_environment(execution_environment)
rescue Runner::Error
# An additional error was raised during synchronization
raise Runner::Error::EnvironmentNotFound.new(
"The execution environment with id #{execution_environment.id} was not found by the runner management. "\
'In addition, it could not be synced so that this probably indicates a permanent error.'
)
else
# No error was raised during synchronization
raise Runner::Error::EnvironmentNotFound.new(
"The execution environment with id #{execution_environment.id} was not found yet by the runner management. "\
'It has been successfully synced now so that the next request should be successful.'
)
else
raise Runner::Error::EnvironmentNotFound.new(
"The execution environment with id #{execution_environment.id} was not found by the runner management."\
'In addition, it could not be synced so that this probably indicates a permanent error.'
)
end
end
end