From ea601b47d989d82ae7df345896d0fff263cb5f18 Mon Sep 17 00:00:00 2001 From: Bastian Schmidt Date: Tue, 26 Nov 2024 15:56:55 +0100 Subject: [PATCH] fixup! Add RecurringLogic to update HostResources --- .rubocop.yml | 4 +++ lib/foreman_resource_quota/engine.rb | 31 +++++++++---------- ...refresh_resource_quota_utilization_test.rb | 6 +--- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index f19eac4..e81de53 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -43,3 +43,7 @@ Rails/SkipsModelValidations: Style/FormatStringToken: Enabled: false + +Rails/DynamicFindBy: + Exclude: + - "lib/foreman_resource_quota/engine.rb" diff --git a/lib/foreman_resource_quota/engine.rb b/lib/foreman_resource_quota/engine.rb index 5460512..d5f8f19 100644 --- a/lib/foreman_resource_quota/engine.rb +++ b/lib/foreman_resource_quota/engine.rb @@ -49,18 +49,17 @@ class Engine < ::Rails::Engine action_paths = [ForemanResourceQuota::Engine.root.join('lib/foreman_resource_quota/async')] ::ForemanTasks.dynflow.config.eager_load_paths.concat(action_paths) - # skip database manipulations while tables do not exist, like in migrations - return unless ActiveRecord::Base.connection.data_source_exists?(::ForemanTasks::Task.table_name) - # Skip object creation if the admin user is not present - return if User.unscoped.find_by(login: User::ANONYMOUS_ADMIN).blank? - - # Register the scheduled tasks - ::ForemanTasks.dynflow.config.on_init(false) do |_world| - ForemanResourceQuota::Engine.register_scheduled_task( - ForemanResourceQuota::Async::RefreshResourceQuotaUtilization, - '0 1 * * *' - ) + # skip database manipulations while tables do not exist, like in migrations + if ActiveRecord::Base.connection.data_source_exists?(ForemanTasks::Task.table_name) && + User.unscoped.find_by_login(User::ANONYMOUS_ADMIN).present? + # Register the scheduled tasks + ::ForemanTasks.dynflow.config.on_init(false) do |_world| + ForemanResourceQuota::Engine.register_scheduled_task( + ForemanResourceQuota::Async::RefreshResourceQuotaUtilization, + '0 1 * * *' + ) + end end rescue ActiveRecord::NoDatabaseError => e Rails.logger.warn "ForemanResourceQuota: skipping ForemanTasks registration hook (#{e})" @@ -74,12 +73,12 @@ class Engine < ::Rails::Engine # Helper to register ForemanTasks def self.register_scheduled_task(task_class, cronline) - ::ForemanTasks::RecurringLogic.transaction(isolation: :serializable) do - return if ::ForemanTasks::RecurringLogic.joins(:tasks) - .merge(::ForemanTasks::Task.where(label: task_class.name)) - .exists? + return if ForemanTasks::RecurringLogic.joins(:tasks) + .merge(ForemanTasks::Task.where(label: task_class.name)) + .exists? + ForemanTasks::RecurringLogic.transaction(isolation: :serializable) do User.as_anonymous_admin do - recurring_logic = ::ForemanTasks::RecurringLogic.new_from_cronline(cronline) + recurring_logic = ForemanTasks::RecurringLogic.new_from_cronline(cronline) recurring_logic.save! recurring_logic.start(task_class) end diff --git a/test/jobs/refresh_resource_quota_utilization_test.rb b/test/jobs/refresh_resource_quota_utilization_test.rb index 930b3ca..71c8aad 100644 --- a/test/jobs/refresh_resource_quota_utilization_test.rb +++ b/test/jobs/refresh_resource_quota_utilization_test.rb @@ -8,11 +8,7 @@ class RefreshResourceQuotaUtilizationTest < ActiveSupport::TestCase setup do User.current = User.find_by(login: 'secret_admin') - @quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, - host_resources: { cpu_cores: 2 }, cpu_cores: 5) - @host = @quota.hosts[0] - - quota = FactoryBot.create(:resource_quota, cpu_cores: 10) + @quota = FactoryBot.create(:resource_quota, cpu_cores: 20, memory_mb: 1024 * 30, disk_gb: 512) stub_host_utilization({ cpu_cores: 2, memory_mb: 1024 * 4, disk_gb: 60 }, {}) @host_a = FactoryBot.create(:host, resource_quota: quota)