feat(api): метод сброса ошибок синхронизации шаблонов
This commit is contained in:
@@ -265,7 +265,7 @@ namespace PARR.API.Controllers.V1
|
||||
var config = await robotConfigurationService.GetAsync(taskId);
|
||||
|
||||
//изменение статуса робота
|
||||
robotConfigurationService.ChangeRobotStatus(RobotStatusEnum.InProgress, ref config!);
|
||||
robotConfigurationService.ChangeRobotStatus(RobotStatusEnum.InProgress, config!);
|
||||
|
||||
if (!await robotConfigurationService.CommitAsync())
|
||||
return false;
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace PARR.API.Controllers.V1
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найдено задание с id: {taskId}" } }));
|
||||
|
||||
//изменение статуса робота
|
||||
robotConfigurationService.ChangeRobotStatus(request.RobotStatusCode, ref config);
|
||||
robotConfigurationService.ChangeRobotStatus(request.RobotStatusCode, config);
|
||||
|
||||
//если успех, изменяем статус задания на успех
|
||||
if (request.RobotStatusCode == RobotStatusEnum.Complete)
|
||||
|
||||
@@ -30,18 +30,21 @@ namespace PARR.API.Controllers.V1
|
||||
private readonly ITemplateService templateService;
|
||||
private readonly IRobotConfigurationService robotConfigurationService;
|
||||
private readonly IClientService clientService;
|
||||
private readonly ILogger<TemplateController> logger;
|
||||
|
||||
public TemplateController(
|
||||
IMapper mapper,
|
||||
ITemplateService templateService,
|
||||
IRobotConfigurationService robotConfigurationService,
|
||||
IClientService clientService
|
||||
IClientService clientService,
|
||||
ILogger<TemplateController> logger
|
||||
)
|
||||
{
|
||||
this.mapper = mapper;
|
||||
this.templateService = templateService;
|
||||
this.robotConfigurationService = robotConfigurationService;
|
||||
this.clientService = clientService;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
@@ -175,87 +178,5 @@ namespace PARR.API.Controllers.V1
|
||||
return Ok(new Response<TemplateResponse>(response, true));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить один шаблон по заданному статусу
|
||||
/// </summary>
|
||||
/// <param name="statusCode"></param>
|
||||
/// <returns></returns>
|
||||
//[HttpGet(ApiRoutes.Template.GetByStatusCode)]
|
||||
//public async Task<IActionResult> GetByStatus([FromRoute] int statusCode, [FromQuery] RobinQuery query)
|
||||
//{
|
||||
// Template? template = null;
|
||||
|
||||
// //1.Ищем `RobotStatusCode` = 22 и `RobotLastStatusUpdated` истекло и `RobotAttemptsNumber` >= допустимого значения из настроек,
|
||||
// //ставим всем этим записям `RobotStatusCode`= 33
|
||||
// //2.Поиск шаблонов со `StatusCode` 10 или 20 и `RobotStatusCode` = 11.Находим, **выбрали эту запись, конец**.
|
||||
// //3.Поиск шаблонов со `StatusCode` 10 или 20 и `RobotStatusCode` = 22.
|
||||
// //Далее проверяется `RobotLastStatusUpdated`, что время последнего смены статуса не превышает допустимого(берется из настроек, поле `RobotWaitTime`)
|
||||
// //и что текущая попытка не больше разрешенной(берется из настроек, поле `RobotAttemptsNumber`) - если это так, берется эта запись.
|
||||
|
||||
// //1.
|
||||
// await templateService.CheckAndSetErrorRobotStatusAsync(settingsFromDb.RobotAttemptsNumber, settingsFromDb.RobotWaitTime);
|
||||
|
||||
|
||||
// //2.
|
||||
// template = await templateService.GetWithIncludes()
|
||||
// .AsSplitQuery()
|
||||
// .FirstOrDefaultAsync(t => t.StatusCode == statusCode && t.RobotStatusCode == (int)RobotStatusEnum.Wait);
|
||||
|
||||
// //3.
|
||||
// if (template == null)
|
||||
// {
|
||||
// var endDate = DateTimeOffset.UtcNow.Add(-settingsFromDb.RobotWaitTime);
|
||||
// template = await templateService.GetWithIncludes()
|
||||
// .AsSplitQuery()
|
||||
// .FirstOrDefaultAsync(t =>
|
||||
// t.StatusCode == statusCode
|
||||
// && t.RobotStatusCode == (int)RobotStatusEnum.InProgress
|
||||
// && t.RobotAttemptsNumber < settingsFromDb.RobotAttemptsNumber
|
||||
// && t.RobotLastStatusUpdated < endDate
|
||||
// );
|
||||
// }
|
||||
|
||||
|
||||
// if (template == null)
|
||||
// return NotFound();
|
||||
|
||||
// var response = mapper.Map<TemplateResponse>(template);
|
||||
|
||||
// if (query.Robin == true)
|
||||
// {
|
||||
// var stringResponse = mapper.Map<TemplateStringResponse>(response);
|
||||
// return Ok(stringResponse);
|
||||
// }
|
||||
|
||||
// return Ok(new Response<TemplateResponse>(response, true));
|
||||
//}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Установить статус ОК для шаблона с id
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
//[HttpPut(ApiRoutes.Template.SetOkStatus)]
|
||||
//public async Task<IActionResult> SetOkStatus([FromRoute] Guid id)
|
||||
//{
|
||||
// var template = await templateService.GetWithIncludes()
|
||||
// .AsSplitQuery()
|
||||
// .FirstOrDefaultAsync(t => t.Id == id);
|
||||
|
||||
// if (template == null)
|
||||
// return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблон с id: {id}" } }));
|
||||
|
||||
// template.StatusCode = (int)TaskStatusEnum.Ok;
|
||||
|
||||
// if (!await templateService.CommitAsync())
|
||||
// return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при изменении статуса у шаблона с id: {id}" } }));
|
||||
|
||||
// var response = mapper.Map<TemplateResponse>(template);
|
||||
|
||||
// return Ok(new Response<TemplateResponse>(response, true));
|
||||
//}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
87
PARR.API/Controllers/V1/TemplateSyncStateController.cs
Normal file
87
PARR.API/Controllers/V1/TemplateSyncStateController.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using AutoMapper;
|
||||
using InfluxDB.Client.Api.Domain;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PARR.API.Contracts.V1;
|
||||
using PARR.API.Contracts.V1.Responses;
|
||||
using PARR.API.Contracts.V1.Responses.Base;
|
||||
using PARR.API.Controllers.V1.Base;
|
||||
using PARR.Constants;
|
||||
using PARR.DAL.Contracts;
|
||||
using PARR.DAL.Services.Interfaces;
|
||||
|
||||
namespace PARR.API.Controllers.V1
|
||||
{
|
||||
/// <summary>
|
||||
/// Управление состоянием синхронизации шаблонов
|
||||
/// </summary>
|
||||
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
||||
public class TemplateSyncStateController : BaseApiController
|
||||
{
|
||||
private readonly IMapper mapper;
|
||||
private readonly ITemplateService templateService;
|
||||
private readonly IRobotConfigurationService robotConfigurationService;
|
||||
private readonly ILogger<TemplateController> logger;
|
||||
|
||||
public TemplateSyncStateController(
|
||||
IMapper mapper,
|
||||
ITemplateService templateService,
|
||||
IRobotConfigurationService robotConfigurationService,
|
||||
ILogger<TemplateController> logger
|
||||
)
|
||||
{
|
||||
this.mapper = mapper;
|
||||
this.templateService = templateService;
|
||||
this.robotConfigurationService = robotConfigurationService;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Сбросить ошибки синхронизации шаблона по ИД
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost(ApiRoutes.TemplateSyncState.ResetSyncErrors)]
|
||||
public async Task<IActionResult> ResetSyncErrors([FromRoute] Guid id)
|
||||
{
|
||||
var template = await templateService.Get()
|
||||
.Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot)
|
||||
.Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus)
|
||||
.Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus)
|
||||
.FirstOrDefaultAsync(t => t.Id == id);
|
||||
|
||||
if (template == null)
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблона с id: {id}" } }));
|
||||
|
||||
var isChanged = false;
|
||||
|
||||
// смотрим RobotStatusCode, если есть завершенный с ошибкой, то сбросим ошибку и кол-во попыток
|
||||
foreach (var config in template.RobotConfigurations)
|
||||
{
|
||||
if (config.RobotStatusCode == (int)RobotStatusEnum.Error)
|
||||
{
|
||||
robotConfigurationService.ChangeRobotStatus(RobotStatusEnum.Wait, config);
|
||||
isChanged = true;
|
||||
logger.LogDebug($"Для шаблона {id} изменен статус ОШИБКА для робота robotCode: {config.RobotCode}, новый статус: {RobotStatusEnum.Wait.ToString()}");
|
||||
}
|
||||
}
|
||||
|
||||
if (!isChanged)
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Для данного шаблона нет ошибочных статусов." } }));
|
||||
|
||||
if (!await templateService.CommitAsync())
|
||||
{
|
||||
logger.LogError($"Ошибка при сбросе статуса ошибки синхрнизации для шаблона {id}");
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при сбросе ошибок синхронизации." } }));
|
||||
}
|
||||
|
||||
var response = mapper.Map<List<RobotConfigurationResponse>>(template.RobotConfigurations);
|
||||
|
||||
|
||||
return Ok(new Response<List<RobotConfigurationResponse>>(response, true));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user