Skip to content

Commit

Permalink
Merge pull request #17046 from NickLaMuro/notification-purging
Browse files Browse the repository at this point in the history
Adds purging for notifications
(cherry picked from commit 427257a)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1553780
  • Loading branch information
jrafanie authored and simaishi committed Mar 9, 2018
1 parent e634942 commit 920e709
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/miq_schedule_worker/jobs.rb
Expand Up @@ -95,6 +95,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
Expand Up @@ -199,6 +199,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
@@ -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
@@ -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
Expand Up @@ -880,6 +880,10 @@
:level_websocket: info
:level_vcloud: info
:level_nuage: info
:notifications:
:history:
:purge_window_size: 1000
:keep_notifications: 1.week
:ntp:
:server:
- 0.pool.ntp.org
Expand Down Expand Up @@ -1184,6 +1188,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
@@ -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

0 comments on commit 920e709

Please sign in to comment.