From c316d01d77ec444ea1ff791e9ed474a102565dff Mon Sep 17 00:00:00 2001 From: d-m-u Date: Thu, 5 Sep 2019 10:34:34 -0400 Subject: [PATCH] Schedule compliance purging :shrug: --- app/models/compliance.rb | 1 + app/models/compliance/purging.rb | 25 ++++++++++++ app/models/miq_schedule_worker/jobs.rb | 4 ++ app/models/miq_schedule_worker/runner.rb | 5 +++ config/settings.yml | 5 +++ spec/models/compliance/purging_spec.rb | 48 ++++++++++++++++++++++++ 6 files changed, 88 insertions(+) create mode 100644 app/models/compliance/purging.rb create mode 100644 spec/models/compliance/purging_spec.rb diff --git a/app/models/compliance.rb b/app/models/compliance.rb index b5f04807e900..a974c62ac3d0 100644 --- a/app/models/compliance.rb +++ b/app/models/compliance.rb @@ -1,4 +1,5 @@ class Compliance < ApplicationRecord + include_concern 'Purging' belongs_to :resource, :polymorphic => true has_many :compliance_details, :dependent => :destroy diff --git a/app/models/compliance/purging.rb b/app/models/compliance/purging.rb new file mode 100644 index 000000000000..7d782822d57a --- /dev/null +++ b/app/models/compliance/purging.rb @@ -0,0 +1,25 @@ +class Compliance < ApplicationRecord + module Purging + extend ActiveSupport::Concern + include PurgingMixin + + module ClassMethods + def purge_date + ::Settings.compliances.history.keep_compliances.to_i_with_method.seconds.ago.utc + end + + def purge_window_size + ::Settings.compliances.history.purge_window_size + end + + def purge_scope(older_than = nil) + # do i need to check that the resource is nil as well? + where(arel_table[:timestamp].lt(older_than)) + end + + def purge_associated_records(ids) + ComplianceDetail.where(:compliance_id => ids).delete_all + end + end + end +end diff --git a/app/models/miq_schedule_worker/jobs.rb b/app/models/miq_schedule_worker/jobs.rb index f23da9f3c4f1..e0a731dcd284 100644 --- a/app/models/miq_schedule_worker/jobs.rb +++ b/app/models/miq_schedule_worker/jobs.rb @@ -108,6 +108,10 @@ def policy_event_purge_timer queue_work(:class_name => "PolicyEvent", :method_name => "purge_timer", :zone => nil) end + def compliance_purge_timer + queue_work(:class_name => "Compliance", :method_name => "purge_timer", :zone => nil) + end + def miq_report_result_purge_timer queue_work(:class_name => "MiqReportResult", :method_name => "purge_timer", :zone => nil) end diff --git a/app/models/miq_schedule_worker/runner.rb b/app/models/miq_schedule_worker/runner.rb index 8c924ab40bdb..eabc640ae1b5 100644 --- a/app/models/miq_schedule_worker/runner.rb +++ b/app/models/miq_schedule_worker/runner.rb @@ -217,6 +217,11 @@ def schedules_for_scheduler_role enqueue(:task_purge_timer) end + every = worker_settings[:compliance_purge_interval] + scheduler.schedule_every(every, :first_in => every) do + enqueue(:compliance_purge_interval) + end + every = worker_settings[:vim_performance_states_purge_interval] scheduler.schedule_every(every, :first_in => every) do enqueue(:vim_performance_states_purge_timer) diff --git a/config/settings.yml b/config/settings.yml index 86e74c78e4e1..9a5c5db784d2 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -74,6 +74,10 @@ :memory_method_description: High Normal Range of Allocated Memory :failover: :rule: discovered +:compliances: + :history: + :keep_compliances: 6.months + :purge_window_size: 10000 :coresident_miqproxy: :concurrent_per_ems: 1 :concurrent_per_host: 1 @@ -1216,6 +1220,7 @@ :authentication_check_interval: 1.hour :chargeback_generation_interval: 1.day :chargeback_generation_time_utc: 01:00:00 + :compliance_purge_interval: 1.week :db_diagnostics_interval: 30.minutes :drift_state_purge_interval: 1.day :event_streams_purge_interval: 1.day diff --git a/spec/models/compliance/purging_spec.rb b/spec/models/compliance/purging_spec.rb new file mode 100644 index 000000000000..8c67688a67b8 --- /dev/null +++ b/spec/models/compliance/purging_spec.rb @@ -0,0 +1,48 @@ +describe Compliance do + context "::Purging" do + context ".purge_queue" do + before do + EvmSpecHelper.create_guid_miq_server_zone + end + let(:purge_time) { (Time.zone.now + 10).round } + + it "submits to the queue" do + expect(described_class).to receive(:purge_date).and_return(purge_time) + described_class.purge_timer + + q = MiqQueue.all + expect(q.length).to eq(1) + expect(q.first).to have_attributes( + :class_name => described_class.name, + :method_name => "purge_by_date", + :args => [purge_time] + ) + end + end + + context ".purge" do + let(:deleted_date) { 6.months.ago } + + before do + @old_compliance = FactoryBot.create(:compliance, :timestamp => deleted_date - 1.day) + @purge_date_compliance = FactoryBot.create(:compliance, :timestamp => deleted_date) + @new_compliance = FactoryBot.create(:compliance, :timestamp => deleted_date + 1.day) + end + + def assert_unpurged_ids(unpurged_ids) + expect(described_class.order(:id).pluck(:id)).to eq(Array(unpurged_ids).sort) + end + + it "purge_date and older" do + described_class.purge(deleted_date) + + assert_unpurged_ids(@new_compliance.id) + end + + it "with a window" do + described_class.purge(deleted_date, 1) + assert_unpurged_ids(@new_compliance.id) + end + end + end +end \ No newline at end of file