mirror of
https://github.com/C9Glax/tranga.git
synced 2025-05-09 16:32:09 +02:00
[postgres-Server-V2] fix: Updated Job logic to spawn a job only if a job with the same id is not already in the RunningJobs list
This commit is contained in:
parent
fc8d8b08da
commit
dd1d67ac11
@ -21,8 +21,8 @@ public class Chapter : IComparable<Chapter>
|
|||||||
public string ParentMangaId { get; internal set; }
|
public string ParentMangaId { get; internal set; }
|
||||||
public Manga? ParentManga { get; init; }
|
public Manga? ParentManga { get; init; }
|
||||||
|
|
||||||
public Chapter(Manga parentManga, string url, string chapterNumberStruct, int? volumeNumber = null, string? title = null)
|
public Chapter(Manga parentManga, string url, string chapterNumber, int? volumeNumber = null, string? title = null)
|
||||||
: this(parentManga.MangaId, url, chapterNumberStruct, volumeNumber, title)
|
: this(parentManga.MangaId, url, chapterNumber, volumeNumber, title)
|
||||||
{
|
{
|
||||||
this.ParentManga = parentManga;
|
this.ParentManga = parentManga;
|
||||||
this.ArchiveFileName = BuildArchiveFileName();
|
this.ArchiveFileName = BuildArchiveFileName();
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
using API.Schema;
|
using API.Schema;
|
||||||
using API.Schema.Jobs;
|
using API.Schema.Jobs;
|
||||||
using API.Schema.NotificationConnectors;
|
|
||||||
using log4net;
|
using log4net;
|
||||||
using log4net.Config;
|
using log4net.Config;
|
||||||
|
|
||||||
@ -8,10 +7,10 @@ namespace API;
|
|||||||
|
|
||||||
public static class Tranga
|
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 Dictionary<Thread, Job> RunningJobs = new();
|
||||||
private static readonly ILog Log = LogManager.GetLogger(typeof(Tranga));
|
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()
|
internal static void StartLogger()
|
||||||
{
|
{
|
||||||
@ -20,10 +19,11 @@ public static class Tranga
|
|||||||
|
|
||||||
private static void NotificationSender(object? pgsqlContext)
|
private static void NotificationSender(object? pgsqlContext)
|
||||||
{
|
{
|
||||||
if(pgsqlContext is null) return;
|
if (pgsqlContext is null) return;
|
||||||
PgsqlContext context = (PgsqlContext)pgsqlContext;
|
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.Notifications.RemoveRange(staleNotifications);
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
while (true)
|
while (true)
|
||||||
@ -39,36 +39,38 @@ public static class Tranga
|
|||||||
|
|
||||||
private static void SendNotifications(PgsqlContext context, NotificationUrgency urgency)
|
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())
|
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))
|
if (DateTime.Now.Subtract(max) > TrangaSettings.NotificationUrgencyDelay(urgency))
|
||||||
{
|
{
|
||||||
foreach (NotificationConnector notificationConnector in context.NotificationConnectors)
|
foreach (var notificationConnector in context.NotificationConnectors)
|
||||||
{
|
foreach (var notification in notifications)
|
||||||
foreach (Notification notification in notifications)
|
notificationConnector.SendNotification(notification.Title, notification.Message);
|
||||||
notificationConnector.SendNotification(notification.Title, notification.Message);
|
|
||||||
}
|
|
||||||
context.Notifications.RemoveRange(notifications);
|
context.Notifications.RemoveRange(notifications);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void JobStarter(object? pgsqlContext)
|
private static void JobStarter(object? pgsqlContext)
|
||||||
{
|
{
|
||||||
if(pgsqlContext is null) return;
|
if (pgsqlContext is null) return;
|
||||||
PgsqlContext context = (PgsqlContext)pgsqlContext;
|
var context = (PgsqlContext)pgsqlContext;
|
||||||
|
|
||||||
string TRANGA = "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
|
var TRANGA =
|
||||||
|
"\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
|
||||||
Log.Info(TRANGA);
|
Log.Info(TRANGA);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
|
var completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
|
||||||
foreach (Job job in completedJobs)
|
foreach (var job in completedJobs)
|
||||||
if(job.RecurrenceMs <= 0)
|
if (job.RecurrenceMs <= 0)
|
||||||
|
{
|
||||||
context.Jobs.Remove(job);
|
context.Jobs.Remove(job);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
job.LastExecution = DateTime.UtcNow;
|
job.LastExecution = DateTime.UtcNow;
|
||||||
@ -76,10 +78,14 @@ public static class Tranga
|
|||||||
context.Jobs.Update(job);
|
context.Jobs.Update(job);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running).ToList().Where(j => j.NextExecution < DateTime.UtcNow).ToList();
|
List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running).AsEnumerable()
|
||||||
foreach (Job job in runJobs)
|
.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);
|
IEnumerable<Job> newJobs = job.Run(context);
|
||||||
context.Jobs.AddRange(newJobs);
|
context.Jobs.AddRange(newJobs);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user