Skip to content

Commit

Permalink
[postgres-Server-V2] fix: Updated Job logic Added a context sync befo…
Browse files Browse the repository at this point in the history
…re each job loop to sync changes that happened between loops Added a check to prevent jobs that are already registered in RunningJobs list to be started again
  • Loading branch information
ale-ben committed Jan 12, 2025
1 parent fc8d8b0 commit 7ccc3cf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
4 changes: 2 additions & 2 deletions API/Schema/Chapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class Chapter : IComparable<Chapter>
public string ParentMangaId { get; internal set; }
public Manga? ParentManga { get; init; }

public Chapter(Manga parentManga, string url, string chapterNumberStruct, int? volumeNumber = null, string? title = null)
: this(parentManga.MangaId, url, chapterNumberStruct, volumeNumber, title)
public Chapter(Manga parentManga, string url, string chapterNumber, int? volumeNumber = null, string? title = null)
: this(parentManga.MangaId, url, chapterNumber, volumeNumber, title)
{
this.ParentManga = parentManga;
this.ArchiveFileName = BuildArchiveFileName();
Expand Down
59 changes: 33 additions & 26 deletions API/Tranga.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using API.Schema;
using API.Schema.Jobs;
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 readonly Dictionary<Thread, Job> RunningJobs = new();
private static readonly ILog Log = LogManager.GetLogger(typeof(Tranga));
public static Thread NotificationSenderThread { get; } = new(NotificationSender);
public static Thread JobStarterThread { get; } = new(JobStarter);

internal static void StartLogger()
{
Expand All @@ -20,66 +19,74 @@ internal static void StartLogger()

private static void NotificationSender(object? pgsqlContext)
{
if(pgsqlContext is null) return;
PgsqlContext context = (PgsqlContext)pgsqlContext;
if (pgsqlContext is null) return;
var context = (PgsqlContext)pgsqlContext;

IQueryable<Notification> staleNotifications = context.Notifications.Where(n => n.Urgency < NotificationUrgency.Normal);
var 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();
var notifications = context.Notifications.Where(n => n.Urgency == urgency).ToList();
if (notifications.Any())
{
DateTime max = notifications.MaxBy(n => n.Date)!.Date;
var 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);
}
foreach (var notificationConnector in context.NotificationConnectors)
foreach (var 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";
if (pgsqlContext is null) return;
var context = (PgsqlContext)pgsqlContext;

var TRANGA =
"\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
Log.Info(TRANGA);
while (true)
{
List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
foreach (Job job in completedJobs)
if(job.RecurrenceMs <= 0)
context.SaveChanges();
var completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
foreach (var job in completedJobs)
if (job.RecurrenceMs <= 0)
{
context.Jobs.Remove(job);
}
else
{
job.LastExecution = DateTime.UtcNow;
job.state = JobState.Waiting;
context.Jobs.Update(job);
}

List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running).ToList().Where(j => j.NextExecution < DateTime.UtcNow).ToList();
foreach (Job job in runJobs)

List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running).AsEnumerable()
.Where(j => j.NextExecution < DateTime.UtcNow).ToList();
foreach (var job in runJobs)
{
Thread t = new (() =>
// If the job is already running, skip it
if (RunningJobs.Values.Any(j => j.JobId == job.JobId)) continue;

Thread t = new(() =>
{
IEnumerable<Job> newJobs = job.Run(context);
context.Jobs.AddRange(newJobs);
Expand All @@ -96,7 +103,7 @@ private static void JobStarter(object? pgsqlContext)
RunningJobs.Remove(thread.thread);
context.Jobs.Update(thread.job);
}

context.SaveChanges();
Thread.Sleep(2000);
}
Expand Down

0 comments on commit 7ccc3cf

Please sign in to comment.