Agent ShutdownService

master
Tudor Stanciu 2021-12-21 11:52:09 +02:00
parent 2e21ad3b60
commit c0b07ed59e
15 changed files with 146 additions and 2 deletions

View File

@ -56,7 +56,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent",
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent.Application", "src\agent\NetworkResurrector.Agent.Application\NetworkResurrector.Agent.Application.csproj", "{3795AB02-7F2A-424B-BFD2-1B915E155829}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent.Application", "src\agent\NetworkResurrector.Agent.Application\NetworkResurrector.Agent.Application.csproj", "{3795AB02-7F2A-424B-BFD2-1B915E155829}"
EndProject 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{BA96E3C3-3E18-4882-8BDB-ED7B40836892}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -139,6 +145,7 @@ Global
{C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} {C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302}
{3795AB02-7F2A-424B-BFD2-1B915E155829} = {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} {BA96E3C3-3E18-4882-8BDB-ED7B40836892} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302}
{2B084ADC-829E-4B72-8FF3-69780E06083D} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {351D76E9-FE02-4C30-A464-7B29AFC64BC7} SolutionGuid = {351D76E9-FE02-4C30-A464-7B29AFC64BC7}

View File

@ -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<Shutdown, ShutdownResult>
{
private readonly ILogger<ShutdownHandler> _logger;
private readonly IShutdownService _shutdownService;
public ShutdownHandler(ILogger<ShutdownHandler> logger, IShutdownService shutdownService)
{
_logger = logger;
_shutdownService = shutdownService;
}
public async Task<ShutdownResult> 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 };
}
}
}

View File

@ -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<IParamProvider, ParamProvider>();
services.AddSingleton<IShutdownService, ShutdownService>();
}
}
}

View File

@ -12,5 +12,9 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="NDB.Application.DataContracts" Version="$(NDBApplicationPackageVersion)" /> <PackageReference Include="NDB.Application.DataContracts" Version="$(NDBApplicationPackageVersion)" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NetworkResurrector.Agent.PublishedLanguage\NetworkResurrector.Agent.PublishedLanguage.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,7 @@
namespace NetworkResurrector.Agent.Application.Services.Abstractions
{
public interface IShutdownService
{
void Run();
}
}

View File

@ -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<ShutdownService> _logger;
public ShutdownService(ILogger<ShutdownService> logger)
{
_logger=logger;
}
public void Run()
{
_logger.LogInformation("In development...");
}
}
}

View File

@ -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<ShutdownResult>
{
public ActionOwner Owner { get; set; }
public ActionOptions Options { get; set; }
}
}

View File

@ -0,0 +1,5 @@
namespace NetworkResurrector.Agent.PublishedLanguage.Dto
{
public record ActionOwner(string OwnerCode);
public record ActionOptions(int Delay);
}

View File

@ -0,0 +1,4 @@
namespace NetworkResurrector.Agent.PublishedLanguage.Dto
{
public record HostInfo(string[] Messages);
}

View File

@ -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; }
}
}

View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NDB.Application.DataContracts" Version="$(NDBApplicationPackageVersion)" />
</ItemGroup>
</Project>

View File

@ -1,6 +1,7 @@
using MediatR; using MediatR;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using NetworkResurrector.Agent.PublishedLanguage.Commands;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NetworkResurrector.Agent.Controllers namespace NetworkResurrector.Agent.Controllers

View File

@ -23,6 +23,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\NetworkResurrector.Agent.Application\NetworkResurrector.Agent.Application.csproj" /> <ProjectReference Include="..\NetworkResurrector.Agent.Application\NetworkResurrector.Agent.Application.csproj" />
<ProjectReference Include="..\NetworkResurrector.Agent.PublishedLanguage\NetworkResurrector.Agent.PublishedLanguage.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -8,6 +8,7 @@ using Microsoft.Extensions.Hosting;
using NDB.Extensions.Swagger; using NDB.Extensions.Swagger;
using NDB.Extensions.Swagger.Constants; using NDB.Extensions.Swagger.Constants;
using NDB.Security.Authentication.Identity; using NDB.Security.Authentication.Identity;
using NetworkResurrector.Agent.Application;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Reflection; using System.Reflection;
@ -48,7 +49,7 @@ namespace NetworkResurrector.Agent
// services.AddDataAccess(); // services.AddDataAccess();
// Application // Application
// services.AddApplicationServices(); services.AddApplicationServices();
} }
private Assembly[] GetMediatRAssemblies() private Assembly[] GetMediatRAssemblies()

View File

@ -17,5 +17,8 @@
}, },
"IdentityServer": { "IdentityServer": {
"BaseAddress": "https://toodle.ddns.net/identity-server-api/" "BaseAddress": "https://toodle.ddns.net/identity-server-api/"
},
"Restrictions": {
"EnforceActionOwner": true
} }
} }