diff --git a/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs b/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs index 6cc629bba0af..55f8290b9acf 100644 --- a/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs +++ b/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs @@ -47,8 +47,17 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) await Task.Delay(_distributedJobSettings.Delay, stoppingToken); } - // Update all jobs, periods might have changed when restarting. - await _distributedJobService.EnsureJobsAsync(); + try + { + // Update all jobs, periods might have changed when restarting. + await _distributedJobService.EnsureJobsAsync(); + } + catch (Exception exception) + { + // We swallow exception here, don't want the app to crash if something goes wrong + _logger.LogError(exception, "An exception occurred while attempting to ensure distributed background jobs on startup."); + } + using PeriodicTimer timer = new(_distributedJobSettings.Period); @@ -56,7 +65,20 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (await timer.WaitForNextTickAsync(stoppingToken)) { - await RunRunnableJob(); + try + { + await RunRunnableJob(); + } + catch (Exception exception) + { + if (exception is OperationCanceledException) + { + // If the operation was canceled, just re-throw to stop the service + throw; + } + + _logger.LogError(exception, "An exception occurred while attempting to run a distributed background job."); + } } } catch (OperationCanceledException)