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>();
try
{
context.Attach(this); context.Attach(this);
this.state = JobState.Running; this.state = JobState.Running;
context.SaveChanges(); context.SaveChanges();
Job[] newJobs = RunInternal(context).ToArray(); ret = RunInternal(context).ToArray();
this.state = JobState.Completed; this.state = JobState.Completed;
context.SaveChanges(); context.Jobs.AddRange(ret);
context.Jobs.AddRange(newJobs); Log.Info($"Job {JobId} completed. Generated {ret.Length} new jobs.");
context.SaveChanges();
Log.Info($"Job {JobId} completed. Generated {newJobs.Length} new jobs.");
return newJobs;
} }
catch (Exception e) catch (Exception e)
{
if (e is not DbUpdateException dbEx)
{ {
this.state = JobState.Failed; this.state = JobState.Failed;
Log.Error($"Failed to run job {JobId}", e); Log.Error($"Failed to run job {JobId}", e);
return [];
} }
else
{
throw;
}
}
finally
{
context.SaveChanges();
}
}
catch (DbUpdateException e)
{
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<Job> RunInternal(PgsqlContext context); protected abstract IEnumerable<Job> RunInternal(PgsqlContext context);