feat(bll): IntervalService интервал рассчитывается правильно, с учетом времени выполнения основного метода, применена к Master, EsppOrderLoader, AIHIT.
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\PARR.BLL\PARR.BLL.csproj" />
|
||||||
<ProjectReference Include="..\..\PARR.DAL\PARR.DAL.csproj" />
|
<ProjectReference Include="..\..\PARR.DAL\PARR.DAL.csproj" />
|
||||||
<ProjectReference Include="..\..\PARR.Mail\PARR.Mail\PARR.Mail.csproj" />
|
<ProjectReference Include="..\..\PARR.Mail\PARR.Mail\PARR.Mail.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -14,15 +14,29 @@ namespace PARR.BLL.Services.Implementations
|
|||||||
|
|
||||||
public async Task IntervalInitAsync(IntervalHandlerDelegate intervalHandler, TimeSpan interval)
|
public async Task IntervalInitAsync(IntervalHandlerDelegate intervalHandler, TimeSpan interval)
|
||||||
{
|
{
|
||||||
|
var nextStart = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
logger.LogInformation($"Запуск по расписанию. Интервал: {interval}");
|
nextStart = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
|
logger.LogInformation($"Запуск по расписанию. Интервал: {interval}, дата: {nextStart}");
|
||||||
|
|
||||||
await intervalHandler.Invoke();
|
await intervalHandler.Invoke();
|
||||||
|
|
||||||
logger.LogInformation($"Следующая дата запуска: {DateTimeOffset.Now.Add(interval)}");
|
nextStart = nextStart.Add(interval);
|
||||||
|
|
||||||
await Task.Delay(interval);
|
logger.LogInformation($"Следующая дата запуска: {nextStart}");
|
||||||
|
|
||||||
|
var intervalOffset = nextStart - DateTimeOffset.UtcNow;
|
||||||
|
if (intervalOffset < TimeSpan.Zero)
|
||||||
|
{
|
||||||
|
intervalOffset = TimeSpan.Zero;
|
||||||
|
logger.LogWarning($"Время следующего запуска изменено на сейчас. Так как рассчитанное время запуска меньше чем сейчас, {nextStart}<{DateTimeOffset.UtcNow}. " +
|
||||||
|
$"Это произошло потому что время выполнения основного метода больше чем заданный интервал повторений.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await Task.Delay(intervalOffset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\PARR.BLL\PARR.BLL.csproj" />
|
||||||
<ProjectReference Include="..\PARR.EsppApi\PARR.EsppApi.csproj" />
|
<ProjectReference Include="..\PARR.EsppApi\PARR.EsppApi.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using PARR.BLL;
|
||||||
using PARR.EsppApi;
|
using PARR.EsppApi;
|
||||||
using PARR.Test;
|
using PARR.Test;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
@@ -6,6 +7,7 @@ IHost host = Host.CreateDefaultBuilder(args)
|
|||||||
.ConfigureServices((hostContext, services) =>
|
.ConfigureServices((hostContext, services) =>
|
||||||
{
|
{
|
||||||
services.InstallEsppApiServices(hostContext.Configuration);
|
services.InstallEsppApiServices(hostContext.Configuration);
|
||||||
|
services.InstallBllServices(hostContext.Configuration);
|
||||||
|
|
||||||
services.AddHostedService<Worker>();
|
services.AddHostedService<Worker>();
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using PARR.BLL.Services.Interfaces;
|
||||||
using PARR.EsppApi;
|
using PARR.EsppApi;
|
||||||
using PARR.EsppApi.Constants;
|
using PARR.EsppApi.Constants;
|
||||||
using PARR.EsppApi.Models.Query;
|
using PARR.EsppApi.Models.Query;
|
||||||
@@ -8,15 +9,26 @@ namespace PARR.Test
|
|||||||
{
|
{
|
||||||
private readonly ILogger<Worker> _logger;
|
private readonly ILogger<Worker> _logger;
|
||||||
private readonly IEsppApiService esppApiService;
|
private readonly IEsppApiService esppApiService;
|
||||||
|
private readonly IIntervalService intervalService;
|
||||||
|
|
||||||
public Worker(ILogger<Worker> logger, IEsppApiService esppApiService)
|
public Worker(ILogger<Worker> logger, IEsppApiService esppApiService, IIntervalService intervalService)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
this.esppApiService = esppApiService;
|
this.esppApiService = esppApiService;
|
||||||
|
this.intervalService = intervalService;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
|
////test interval
|
||||||
|
//await intervalService.IntervalInitAsync(async () =>
|
||||||
|
//{
|
||||||
|
// _logger.LogInformation("---Выполняю метод---");
|
||||||
|
// await Task.Delay(29*1000);
|
||||||
|
// _logger.LogInformation("===Выполнил метод===");
|
||||||
|
//}, new TimeSpan(0, 0, 30));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
await TestEsppApi();
|
await TestEsppApi();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,5 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\PARR.AIHIT\AIHIT\PARR.AIHIT.csproj" />
|
<ProjectReference Include="..\..\PARR.AIHIT\AIHIT\PARR.AIHIT.csproj" />
|
||||||
<ProjectReference Include="..\..\PARR.DAL\PARR.DAL.csproj" />
|
|
||||||
<ProjectReference Include="..\..\PARR.Mail\PARR.Mail\PARR.Mail.csproj" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using PARR.AIHIT;
|
using PARR.AIHIT;
|
||||||
|
using PARR.BLL.Services.Interfaces;
|
||||||
using PARR.Worker.Settings;
|
using PARR.Worker.Settings;
|
||||||
|
|
||||||
namespace PARR.Worker
|
namespace PARR.Worker
|
||||||
@@ -8,42 +9,64 @@ namespace PARR.Worker
|
|||||||
private readonly ILogger<Worker> logger;
|
private readonly ILogger<Worker> logger;
|
||||||
private readonly IServiceProvider serviceProvider;
|
private readonly IServiceProvider serviceProvider;
|
||||||
private readonly WorkerSettings workerSettings;
|
private readonly WorkerSettings workerSettings;
|
||||||
|
private readonly IIntervalService intervalService;
|
||||||
|
|
||||||
public Worker(ILogger<Worker> logger,
|
public Worker(
|
||||||
|
ILogger<Worker> logger,
|
||||||
IServiceProvider serviceProvider,
|
IServiceProvider serviceProvider,
|
||||||
WorkerSettings workerSettings
|
WorkerSettings workerSettings,
|
||||||
|
IIntervalService intervalService
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.serviceProvider = serviceProvider;
|
this.serviceProvider = serviceProvider;
|
||||||
this.workerSettings = workerSettings;
|
this.workerSettings = workerSettings;
|
||||||
|
this.intervalService = intervalService;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
logger.LogInformation("--- Сервис запущен! ---");
|
logger.LogInformation("--- Сервис запущен! ---");
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
|
||||||
{
|
|
||||||
logger.LogInformation("--- --- --- --- Начало синхронизации --- --- --- ---");
|
|
||||||
using var scope = serviceProvider.CreateScope();
|
|
||||||
var services = scope.ServiceProvider;
|
|
||||||
var syncer = services.GetService<ISyncher>();
|
|
||||||
|
|
||||||
if (syncer != null) await syncer.InvokeFromDatabase();
|
await intervalService.IntervalInitAsync(RunAihitSyncAsync, workerSettings.OccursEvery);
|
||||||
|
|
||||||
//var hosts = await hostService.Get().ToListAsync();
|
//while (!stoppingToken.IsCancellationRequested)
|
||||||
//foreach ( var host in hosts ) { logger.LogInformation($"{host.HostName}"); }
|
//{
|
||||||
|
// logger.LogInformation("--- --- --- --- Начало синхронизации --- --- --- ---");
|
||||||
|
// using var scope = serviceProvider.CreateScope();
|
||||||
|
// var services = scope.ServiceProvider;
|
||||||
|
// var syncer = services.GetService<ISyncher>();
|
||||||
|
|
||||||
|
// if (syncer != null) await syncer.InvokeFromDatabase();
|
||||||
|
|
||||||
|
// //var hosts = await hostService.Get().ToListAsync();
|
||||||
|
// //foreach ( var host in hosts ) { logger.LogInformation($"{host.HostName}"); }
|
||||||
|
|
||||||
|
|
||||||
//await attachmentUploader.UploadAsync();
|
// //await attachmentUploader.UploadAsync();
|
||||||
//logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
// //logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||||
logger.LogInformation("=== === === === Конец синхронизации === === === ===");
|
// logger.LogInformation("=== === === === Конец синхронизации === === === ===");
|
||||||
|
|
||||||
|
|
||||||
var mills = (int?) workerSettings?.OccursEvery.TotalMilliseconds ?? 60*1000;
|
// var mills = (int?) workerSettings?.OccursEvery.TotalMilliseconds ?? 60*1000;
|
||||||
await Task.Delay(mills, stoppingToken);
|
// await Task.Delay(mills, stoppingToken);
|
||||||
|
|
||||||
}
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private async Task RunAihitSyncAsync()
|
||||||
|
{
|
||||||
|
logger.LogInformation("--- --- --- --- Начало синхронизации --- --- --- ---");
|
||||||
|
|
||||||
|
using var scope = serviceProvider.CreateScope();
|
||||||
|
var services = scope.ServiceProvider;
|
||||||
|
var syncer = services.GetService<ISyncher>();
|
||||||
|
|
||||||
|
if (syncer != null)
|
||||||
|
await syncer.InvokeFromDatabase();
|
||||||
|
|
||||||
|
logger.LogInformation("=== === === === Конец синхронизации === === === ===");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user