Changes to Job.cs:

- Nest try-catch for DBUpdateException and other Exceptions
- More Logging for Jobs
This commit is contained in:
Glax 2025-05-16 21:32:42 +02:00
parent 49b382fe1f
commit 622198a09e

View File

@ -64,29 +64,49 @@ public abstract class Job
public IEnumerable<Job> Run(IServiceProvider serviceProvider) public IEnumerable<Job> Run(IServiceProvider serviceProvider)
{ {
Log.Debug($"Running job {JobId}"); Log.Info($"Running job {JobId}");
using IServiceScope scope = serviceProvider.CreateScope(); DateTime jobStart = DateTime.UtcNow;
Job[]? ret = null;
try try
{ {
using IServiceScope scope = serviceProvider.CreateScope();
PgsqlContext context = scope.ServiceProvider.GetRequiredService<PgsqlContext>(); PgsqlContext context = scope.ServiceProvider.GetRequiredService<PgsqlContext>();
context.Attach(this); try
this.state = JobState.Running; {
context.SaveChanges(); context.Attach(this);
Job[] newJobs = RunInternal(context).ToArray(); this.state = JobState.Running;
this.state = JobState.Completed; context.SaveChanges();
context.SaveChanges(); ret = RunInternal(context).ToArray();
context.Jobs.AddRange(newJobs); this.state = JobState.Completed;
context.SaveChanges(); context.Jobs.AddRange(ret);
Log.Info($"Job {JobId} completed. Generated {newJobs.Length} new jobs."); Log.Info($"Job {JobId} completed. Generated {ret.Length} new jobs.");
return newJobs; }
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 update Database {JobId}", e);
Log.Error($"Failed to run job {JobId}", e);
return [];
} }
Log.Info($"Finished Job {JobId}! (took {DateTime.UtcNow.Subtract(jobStart).TotalMilliseconds}ms)");
return ret ?? [];
} }
protected abstract IEnumerable<Job> RunInternal(PgsqlContext context); protected abstract IEnumerable<Job> RunInternal(PgsqlContext context);