From 622198a09e8ea6f06286cc0340bfd2db79312077 Mon Sep 17 00:00:00 2001 From: Glax Date: Fri, 16 May 2025 21:32:42 +0200 Subject: [PATCH] Changes to Job.cs: - Nest try-catch for DBUpdateException and other Exceptions - More Logging for Jobs --- API/Schema/Jobs/Job.cs | 52 +++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/API/Schema/Jobs/Job.cs b/API/Schema/Jobs/Job.cs index 7b601b5..cf2b5d9 100644 --- a/API/Schema/Jobs/Job.cs +++ b/API/Schema/Jobs/Job.cs @@ -64,29 +64,49 @@ public abstract class Job public IEnumerable Run(IServiceProvider serviceProvider) { - Log.Debug($"Running job {JobId}"); - using IServiceScope scope = serviceProvider.CreateScope(); + Log.Info($"Running job {JobId}"); + DateTime jobStart = DateTime.UtcNow; + Job[]? ret = null; try { + + using IServiceScope scope = serviceProvider.CreateScope(); PgsqlContext context = scope.ServiceProvider.GetRequiredService(); - context.Attach(this); - this.state = JobState.Running; - context.SaveChanges(); - Job[] newJobs = RunInternal(context).ToArray(); - this.state = JobState.Completed; - context.SaveChanges(); - context.Jobs.AddRange(newJobs); - context.SaveChanges(); - Log.Info($"Job {JobId} completed. Generated {newJobs.Length} new jobs."); - return newJobs; + try + { + context.Attach(this); + this.state = JobState.Running; + context.SaveChanges(); + ret = RunInternal(context).ToArray(); + this.state = JobState.Completed; + context.Jobs.AddRange(ret); + Log.Info($"Job {JobId} completed. Generated {ret.Length} new jobs."); + } + catch (Exception e) + { + if (e is not DbUpdateException dbEx) + { + this.state = JobState.Failed; + Log.Error($"Failed to run job {JobId}", e); + } + else + { + throw; + } + } + finally + { + context.SaveChanges(); + } } - catch (Exception e) + catch (DbUpdateException e) { - this.state = JobState.Failed; - Log.Error($"Failed to run job {JobId}", e); - return []; + Log.Error($"Failed to update Database {JobId}", e); } + + Log.Info($"Finished Job {JobId}! (took {DateTime.UtcNow.Subtract(jobStart).TotalMilliseconds}ms)"); + return ret ?? []; } protected abstract IEnumerable RunInternal(PgsqlContext context);