Jobs change RunningJobs to Dictionary of Thread and Job instead of just List of threads

This commit is contained in:
Glax 2024-12-16 23:21:13 +01:00
parent 84388a469a
commit df319e9afb

View File

@ -10,7 +10,7 @@ public static class Tranga
{ {
public static Thread NotificationSenderThread { get; } = new (NotificationSender); public static Thread NotificationSenderThread { get; } = new (NotificationSender);
public static Thread JobStarterThread { get; } = new (JobStarter); public static Thread JobStarterThread { get; } = new (JobStarter);
private static readonly List<Thread> 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));
internal static void StartLogger() internal static void StartLogger()
@ -63,6 +63,7 @@ public static class Tranga
string TRANGA = "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n"; string TRANGA = "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
Log.Info(TRANGA); Log.Info(TRANGA);
List<Job> newJobs = new();
while (true) while (true)
{ {
List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList(); List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
@ -81,17 +82,22 @@ public static class Tranga
{ {
Thread t = new (() => Thread t = new (() =>
{ {
IEnumerable<Job> newJobs = job.Run(); newJobs.AddRange(job.Run());
context.Jobs.AddRange(newJobs);
}); });
RunningJobs.Add(t); RunningJobs.Add(t, job);
t.Start(); t.Start();
context.Jobs.Update(job); context.Jobs.Update(job);
} }
context.Jobs.AddRange(newJobs);
Thread[] removeFromThreadsList = RunningJobs.Where(t => !t.IsAlive).ToArray(); newJobs.Clear();
foreach (Thread thread in removeFromThreadsList)
RunningJobs.Remove(thread); (Thread, Job)[] removeFromThreadsList = RunningJobs.Where(t => !t.Key.IsAlive)
.Select(t => (t.Key, t.Value)).ToArray();
foreach ((Thread thread, Job job) thread in removeFromThreadsList)
{
RunningJobs.Remove(thread.thread);
context.Jobs.Update(thread.job);
}
context.SaveChanges(); context.SaveChanges();
Thread.Sleep(2000); Thread.Sleep(2000);