feat(nextRun): рассчет поля NextRun по расписанию
This commit is contained in:
7
PARR.NextRun/INextRunManager.cs
Normal file
7
PARR.NextRun/INextRunManager.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace PARR.NextRun
|
||||
{
|
||||
public interface INextRunManager
|
||||
{
|
||||
Task StartAsync();
|
||||
}
|
||||
}
|
||||
23
PARR.NextRun/NextRunInstaller.cs
Normal file
23
PARR.NextRun/NextRunInstaller.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PARR.BLL;
|
||||
using PARR.DAL;
|
||||
using PARR.NextRun.Settings;
|
||||
|
||||
namespace PARR.NextRun
|
||||
{
|
||||
public static class NextRunInstaller
|
||||
{
|
||||
public static void InstallNextRunServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.InstallBllServices(configuration);
|
||||
services.InstallDalServices(configuration);
|
||||
|
||||
var settings = new WorkerSettings();
|
||||
configuration.GetSection(nameof(WorkerSettings)).Bind(settings);
|
||||
services.AddSingleton(settings);
|
||||
|
||||
services.AddTransient<INextRunManager, NextRunManager>();
|
||||
}
|
||||
}
|
||||
}
|
||||
80
PARR.NextRun/NextRunManager.cs
Normal file
80
PARR.NextRun/NextRunManager.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.BLL.Services.Interfaces;
|
||||
using PARR.DAL.Services.Interfaces;
|
||||
using PARR.DAL.TransformServices;
|
||||
using PARR.NextRun.Settings;
|
||||
|
||||
namespace PARR.NextRun
|
||||
{
|
||||
internal class NextRunManager : INextRunManager
|
||||
{
|
||||
private readonly WorkerSettings workerSettings;
|
||||
private readonly ILogger<NextRunManager> logger;
|
||||
private readonly IIntervalService intervalService;
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
public NextRunManager(
|
||||
WorkerSettings workerSettings,
|
||||
ILogger<NextRunManager> logger,
|
||||
IIntervalService intervalService,
|
||||
IServiceProvider serviceProvider
|
||||
)
|
||||
{
|
||||
this.workerSettings = workerSettings;
|
||||
this.logger = logger;
|
||||
this.intervalService = intervalService;
|
||||
this.serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
logger.LogInformation("Запуск сервиса рассчета времени следующего создания наряда (nextRun)");
|
||||
|
||||
await intervalService.IntervalInitAsync(async () =>
|
||||
{
|
||||
using (var scope = serviceProvider.CreateScope())
|
||||
{
|
||||
var appInWorkService = scope.ServiceProvider.GetService<IApplicationsInWorkService>();
|
||||
var esppSchService = scope.ServiceProvider.GetService<IEsppScheduleTransformService>();
|
||||
|
||||
if (appInWorkService == null || esppSchService == null)
|
||||
throw new Exception("Не смог получить серивс IApplicationsInWorkService или IEsppScheduleTransformService");
|
||||
|
||||
await HandlerAsync(appInWorkService, esppSchService);
|
||||
}
|
||||
}, workerSettings.RepeatEvery);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Рассчет следующей даты срабатываения
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task HandlerAsync(IApplicationsInWorkService applicationsInWorkService, IEsppScheduleTransformService esppScheduleTransformService)
|
||||
{
|
||||
var objs = await applicationsInWorkService.Get().Where(t => t.NextRun < DateTimeOffset.UtcNow).ToListAsync();
|
||||
|
||||
logger.LogDebug($"Объектов для обновления: {objs.Count()} шт.");
|
||||
|
||||
int errorsCount = 0;
|
||||
|
||||
foreach (var obj in objs)
|
||||
{
|
||||
var nextRun = await esppScheduleTransformService.GetNextDateAsync(obj.Id, obj.NextRun);
|
||||
obj.NextRun = nextRun;
|
||||
|
||||
if (await applicationsInWorkService.CommitAsync())
|
||||
logger.LogDebug($"Обновлено значение поле NextRun {obj.NextRun} для строки {obj.Id}");
|
||||
else
|
||||
{
|
||||
logger.LogError($"Ошибка при обновлении значения поля NextRun {obj.NextRun} для строки {obj.Id}");
|
||||
errorsCount++;
|
||||
}
|
||||
}
|
||||
|
||||
logger.LogInformation($"Завершено обновление полей NextRun. Полей в задании: {objs.Count()}, обновлено: {objs.Count() - errorsCount}, ошибок: {errorsCount}");
|
||||
}
|
||||
}
|
||||
}
|
||||
14
PARR.NextRun/PARR.NextRun.csproj
Normal file
14
PARR.NextRun/PARR.NextRun.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PARR.BLL\PARR.BLL.csproj" />
|
||||
<ProjectReference Include="..\PARR.DAL\PARR.DAL.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
7
PARR.NextRun/Settings/WorkerSettings.cs
Normal file
7
PARR.NextRun/Settings/WorkerSettings.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace PARR.NextRun.Settings
|
||||
{
|
||||
internal class WorkerSettings
|
||||
{
|
||||
public TimeSpan RepeatEvery { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user