Files
parr_api/PARR.NextRun/NextRunManager.cs

81 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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}");
}
}
}