Skip to content

Commit

Permalink
fixup! Add RecurringLogic to update HostResources
Browse files Browse the repository at this point in the history
  • Loading branch information
bastian-src committed Nov 26, 2024
1 parent 7273fb2 commit ea601b4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ Rails/SkipsModelValidations:

Style/FormatStringToken:
Enabled: false

Rails/DynamicFindBy:
Exclude:
- "lib/foreman_resource_quota/engine.rb"
31 changes: 15 additions & 16 deletions lib/foreman_resource_quota/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions test/jobs/refresh_resource_quota_utilization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ea601b4

Please sign in to comment.