Adding
This commit is contained in:
+58
@@ -0,0 +1,58 @@
|
||||
using System.Threading;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NCrontab;
|
||||
using LCU.Api.Internal.DispatcherSharedLib.Utilities;
|
||||
using LCU.Api.Internal.Dispatcher.Configurations;
|
||||
using LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices;
|
||||
using LCU.Library.Internal.LCUHostedService.LCUBackgroundService;
|
||||
namespace LCU.Api.Internal.Dispatcher.HostedServices;
|
||||
|
||||
public class JobSchedulerHostedService : LCUBackgroundService, IJobSchedulerHostedService
|
||||
{
|
||||
private readonly ApiConfiguration _apiConfiguration;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private CancellationToken _cancellationToken;
|
||||
|
||||
public JobSchedulerHostedService(IServiceScopeFactory scopeFactory,
|
||||
ApiConfiguration apiConfiguration)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
_apiConfiguration = apiConfiguration;
|
||||
_cancellationToken = new CancellationToken();
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!_apiConfiguration.FeatureFlags.RunJobSchedulerHostedService)
|
||||
{
|
||||
Log.Error("Job Scheduler Hosted Service is disabled by feature flag. Service not started.");
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Debug("Dispatcher's Job Schedule Hosted Service is starting.");
|
||||
try
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(_apiConfiguration.DefaultDelayMilliseconds, cancellationToken); // wait until next time
|
||||
|
||||
using (var scope = _scopeFactory.CreateAsyncScope())
|
||||
{
|
||||
Log.Debug("Running Job Schedule Service");
|
||||
await scope.ServiceProvider.GetRequiredService<IJobScheduleService>().PerformSchedule(_cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(OperationCanceledException taskCanceledEx)
|
||||
{
|
||||
Log.Error(taskCanceledEx, $"Manual start/stop of Dispatcher's Job Scheduler Hosted Service was initiated.");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception,
|
||||
"Error trying to startup main thread for Dispatcher's Job Schedule Hosted Service");
|
||||
}
|
||||
|
||||
Log.Debug("Dispatcher's Job Schedule Hosted Service is stopping.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user