add stopping of services
This commit is contained in:
parent
9771a55394
commit
4015f1b3e5
9 changed files with 89 additions and 31 deletions
|
@ -5,10 +5,10 @@ using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using netsd.Utils;
|
||||
using NetsdRPCs;
|
||||
using Serilog;
|
||||
using Serilog.Core;
|
||||
using Serilog.Events;
|
||||
using StartGrpc;
|
||||
|
||||
namespace netsd.Handlers;
|
||||
|
||||
|
@ -39,15 +39,15 @@ public class Init
|
|||
|
||||
var app = _socketBuilder.Build();
|
||||
|
||||
app.MapGrpcService<StartRPC>();
|
||||
app.MapGrpcService<NetsdRPCs>();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
|
||||
public class StartRPC : StartRpc.StartRpcBase
|
||||
public class NetsdRPCs : Messages.MessagesBase
|
||||
{
|
||||
public override async Task<StartReply> StartService(StartRequest request, ServerCallContext context)
|
||||
public override async Task<StartStopReply> StartService(StartRequest request, ServerCallContext context)
|
||||
{
|
||||
Init.Logger.Information($"Received request to start service {Path.GetFileName(request.Path)}.");
|
||||
|
||||
|
@ -59,6 +59,23 @@ public class StartRPC : StartRpc.StartRpcBase
|
|||
|
||||
Globals.Services.Add(startService);
|
||||
|
||||
return new StartReply { Success = true };
|
||||
return new StartStopReply { Success = true };
|
||||
}
|
||||
|
||||
public override async Task<StartStopReply> StopService(StopRequest request, ServerCallContext context)
|
||||
{
|
||||
Init.Logger.Information($"Received request to stop service {request.Name}.");
|
||||
|
||||
var findService = Globals.Services.FindAll(service => service.Name == request.Name);
|
||||
|
||||
if (findService.Count == 0)
|
||||
return new StartStopReply { Success = false, Error = $"Could not find service with name {request.Name}!" };
|
||||
|
||||
await findService.First().StopServiceAsync();
|
||||
Globals.Services.Remove(findService.First());
|
||||
|
||||
Init.Logger.Information($"Stopped service {request.Name}!");
|
||||
|
||||
return new StartStopReply { Success = true };
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
using netsd.Utils;
|
||||
using StartGrpc;
|
||||
using NetsdRPCs;
|
||||
|
||||
namespace netsd.Handlers;
|
||||
|
||||
public class Start
|
||||
public static class Start
|
||||
{
|
||||
public async Task StartAsync(string? ServiceFilePath = null, string? ServiceName = null)
|
||||
public static async Task StartAsync(string? ServiceFilePath = null, string? ServiceName = null)
|
||||
{
|
||||
if (ServiceFilePath == null && ServiceName != null)
|
||||
{
|
||||
|
@ -21,7 +21,7 @@ public class Start
|
|||
|
||||
using var channel = UDSConnector.CreateChannel();
|
||||
|
||||
var client = new StartRpc.StartRpcClient(channel);
|
||||
var client = new Messages.MessagesClient(channel);
|
||||
var reply = await client.StartServiceAsync(new StartRequest { Path = ServiceFilePath });
|
||||
|
||||
Console.WriteLine($"{reply.Success}");
|
||||
|
|
17
netsd/Handlers/Stop.cs
Normal file
17
netsd/Handlers/Stop.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
using netsd.Utils;
|
||||
using NetsdRPCs;
|
||||
|
||||
namespace netsd.Handlers;
|
||||
|
||||
public static class Stop
|
||||
{
|
||||
public static async Task StopAsync(string ServiceName)
|
||||
{
|
||||
using var channel = UDSConnector.CreateChannel();
|
||||
|
||||
var client = new Messages.MessagesClient(channel);
|
||||
var reply = await client.StopServiceAsync(new StopRequest { Name = ServiceName });
|
||||
|
||||
Console.WriteLine($"{reply.Success}");
|
||||
}
|
||||
}
|
|
@ -4,27 +4,35 @@ using netsd.Handlers;
|
|||
var initCommand = new Command("init", "Run and bring up all default services");
|
||||
|
||||
var startCommand = new Command("start", "Starts up a netsd service");
|
||||
var startServiceName = new Argument<string>("file", "The name of the service you want to start");
|
||||
var startServiceName = new Argument<string>("name", "The name of the service you want to start");
|
||||
var startServicePath = new Option<FileInfo>("--file", "The path to the service file");
|
||||
|
||||
var stopCommand = new Command("stop", "Stops a running netsd service");
|
||||
var stopServiceName = new Argument<string>("name", "The name of the service you want to stop");
|
||||
|
||||
startServiceName.SetDefaultValue("");
|
||||
startServicePath.AddAlias("-f");
|
||||
|
||||
startCommand.AddArgument(startServiceName);
|
||||
startCommand.AddOption(startServicePath);
|
||||
|
||||
stopCommand.AddArgument(stopServiceName);
|
||||
|
||||
var rootCommand = new RootCommand("Linux service manager written in C#");
|
||||
rootCommand.AddCommand(initCommand);
|
||||
rootCommand.AddCommand(startCommand);
|
||||
rootCommand.AddCommand(stopCommand);
|
||||
|
||||
initCommand.SetHandler(() => new Init());
|
||||
|
||||
startCommand.SetHandler(async (file, name) =>
|
||||
{
|
||||
if (name.Length != 0)
|
||||
await new Start().StartAsync(ServiceName: name);
|
||||
await Start.StartAsync(ServiceName: name);
|
||||
else
|
||||
await new Start().StartAsync(file.FullName);
|
||||
await Start.StartAsync(file.FullName);
|
||||
}, startServicePath, startServiceName);
|
||||
|
||||
stopCommand.SetHandler(async name => { await Stop.StopAsync(name); }, stopServiceName);
|
||||
|
||||
return await rootCommand.InvokeAsync(args);
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
syntax = "proto3";
|
||||
|
||||
option csharp_namespace = "StartGrpc";
|
||||
|
||||
package netsd;
|
||||
|
||||
service StartRpc {
|
||||
rpc StartService (StartRequest) returns (StartReply);
|
||||
}
|
||||
|
||||
message StartRequest {
|
||||
string Path = 1;
|
||||
}
|
||||
|
||||
message StartReply {
|
||||
bool Success = 1;
|
||||
optional string Error = 2;
|
||||
}
|
23
netsd/Protos/rpcs.proto
Normal file
23
netsd/Protos/rpcs.proto
Normal file
|
@ -0,0 +1,23 @@
|
|||
syntax = "proto3";
|
||||
|
||||
option csharp_namespace = "NetsdRPCs";
|
||||
|
||||
package netsd;
|
||||
|
||||
service Messages {
|
||||
rpc StartService (StartRequest) returns (StartStopReply);
|
||||
rpc StopService (StopRequest) returns (StartStopReply);
|
||||
}
|
||||
|
||||
message StartRequest {
|
||||
string Path = 1;
|
||||
}
|
||||
|
||||
message StartStopReply {
|
||||
bool Success = 1;
|
||||
optional string Error = 2;
|
||||
}
|
||||
|
||||
message StopRequest {
|
||||
string Name = 1;
|
||||
}
|
|
@ -18,3 +18,4 @@ public class ServiceParser
|
|||
return sv;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,9 +9,12 @@ public class StartService
|
|||
private readonly ServiceParser _parseServiceFile;
|
||||
private readonly Process _process;
|
||||
private readonly string _servicePath;
|
||||
public readonly string Name;
|
||||
|
||||
private StartService(string servicePath, ServiceParser parseServiceFile)
|
||||
{
|
||||
Name = Path.GetFileName(servicePath).Split(".")[0];
|
||||
|
||||
_parseServiceFile = parseServiceFile;
|
||||
_servicePath = servicePath;
|
||||
_process = new Process();
|
||||
|
@ -43,6 +46,13 @@ public class StartService
|
|||
return new StartService(servicePath, parseServiceFile);
|
||||
}
|
||||
|
||||
public async Task StopServiceAsync()
|
||||
{
|
||||
_process.Kill();
|
||||
await _process.WaitForExitAsync();
|
||||
_process.Dispose();
|
||||
}
|
||||
|
||||
public async void StartServiceAsync()
|
||||
{
|
||||
_process.Start();
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Protobuf Include="Protos\Start.proto" />
|
||||
<Protobuf Include="Protos\rpcs.proto" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Reference in a new issue