Log meaningful job names and use template based log messages (#15307)

* Log meaningful job names and use template based log messages

* Review changes
This commit is contained in:
Kenn Jacobsen
2023-11-29 15:35:43 +01:00
committed by GitHub
parent a8dc058cb5
commit b596900246

View File

@@ -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<RecurringBackgroundJobHostedServiceRunner> _logger;
private readonly List<IRecurringBackgroundJob> _jobs;
private readonly Func<IRecurringBackgroundJob, IHostedService> _jobFactory;
private IList<IHostedService> _hostedServices = new List<IHostedService>();
private readonly List<NamedServiceJob> _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; }
}
}