Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schedule compliance purging #19264

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

@kbrock kbrock Sep 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TANGENT: I do wonder if we could pass scopes into purge_associated_records

no action required

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcarter12 Do you have a suggestion for this value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response - check with loic

: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