Skip to content

Commit

Permalink
Schedule compliance purging 🤷
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Sep 6, 2019
1 parent 6ccbc96 commit 51f4a51
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/compliance.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Compliance < ApplicationRecord
include_concern 'Purging'
belongs_to :resource, :polymorphic => true
has_many :compliance_details, :dependent => :destroy

Expand Down
24 changes: 24 additions & 0 deletions app/models/compliance/purging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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)
where(arel_table[:timestamp].lteq(older_than))
end

def purge_associated_records(ids)
ComplianceDetail.where(:compliance_id => ids).delete_all
end
end
end
end
4 changes: 4 additions & 0 deletions app/models/miq_schedule_worker/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/models/miq_schedule_worker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.day
:db_diagnostics_interval: 30.minutes
:drift_state_purge_interval: 1.day
:event_streams_purge_interval: 1.day
Expand Down
54 changes: 54 additions & 0 deletions spec/models/compliance/purging_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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)
@old_compliance_detail = FactoryBot.create(:compliance_detail, :compliance_id => @old_compliance.id)
@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

def assert_purged_associated_records
expect(ComplianceDetail.count).to eq(0)
end

it "purge_date and older" do
described_class.purge(deleted_date)

assert_unpurged_ids(@new_compliance.id)
assert_purged_associated_records
end

it "with a window" do
described_class.purge(deleted_date, 1)
assert_unpurged_ids(@new_compliance.id)
end
end
end
end

0 comments on commit 51f4a51

Please sign in to comment.