diff --git a/server/AyaNova/generator/CoreNotificationSweeper.cs b/server/AyaNova/generator/CoreNotificationSweeper.cs new file mode 100644 index 00000000..b67c50bb --- /dev/null +++ b/server/AyaNova/generator/CoreNotificationSweeper.cs @@ -0,0 +1,135 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using AyaNova.Models; + +namespace AyaNova.Biz +{ + + + /// + /// Clean up notification system, keep items down to 90 days + /// + /// + internal static class CoreNotificationSweeper + { + private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("CoreNotificationSweeper"); + private static DateTime lastSweep = DateTime.MinValue; + private static TimeSpan DELETE_AFTER_AGE = new TimeSpan(90, 0, 0, 0); + 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.LogTrace("Sweep starting"); + using (AyContext ct = AyaNova.Util.ServiceProviderProvider.DBContext) + { + //Notification (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 anotification 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}"); + + } + lastSweep = DateTime.UtcNow; + } + + + private static async Task sweepAsync(AyContext ct, DateTime dtDeleteCutoff, JobStatus jobStatus) + { + //Get the deleteable succeeded jobs list + var jobs = await ct.OpsJob + .AsNoTracking() + .Where(z => z.Created < dtDeleteCutoff && z.JobStatus == jobStatus) + .OrderBy(z => z.Created) + .ToListAsync(); + + log.LogTrace($"SweepAsync processing: cutoff={dtDeleteCutoff.ToString()}, for {jobs.Count.ToString()} jobs of status {jobStatus.ToString()}"); + + foreach (OpsJob j in jobs) + { + try + { + + await JobsBiz.RemoveJobAndLogsAsync(j.GId); + } + catch (Exception ex) + { + log.LogError(ex, "sweepAsync exception calling JobsBiz.RemoveJobAndLogsAsync"); + //for now just throw it but this needs to be removed when logging added and better handling + throw (ex); + } + } + } + + + /// + /// Kill jobs that have been stuck in "running" state for too long + /// + private static async Task killStuckJobsAsync(AyContext ct, DateTime dtRunningDeadline) + { + //Get the deleteable succeeded jobs list + var jobs = await ct.OpsJob + .AsNoTracking() + .Where(z => z.Created < dtRunningDeadline && z.JobStatus == JobStatus.Running) + .OrderBy(z => z.Created) + .ToListAsync(); + + log.LogTrace($"killStuckJobsAsync processing: cutoff={dtRunningDeadline.ToString()}, for {jobs.Count.ToString()} jobs of status {JobStatus.Running.ToString()}"); + + foreach (OpsJob j in jobs) + { + //OPSMETRIC + await JobsBiz.LogJobAsync(j.GId, "Job took too long to run - setting to failed"); + log.LogError($"Job found job stuck in running status and set to failed: deadline={dtRunningDeadline.ToString()}, jobId={j.GId.ToString()}, jobname={j.Name}, jobtype={j.JobType.ToString()}, jobObjectType={j.ObjectType.ToString()}, jobObjectId={j.ObjectId.ToString()}"); + await JobsBiz.UpdateJobStatusAsync(j.GId, JobStatus.Failed); + } + } + + + private static async Task SweepInternalJobsLogsAsync(AyContext ct, DateTime dtDeleteCutoff) + { + //Get the deleteable list (this is for reporting, could easily just do it in one go) + var logs = await ct.OpsJobLog + .AsNoTracking() + .Where(z => z.Created < dtDeleteCutoff) + .OrderBy(z => z.Created) + .ToListAsync(); + + log.LogTrace($"SweepInternalJobsLogsAsync processing: cutoff={dtDeleteCutoff.ToString()}, for {logs.Count.ToString()} log entries"); + + foreach (OpsJobLog l in logs) + { + try + { + await ct.Database.ExecuteSqlInterpolatedAsync($"delete from aopsjoblog where gid = {l.GId}"); + } + catch (Exception ex) + { + log.LogError(ex, "SweepInternalJobsLogsAsync exception removed old log entries"); + throw (ex); + } + } + } + + ///////////////////////////////////////////////////////////////////// + + }//eoc + + +}//eons + diff --git a/server/AyaNova/models/GlobalBizSettings.cs b/server/AyaNova/models/GlobalBizSettings.cs index c90ba6f8..b892ab59 100644 --- a/server/AyaNova/models/GlobalBizSettings.cs +++ b/server/AyaNova/models/GlobalBizSettings.cs @@ -16,13 +16,14 @@ namespace AyaNova.Models //this is precautionarily added for non latinate languages where it could be an issue public bool SearchCaseSensitiveOnly { get; set; } - public bool NotificationSystemActive { get; set; } + // public bool NotificationSystemActive { get; set; } public GlobalBizSettings() { Id = 1;//always 1 SearchCaseSensitiveOnly = false; - NotificationSystemActive = true; + //NO, notification system is *always* active, if they don't want email delivery that's an OPS setting NotificationSystemActive = true; + //keeping this in case I forget and try to re-implement } } } diff --git a/server/AyaNova/models/GlobalOpsNotificationSettings.cs b/server/AyaNova/models/GlobalOpsNotificationSettings.cs index 055ed55d..793d9bc9 100644 --- a/server/AyaNova/models/GlobalOpsNotificationSettings.cs +++ b/server/AyaNova/models/GlobalOpsNotificationSettings.cs @@ -7,7 +7,7 @@ namespace AyaNova.Models { public long Id { get; set; }//this is always 1 as there is only ever one single global Ops object public uint Concurrency { get; set; } - public bool Active { get; set; } + public bool SmtpDeliveryActive { get; set; } public string SmtpServerAddress { get; set; } public string SmtpAccount { get; set; } public string SmtpPassword { get; set; } @@ -17,7 +17,7 @@ namespace AyaNova.Models public GlobalOpsNotificationSettings() { - Active = true; + SmtpDeliveryActive = true; Id = 1; SmtpServerAddress="mail.example.com"; SmtpAccount="notifydeliverfromaccount@example.com"; diff --git a/server/AyaNova/util/AySchema.cs b/server/AyaNova/util/AySchema.cs index a48d8c61..63aeb3c8 100644 --- a/server/AyaNova/util/AySchema.cs +++ b/server/AyaNova/util/AySchema.cs @@ -22,7 +22,7 @@ namespace AyaNova.Util //!!!!WARNING: BE SURE TO UPDATE THE DbUtil::EmptyBizDataFromDatabaseForSeedingOrImporting WHEN NEW TABLES ADDED!!!! private const int DESIRED_SCHEMA_LEVEL = 12; - internal const long EXPECTED_COLUMN_COUNT = 381; + internal const long EXPECTED_COLUMN_COUNT = 380; internal const long EXPECTED_INDEX_COUNT = 139; //!!!!WARNING: BE SURE TO UPDATE THE DbUtil::EmptyBizDataFromDatabaseForSeedingOrImporting WHEN NEW TABLES ADDED!!!! @@ -230,13 +230,13 @@ namespace AyaNova.Util //create global biz settings table await ExecQueryAsync("CREATE TABLE aglobalbizsettings (id integer NOT NULL PRIMARY KEY, " + - "searchcasesensitiveonly bool default false, notificationsystemactive bool not null default true)"); + "searchcasesensitiveonly bool default false)"); //create global ops BACKUP settings table await ExecQueryAsync("CREATE TABLE aglobalopsbackupsettings (id integer NOT NULL PRIMARY KEY, active bool not null, " + "backuptime timestamp, backupsetstokeep int, backupattachments bool)"); - await ExecQueryAsync("CREATE TABLE aglobalopsnotificationsettings (id integer NOT NULL PRIMARY KEY, active bool not null, " + + await ExecQueryAsync("CREATE TABLE aglobalopsnotificationsettings (id integer NOT NULL PRIMARY KEY, smtpdeliveryactive bool not null, " + "smtpserveraddress text, smtpaccount text, smtppassword text, connectionsecurity integer not null default 0, smtpserverport integer, notifyfromaddress text)"); //create aevent biz event log table diff --git a/server/AyaNova/util/ServerGlobalBizSettings.cs b/server/AyaNova/util/ServerGlobalBizSettings.cs index fd99a270..934f81dc 100644 --- a/server/AyaNova/util/ServerGlobalBizSettings.cs +++ b/server/AyaNova/util/ServerGlobalBizSettings.cs @@ -18,7 +18,7 @@ namespace AyaNova.Util { internal static bool SearchCaseSensitiveOnly { get; set; } - internal static bool NotificationSystemActive { get; set; } + //NO always active, email delivery is a separate setting but app delivery is always there internal static bool NotificationSystemActive { get; set; } /// /// Populate and / or create the settings @@ -40,7 +40,7 @@ namespace AyaNova.Util //We have the object, now copy the static values here SearchCaseSensitiveOnly = global.SearchCaseSensitiveOnly; - NotificationSystemActive = global.NotificationSystemActive; + // NotificationSystemActive = global.NotificationSystemActive; }