Enhanced Purge Job to include retention policy for Notifications
This commit is contained in:
@ -31,6 +31,7 @@ namespace Oqtane.Infrastructure
|
||||
var settingRepository = provider.GetRequiredService<ISettingRepository>();
|
||||
var logRepository = provider.GetRequiredService<ILogRepository>();
|
||||
var visitorRepository = provider.GetRequiredService<IVisitorRepository>();
|
||||
var notificationRepository = provider.GetRequiredService<INotificationRepository>();
|
||||
|
||||
// iterate through sites for current tenant
|
||||
List<Site> sites = siteRepository.GetSites().ToList();
|
||||
@ -51,7 +52,7 @@ namespace Oqtane.Infrastructure
|
||||
}
|
||||
try
|
||||
{
|
||||
count = logRepository.DeleteLogs(retention);
|
||||
count = logRepository.DeleteLogs(site.SiteId, retention);
|
||||
log += count.ToString() + " Events Purged<br />";
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -69,7 +70,7 @@ namespace Oqtane.Infrastructure
|
||||
}
|
||||
try
|
||||
{
|
||||
count = visitorRepository.DeleteVisitors(retention);
|
||||
count = visitorRepository.DeleteVisitors(site.SiteId, retention);
|
||||
log += count.ToString() + " Visitors Purged<br />";
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -77,6 +78,22 @@ namespace Oqtane.Infrastructure
|
||||
log += $"Error Purging Visitors - {ex.Message}<br />";
|
||||
}
|
||||
}
|
||||
|
||||
// purge notifications
|
||||
retention = 30; // 30 days
|
||||
if (settings.ContainsKey("NotificationRetention") && !string.IsNullOrEmpty(settings["NotificationRetention"]))
|
||||
{
|
||||
retention = int.Parse(settings["NotificationRetention"]);
|
||||
}
|
||||
try
|
||||
{
|
||||
count = notificationRepository.DeleteNotifications(site.SiteId, retention);
|
||||
log += count.ToString() + " Notifications Purged<br />";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log += $"Error Purging Notifications - {ex.Message}<br />";
|
||||
}
|
||||
}
|
||||
|
||||
return log;
|
||||
|
@ -8,6 +8,6 @@ namespace Oqtane.Repository
|
||||
IEnumerable<Log> GetLogs(int siteId, string level, string function, int rows);
|
||||
Log GetLog(int logId);
|
||||
void AddLog(Log log);
|
||||
int DeleteLogs(int age);
|
||||
int DeleteLogs(int siteId, int age);
|
||||
}
|
||||
}
|
||||
|
@ -11,5 +11,6 @@ namespace Oqtane.Repository
|
||||
Notification GetNotification(int notificationId);
|
||||
Notification GetNotification(int notificationId, bool tracking);
|
||||
void DeleteNotification(int notificationId);
|
||||
int DeleteNotifications(int siteId, int age);
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,6 @@ namespace Oqtane.Repository
|
||||
Visitor GetVisitor(int visitorId);
|
||||
Visitor GetVisitor(int siteId, string IPAddress);
|
||||
void DeleteVisitor(int visitorId);
|
||||
int DeleteVisitors(int age);
|
||||
int DeleteVisitors(int siteId, int age);
|
||||
}
|
||||
}
|
||||
|
@ -49,19 +49,19 @@ namespace Oqtane.Repository
|
||||
_db.SaveChanges();
|
||||
}
|
||||
|
||||
public int DeleteLogs(int age)
|
||||
public int DeleteLogs(int siteId, int age)
|
||||
{
|
||||
// delete logs in batches of 100 records
|
||||
int count = 0;
|
||||
var purgedate = DateTime.UtcNow.AddDays(-age);
|
||||
var logs = _db.Log.Where(item => item.Level != "Error" && item.LogDate < purgedate)
|
||||
var logs = _db.Log.Where(item => item.SiteId == siteId && item.Level != "Error" && item.LogDate < purgedate)
|
||||
.OrderBy(item => item.LogDate).Take(100).ToList();
|
||||
while (logs.Count > 0)
|
||||
{
|
||||
count += logs.Count;
|
||||
_db.Log.RemoveRange(logs);
|
||||
_db.SaveChanges();
|
||||
logs = _db.Log.Where(item => item.Level != "Error" && item.LogDate < purgedate)
|
||||
logs = _db.Log.Where(item => item.SiteId == siteId && item.Level != "Error" && item.LogDate < purgedate)
|
||||
.OrderBy(item => item.LogDate).Take(100).ToList();
|
||||
}
|
||||
return count;
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@ -68,6 +69,25 @@ namespace Oqtane.Repository
|
||||
_db.Notification.Remove(notification);
|
||||
_db.SaveChanges();
|
||||
}
|
||||
|
||||
public int DeleteNotifications(int siteId, int age)
|
||||
{
|
||||
// delete notifications in batches of 100 records
|
||||
int count = 0;
|
||||
var purgedate = DateTime.UtcNow.AddDays(-age);
|
||||
var notifications = _db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && item.IsDelivered && item.DeliveredOn < purgedate)
|
||||
.OrderBy(item => item.DeliveredOn).Take(100).ToList();
|
||||
while (notifications.Count > 0)
|
||||
{
|
||||
count += notifications.Count;
|
||||
_db.Notification.RemoveRange(notifications);
|
||||
_db.SaveChanges();
|
||||
notifications = _db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && item.IsDelivered && item.DeliveredOn < purgedate)
|
||||
.OrderBy(item => item.DeliveredOn).Take(100).ToList();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -53,19 +53,19 @@ namespace Oqtane.Repository
|
||||
_db.SaveChanges();
|
||||
}
|
||||
|
||||
public int DeleteVisitors(int age)
|
||||
public int DeleteVisitors(int siteId, int age)
|
||||
{
|
||||
// delete visitors in batches of 100 records
|
||||
int count = 0;
|
||||
var purgedate = DateTime.UtcNow.AddDays(-age);
|
||||
var visitors = _db.Visitor.Where(item => item.Visits <= 1 && item.VisitedOn < purgedate)
|
||||
var visitors = _db.Visitor.Where(item => item.SiteId == siteId && item.Visits < 2 && item.VisitedOn < purgedate)
|
||||
.OrderBy(item => item.VisitedOn).Take(100).ToList();
|
||||
while (visitors.Count > 0)
|
||||
{
|
||||
count += visitors.Count;
|
||||
_db.Visitor.RemoveRange(visitors);
|
||||
_db.SaveChanges();
|
||||
visitors = _db.Visitor.Where(item => item.Visits < 2 && item.VisitedOn < purgedate)
|
||||
visitors = _db.Visitor.Where(item => item.SiteId == siteId && item.Visits < 2 && item.VisitedOn < purgedate)
|
||||
.OrderBy(item => item.VisitedOn).Take(100).ToList();
|
||||
}
|
||||
return count;
|
||||
|
Reference in New Issue
Block a user