Do not update context.Jobs on every cycle

This commit is contained in:
Glax 2025-05-16 20:16:11 +02:00
parent 065cac62af
commit 110dd36166

View File

@ -96,6 +96,7 @@ public static class Tranga
private static readonly Dictionary<Thread, Job> RunningJobs = new(); private static readonly Dictionary<Thread, Job> RunningJobs = new();
private static void JobStarter(object? serviceProviderObj) private static void JobStarter(object? serviceProviderObj)
{ {
Log.Info("JobStarter Thread running.");
if (serviceProviderObj is null) if (serviceProviderObj is null)
{ {
Log.Error("serviceProviderObj is null"); Log.Error("serviceProviderObj is null");
@ -103,23 +104,22 @@ public static class Tranga
} }
IServiceProvider serviceProvider = (IServiceProvider)serviceProviderObj; IServiceProvider serviceProvider = (IServiceProvider)serviceProviderObj;
using IServiceScope scope = serviceProvider.CreateScope(); using IServiceScope scope = serviceProvider.CreateScope();
PgsqlContext? context = scope.ServiceProvider.GetService<PgsqlContext>(); PgsqlContext context = scope.ServiceProvider.GetRequiredService<PgsqlContext>();
if (context is null)
{
Log.Error("PgsqlContext is null");
return;
}
Log.Info(TRANGA); DateTime lastContextUpdate = DateTime.UnixEpoch;
Log.Info("Loading Jobs");
context.Jobs.Load();
Log.Info("JobStarter Thread running.");
while (true) while (true)
{ {
if (lastContextUpdate.AddMilliseconds(TrangaSettings.startNewJobTimeoutMs * 10) < DateTime.UtcNow)
{
Log.Info("Loading Jobs...");
context.Jobs.Load();
lastContextUpdate = DateTime.UtcNow;
Log.Info("Jobs Loaded!");
}
foreach (EntityEntry entityEntry in context.ChangeTracker.Entries().ToArray()) foreach (EntityEntry entityEntry in context.ChangeTracker.Entries().ToArray())
entityEntry.Reload(); entityEntry.Reload();
//Update finished Jobs to new states //Update finished Jobs to new states
context.Jobs.Load();
List<Job> completedJobs = context.Jobs.Local.Where(j => j.state == JobState.Completed).ToList(); List<Job> completedJobs = context.Jobs.Local.Where(j => j.state == JobState.Completed).ToList();
foreach (Job completedJob in completedJobs) foreach (Job completedJob in completedJobs)
if (completedJob.RecurrenceMs <= 0) if (completedJob.RecurrenceMs <= 0)