Adding
This commit is contained in:
+68
@@ -0,0 +1,68 @@
|
||||
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 BatchErroredJobsHostedService : LCUBackgroundService, IBatchErroredJobsHostedService
|
||||
{
|
||||
private readonly ApiConfiguration _apiConfiguration;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly CrontabSchedule _batchErroredJobsSchedule;
|
||||
private DateTime _nextScheduleRun;
|
||||
public const string DefaultBatchErroredJobsCronPattern = "*/15 * * * *";
|
||||
private CancellationToken _cancellationToken;
|
||||
|
||||
public BatchErroredJobsHostedService(IServiceScopeFactory scopeFactory,
|
||||
ApiConfiguration apiConfiguration)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
_apiConfiguration = apiConfiguration;
|
||||
_batchErroredJobsSchedule = CronHelpers.SetCronSchedule(_apiConfiguration.BatchErroredJobsCronSchedule,
|
||||
DefaultBatchErroredJobsCronPattern);
|
||||
_nextScheduleRun = _batchErroredJobsSchedule.GetNextOccurrence(DateTime.Now);
|
||||
_cancellationToken = new CancellationToken();
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!_apiConfiguration.FeatureFlags.RunBatchErroredJobsHostedService)
|
||||
{
|
||||
Log.Error("Batch Errored Jobs Hosted Service is disabled by feature flag. Service not started.");
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Debug("Dispatcher's Batch Errored Jobs Hosted Service is starting.");
|
||||
try
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(UntilNextExecution(), cancellationToken); // wait until next time
|
||||
|
||||
using (var scope = _scopeFactory.CreateAsyncScope())
|
||||
{
|
||||
Log.Debug("Running Batch Errored Jobs Service");
|
||||
await scope.ServiceProvider.GetRequiredService<IBatchErroredJobsService>().PerformBatchErroredJobs(_cancellationToken);
|
||||
}
|
||||
|
||||
_nextScheduleRun = _batchErroredJobsSchedule.GetNextOccurrence(DateTime.Now);
|
||||
}
|
||||
}
|
||||
catch(OperationCanceledException taskCanceledEx)
|
||||
{
|
||||
Log.Error(taskCanceledEx, $"Manual start/stop of Dispatcher's Batch Errored Jobs Hosted Service was initiated.");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception,
|
||||
"Error trying to startup main thread for Dispatcher's Batch Errored Jobs Hosted Service");
|
||||
}
|
||||
|
||||
Log.Debug("Dispatcher's Batch Errored Jobs Hosted Service is stopping.");
|
||||
}
|
||||
|
||||
private int UntilNextExecution() => Math.Max(0, (int)_nextScheduleRun.Subtract(DateTime.Now).TotalMilliseconds);
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
using LCU.Api.Internal.Dispatcher.Configurations;
|
||||
using LCU.Api.Internal.DispatcherSharedLib.Utilities;
|
||||
using LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NCrontab;
|
||||
using System.Threading;
|
||||
using LCU.Library.Internal.LCUHostedService.LCUBackgroundService;
|
||||
|
||||
namespace LCU.Api.Internal.Dispatcher.HostedServices;
|
||||
|
||||
public class CleanUpJobsHostedService : LCUBackgroundService, ICleanUpJobsHostedService
|
||||
{
|
||||
private readonly ApiConfiguration _apiConfiguration;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly CrontabSchedule _cleanUpJobSchedule;
|
||||
private DateTime _nextScheduleRun;
|
||||
public const string DefaultCleanUpJobCronPattern = "* 22 * * *";
|
||||
private CancellationToken _cancellationToken;
|
||||
|
||||
public CleanUpJobsHostedService(IServiceScopeFactory scopeFactory,
|
||||
ApiConfiguration apiConfiguration)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
_apiConfiguration = apiConfiguration;
|
||||
_cleanUpJobSchedule = CronHelpers.SetCronSchedule(_apiConfiguration.CleanUpJobsCronSchedule,
|
||||
DefaultCleanUpJobCronPattern);
|
||||
_nextScheduleRun = _cleanUpJobSchedule.GetNextOccurrence(DateTime.Now);
|
||||
_cancellationToken = new CancellationToken();
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!_apiConfiguration.FeatureFlags.RunCleanUpJobsHostedService)
|
||||
{
|
||||
Log.Error("Clean Up Job Hosted Service is disabled by feature flag. Service not started.");
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Debug("Dispatcher's Clean Up Job Hosted Service is starting.");
|
||||
try
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(UntilNextExecution(), cancellationToken); // wait until next time
|
||||
|
||||
using (var scope = _scopeFactory.CreateAsyncScope())
|
||||
{
|
||||
Log.Debug("Running Clean Up Job Service");
|
||||
await scope.ServiceProvider.GetRequiredService<ICleanUpJobsService>().PerformCleanUpJobs(_cancellationToken);
|
||||
}
|
||||
|
||||
_nextScheduleRun = _cleanUpJobSchedule.GetNextOccurrence(DateTime.Now);
|
||||
}
|
||||
}
|
||||
catch(OperationCanceledException taskCanceledEx)
|
||||
{
|
||||
Log.Error(taskCanceledEx, $"Manual start/stop of Dispatcher's Clean Up Job Hosted Service was initiated.");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception,
|
||||
"Error trying to startup main thread for Dispatcher's Clean Up Job Hosted Service");
|
||||
}
|
||||
|
||||
Log.Debug("Dispatcher's Clean Up Job Hosted Service is stopping.");
|
||||
}
|
||||
|
||||
private int UntilNextExecution() => Math.Max(0, (int)_nextScheduleRun.Subtract(DateTime.Now).TotalMilliseconds);
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using LCU.Library.Internal.LCUHostedService.LCUBackgroundService;
|
||||
|
||||
namespace LCU.Api.Internal.Dispatcher.HostedServices
|
||||
{
|
||||
public interface IBatchErroredJobsHostedService : ILCUBackgroundService
|
||||
{
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using LCU.Library.Internal.LCUHostedService.LCUBackgroundService;
|
||||
|
||||
namespace LCU.Api.Internal.Dispatcher.HostedServices
|
||||
{
|
||||
public interface ICleanUpJobsHostedService : ILCUBackgroundService
|
||||
{
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using LCU.Library.Internal.LCUHostedService.LCUBackgroundService;
|
||||
|
||||
namespace LCU.Api.Internal.Dispatcher.HostedServices
|
||||
{
|
||||
public interface IJobSchedulerHostedService : ILCUBackgroundService
|
||||
{
|
||||
}
|
||||
}
|
||||
+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