AuctionBackgroundService improvements, fixes, etc...

This commit is contained in:
Loretta 2024-12-21 16:15:49 +01:00
parent c1bd5e6cfb
commit cdc1ecebb9
2 changed files with 33 additions and 7 deletions

View File

@ -0,0 +1,7 @@
using Microsoft.Extensions.Hosting;
namespace Mango.Nop.Services;
public interface IMgBackgroundService : IHostedService, IDisposable
{
}

View File

@ -3,9 +3,20 @@ using Nop.Services.Logging;
namespace Mango.Nop.Services;
public abstract class MgBackgroundServiceBase(ILogger logger, IServiceProvider service) : BackgroundService
public abstract class MgBackgroundServiceBase : BackgroundService, IMgBackgroundService
{
protected abstract Task OnExecuteAsync();
private bool _isPaused;
protected int ExecuteIntervalMs;
protected readonly ILogger Logger;
protected MgBackgroundServiceBase(ILogger logger, IServiceProvider service, int executeIntervalMs)
{
Logger = logger;
ExecuteIntervalMs = executeIntervalMs;
}
protected abstract Task OnExecuteAsync(CancellationToken stoppingToken);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
@ -13,25 +24,33 @@ public abstract class MgBackgroundServiceBase(ILogger logger, IServiceProvider s
{
try
{
await logger.InformationAsync($"MgBackgroundServiceBase.ExecuteAsync(); Processing ExecuteAsync");
await OnExecuteAsync();
await Task.Delay(ExecuteIntervalMs, stoppingToken);
if (_isPaused || stoppingToken.IsCancellationRequested) continue;
await Logger.InformationAsync($"MgBackgroundServiceBase.ExecuteAsync(); Processing ExecuteAsync");
await OnExecuteAsync(stoppingToken);
}
catch (Exception ex)
{
await logger.ErrorAsync($"MgBackgroundServiceBase.ExecuteAsync(); Failure while processing ExecuteAsync", ex, null);
await Logger.ErrorAsync($"MgBackgroundServiceBase.ExecuteAsync(); Failure while processing ExecuteAsync", ex, null);
}
}
}
public override async Task StartAsync(CancellationToken cancellationToken)
{
await logger.InformationAsync("MgBackgroundServiceBase.ExecuteAsync(); Starting MgBackgroundServiceBase");
Pause(false);
await Logger.InformationAsync("MgBackgroundServiceBase.ExecuteAsync(); Starting MgBackgroundServiceBase");
await base.StartAsync(cancellationToken);
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
await logger.InformationAsync("MgBackgroundServiceBase.StopAsync(); Stopping MgBackgroundServiceBase");
await Logger.InformationAsync("MgBackgroundServiceBase.StopAsync(); Stopping MgBackgroundServiceBase");
await base.StopAsync(cancellationToken);
}
public virtual void Pause(bool pause) => _isPaused = pause;
}