From 50b682c8753b6fe6191e9496400436a24cf97a26 Mon Sep 17 00:00:00 2001 From: Daryl Ronningen Date: Fri, 18 Nov 2022 01:33:42 -0800 Subject: [PATCH] add basic logging and process logs --- netsd/Handlers/Init.cs | 49 ++++++++++++++++++++++++++++------------ netsd/Handlers/Start.cs | 14 ++++++++++-- netsd/Program.cs | 6 ++--- netsd/Protos/Start.proto | 5 ++-- netsd/netsd.csproj | 5 +++- 5 files changed, 56 insertions(+), 23 deletions(-) diff --git a/netsd/Handlers/Init.cs b/netsd/Handlers/Init.cs index 178ae79..2da40ad 100644 --- a/netsd/Handlers/Init.cs +++ b/netsd/Handlers/Init.cs @@ -6,24 +6,34 @@ using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using netsd.Utils; +using Serilog; +using Serilog.Core; +using Serilog.Events; using StartGrpc; namespace netsd.Handlers; public class Init { + public static readonly Logger Logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .Enrich.FromLogContext() + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") + .CreateLogger(); + private readonly WebApplicationBuilder _socketBuilder = WebApplication.CreateBuilder(); public Init() { _socketBuilder.WebHost.ConfigureKestrel(options => { - options.ListenUnixSocket("/tmp/netsd.sock", listenOptions => - { - listenOptions.Protocols = HttpProtocols.Http2; - }); + options.ListenUnixSocket("/tmp/netsd.sock", + listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; }); }); + _socketBuilder.Host.UseSerilog(Logger); + _socketBuilder.Logging.ClearProviders(); _socketBuilder.Services.AddGrpc(); @@ -38,25 +48,34 @@ public class Init public class StartGrpc : StartRpc.StartRpcBase { - public override async Task StartService(StartRequest request, ServerCallContext context) + public override async Task StartService(StartRequest request, IServerStreamWriter responseStream, + ServerCallContext context) { + Init.Logger.Information($"Received request to start service {Path.GetFileName(request.Path)}."); + var parseServiceFile = await ServiceParser.Parse(request.Path); using var startService = new Process(); - startService.StartInfo.CreateNoWindow = true; startService.StartInfo.FileName = parseServiceFile.ExecStart?.Split(' ').First(); startService.StartInfo.Arguments = string.Join(" ", parseServiceFile.ExecStart?.Split(' ').Skip(1)!); + startService.StartInfo.UseShellExecute = false; + startService.StartInfo.RedirectStandardOutput = true; + startService.StartInfo.RedirectStandardError = true; + startService.EnableRaisingEvents = true; - startService.Start(); + startService.OutputDataReceived += (sender, args) => Init.Logger.Information(args.Data!); + startService.ErrorDataReceived += (sender, args) => Init.Logger.Error(args.Data!); - startService.OutputDataReceived += (sender, args) => - { - Console.WriteLine(args); - }; + if (!startService.Start()) + throw new Exception("Process failed to start"); - return new StartReply - { - Success = true, - }; + startService.BeginOutputReadLine(); + startService.BeginErrorReadLine(); + + Init.Logger.Information($"Started service {Path.GetFileName(request.Path)}."); + + await responseStream.WriteAsync(new StartReply { Success = true }); + + await startService.WaitForExitAsync(); } } diff --git a/netsd/Handlers/Start.cs b/netsd/Handlers/Start.cs index 239f61f..9365d4e 100644 --- a/netsd/Handlers/Start.cs +++ b/netsd/Handlers/Start.cs @@ -1,3 +1,4 @@ +using Grpc.Core; using netsd.Utils; using StartGrpc; @@ -5,7 +6,7 @@ namespace netsd.Handlers; public class Start { - public Start(string? ServiceFilePath = null, string? ServiceName = null) + public async Task StartAsync(string? ServiceFilePath = null, string? ServiceName = null) { if (ServiceFilePath == null && ServiceName != null) { @@ -24,6 +25,15 @@ public class Start var client = new StartRpc.StartRpcClient(channel); var reply = client.StartService(new StartRequest{ Path = ServiceFilePath }); - Console.WriteLine(reply.Success); + while (await reply.ResponseStream.MoveNext()) + { + var current = reply.ResponseStream.Current; + Console.WriteLine($"{current.Success}"); + + if (!current.Success) continue; + + Environment.Exit(0); + break; + } } } diff --git a/netsd/Program.cs b/netsd/Program.cs index cd0093a..4c5f9f3 100644 --- a/netsd/Program.cs +++ b/netsd/Program.cs @@ -19,15 +19,15 @@ rootCommand.AddCommand(startCommand); initCommand.SetHandler(() => new Init()); -startCommand.SetHandler((file, name) => +startCommand.SetHandler(async (file, name) => { if (name.Length != 0) { - new Start(ServiceName: name); + await new Start().StartAsync(ServiceName: name); } else { - new Start(ServiceFilePath: file.FullName); + await new Start().StartAsync(ServiceFilePath: file.FullName); } }, startServicePath, startServiceName); diff --git a/netsd/Protos/Start.proto b/netsd/Protos/Start.proto index edf079f..6c59817 100644 --- a/netsd/Protos/Start.proto +++ b/netsd/Protos/Start.proto @@ -5,13 +5,14 @@ option csharp_namespace = "StartGrpc"; package netsd; service StartRpc { - rpc StartService (StartRequest) returns (StartReply); + rpc StartService (StartRequest) returns (stream StartReply); } message StartRequest { - string path = 1; + string Path = 1; } message StartReply { bool Success = 1; + optional string Error = 2; } diff --git a/netsd/netsd.csproj b/netsd/netsd.csproj index ee2b409..6ca578a 100644 --- a/netsd/netsd.csproj +++ b/netsd/netsd.csproj @@ -32,10 +32,13 @@ - + + + +