From c4fc2f436b84f212d456468ec6716f8e125a5c1b Mon Sep 17 00:00:00 2001 From: Glax Date: Mon, 16 Dec 2024 21:02:55 +0100 Subject: [PATCH] Notification-Thread Implemented --- API/Program.cs | 14 ++++----- API/Tranga.cs | 69 +++++++++++++++++++++++++++++++++++++++++++ API/TrangaSettings.cs | 9 ++++++ 3 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 API/Tranga.cs diff --git a/API/Program.cs b/API/Program.cs index 5f414ab..b3b1ec7 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -123,16 +123,14 @@ using (var scope = app.Services.CreateScope()) context.Notifications.Add(new Notification("Tranga Started", emojis[Random.Shared.Next(0, emojis.Length - 1)], NotificationUrgency.High)); context.SaveChanges(); - - string TRANGA = "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n"; - ILog Log = LogManager.GetLogger("Tranga"); - BasicConfigurator.Configure(); - - Log.Info(TRANGA); - TrangaSettings.Load(); - Log.Info(TrangaSettings.Serialize()); } + +TrangaSettings.Load(); +Tranga.StartLogger(); +Tranga.JobStarterThread.Start(app.Services.GetService()!); +Tranga.NotificationSenderThread.Start(app.Services.GetService()!); + app.UseCors("AllowAll"); app.Run(); \ No newline at end of file diff --git a/API/Tranga.cs b/API/Tranga.cs new file mode 100644 index 0000000..36f4967 --- /dev/null +++ b/API/Tranga.cs @@ -0,0 +1,69 @@ +using API.Schema; +using API.Schema.NotificationConnectors; +using log4net; +using log4net.Config; + +namespace API; + +public static class Tranga +{ + public static Thread NotificationSenderThread { get; } = new (NotificationSender); + public static Thread JobStarterThread { get; } = new (JobStarter); + private static ILog Log = LogManager.GetLogger(typeof(Tranga)); + + internal static void StartLogger() + { + BasicConfigurator.Configure(); + } + + private static void NotificationSender(object? pgsqlContext) + { + if(pgsqlContext is null) return; + PgsqlContext context = (PgsqlContext)pgsqlContext; + + IQueryable staleNotifications = context.Notifications.Where(n => n.Urgency < NotificationUrgency.Normal); + context.Notifications.RemoveRange(staleNotifications); + context.SaveChanges(); + while (true) + { + SendNotifications(context, NotificationUrgency.High); + SendNotifications(context, NotificationUrgency.Normal); + SendNotifications(context, NotificationUrgency.Low); + + context.SaveChanges(); + Thread.Sleep(2000); + } + } + + private static void SendNotifications(PgsqlContext context, NotificationUrgency urgency) + { + List notifications = context.Notifications.Where(n => n.Urgency == urgency).ToList(); + if (notifications.Any()) + { + DateTime max = notifications.MaxBy(n => n.Date)!.Date; + if (DateTime.Now.Subtract(max) > TrangaSettings.NotificationUrgencyDelay(urgency)) + { + foreach (NotificationConnector notificationConnector in context.NotificationConnectors) + { + foreach (Notification notification in notifications) + notificationConnector.SendNotification(notification.Title, notification.Message); + } + context.Notifications.RemoveRange(notifications); + } + } + context.SaveChanges(); + } + + private static void JobStarter(object? pgsqlContext) + { + if(pgsqlContext is null) return; + PgsqlContext context = (PgsqlContext)pgsqlContext; + + string TRANGA = "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n"; + Log.Info(TRANGA); + while (true) + { + + } + } +} \ No newline at end of file diff --git a/API/TrangaSettings.cs b/API/TrangaSettings.cs index 66b2d8a..8fb7782 100644 --- a/API/TrangaSettings.cs +++ b/API/TrangaSettings.cs @@ -1,5 +1,6 @@ using System.Runtime.InteropServices; using API.MangaDownloadClients; +using API.Schema; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using static System.IO.UnixFileMode; @@ -33,6 +34,14 @@ public static class TrangaSettings }; public static Dictionary requestLimits { get; set; } = DefaultRequestLimits; + public static TimeSpan NotificationUrgencyDelay(NotificationUrgency urgency) => urgency switch + { + NotificationUrgency.High => TimeSpan.Zero, + NotificationUrgency.Normal => TimeSpan.FromMinutes(5), + NotificationUrgency.Low => TimeSpan.FromMinutes(10), + _ => TimeSpan.FromHours(1) + }; + public static void Load() { if(File.Exists(settingsFilePath))