From 50cb5e80e2a43ec84701bbabdf76e97129747c2e Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Fri, 14 Jan 2022 19:37:39 +0200 Subject: [PATCH] SleepMachineHandler --- .../CommandHandlers/RestartMachineHandler.cs | 11 ++-- .../CommandHandlers/SleepMachineHandler.cs | 64 +++++++++++++++++++ .../Commands/SleepMachine.cs | 12 ++++ .../Events/MachineSlept.cs | 4 ++ .../Controllers/ResurrectorController.cs | 7 ++ 5 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 src/api/NetworkResurrector.Api.Application/CommandHandlers/SleepMachineHandler.cs create mode 100644 src/api/NetworkResurrector.Api.PublishedLanguage/Commands/SleepMachine.cs create mode 100644 src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineSlept.cs diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/RestartMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/RestartMachineHandler.cs index e5502a7..d0d2384 100644 --- a/src/api/NetworkResurrector.Api.Application/CommandHandlers/RestartMachineHandler.cs +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/RestartMachineHandler.cs @@ -7,7 +7,6 @@ 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; @@ -17,15 +16,13 @@ namespace NetworkResurrector.Api.Application.CommandHandlers public class RestartMachineHandler : IRequestHandler { private readonly ILogger _logger; - private readonly IResurrectorService _resurrectorService; private readonly INetworkRepository _repository; private readonly IResurrectorAgentService _resurrectorAgentService; private readonly IConfiguration _configuration; - public RestartMachineHandler(ILogger logger, IResurrectorService resurrectorService, INetworkRepository repository, IResurrectorAgentService resurrectorAgentService, IConfiguration configuration) + public RestartMachineHandler(ILogger logger, INetworkRepository repository, IResurrectorAgentService resurrectorAgentService, IConfiguration configuration) { _logger=logger; - _resurrectorService=resurrectorService; _repository=repository; _resurrectorAgentService=resurrectorAgentService; _configuration=configuration; @@ -50,9 +47,9 @@ namespace NetworkResurrector.Api.Application.CommandHandlers var owner = new ActionOwner(_configuration.GetValue("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); + 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: diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/SleepMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/SleepMachineHandler.cs new file mode 100644 index 0000000..e24dac1 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/SleepMachineHandler.cs @@ -0,0 +1,64 @@ +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 System; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Api.Application.CommandHandlers +{ + public class SleepMachineHandler : IRequestHandler + { + private readonly ILogger _logger; + private readonly INetworkRepository _repository; + private readonly IResurrectorAgentService _resurrectorAgentService; + private readonly IConfiguration _configuration; + + public SleepMachineHandler(ILogger logger, INetworkRepository repository, IResurrectorAgentService resurrectorAgentService, IConfiguration configuration) + { + _logger=logger; + _repository=repository; + _resurrectorAgentService=resurrectorAgentService; + _configuration=configuration; + } + + public async Task Handle(SleepMachine command, CancellationToken cancellationToken) + { + _logger.LogDebug($"Start sleeping machine {command.MachineId}"); + var machine = await _repository.GetMachine(command.MachineId); + var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.SLEEP); + + //log activity + + var ipAddressOrMachineName = machine.IPv4Address ?? machine.MachineName; + MachineSlept result; + + switch (powerConfiguration.Performer.PerformerCode) + { + case PowerActionPerformers.NETWORK_RESURRECTOR_AGENT: + if (machine.Agent == null) + throw new Exception($"Cannot use network resurrector agent as sleep performer for machine '{ipAddressOrMachineName}' because it is not configured."); + + var owner = new ActionOwner(_configuration.GetValue("Service:Code")); + var options = new ActionOptions(command.Delay ?? 0, command.Force); + var sleepResultByAgent = await _resurrectorAgentService.Sleep(ipAddressOrMachineName, machine.Agent.AgentPort, owner, options); + var status = sleepResultByAgent.Success ? $"Machine '{ipAddressOrMachineName}' successfully slept." : $"Machine '{ipAddressOrMachineName}' could not be slept."; + result = new MachineSlept(sleepResultByAgent.Success, status); + break; + + default: + throw new Exception($"Power action performer {powerConfiguration.Performer.PerformerCode} is not implemented."); + } + + _logger.LogDebug($"Machine {command.MachineId} sleeping finished. Success: {result.Success}; Status: {result.Status}"); + + return result; + } + } +} diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/SleepMachine.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/SleepMachine.cs new file mode 100644 index 0000000..5d6af2b --- /dev/null +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/SleepMachine.cs @@ -0,0 +1,12 @@ +using NDB.Application.DataContracts; +using NetworkResurrector.Api.PublishedLanguage.Events; + +namespace NetworkResurrector.Api.PublishedLanguage.Commands +{ + public class SleepMachine : Command + { + public int MachineId { get; set; } + public int? Delay { get; set; } + public bool Force { get; set; } + } +} diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineSlept.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineSlept.cs new file mode 100644 index 0000000..569cab0 --- /dev/null +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineSlept.cs @@ -0,0 +1,4 @@ +namespace NetworkResurrector.Api.PublishedLanguage.Events +{ + public record MachineSlept(bool Success, string Status); +} diff --git a/src/api/NetworkResurrector.Api/Controllers/ResurrectorController.cs b/src/api/NetworkResurrector.Api/Controllers/ResurrectorController.cs index 984e018..9e3ae32 100644 --- a/src/api/NetworkResurrector.Api/Controllers/ResurrectorController.cs +++ b/src/api/NetworkResurrector.Api/Controllers/ResurrectorController.cs @@ -45,5 +45,12 @@ namespace NetworkResurrector.Api.Controllers var result = await _mediator.Send(restartMachine); return Ok(result); } + + [HttpPost("sleep")] + public async Task SleepMachine([FromBody] SleepMachine sleepMachine) + { + var result = await _mediator.Send(sleepMachine); + return Ok(result); + } } }