From 3fe3fc09b0b30577926d25e10ead145c940d5163 Mon Sep 17 00:00:00 2001 From: Glax Date: Sun, 18 May 2025 15:21:59 +0200 Subject: [PATCH] JobContext per Job --- API/Schema/Jobs/Job.cs | 5 ++--- API/Tranga.cs | 4 +++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/API/Schema/Jobs/Job.cs b/API/Schema/Jobs/Job.cs index e7c4ac6..7b70db2 100644 --- a/API/Schema/Jobs/Job.cs +++ b/API/Schema/Jobs/Job.cs @@ -67,14 +67,13 @@ public abstract class Job this.Log = LogManager.GetLogger(this.GetType()); } - public IEnumerable Run(IServiceProvider serviceProvider) + public IEnumerable Run(PgsqlContext context) { Log.Info($"Running job {JobId}"); DateTime jobStart = DateTime.UtcNow; + context.Attach(this); Job[]? ret = null; - using IServiceScope scope = serviceProvider.CreateScope(); - PgsqlContext context = scope.ServiceProvider.GetRequiredService(); try { this.state = JobState.Running; diff --git a/API/Tranga.cs b/API/Tranga.cs index fa37bd4..a5f7e8d 100644 --- a/API/Tranga.cs +++ b/API/Tranga.cs @@ -172,7 +172,9 @@ public static class Tranga { Thread t = new(() => { - job.Run(serviceProvider); + using IServiceScope jobScope = serviceProvider.CreateScope(); + PgsqlContext jobContext = jobScope.ServiceProvider.GetRequiredService(); + job.Run(jobContext); }); RunningJobs.Add(t, job); t.Start();