diff --git a/src/Umbraco.Infrastructure/BackgroundJobs/RecurringBackgroundJobHostedServiceRunner.cs b/src/Umbraco.Infrastructure/BackgroundJobs/RecurringBackgroundJobHostedServiceRunner.cs index 0e56bfa2b1..e86b8c64bd 100644 --- a/src/Umbraco.Infrastructure/BackgroundJobs/RecurringBackgroundJobHostedServiceRunner.cs +++ b/src/Umbraco.Infrastructure/BackgroundJobs/RecurringBackgroundJobHostedServiceRunner.cs @@ -1,12 +1,5 @@ -using System.Linq; -using System.Threading; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using Umbraco.Cms.Core.Runtime; -using Umbraco.Cms.Core.Services; -using Umbraco.Cms.Core.Sync; -using Umbraco.Cms.Infrastructure.ModelsBuilder; namespace Umbraco.Cms.Infrastructure.BackgroundJobs; @@ -18,7 +11,7 @@ public class RecurringBackgroundJobHostedServiceRunner : IHostedService private readonly ILogger _logger; private readonly List _jobs; private readonly Func _jobFactory; - private IList _hostedServices = new List(); + private readonly List _hostedServices = new(); public RecurringBackgroundJobHostedServiceRunner( @@ -33,49 +26,61 @@ public class RecurringBackgroundJobHostedServiceRunner : IHostedService public async Task StartAsync(CancellationToken cancellationToken) { - _logger.LogInformation("Creating recurring background jobs hosted services"); - - // create hosted services for each background job - _hostedServices = _jobs.Select(_jobFactory).ToList(); - _logger.LogInformation("Starting recurring background jobs hosted services"); - foreach (IHostedService hostedService in _hostedServices) + foreach (IRecurringBackgroundJob job in _jobs) { + var jobName = job.GetType().Name; try { - _logger.LogInformation($"Starting background hosted service for {hostedService.GetType().Name}"); + + _logger.LogDebug("Creating background hosted service for {job}", jobName); + IHostedService hostedService = _jobFactory(job); + + _logger.LogInformation("Starting background hosted service for {job}", jobName); await hostedService.StartAsync(cancellationToken).ConfigureAwait(false); + + _hostedServices.Add(new NamedServiceJob(jobName, hostedService)); } catch (Exception exception) { - _logger.LogError(exception, $"Failed to start background hosted service for {hostedService.GetType().Name}"); + _logger.LogError(exception, "Failed to start background hosted service for {job}", jobName); } } _logger.LogInformation("Completed starting recurring background jobs hosted services"); - - } public async Task StopAsync(CancellationToken stoppingToken) { _logger.LogInformation("Stopping recurring background jobs hosted services"); - foreach (IHostedService hostedService in _hostedServices) + foreach (NamedServiceJob namedServiceJob in _hostedServices) { try { - _logger.LogInformation($"Stopping background hosted service for {hostedService.GetType().Name}"); - await hostedService.StopAsync(stoppingToken).ConfigureAwait(false); + _logger.LogInformation("Stopping background hosted service for {job}", namedServiceJob.Name); + await namedServiceJob.HostedService.StopAsync(stoppingToken).ConfigureAwait(false); } catch (Exception exception) { - _logger.LogError(exception, $"Failed to stop background hosted service for {hostedService.GetType().Name}"); + _logger.LogError(exception, "Failed to stop background hosted service for {job}", namedServiceJob.Name); } } _logger.LogInformation("Completed stopping recurring background jobs hosted services"); + } + private class NamedServiceJob + { + public NamedServiceJob(string name, IHostedService hostedService) + { + Name = name; + HostedService = hostedService; + } + + public string Name { get; } + + public IHostedService HostedService { get; } } }