Notification-Thread Implemented

This commit is contained in:
Glax 2024-12-16 21:02:55 +01:00
parent ebc30c85bf
commit c4fc2f436b
3 changed files with 84 additions and 8 deletions

View File

@ -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.Notifications.Add(new Notification("Tranga Started", emojis[Random.Shared.Next(0, emojis.Length - 1)], NotificationUrgency.High));
context.SaveChanges(); 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<PgsqlContext>()!);
Tranga.NotificationSenderThread.Start(app.Services.GetService<PgsqlContext>()!);
app.UseCors("AllowAll"); app.UseCors("AllowAll");
app.Run(); app.Run();

69
API/Tranga.cs Normal file
View File

@ -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<Notification> 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<Notification> 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)
{
}
}
}

View File

@ -1,5 +1,6 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using API.MangaDownloadClients; using API.MangaDownloadClients;
using API.Schema;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using static System.IO.UnixFileMode; using static System.IO.UnixFileMode;
@ -33,6 +34,14 @@ public static class TrangaSettings
}; };
public static Dictionary<RequestType, int> requestLimits { get; set; } = DefaultRequestLimits; public static Dictionary<RequestType, int> 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() public static void Load()
{ {
if(File.Exists(settingsFilePath)) if(File.Exists(settingsFilePath))