From d73784e7fd9eb325ba36742827e454bdfd960af0 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 1 Jul 2015 00:25:05 +0200 Subject: [PATCH] Added cleanup jobs. --- app/models/activity_stream.rb | 17 ++++++++++++++ app/models/online_notification.rb | 28 ++++++++++++++++++++++++ db/migrate/20150701000001_add_cleanup.rb | 24 ++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 db/migrate/20150701000001_add_cleanup.rb diff --git a/app/models/activity_stream.rb b/app/models/activity_stream.rb index 4ac607f50..242086483 100644 --- a/app/models/activity_stream.rb +++ b/app/models/activity_stream.rb @@ -118,4 +118,21 @@ return all activity entries of an user list end +=begin + +cleanup old stream messages + + ActivityStream.cleanup + +optional you can parse the max oldest stream entries + + ActivityStream.cleanup(4.months) + +=end + + def self.cleanup(diff = 4.months) + ActivityStream.where('created_at < ?', Time.zone.now - diff).delete_all + true + end + end diff --git a/app/models/online_notification.rb b/app/models/online_notification.rb index 96ac1d653..808c93457 100644 --- a/app/models/online_notification.rb +++ b/app/models/online_notification.rb @@ -3,6 +3,7 @@ class OnlineNotification < ApplicationModel belongs_to :type_lookup, class_name: 'TypeLookup' belongs_to :object_lookup, class_name: 'ObjectLookup' + belongs_to :user after_create :notify_clients_after_change after_update :notify_clients_after_change @@ -188,4 +189,31 @@ returns: ) end +=begin + +cleanup old online notifications + + OnlineNotification.cleanup + +=end + + def self.cleanup + OnlineNotification.where('created_at < ?', Time.zone.now - 12.months).delete_all + OnlineNotification.where('seen = ? AND created_at < ?', true, Time.zone.now - 4.months).delete_all + + # notify all agents + users = Ticket::ScreenOptions.agents + users.each {|user| + Sessions.send_to( + user.id, + { + event: 'OnlineNotification::changed', + data: {} + } + ) + } + + true + end + end diff --git a/db/migrate/20150701000001_add_cleanup.rb b/db/migrate/20150701000001_add_cleanup.rb new file mode 100644 index 000000000..fbb0c1f56 --- /dev/null +++ b/db/migrate/20150701000001_add_cleanup.rb @@ -0,0 +1,24 @@ +class AddCleanup < ActiveRecord::Migration + def up + + # delete old entries + Scheduler.create_or_update( + name: 'Delete old activity stream entries.', + method: 'ActivityStream.cleanup', + period: 1.day, + prio: 2, + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + Scheduler.create_or_update( + name: 'Delete old online notification entries.', + method: 'OnlineNotification.cleanup', + period: 1.day, + prio: 2, + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + end +end