feat(dal, esppSync, nextRun): При синхронизации расписаний и шаблонов, при расчете nextRun в NextRunWorker - установить задание на обновление объекта, можно только если объект в статусе Ок. NextRunWorker - рефакторинг.
This commit is contained in:
17
PARR.NextRun/Services/INextRunUpdateService.cs
Normal file
17
PARR.NextRun/Services/INextRunUpdateService.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using PARR.Common.Domain;
|
||||
using PARR.DAL.Models;
|
||||
|
||||
namespace PARR.NextRun.Services
|
||||
{
|
||||
internal interface INextRunUpdateService
|
||||
{
|
||||
/// <summary>
|
||||
/// Обновить NextRun для списка шаблонов
|
||||
/// </summary>
|
||||
/// <param name="filter">Фильтр выборки шаблонов</param>
|
||||
/// <param name="getInitiator"></param>
|
||||
/// <param name="operationName"></param>
|
||||
/// <returns></returns>
|
||||
Task UpdateNextRunForTemplatesAsync(Func<IQueryable<Template>, IQueryable<Template>> filter, Func<IHistoryInitiator> getInitiator, string operationName);
|
||||
}
|
||||
}
|
||||
114
PARR.NextRun/Services/NextRunUpdateService.cs
Normal file
114
PARR.NextRun/Services/NextRunUpdateService.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.Common.Domain;
|
||||
using PARR.Constants;
|
||||
using PARR.DAL.Models;
|
||||
using PARR.DAL.NextRunServices;
|
||||
using PARR.DAL.Services.Interfaces;
|
||||
|
||||
namespace PARR.NextRun.Services
|
||||
{
|
||||
internal class NextRunUpdateService : INextRunUpdateService
|
||||
{
|
||||
private readonly ILogger<NextRunUpdateService> logger;
|
||||
private readonly ITemplateService templateService;
|
||||
private readonly INextRunService nextRunService;
|
||||
private readonly IRobotConfigurationService robotConfigurationService;
|
||||
|
||||
public NextRunUpdateService(
|
||||
ILogger<NextRunUpdateService> logger,
|
||||
ITemplateService templateService,
|
||||
INextRunService nextRunService,
|
||||
IRobotConfigurationService robotConfigurationService
|
||||
)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.templateService = templateService;
|
||||
this.nextRunService = nextRunService;
|
||||
this.robotConfigurationService = robotConfigurationService;
|
||||
}
|
||||
|
||||
public async Task UpdateNextRunForTemplatesAsync(Func<IQueryable<Template>, IQueryable<Template>> filter, Func<IHistoryInitiator> getInitiator, string operationName)
|
||||
{
|
||||
var query = templateService.Get()
|
||||
.Include(t => t.RobotConfigurations)
|
||||
.Where(t => t.StatusTypeId == TemplateStatusTypeEnum.Used);
|
||||
|
||||
var templates = await filter(query).ToListAsync();
|
||||
|
||||
logger.LogInformation("[{operationName}] Найдено шаблонов в статусе Used для обработки: {count}", operationName, templates.Count);
|
||||
|
||||
if (!templates.Any())
|
||||
{
|
||||
logger.LogInformation("[{operationName}] Шаблоны не найдены, выходим", operationName);
|
||||
return;
|
||||
}
|
||||
|
||||
var updatedCount = 0;
|
||||
|
||||
foreach (var template in templates)
|
||||
{
|
||||
var wasUpdated = await UpdateNextRunAsync(template);
|
||||
|
||||
if (wasUpdated)
|
||||
updatedCount++;
|
||||
}
|
||||
|
||||
if (updatedCount == 0)
|
||||
{
|
||||
logger.LogInformation("[{operationName}] Нет изменённых nextRun, сохранение не требуется", operationName);
|
||||
return;
|
||||
}
|
||||
|
||||
var initiator = getInitiator();
|
||||
var commitResult = await templateService.CommitAsync(initiator);
|
||||
|
||||
if (commitResult)
|
||||
logger.LogInformation("[{operationName}] Успешно обновлено {count} шаблонов", operationName, updatedCount);
|
||||
else
|
||||
logger.LogError("[{operationName}] Ошибка при сохранении {count} шаблонов в БД", operationName, updatedCount);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Обновляет если надо nextRun для одного шаблона, если может, ставит задание на обновление
|
||||
/// </summary>
|
||||
/// <param name="template"></param>
|
||||
/// <param name="nextRunService"></param>
|
||||
/// <param name="robotConfigurationService"></param>
|
||||
/// <param name="templateName"></param>
|
||||
/// <param name="logger"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<bool> UpdateNextRunAsync(Template template)
|
||||
{
|
||||
var newNextRun = await nextRunService.GetNextRunForTemplateAsync(template.Id, false);
|
||||
|
||||
if (!newNextRun.HasValue)
|
||||
{
|
||||
logger.LogError("При расчете nextRun для шаблона {templateId}, '{templateName}' вернулся null. Это значит что при расчете возникла ошибка.", template.Id, template.Name);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (template.NextRun == newNextRun.Value)
|
||||
{
|
||||
logger.LogDebug("NextRun для шаблона {templateId}, '{templateName}' не изменился: {nextRun}", template.Id, template.Name, newNextRun.Value);
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.LogInformation("Обновление NextRun: шаблон {templateId}, '{templateName}'. Было: {oldNextRun}, стало: {newNextRun}", template.Id, template.Name, template.NextRun, newNextRun.Value);
|
||||
|
||||
template.LastRun = template.NextRun;
|
||||
template.NextRun = newNextRun.Value;
|
||||
|
||||
//так как nextRun обновился, пробуем поставить задание на обновление
|
||||
var config = robotConfigurationService.GetFromTemplateByRobotCode(RobotsEnum.ScheduleOrder, template);
|
||||
robotConfigurationService.SetUpdateTaskStatusIfAllow(config);
|
||||
|
||||
//шаблон изменен и нужен Commit
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user