From 725813c2f390945362c02a96fa89d2f967ae9a20 Mon Sep 17 00:00:00 2001 From: Alessandro Benetton Date: Sat, 1 Feb 2025 23:08:13 +0100 Subject: [PATCH] [postgres-Server-V2] feat(Jobs): Instead of passing context to each job, pass service provider and let the job create its own context --- API/Program.cs | 2 +- API/Schema/Jobs/Job.cs | 5 ++++- API/Tranga.cs | 29 +++++++++++++++++------------ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/API/Program.cs b/API/Program.cs index 983853b..e904eac 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -124,7 +124,7 @@ using (var scope = app.Services.CreateScope()) TrangaSettings.Load(); Tranga.StartLogger(); -Tranga.JobStarterThread.Start(app.Services.CreateScope().ServiceProvider.GetService()); +Tranga.JobStarterThread.Start(app.Services); Tranga.NotificationSenderThread.Start(app.Services.CreateScope().ServiceProvider.GetService()); app.UseCors("AllowAll"); diff --git a/API/Schema/Jobs/Job.cs b/API/Schema/Jobs/Job.cs index 16553d2..e5c078e 100644 --- a/API/Schema/Jobs/Job.cs +++ b/API/Schema/Jobs/Job.cs @@ -43,8 +43,11 @@ public abstract class Job RecurrenceMs = recurrenceMs; } - public IEnumerable Run(PgsqlContext context) + public IEnumerable Run(IServiceProvider serviceProvider) { + using IServiceScope scope = serviceProvider.CreateScope(); + PgsqlContext context = scope.ServiceProvider.GetRequiredService(); + this.state = JobState.Running; IEnumerable newJobs = RunInternal(context); this.state = JobState.Completed; diff --git a/API/Tranga.cs b/API/Tranga.cs index 4e958b1..e494fb3 100644 --- a/API/Tranga.cs +++ b/API/Tranga.cs @@ -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(); + if (context is null) return; + + string TRANGA = + "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n"; Log.Info(TRANGA); while (true) { List 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 runJobs = context.Jobs.Where(j => j.state <= JobState.Running).ToList().Where(j => j.NextExecution < DateTime.UtcNow).ToList(); + + List 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 newJobs = job.Run(context); + IEnumerable 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); }