[postgres-Server-V2] feat(Jobs): Instead of passing context to each job, pass service provider and let the job create its own context

This commit is contained in:
Alessandro Benetton 2025-02-01 23:08:13 +01:00
parent a69e12179b
commit 725813c2f3
No known key found for this signature in database
GPG Key ID: ED63A304CE2C0303
3 changed files with 22 additions and 14 deletions

View File

@ -124,7 +124,7 @@ using (var scope = app.Services.CreateScope())
TrangaSettings.Load();
Tranga.StartLogger();
Tranga.JobStarterThread.Start(app.Services.CreateScope().ServiceProvider.GetService<PgsqlContext>());
Tranga.JobStarterThread.Start(app.Services);
Tranga.NotificationSenderThread.Start(app.Services.CreateScope().ServiceProvider.GetService<PgsqlContext>());
app.UseCors("AllowAll");

View File

@ -43,8 +43,11 @@ public abstract class Job
RecurrenceMs = recurrenceMs;
}
public IEnumerable<Job> Run(PgsqlContext context)
public IEnumerable<Job> Run(IServiceProvider serviceProvider)
{
using IServiceScope scope = serviceProvider.CreateScope();
PgsqlContext context = scope.ServiceProvider.GetRequiredService<PgsqlContext>();
this.state = JobState.Running;
IEnumerable<Job> newJobs = RunInternal(context);
this.state = JobState.Completed;

View File

@ -56,18 +56,22 @@ public static class Tranga
context.SaveChanges();
}
private static void JobStarter(object? pgsqlContext)
private static void JobStarter(object? serviceProviderObj)
{
if(pgsqlContext is null) return;
PgsqlContext context = (PgsqlContext)pgsqlContext;
string TRANGA = "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
if(serviceProviderObj is null) return;
IServiceProvider serviceProvider = (IServiceProvider)serviceProviderObj;
using IServiceScope scope = serviceProvider.CreateScope();
PgsqlContext? context = scope.ServiceProvider.GetService<PgsqlContext>();
if (context is null) return;
string 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)
if (job.RecurrenceMs <= 0)
context.Jobs.Remove(job);
else
{
@ -75,16 +79,17 @@ public static class Tranga
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();
List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running).ToList()
.Where(j => j.NextExecution < DateTime.UtcNow).ToList();
foreach (Job job in runJobs)
{
// If the job is already running, skip it
if (RunningJobs.Values.Any(j => j.JobId == job.JobId)) continue;
Thread t = new (() =>
Thread t = new(() =>
{
IEnumerable<Job> newJobs = job.Run(context);
IEnumerable<Job> newJobs = job.Run(serviceProvider);
context.Jobs.AddRange(newJobs);
});
RunningJobs.Add(t, job);
@ -99,7 +104,7 @@ public static class Tranga
RunningJobs.Remove(thread.thread);
context.Jobs.Update(thread.job);
}
context.SaveChanges();
Thread.Sleep(2000);
}