Archived
0
0
Fork 0

add basic logging and process logs

This commit is contained in:
Daryl Ronningen 2022-11-18 01:33:42 -08:00
parent 308b46c26a
commit 50b682c875
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
5 changed files with 56 additions and 23 deletions

View file

@ -6,24 +6,34 @@ using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using netsd.Utils; using netsd.Utils;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using StartGrpc; using StartGrpc;
namespace netsd.Handlers; namespace netsd.Handlers;
public class Init 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(); private readonly WebApplicationBuilder _socketBuilder = WebApplication.CreateBuilder();
public Init() public Init()
{ {
_socketBuilder.WebHost.ConfigureKestrel(options => _socketBuilder.WebHost.ConfigureKestrel(options =>
{ {
options.ListenUnixSocket("/tmp/netsd.sock", listenOptions => options.ListenUnixSocket("/tmp/netsd.sock",
{ listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; });
listenOptions.Protocols = HttpProtocols.Http2;
});
}); });
_socketBuilder.Host.UseSerilog(Logger);
_socketBuilder.Logging.ClearProviders(); _socketBuilder.Logging.ClearProviders();
_socketBuilder.Services.AddGrpc(); _socketBuilder.Services.AddGrpc();
@ -38,25 +48,34 @@ public class Init
public class StartGrpc : StartRpc.StartRpcBase public class StartGrpc : StartRpc.StartRpcBase
{ {
public override async Task<StartReply> StartService(StartRequest request, ServerCallContext context) public override async Task StartService(StartRequest request, IServerStreamWriter<StartReply> responseStream,
ServerCallContext context)
{ {
Init.Logger.Information($"Received request to start service {Path.GetFileName(request.Path)}.");
var parseServiceFile = await ServiceParser.Parse(request.Path); var parseServiceFile = await ServiceParser.Parse(request.Path);
using var startService = new Process(); using var startService = new Process();
startService.StartInfo.CreateNoWindow = true;
startService.StartInfo.FileName = parseServiceFile.ExecStart?.Split(' ').First(); startService.StartInfo.FileName = parseServiceFile.ExecStart?.Split(' ').First();
startService.StartInfo.Arguments = string.Join(" ", parseServiceFile.ExecStart?.Split(' ').Skip(1)!); 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) => if (!startService.Start())
{ throw new Exception("Process failed to start");
Console.WriteLine(args);
};
return new StartReply startService.BeginOutputReadLine();
{ startService.BeginErrorReadLine();
Success = true,
}; Init.Logger.Information($"Started service {Path.GetFileName(request.Path)}.");
await responseStream.WriteAsync(new StartReply { Success = true });
await startService.WaitForExitAsync();
} }
} }

View file

@ -1,3 +1,4 @@
using Grpc.Core;
using netsd.Utils; using netsd.Utils;
using StartGrpc; using StartGrpc;
@ -5,7 +6,7 @@ namespace netsd.Handlers;
public class Start 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) if (ServiceFilePath == null && ServiceName != null)
{ {
@ -24,6 +25,15 @@ public class Start
var client = new StartRpc.StartRpcClient(channel); var client = new StartRpc.StartRpcClient(channel);
var reply = client.StartService(new StartRequest{ Path = ServiceFilePath }); 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;
}
} }
} }

View file

@ -19,15 +19,15 @@ rootCommand.AddCommand(startCommand);
initCommand.SetHandler(() => new Init()); initCommand.SetHandler(() => new Init());
startCommand.SetHandler((file, name) => startCommand.SetHandler(async (file, name) =>
{ {
if (name.Length != 0) if (name.Length != 0)
{ {
new Start(ServiceName: name); await new Start().StartAsync(ServiceName: name);
} }
else else
{ {
new Start(ServiceFilePath: file.FullName); await new Start().StartAsync(ServiceFilePath: file.FullName);
} }
}, startServicePath, startServiceName); }, startServicePath, startServiceName);

View file

@ -5,13 +5,14 @@ option csharp_namespace = "StartGrpc";
package netsd; package netsd;
service StartRpc { service StartRpc {
rpc StartService (StartRequest) returns (StartReply); rpc StartService (StartRequest) returns (stream StartReply);
} }
message StartRequest { message StartRequest {
string path = 1; string Path = 1;
} }
message StartReply { message StartReply {
bool Success = 1; bool Success = 1;
optional string Error = 2;
} }

View file

@ -32,10 +32,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.50.0-pre1" /> <PackageReference Include="Grpc.AspNetCore" Version="2.50.0" />
<PackageReference Include="ini-parser" Version="3.4.0" /> <PackageReference Include="ini-parser" Version="3.4.0" />
<PackageReference Include="runtime.linux-musl-x64.Microsoft.DotNet.ILCompiler" Version="7.0.0" /> <PackageReference Include="runtime.linux-musl-x64.Microsoft.DotNet.ILCompiler" Version="7.0.0" />
<PackageReference Include="runtime.linux-x64.Microsoft.DotNet.ILCompiler" Version="7.0.0" /> <PackageReference Include="runtime.linux-x64.Microsoft.DotNet.ILCompiler" Version="7.0.0" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" /> <PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup> </ItemGroup>