Files
sockeye/server/generator/CoreNotificationSweeper.cs
2022-12-16 06:01:23 +00:00

65 lines
3.4 KiB
C#

using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Sockeye.Models;
namespace Sockeye.Biz
{
/// <summary>
/// Clear out old data no longer required for notification system
/// </summary>
internal static class CoreNotificationSweeper
{
private static ILogger log = Sockeye.Util.ApplicationLogging.CreateLogger("CoreNotificationSweeper");
private static DateTime lastSweep = DateTime.MinValue;
private static TimeSpan DELETE_AFTER_AGE = new TimeSpan(90, 0, 0, 0);//## WARNING CoreJobPMInventoryCheck EXPECTS THIS TO BE a pretty big value 90 days right now
private static TimeSpan SWEEP_EVERY_INTERVAL = new TimeSpan(8, 0, 0);//once every 8 hours, three times a day
////////////////////////////////////////////////////////////////////////////////////////////////
// DoSweep
//
public static async Task DoWorkAsync()
{
//This will get triggered roughly every minute, but we don't want to sweep that frequently
if (DateTime.UtcNow - lastSweep < SWEEP_EVERY_INTERVAL)
return;
DateTime dtDeleteCutoff = DateTime.UtcNow - DELETE_AFTER_AGE;
DateTime dtPastEventCutoff = DateTime.UtcNow - SWEEP_EVERY_INTERVAL;
log.LogDebug("Sweep starting");
using (AyContext ct = Sockeye.Util.ServiceProviderProvider.DBContext)
{
//Notification (in-App notifications table) - deletes all APP notifications older than 90 days (if they want to keep it then can turn it into a reminder or SAVE it somehow)
await ct.Database.ExecuteSqlInterpolatedAsync($"delete from ainappnotification where created < {dtDeleteCutoff}");
//NotifyEvent - deletes any notifyevent with no event date created more than 90 days ago
await ct.Database.ExecuteSqlInterpolatedAsync($"delete from anotifyevent where eventdate is null and created < {dtDeleteCutoff}");
//NotifyEvent - If has event date and it's in the past by more than an 8 hours (or whatever sweep interval is, to allow for items about to be delivered momentarily as delivery is on briefer cycle than sweep)
//then deletes it if created more than 90 days ago (pretty sure there are no back dated events, once it's passed it's past)
await ct.Database.ExecuteSqlInterpolatedAsync($"delete from anotifyevent where eventdate < {dtPastEventCutoff} and created < {dtDeleteCutoff}");
//NotifyDeliveryLog - deletes all log items older than 90 days (NOTE: this log is also used to identify and prevent high frequency repetitive dupes)
//Note: also has bearing on CoreJobPMInventoryCheck which uses notifydeliverylog to check if pm inventory notify has been done it's one time already
await ct.Database.ExecuteSqlInterpolatedAsync($"delete from anotifydeliverylog where processed < {dtDeleteCutoff}");
//CustomerNotifyDeliveryLog - deletes all log items older than 90 days
await ct.Database.ExecuteSqlInterpolatedAsync($"delete from acustomernotifydeliverylog where processed < {dtDeleteCutoff}");
}
lastSweep = DateTime.UtcNow;
}
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons