RestartMachineHandler

master
Tudor Stanciu 2022-01-14 19:31:27 +02:00
parent e92c143258
commit 8a32ebe7d7
4 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,67 @@
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NetworkResurrector.Agent.PublishedLanguage.Dto;
using NetworkResurrector.Agent.Wrapper.Services;
using NetworkResurrector.Api.Domain.Constants;
using NetworkResurrector.Api.Domain.Repositories;
using NetworkResurrector.Api.PublishedLanguage.Commands;
using NetworkResurrector.Api.PublishedLanguage.Events;
using NetworkResurrector.Server.Wrapper.Services;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace NetworkResurrector.Api.Application.CommandHandlers
{
public class RestartMachineHandler : IRequestHandler<RestartMachine, MachineRestarted>
{
private readonly ILogger<RestartMachineHandler> _logger;
private readonly IResurrectorService _resurrectorService;
private readonly INetworkRepository _repository;
private readonly IResurrectorAgentService _resurrectorAgentService;
private readonly IConfiguration _configuration;
public RestartMachineHandler(ILogger<RestartMachineHandler> logger, IResurrectorService resurrectorService, INetworkRepository repository, IResurrectorAgentService resurrectorAgentService, IConfiguration configuration)
{
_logger=logger;
_resurrectorService=resurrectorService;
_repository=repository;
_resurrectorAgentService=resurrectorAgentService;
_configuration=configuration;
}
public async Task<MachineRestarted> Handle(RestartMachine command, CancellationToken cancellationToken)
{
_logger.LogDebug($"Start restart machine {command.MachineId}");
var machine = await _repository.GetMachine(command.MachineId);
var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.RESTART);
//log activity
var ipAddressOrMachineName = machine.IPv4Address ?? machine.MachineName;
MachineRestarted result;
switch (powerConfiguration.Performer.PerformerCode)
{
case PowerActionPerformers.NETWORK_RESURRECTOR_AGENT:
if (machine.Agent == null)
throw new Exception($"Cannot use network resurrector agent as restart performer for machine '{ipAddressOrMachineName}' because it is not configured.");
var owner = new ActionOwner(_configuration.GetValue<string>("Service:Code"));
var options = new ActionOptions(command.Delay ?? 0, command.Force);
var RestartResultByAgent = await _resurrectorAgentService.Restart(ipAddressOrMachineName, machine.Agent.AgentPort, owner, options);
var status = RestartResultByAgent.Success ? $"Machine '{ipAddressOrMachineName}' successfully restarted." : $"Machine '{ipAddressOrMachineName}' could not be restarted.";
result = new MachineRestarted(RestartResultByAgent.Success, status);
break;
default:
throw new Exception($"Power action performer {powerConfiguration.Performer.PerformerCode} is not implemented.");
}
_logger.LogDebug($"Machine {command.MachineId} restart finished. Success: {result.Success}; Status: {result.Status}");
return result;
}
}
}

View File

@ -0,0 +1,12 @@
using NDB.Application.DataContracts;
using NetworkResurrector.Api.PublishedLanguage.Events;
namespace NetworkResurrector.Api.PublishedLanguage.Commands
{
public class RestartMachine : Command<MachineRestarted>
{
public int MachineId { get; set; }
public int? Delay { get; set; }
public bool Force { get; set; }
}
}

View File

@ -0,0 +1,4 @@
namespace NetworkResurrector.Api.PublishedLanguage.Events
{
public record MachineRestarted(bool Success, string Status);
}

View File

@ -38,5 +38,12 @@ namespace NetworkResurrector.Api.Controllers
var result = await _mediator.Send(shutdownMachine);
return Ok(result);
}
[HttpPost("restart")]
public async Task<IActionResult> RestartMachine([FromBody] RestartMachine restartMachine)
{
var result = await _mediator.Send(restartMachine);
return Ok(result);
}
}
}