feat(dal, esppSync, nextRun): При синхронизации расписаний и шаблонов, при расчете nextRun в NextRunWorker - установить задание на обновление объекта, можно только если объект в статусе Ок. NextRunWorker - рефакторинг.

This commit is contained in:
Mikhail Trubnikov
2026-03-11 12:10:11 +10:00
parent 8cfccd7671
commit b9d1f923b3
10 changed files with 359 additions and 175 deletions

View File

@@ -3,11 +3,8 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PARR.BLL.Domain.Mq;
using PARR.BLL.Services.Interfaces;
using PARR.Common.Domain;
using PARR.Constants;
using PARR.DAL.NextRunServices;
using PARR.DAL.Services.Interfaces;
using PARR.DAL.Services.Interfaces.Job;
using PARR.NextRun.Services;
using PARR.NextRun.Settings;
namespace PARR.NextRun
@@ -61,17 +58,29 @@ namespace PARR.NextRun
{
logger.LogInformation("Получили запрос: {message}", msg);
var query = transformService.GetModelFromJson<NextRunUpdateMq>(msg);
if (query == null)
var queryMq = transformService.GetModelFromJson<NextRunUpdateMq>(msg);
if (queryMq == null)
return;
if (!await IsValidJobGroupAsync(query.JobGroupId))
if (!await IsValidJobGroupAsync(queryMq.JobGroupId))
{
logger.LogError("Не найдена группа работ с id: {jobGroupId}", query.JobGroupId);
logger.LogError("Не найдена группа работ с id: {jobGroupId}", queryMq.JobGroupId);
return;
}
await UpdateNextRunAsync(query);
using (var scope = serviceProvider.CreateScope())
{
var nextRunUpdateService = scope.ServiceProvider.GetRequiredService<INextRunUpdateService>();
await nextRunUpdateService.UpdateNextRunForTemplatesAsync(
// фильтр по JobGroupId
query => query.Where(t => t.Job!.GroupId == queryMq.JobGroupId),
() => queryMq.Initiator,
$"RabbitMq_JobGroupId_{queryMq.JobGroupId}"
);
}
//await UpdateNextRunAsync(query);
}
/// <summary>
@@ -90,74 +99,80 @@ namespace PARR.NextRun
}
/// <summary>
/// Обновить все NextRun в JobGroup
/// </summary>
/// <param name="jobGroupId"></param>
/// <returns></returns>
private async Task UpdateNextRunAsync(NextRunUpdateMq queryMq)
{
using var scope = serviceProvider.CreateScope();
///// <summary>
///// Обновить все NextRun в JobGroup
///// </summary>
///// <param name="jobGroupId"></param>
///// <returns></returns>
//private async Task UpdateNextRunAsync(NextRunUpdateMq queryMq)
//{
// using var scope = serviceProvider.CreateScope();
var templateService = scope.ServiceProvider.GetRequiredService<ITemplateService>();
var nextRunService = scope.ServiceProvider.GetRequiredService<INextRunService>();
// var templateService = scope.ServiceProvider.GetRequiredService<ITemplateService>();
// var nextRunService = scope.ServiceProvider.GetRequiredService<INextRunService>();
// var robotConfigurationService = scope.ServiceProvider.GetRequiredService<IRobotConfigurationService>();
// Берем шаблоны только в статусе Used
var templates = await templateService.Get()
.Where(t =>
t.Job!.GroupId == queryMq.JobGroupId
&& t.StatusTypeId == TemplateStatusTypeEnum.Used
).ToListAsync();
// // Берем шаблоны только в статусе Used
// var templates = await templateService.Get()
// .Include(t => t.RobotConfigurations)
// .Where(t =>
// t.Job!.GroupId == queryMq.JobGroupId
// && t.StatusTypeId == TemplateStatusTypeEnum.Used
// ).ToListAsync();
logger.LogInformation("Найдено шаблонов {count} шт. в статусе Used в группе работ {jobGroupId}", templates.Count, queryMq.JobGroupId);
// logger.LogInformation("Найдено шаблонов {count} шт. в статусе Used в группе работ {jobGroupId}", templates.Count, queryMq.JobGroupId);
if (!templates.Any())
return;
// if (!templates.Any())
// return;
var updatedTemplates = 0;
// var updatedTemplates = 0;
foreach (var template in templates)
{
var newNextRun = await nextRunService.GetNextRunForTemplateAsync(template.Id, false);
// foreach (var template in templates)
// {
// var newNextRun = await nextRunService.GetNextRunForTemplateAsync(template.Id, false);
if (newNextRun == null)
{
logger.LogError("При расчете nextRun для шаблона {templateId}, {templateName} вернулся null", template.Id, template.Name);
continue;
}
// if (newNextRun == null)
// {
// logger.LogError("При расчете nextRun для шаблона {templateId}, {templateName} вернулся null", template.Id, template.Name);
// continue;
// }
if (template.NextRun != newNextRun)
{
logger.LogInformation("Обновлен nextRun для шаблона {templateId}, {templateName}, newNextRun: {newNextRun}, oldNextRun: {oldNextRun}",
template.Id, template.Name, newNextRun, template.NextRun);
// if (template.NextRun != newNextRun)
// {
// logger.LogInformation("Обновлен nextRun для шаблона {templateId}, {templateName}, newNextRun: {newNextRun}, oldNextRun: {oldNextRun}",
// template.Id, template.Name, newNextRun, template.NextRun);
template.LastRun = template.NextRun;
template.NextRun = newNextRun.Value;
// template.LastRun = template.NextRun;
// template.NextRun = newNextRun.Value;
updatedTemplates++;
}
else
{
logger.LogDebug("Не требуется обновлять nextRun для шаблона {templateId}, {templateName}. Рассчитанный и исходный равны. NextRun: {NextRun}",
template.Id, template.Name, template.NextRun);
}
}
// //так как nextRun обновился, пробуем поставить задание на обновление
// var config = robotConfigurationService.GetFromTemplateByRobotCode(RobotsEnum.ScheduleOrder, template);
// robotConfigurationService.SetUpdateTaskStatusIfAllow(config);
if (updatedTemplates > 0)
{
if (await templateService.CommitAsync(queryMq.Initiator))
{
logger.LogInformation("Успешно обновлены nextRun у {count} шаблонов, группа работ: {jobGroupId}", updatedTemplates, queryMq.JobGroupId);
}
else
{
logger.LogError("При сохранении nextRun для шаблонов {count} шт, произошла ошибка при сохранении в БД. JobGroupId: {jobGroupId}", updatedTemplates, queryMq.JobGroupId);
}
}
else
{
logger.LogInformation("Для группы работ {jobGroupId}, все nextRun актуальны. Нечего обновлять.", queryMq.JobGroupId);
}
}
// updatedTemplates++;
// }
// else
// {
// logger.LogDebug("Не требуется обновлять nextRun для шаблона {templateId}, {templateName}. Рассчитанный и исходный равны. NextRun: {NextRun}",
// template.Id, template.Name, template.NextRun);
// }
// }
// if (updatedTemplates > 0)
// {
// if (await templateService.CommitAsync(queryMq.Initiator))
// {
// logger.LogInformation("Успешно обновлены nextRun у {count} шаблонов, группа работ: {jobGroupId}", updatedTemplates, queryMq.JobGroupId);
// }
// else
// {
// logger.LogError("При сохранении nextRun для шаблонов {count} шт, произошла ошибка при сохранении в БД. JobGroupId: {jobGroupId}", updatedTemplates, queryMq.JobGroupId);
// }
// }
// else
// {
// logger.LogInformation("Для группы работ {jobGroupId}, все nextRun актуальны. Нечего обновлять.", queryMq.JobGroupId);
// }
//}
}
}