Generalize method and constant names for runner management

This commit is contained in:
Sebastian Serth
2021-09-13 12:49:56 +02:00
parent e752df1b3c
commit 30603cb7ab
18 changed files with 139 additions and 110 deletions

View File

@@ -3,8 +3,6 @@
class ExecutionEnvironmentsController < ApplicationController
include CommonBehavior
RUNNER_MANAGEMENT_PRESENT = CodeOcean::Config.new(:code_ocean).read[:runner_management].present?
before_action :set_docker_images, only: %i[create edit new update]
before_action :set_execution_environment, only: MEMBER_ACTIONS + %i[execute_command shell statistics]
before_action :set_testing_framework_adapters, only: %i[create edit new update]
@@ -18,7 +16,7 @@ class ExecutionEnvironmentsController < ApplicationController
@execution_environment = ExecutionEnvironment.new(execution_environment_params)
authorize!
create_and_respond(object: @execution_environment) do
copy_execution_environment_to_poseidon
sync_to_runner_management
end
end
@@ -160,27 +158,29 @@ class ExecutionEnvironmentsController < ApplicationController
def update
update_and_respond(object: @execution_environment, params: execution_environment_params) do
copy_execution_environment_to_poseidon
sync_to_runner_management
end
end
def synchronize_all_to_poseidon
def sync_all_to_runner_management
authorize ExecutionEnvironment
return unless RUNNER_MANAGEMENT_PRESENT
return unless Runner.management_active?
success = ExecutionEnvironment.all.map(&:copy_to_poseidon).all?
if success
success = ExecutionEnvironment.all.map do |execution_environment|
Runner.strategy_class.sync_environment(execution_environment)
end
if success.all?
redirect_to ExecutionEnvironment, notice: t('execution_environments.index.synchronize_all.success')
else
redirect_to ExecutionEnvironment, alert: t('execution_environments.index.synchronize_all.failure')
end
end
def copy_execution_environment_to_poseidon
unless RUNNER_MANAGEMENT_PRESENT && @execution_environment.copy_to_poseidon
t('execution_environments.form.errors.not_synced_to_poseidon')
def sync_to_runner_management
unless Runner.management_active? && Runner.strategy_class.sync_environment(@execution_environment)
t('execution_environments.form.errors.not_synced_to_runner_management')
end
end
private :copy_execution_environment_to_poseidon
private :sync_to_runner_management
end

View File

@@ -7,9 +7,6 @@ class ExecutionEnvironment < ApplicationRecord
include DefaultValues
VALIDATION_COMMAND = 'whoami'
RUNNER_MANAGEMENT_PRESENT = CodeOcean::Config.new(:code_ocean).read[:runner_management].present?
BASE_URL = CodeOcean::Config.new(:code_ocean).read[:runner_management][:url] if RUNNER_MANAGEMENT_PRESENT
HEADERS = {'Content-Type' => 'application/json'}.freeze
DEFAULT_CPU_LIMIT = 20
after_initialize :set_default_values
@@ -42,20 +39,6 @@ class ExecutionEnvironment < ApplicationRecord
name
end
def copy_to_poseidon
return false unless RUNNER_MANAGEMENT_PRESENT
url = "#{BASE_URL}/execution-environments/#{id}"
response = Faraday.put(url, to_json, HEADERS)
return true if [201, 204].include? response.status
Rails.logger.warn("Could not create execution environment in Poseidon, got response: #{response.as_json}")
false
rescue Faraday::Error => e
Rails.logger.warn("Could not create execution environment because of Faraday error: #{e.inspect}")
false
end
def to_json(*_args)
{
id: id,

View File

@@ -8,14 +8,15 @@ class Runner < ApplicationRecord
validates :execution_environment, :user, :runner_id, presence: true
STRATEGY_NAME = CodeOcean::Config.new(:code_ocean).read[:runner_management][:strategy]
UNUSED_EXPIRATION_TIME = CodeOcean::Config.new(:code_ocean).read[:runner_management][:unused_runner_expiration_time].seconds
BASE_URL = CodeOcean::Config.new(:code_ocean).read[:runner_management][:url]
attr_accessor :strategy
def self.strategy_class
"runner/strategy/#{STRATEGY_NAME}".camelize.constantize
strategy_name = CodeOcean::Config.new(:code_ocean).read[:runner_management][:strategy]
@strategy_class ||= "runner/strategy/#{strategy_name}".camelize.constantize
end
def self.management_active?
@management_active ||= CodeOcean::Config.new(:code_ocean).read[:runner_management][:enabled]
end
def self.for(user, exercise)

View File

@@ -9,7 +9,7 @@ class ExecutionEnvironmentPolicy < AdminOnlyPolicy
define_method(action) { admin? || teacher? }
end
def synchronize_all_to_poseidon?
def sync_all_to_runner_management?
admin?
end
end

View File

@@ -1,7 +1,7 @@
h1.d-inline-block = ExecutionEnvironment.model_name.human(count: 2)
- if ExecutionEnvironment::RUNNER_MANAGEMENT_PRESENT
= button_to( { action: :synchronize_all_to_poseidon, method: :post }, { form_class: 'float-right mb-2', class: 'btn btn-success' })
- if Runner.management_active?
= button_to( { action: :sync_all_to_runner_management, method: :post }, { form_class: 'float-right mb-2', class: 'btn btn-success' })
i.fa.fa-upload
= t('execution_environments.index.synchronize_all.button')