diff --git a/LibSystemdCli/CommandExecutor.cs b/LibSystemdCli/CommandExecutor.cs
index 096f1c1..8a21bc5 100644
--- a/LibSystemdCli/CommandExecutor.cs
+++ b/LibSystemdCli/CommandExecutor.cs
@@ -27,6 +27,16 @@ public class CommandExecutor
throw new Exception($"Command {command} {args} failed with exit code {process.ExitCode} and error: {error}");
}
+ if (string.IsNullOrWhiteSpace(output))
+ {
+ Console.WriteLine($"[{DateTime.Now:O}] Command {command} {args} produced no output.");
+ }
+
+ if (!string.IsNullOrWhiteSpace(error))
+ {
+ Console.WriteLine($"[{DateTime.Now:O}] Command {command} {args} produced error output: {error}");
+ }
+
return output;
}
diff --git a/LibSystemdCli/SystemdExecutor.cs b/LibSystemdCli/SystemdExecutor.cs
index ead11fd..f252891 100644
--- a/LibSystemdCli/SystemdExecutor.cs
+++ b/LibSystemdCli/SystemdExecutor.cs
@@ -8,7 +8,15 @@ public class SystemdExecutor {
public static async IAsyncEnumerable<SystemdUnitListItem> GetUnits() {
var output = await CommandExecutor.ExecuteCommand("systemctl", "list-units --all --no-legend --no-pager --no-legend -o json-pretty");
- var data = JsonSerializer.Deserialize<List<SystemdUnitListItem>>(output);
+ List<SystemdUnitListItem>? data;
+ try {
+ data = JsonSerializer.Deserialize<List<SystemdUnitListItem>>(output);
+ }
+ catch (Exception ex) {
+ Console.WriteLine("Failed to parse systemctl output: " + ex);
+ Console.WriteLine("Output was: " + output);
+ yield break;
+ }
foreach (var unit in data) {
try {
@@ -16,7 +24,9 @@ public class SystemdExecutor {
// Console.WriteLine(fragmentOutput);
unit.FragmentPaths = fragmentOutput.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList();
}
- catch { }
+ catch (Exception e) {
+ Console.WriteLine("Failed to get fragment path for unit " + unit.Unit + ": " + e);
+ }
yield return unit;
// await Task.Delay(100);
diff --git a/SystemdCtl/Controllers/UnitController.cs b/SystemdCtl/Controllers/UnitController.cs
index da1c534..40ef46a 100644
--- a/SystemdCtl/Controllers/UnitController.cs
+++ b/SystemdCtl/Controllers/UnitController.cs
@@ -7,20 +7,20 @@ using LibSystemdCli.Models;
using Microsoft.AspNetCore.Mvc;
namespace SystemdCtl.Controllers;
+
[ApiController]
[Route("/api/")]
-public class UnitController : ControllerBase
-{
+public class UnitController : ControllerBase {
[HttpGet("listUnits")]
- public async IAsyncEnumerable<SystemdUnitListItem> GetUnits()
- {
- await foreach (var unit in SystemdExecutor.GetUnits())
- {
+ public async IAsyncEnumerable<SystemdUnitListItem> GetUnits() {
+ await foreach (var unit in SystemdExecutor.GetUnits()) {
yield return unit;
- await Response.Body.FlushAsync();
+
+ if (Response.HasStarted)
+ await Response.Body.FlushAsync();
}
}
-
+
// [HttpGet("unit/{serviceName}/logs")]
// public async IAsyncEnumerable<SystemdJournalLogItem> GetUnitLogs(string serviceName, [FromQuery] int contextLines = 100)
// {
@@ -32,32 +32,29 @@ public class UnitController : ControllerBase
// }
// }
[HttpGet("unit/{serviceName}/logs")]
- public async Task GetUnitLogs(string serviceName, [FromQuery] int contextLines = 100)
- {
+ public async Task GetUnitLogs(string serviceName, [FromQuery] int contextLines = 100) {
Response.ContentType = "application/json";
await Response.StartAsync();
await Response.Body.WriteAsync("[\n"u8.ToArray());
- await foreach (var log in SystemdExecutor.GetUnitLogs(serviceName, contextLines: contextLines))
- {
+ await foreach (var log in SystemdExecutor.GetUnitLogs(serviceName, contextLines: contextLines)) {
Console.WriteLine(log.Message);
var bytes = Encoding.UTF8.GetBytes($" {log.ToJson(indent: false)},\n");
await Response.Body.WriteAsync(bytes);
await Response.Body.FlushAsync();
}
+
await Response.Body.WriteAsync("]\n"u8.ToArray());
await Response.Body.FlushAsync();
await Response.CompleteAsync();
}
-
+
[HttpGet("unit/{serviceName}/dataStream")]
- public async Task GetUnitDataStream(string serviceName, [FromQuery] int contextLines = 100)
- {
+ public async Task GetUnitDataStream(string serviceName, [FromQuery] int contextLines = 100) {
Response.ContentType = "application/json";
await Response.StartAsync();
await Response.Body.WriteAsync("[\n"u8.ToArray());
var oldData = await SystemdExecutor.GetUnitData(serviceName);
- while(true)
- {
+ while (true) {
var data = await SystemdExecutor.GetUnitData(serviceName);
data.SetOldData(oldData);
var bytes = Encoding.UTF8.GetBytes($" {data.ToJson(indent: false)},\n");
@@ -66,29 +63,30 @@ public class UnitController : ControllerBase
oldData = data;
await Task.Delay(1000);
}
+
await Response.Body.WriteAsync("]\n"u8.ToArray());
await Response.Body.FlushAsync();
await Response.CompleteAsync();
}
-
+
[HttpGet("unit/{serviceName}/data")]
public Task<SystemdServiceData> GetUnitData(string serviceName) => SystemdExecutor.GetUnitData(serviceName);
-
+
[HttpGet("unit/{serviceName}/start")]
public Task StartUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"start {serviceName}");
-
+
[HttpGet("unit/{serviceName}/stop")]
public Task StopUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"stop {serviceName}");
-
+
[HttpGet("unit/{serviceName}/restart")]
public Task RestartUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"restart {serviceName}");
-
+
[HttpGet("unit/{serviceName}/reload")]
public Task ReloadUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"reload {serviceName}");
-
+
[HttpGet("unit/{serviceName}/enable")]
public Task EnableUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"enable {serviceName}");
-
+
[HttpGet("unit/{serviceName}/disable")]
public Task DisableUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"disable {serviceName}");
}
\ No newline at end of file
diff --git a/SystemdCtl/SystemdCtl.csproj b/SystemdCtl/SystemdCtl.csproj
index de289c6..4425832 100644
--- a/SystemdCtl/SystemdCtl.csproj
+++ b/SystemdCtl/SystemdCtl.csproj
@@ -9,7 +9,7 @@
<ItemGroup>
<ProjectReference Include="..\LibSystemdCli\LibSystemdCli.csproj" />
<ProjectReference Include="..\SystemdCtl.Client\SystemdCtl.Client.csproj" />
- <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.0" />
+ <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.8" />
</ItemGroup>
<ItemGroup>
|