mirror of
https://github.com/C9Glax/tranga.git
synced 2025-06-19 09:37:53 +02:00
Scoped PGSql Contexts for Threads
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using API.Schema;
|
||||
using API.Schema.Jobs;
|
||||
using API.Schema.NotificationConnectors;
|
||||
using log4net;
|
||||
using log4net.Config;
|
||||
@ -9,7 +10,8 @@ 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));
|
||||
private static readonly List<Thread> RunningJobs = new();
|
||||
private static readonly ILog Log = LogManager.GetLogger(typeof(Tranga));
|
||||
|
||||
internal static void StartLogger()
|
||||
{
|
||||
@ -63,7 +65,36 @@ public static class Tranga
|
||||
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 < 1)
|
||||
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 && j.NextExecution < DateTime.UtcNow).ToList();
|
||||
foreach (Job job in runJobs)
|
||||
{
|
||||
Thread t = new (() =>
|
||||
{
|
||||
IEnumerable<Job> newJobs = job.Run();
|
||||
context.Jobs.AddRange(newJobs);
|
||||
});
|
||||
RunningJobs.Add(t);
|
||||
t.Start();
|
||||
context.Jobs.Update(job);
|
||||
}
|
||||
|
||||
Thread[] removeFromThreadsList = RunningJobs.Where(t => !t.IsAlive).ToArray();
|
||||
foreach (Thread thread in removeFromThreadsList)
|
||||
RunningJobs.Remove(thread);
|
||||
|
||||
context.SaveChanges();
|
||||
Thread.Sleep(2000);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user