win cancel action

master
Tudor Stanciu 2022-01-12 19:12:16 +02:00
parent 69ce2bfc53
commit d97e1fa8b5
4 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,47 @@
using MediatR;
using Microsoft.Extensions.Logging;
using NetworkResurrector.Agent.Application.Services.Abstractions;
using NetworkResurrector.Agent.PublishedLanguage.Commands;
using NetworkResurrector.Agent.PublishedLanguage.Events;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace NetworkResurrector.Agent.Application.CommandHandlers
{
public class CancelHandler : IRequestHandler<Cancel, CancelResult>
{
private readonly ILogger<CancelHandler> _logger;
private readonly IPowerService _powerService;
private readonly IValidationService _validationService;
public CancelHandler(ILogger<CancelHandler> logger, IPowerService powerService, IValidationService validationService)
{
_logger=logger;
_powerService=powerService;
_validationService=validationService;
}
public async Task<CancelResult> Handle(Cancel command, CancellationToken cancellationToken)
{
return await Task.Run(() => Handle(command));
}
private CancelResult Handle(Cancel command)
{
_validationService.ValidateRestrictions(command.Owner);
_logger.LogDebug($"Start canceling the previous action.");
var stopWatch = new Stopwatch();
stopWatch.Start();
_powerService.Cancel();
stopWatch.Stop();
_logger.LogDebug($"Action cancellation finished - {stopWatch.ElapsedMilliseconds:N0} ms");
return new CancelResult() { Success = true };
}
}
}

View File

@ -0,0 +1,11 @@
using NDB.Application.DataContracts;
using NetworkResurrector.Agent.PublishedLanguage.Dto;
using NetworkResurrector.Agent.PublishedLanguage.Events;
namespace NetworkResurrector.Agent.PublishedLanguage.Commands
{
public class Cancel : Command<CancelResult>
{
public ActionOwner Owner { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using NetworkResurrector.Agent.PublishedLanguage.Dto;
namespace NetworkResurrector.Agent.PublishedLanguage.Events
{
public class CancelResult
{
public bool Success { get; set; }
public HostInfo Host { get; set; }
}
}

View File

@ -52,5 +52,12 @@ namespace NetworkResurrector.Agent.Controllers
var result = await _mediator.Send(_lock);
return Ok(result);
}
[HttpPost("cancel")]
public async Task<IActionResult> Cancel([FromBody] Cancel cancel)
{
var result = await _mediator.Send(cancel);
return Ok(result);
}
}
}