From ae1591dc54da837c4c4be7cb4e7381858bad72a8 Mon Sep 17 00:00:00 2001 From: mole Date: Tue, 9 Dec 2025 13:42:11 +0100 Subject: [PATCH 1/3] Set exit code when exception is thrown --- .../DistributedBackgroundJobHostedService.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs b/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs index 01711d64ef2f..9da9c06922f3 100644 --- a/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs +++ b/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Umbraco.Cms.Core; @@ -22,10 +21,6 @@ public class DistributedBackgroundJobHostedService : BackgroundService /// /// Initializes a new instance of the class. /// - /// - /// - /// - /// public DistributedBackgroundJobHostedService( ILogger logger, IRuntimeState runtimeState, @@ -68,6 +63,13 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Timed Hosted Service is stopping."); } + catch (Exception) + { + // Ensure that the app doesn't shut down gracefully so that environments like Kubernetes can restart it + // See https://github.com/dotnet/runtime/issues/67146 for more information. + Environment.ExitCode = 1; + throw; + } } private async Task RunRunnableJob() From f58e2114c207f7638726c133160c08d932487428 Mon Sep 17 00:00:00 2001 From: mole Date: Wed, 10 Dec 2025 14:37:22 +0100 Subject: [PATCH 2/3] Catch any error instead so we don't stop the application --- .../DistributedBackgroundJobHostedService.cs | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs b/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs index 9da9c06922f3..3f9983f91799 100644 --- a/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs +++ b/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs @@ -56,20 +56,26 @@ 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) { _logger.LogInformation("Timed Hosted Service is stopping."); } - catch (Exception) - { - // Ensure that the app doesn't shut down gracefully so that environments like Kubernetes can restart it - // See https://github.com/dotnet/runtime/issues/67146 for more information. - Environment.ExitCode = 1; - throw; - } } private async Task RunRunnableJob() From d0857acc459fc58b9a6cc44a2a17fbffe7e30bd2 Mon Sep 17 00:00:00 2001 From: mole Date: Fri, 12 Dec 2025 12:41:22 +0100 Subject: [PATCH 3/3] Handle exception when ensuring jobs --- .../DistributedBackgroundJobHostedService.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs b/src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.cs index 3f9983f91799..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);