From f997efbed19cc48ed9d42cc436716269c1eb5845 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Thu, 13 Jan 2022 12:21:32 +0200 Subject: [PATCH] ping machine from API by machineId --- .../CommandHandlers/PingMachineHandler.cs | 38 +++++++++++++++++++ .../CommandHandlers/WakeMachineHandler.cs | 3 ++ .../Events/MachinePinged.cs | 6 +-- .../Events/MachineShutdown.cs | 6 +-- 4 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs new file mode 100644 index 0000000..309d5e9 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs @@ -0,0 +1,38 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using NetworkResurrector.Api.Domain.Repositories; +using NetworkResurrector.Api.PublishedLanguage.Commands; +using NetworkResurrector.Api.PublishedLanguage.Events; +using NetworkResurrector.Server.Wrapper.Services; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Api.Application.CommandHandlers +{ + public class PingMachineHandler : IRequestHandler + { + private readonly ILogger _logger; + private readonly IResurrectorService _resurrectorService; + private readonly INetworkRepository _repository; + + public PingMachineHandler(ILogger logger, IResurrectorService resurrectorService, INetworkRepository repository) + { + _logger=logger; + _resurrectorService=resurrectorService; + _repository=repository; + } + + public async Task Handle(PingMachine command, CancellationToken cancellationToken) + { + _logger.LogDebug($"Start pinging machine {command.MachineId}"); + var machine = await _repository.GetMachine(command.MachineId); + + //log activity + var pingResult = await _resurrectorService.Ping(machine.IPv4Address ?? machine.MachineName); + var result = new MachinePinged(pingResult.Success, pingResult.Status); + _logger.LogDebug($"Machine {command.MachineId} pinging finished. Success: {result.Success}; Status: {result.Status}"); + + return result; + } + } +} diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs index f621fb3..c5c4351 100644 --- a/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs @@ -24,11 +24,14 @@ namespace NetworkResurrector.Api.Application.CommandHandlers public async Task Handle(WakeMachine command, CancellationToken cancellationToken) { + _logger.LogDebug($"Start waking up machine {command.MachineId}"); var machine = await _repository.GetMachine(command.MachineId); //log activity var wakeResult = await _resurrectorService.Wake(machine.MACAddress); var result = new MachineWaked(wakeResult.Success, wakeResult.Status); + _logger.LogDebug($"Machine {command.MachineId} wake up finished. Success: {result.Success}; Status: {result.Status}"); + return result; } } diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachinePinged.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachinePinged.cs index 80d4e00..37234c1 100644 --- a/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachinePinged.cs +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachinePinged.cs @@ -1,8 +1,4 @@ namespace NetworkResurrector.Api.PublishedLanguage.Events { - public class MachinePinged - { - public bool Success { get; set; } - public string Status { get; set; } - } + public record MachinePinged(bool Success, string Status); } diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineShutdown.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineShutdown.cs index 0275161..5a70ead 100644 --- a/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineShutdown.cs +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineShutdown.cs @@ -1,8 +1,4 @@ namespace NetworkResurrector.Api.PublishedLanguage.Events { - public class MachineShutdown - { - public bool Success { get; set; } - public string Status { get; set; } - } + public record MachineShutdown(bool Success, string Status); }