From c0b07ed59e2aa008fa0c4bc6df021d2cd03484e7 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Tue, 21 Dec 2021 11:52:09 +0200 Subject: [PATCH] Agent ShutdownService --- NetworkResurrector.sln | 9 +++- .../CommandHandlers/ShutdownHandler.cs | 43 +++++++++++++++++++ .../DependencyInjectionExtensions.cs | 15 +++++++ ...etworkResurrector.Agent.Application.csproj | 4 ++ .../Services/Abstractions/IShutdownService.cs | 7 +++ .../Services/ShutdownService.cs | 20 +++++++++ .../Commands/Shutdown.cs | 12 ++++++ .../Dto/CommandRecords.cs | 5 +++ .../Dto/EventRecords.cs | 4 ++ .../Events/ShutdownResult.cs | 10 +++++ ...Resurrector.Agent.PublishedLanguage.csproj | 11 +++++ .../Controllers/HostController.cs | 1 + .../NetworkResurrector.Agent.csproj | 1 + src/agent/NetworkResurrector.Agent/Startup.cs | 3 +- .../NetworkResurrector.Agent/appsettings.json | 3 ++ 15 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 src/agent/NetworkResurrector.Agent.Application/CommandHandlers/ShutdownHandler.cs create mode 100644 src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs create mode 100644 src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IShutdownService.cs create mode 100644 src/agent/NetworkResurrector.Agent.Application/Services/ShutdownService.cs create mode 100644 src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Shutdown.cs create mode 100644 src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/CommandRecords.cs create mode 100644 src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/EventRecords.cs create mode 100644 src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/ShutdownResult.cs create mode 100644 src/agent/NetworkResurrector.Agent.PublishedLanguage/NetworkResurrector.Agent.PublishedLanguage.csproj diff --git a/NetworkResurrector.sln b/NetworkResurrector.sln index 6cbb5da..be95ef9 100644 --- a/NetworkResurrector.sln +++ b/NetworkResurrector.sln @@ -56,7 +56,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent.Application", "src\agent\NetworkResurrector.Agent.Application\NetworkResurrector.Agent.Application.csproj", "{3795AB02-7F2A-424B-BFD2-1B915E155829}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkResurrector.Agent.Wrapper", "src\agent\NetworkResurrector.Agent.Wrapper\NetworkResurrector.Agent.Wrapper.csproj", "{BA96E3C3-3E18-4882-8BDB-ED7B40836892}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent.Wrapper", "src\agent\NetworkResurrector.Agent.Wrapper\NetworkResurrector.Agent.Wrapper.csproj", "{BA96E3C3-3E18-4882-8BDB-ED7B40836892}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkResurrector.Agent.PublishedLanguage", "src\agent\NetworkResurrector.Agent.PublishedLanguage\NetworkResurrector.Agent.PublishedLanguage.csproj", "{2B084ADC-829E-4B72-8FF3-69780E06083D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -116,6 +118,10 @@ Global {BA96E3C3-3E18-4882-8BDB-ED7B40836892}.Debug|Any CPU.Build.0 = Debug|Any CPU {BA96E3C3-3E18-4882-8BDB-ED7B40836892}.Release|Any CPU.ActiveCfg = Release|Any CPU {BA96E3C3-3E18-4882-8BDB-ED7B40836892}.Release|Any CPU.Build.0 = Release|Any CPU + {2B084ADC-829E-4B72-8FF3-69780E06083D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2B084ADC-829E-4B72-8FF3-69780E06083D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2B084ADC-829E-4B72-8FF3-69780E06083D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2B084ADC-829E-4B72-8FF3-69780E06083D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -139,6 +145,7 @@ Global {C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} {3795AB02-7F2A-424B-BFD2-1B915E155829} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} {BA96E3C3-3E18-4882-8BDB-ED7B40836892} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} + {2B084ADC-829E-4B72-8FF3-69780E06083D} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {351D76E9-FE02-4C30-A464-7B29AFC64BC7} diff --git a/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/ShutdownHandler.cs b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/ShutdownHandler.cs new file mode 100644 index 0000000..a24d408 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/ShutdownHandler.cs @@ -0,0 +1,43 @@ +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 ShutdownHandler : IRequestHandler + { + private readonly ILogger _logger; + private readonly IShutdownService _shutdownService; + + public ShutdownHandler(ILogger logger, IShutdownService shutdownService) + { + _logger = logger; + _shutdownService = shutdownService; + } + + public async Task Handle(Shutdown command, CancellationToken cancellationToken) + { + return await Task.Run(() => Handle(command)); + } + + private ShutdownResult Handle(Shutdown command) + { + _logger.LogDebug($"Start shutting down the system."); + + var stopWatch = new Stopwatch(); + stopWatch.Start(); + + _shutdownService.Run(); + + stopWatch.Stop(); + _logger.LogDebug($"System shutdown finished - {stopWatch.ElapsedMilliseconds:N0} ms"); + + return new ShutdownResult() { Success = true }; + } + } +} diff --git a/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs b/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs new file mode 100644 index 0000000..75c055c --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.DependencyInjection; +using NetworkResurrector.Agent.Application.Services; +using NetworkResurrector.Agent.Application.Services.Abstractions; + +namespace NetworkResurrector.Agent.Application +{ + public static class DependencyInjectionExtensions + { + public static void AddApplicationServices(this IServiceCollection services) + { + // services.AddSingleton(); + services.AddSingleton(); + } + } +} diff --git a/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj b/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj index 7aeb85a..3ad4980 100644 --- a/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj +++ b/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj @@ -12,5 +12,9 @@ + + + + diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IShutdownService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IShutdownService.cs new file mode 100644 index 0000000..427cb6e --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IShutdownService.cs @@ -0,0 +1,7 @@ +namespace NetworkResurrector.Agent.Application.Services.Abstractions +{ + public interface IShutdownService + { + void Run(); + } +} diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/ShutdownService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/ShutdownService.cs new file mode 100644 index 0000000..1672cde --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/Services/ShutdownService.cs @@ -0,0 +1,20 @@ +using Microsoft.Extensions.Logging; +using NetworkResurrector.Agent.Application.Services.Abstractions; + +namespace NetworkResurrector.Agent.Application.Services +{ + internal class ShutdownService : IShutdownService + { + private readonly ILogger _logger; + + public ShutdownService(ILogger logger) + { + _logger=logger; + } + + public void Run() + { + _logger.LogInformation("In development..."); + } + } +} diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Shutdown.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Shutdown.cs new file mode 100644 index 0000000..915ef16 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Shutdown.cs @@ -0,0 +1,12 @@ +using NDB.Application.DataContracts; +using NetworkResurrector.Agent.PublishedLanguage.Dto; +using NetworkResurrector.Agent.PublishedLanguage.Events; + +namespace NetworkResurrector.Agent.PublishedLanguage.Commands +{ + public class Shutdown : Command + { + public ActionOwner Owner { get; set; } + public ActionOptions Options { get; set; } + } +} diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/CommandRecords.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/CommandRecords.cs new file mode 100644 index 0000000..5a8ffb6 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/CommandRecords.cs @@ -0,0 +1,5 @@ +namespace NetworkResurrector.Agent.PublishedLanguage.Dto +{ + public record ActionOwner(string OwnerCode); + public record ActionOptions(int Delay); +} diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/EventRecords.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/EventRecords.cs new file mode 100644 index 0000000..a72111b --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/EventRecords.cs @@ -0,0 +1,4 @@ +namespace NetworkResurrector.Agent.PublishedLanguage.Dto +{ + public record HostInfo(string[] Messages); +} diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/ShutdownResult.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/ShutdownResult.cs new file mode 100644 index 0000000..284c0db --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/ShutdownResult.cs @@ -0,0 +1,10 @@ +using NetworkResurrector.Agent.PublishedLanguage.Dto; + +namespace NetworkResurrector.Agent.PublishedLanguage.Events +{ + public class ShutdownResult + { + public bool Success { get; set; } + public HostInfo Host { get; set; } + } +} diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/NetworkResurrector.Agent.PublishedLanguage.csproj b/src/agent/NetworkResurrector.Agent.PublishedLanguage/NetworkResurrector.Agent.PublishedLanguage.csproj new file mode 100644 index 0000000..68d208b --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/NetworkResurrector.Agent.PublishedLanguage.csproj @@ -0,0 +1,11 @@ + + + + net5.0 + + + + + + + diff --git a/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs b/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs index 7234080..b8ea0e0 100644 --- a/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs +++ b/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs @@ -1,6 +1,7 @@ using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using NetworkResurrector.Agent.PublishedLanguage.Commands; using System.Threading.Tasks; namespace NetworkResurrector.Agent.Controllers diff --git a/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj b/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj index 091714a..18a99ea 100644 --- a/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj +++ b/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj @@ -23,6 +23,7 @@ + diff --git a/src/agent/NetworkResurrector.Agent/Startup.cs b/src/agent/NetworkResurrector.Agent/Startup.cs index 1fc6f6f..6d542a0 100644 --- a/src/agent/NetworkResurrector.Agent/Startup.cs +++ b/src/agent/NetworkResurrector.Agent/Startup.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Hosting; using NDB.Extensions.Swagger; using NDB.Extensions.Swagger.Constants; using NDB.Security.Authentication.Identity; +using NetworkResurrector.Agent.Application; using Newtonsoft.Json; using System.Reflection; @@ -48,7 +49,7 @@ namespace NetworkResurrector.Agent // services.AddDataAccess(); // Application - // services.AddApplicationServices(); + services.AddApplicationServices(); } private Assembly[] GetMediatRAssemblies() diff --git a/src/agent/NetworkResurrector.Agent/appsettings.json b/src/agent/NetworkResurrector.Agent/appsettings.json index 07332ee..7f54a28 100644 --- a/src/agent/NetworkResurrector.Agent/appsettings.json +++ b/src/agent/NetworkResurrector.Agent/appsettings.json @@ -17,5 +17,8 @@ }, "IdentityServer": { "BaseAddress": "https://toodle.ddns.net/identity-server-api/" + }, + "Restrictions": { + "EnforceActionOwner": true } }