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

[FINE] Adds purging for notifications #17294

Merged
merged 1 commit into from
Apr 16, 2018
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
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 @@ -113,6 +113,10 @@ def event_stream_purge_timer
queue_work(:class_name => "EventStream", :method_name => "purge_timer", :zone => nil)
end

def notification_purge_timer
queue_work(:class_name => "Notification", :method_name => "purge_timer", :zone => nil)
end

def policy_event_purge_timer
queue_work(:class_name => "PolicyEvent", :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 @@ -228,6 +228,11 @@ def schedules_for_scheduler_role
enqueue :archived_entities_purge_timer
end

every = worker_settings[:notifications_purge_interval]
scheduler.schedule_every(every, :first_in => every) do
enqueue(:notification_purge_timer)
end

# Schedule every 24 hours
at = worker_settings[:storage_file_collection_time_utc]
if Time.now.strftime("%Y-%m-%d #{at}").to_time(:utc) < Time.now.utc
Expand Down
2 changes: 2 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Notification < ApplicationRecord
include_concern 'Purging'

belongs_to :notification_type
belongs_to :initiator, :class_name => User, :foreign_key => 'user_id'
belongs_to :subject, :polymorphic => true
Expand Down
24 changes: 24 additions & 0 deletions app/models/notification/purging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Notification
module Purging
extend ActiveSupport::Concern
include PurgingMixin

module ClassMethods
def purge_date
::Settings.notifications.history.keep_notifications.to_i_with_method.seconds.ago.utc
end

def purge_window_size
::Settings.notifications.history.purge_window_size
end

def purge_scope(older_than)
where(arel_table[:created_at].lt(older_than))
end

def purge_associated_records(ids)
NotificationRecipient.where(:notification_id => ids).delete_all
end
end
end
end
5 changes: 5 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,10 @@
:level_websocket_in_evm: error
:level_vcloud: info
:level_vcloud_in_evm: error
:notifications:
:history:
:purge_window_size: 1000
:keep_notifications: 1.week
:management_system:
:power_operation_expiration: 10.minutes
:performance:
Expand Down Expand Up @@ -1421,6 +1425,7 @@
:log_database_statistics_interval: 1.days
:memory_threshold: 500.megabytes
:nice_delta: 3
:notifications_purge_interval: 1.day
:orchestration_stack_retired_interval: 10.minutes
:performance_collection_interval: 3.minutes
:performance_collection_start_delay: 5.minutes
Expand Down
40 changes: 40 additions & 0 deletions spec/models/notification/purging_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
describe Notification do
context "::Purging" do
describe ".purge_by_date" do
it "purges old notifications" do
FactoryGirl.create(:user)
FactoryGirl.create(:user)
type = FactoryGirl.create(:notification_type, :audience => NotificationType::AUDIENCE_GLOBAL)

# Notification and recipients that will not be purged
new_notification = FactoryGirl.create(:notification, :notification_type => type)

old_notification, semi_old_notification = nil
Timecop.freeze(6.days.ago) do
semi_old_notification = FactoryGirl.create(:notification, :notification_type => type)
end

Timecop.freeze(8.days.ago) do
# Notification and recipients that will be purged
old_notification = FactoryGirl.create(:notification, :notification_type => type)
end

expect(described_class.all).to match_array([new_notification, semi_old_notification, old_notification])
expect(NotificationRecipient.count).to eq(6)
count = described_class.purge_by_date(described_class.purge_date)
expect(described_class.all).to match_array([new_notification, semi_old_notification])
expect(NotificationRecipient.count).to eq(4)
expect(count).to eq(1)
end
end

describe ".purge_timer" do
it "queues the correct purge method" do
EvmSpecHelper.local_miq_server
described_class.purge_timer
q = MiqQueue.first
expect(q).to have_attributes(:class_name => described_class.name, :method_name => "purge_by_date")
end
end
end
end