From 0a9a8a38ecd5469f8cd347265ea7e82a05d2c705 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Sun, 15 Nov 2026 11:41:31 +0100 Subject: [PATCH] Print the PostgreSQL service log at the end of the CI job (refs #98) The job log shows the tests losing every connection but not why the server went into recovery. A new tasks command reads the service container's log, state and memory through the Docker socket the runner mounts in the job. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/ci.yml | 4 + tools/Anubis.Tasks/Program.cs | 2 + tools/Anubis.Tasks/ServiceLog.cs | 126 +++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 tools/Anubis.Tasks/ServiceLog.cs diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 5e424b3..42c74c1 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -45,6 +45,10 @@ jobs: git checkout -q FETCH_HEAD - name: Check run: sh ./tasks.sh check + # The log of the PostgreSQL service, read through the Docker socket the runner mounts in the job (issue #98). + - name: PostgreSQL log + if: always() && matrix.db == 'postgres' + run: sh ./tasks.sh ci-service-log postgres image: needs: test diff --git a/tools/Anubis.Tasks/Program.cs b/tools/Anubis.Tasks/Program.cs index 25c9906..cc54098 100644 --- a/tools/Anubis.Tasks/Program.cs +++ b/tools/Anubis.Tasks/Program.cs @@ -19,6 +19,7 @@ return command switch "seed-demo" => Run("dotnet", ["run", "--project", "src/Anubis.Server", "-c", "Release", "--", "--demo", .. rest]), "screenshots" => Screenshots(rest), "release-win" => ReleaseWindows(), + "ci-service-log" => ServiceLog.Print(rest.Length > 0 ? rest[0] : "postgres"), "desktop-win" => NotYet("desktop-win", 10), _ => Help(), }; @@ -237,6 +238,7 @@ int Help() tasks seed-demo avvia la modalità dimostrativa (dati dell'appendice F, database a parte) tasks screenshots fotografa le schermate dimostrative a 1440 e 390 pixel tasks release-win zip di prova per Windows in artifacts/release (ADR 0013) + tasks ci-service-log nella CI: registro e memoria del PostgreSQL dei test tasks desktop-win installatore Windows (Fase 10) """); return command == "help" ? 0 : 1; diff --git a/tools/Anubis.Tasks/ServiceLog.cs b/tools/Anubis.Tasks/ServiceLog.cs new file mode 100644 index 0000000..e672490 --- /dev/null +++ b/tools/Anubis.Tasks/ServiceLog.cs @@ -0,0 +1,126 @@ +using System.Globalization; +using System.Net.Http.Headers; +using System.Net.Sockets; +using System.Text; +using System.Text.Json.Nodes; + +/// +/// Prints what the CI cannot see from inside the job: the log, state and memory of the service containers next to this +/// one (the PostgreSQL of the tests). It talks to the Docker Engine API through the socket the runner mounts in the job +/// container, so it needs neither the docker client nor anything else to install (ADR 0004). +/// +internal static class ServiceLog +{ + private const string Socket = "/var/run/docker.sock"; + + public static int Print(string imagePrefix) + { + if (!File.Exists(Socket)) + { + Console.WriteLine($"Nessun {Socket}: il registro dei servizi si legge solo nella CI."); + return 0; + } + + using var http = new HttpClient(new SocketsHttpHandler + { + ConnectCallback = async (_, token) => + { + var socket = new System.Net.Sockets.Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified); + await socket.ConnectAsync(new UnixDomainSocketEndPoint(Socket), token).ConfigureAwait(false); + return new NetworkStream(socket, ownsSocket: true); + }, + }) + { + BaseAddress = new Uri("http://docker/"), + Timeout = TimeSpan.FromMinutes(2), + }; + + var info = Json(http, "info"); + Console.WriteLine($"Host: {info["Name"]}, {info["NCPU"]} CPU, {Mib(info["MemTotal"])} di memoria, kernel {info["KernelVersion"]}"); + + var self = Json(http, $"containers/{Uri.EscapeDataString(Environment.MachineName)}/json"); + var networks = self["NetworkSettings"]?["Networks"]?.AsObject().Select(pair => pair.Key).ToList() ?? []; + var found = 0; + foreach (var network in networks) + { + var filter = Uri.EscapeDataString($$"""{"network":["{{network}}"]}"""); + foreach (var container in Json(http, $"containers/json?all=1&filters={filter}").AsArray()) + { + var image = container?["Image"]?.GetValue() ?? ""; + if (!image.StartsWith(imagePrefix, StringComparison.Ordinal)) + { + continue; + } + + found++; + var id = container!["Id"]!.GetValue(); + Describe(http, id, image); + } + } + + if (found == 0) + { + Console.WriteLine($"Nessun contenitore '{imagePrefix}*' sulle reti {string.Join(", ", networks)}."); + } + + return 0; + } + + private static void Describe(HttpClient http, string id, string image) + { + var inspect = Json(http, $"containers/{id}/json"); + var state = inspect["State"]; + var host = inspect["HostConfig"]; + Console.WriteLine(); + Console.WriteLine($"== {image} {id[..12]}"); + Console.WriteLine($"Stato: {state?["Status"]}, avviato {state?["StartedAt"]}, riavvii {inspect["RestartCount"]}, OOMKilled {state?["OOMKilled"]}, uscita {state?["ExitCode"]}"); + Console.WriteLine($"Limiti: memoria {Mib(host?["Memory"])}, /dev/shm {Mib(host?["ShmSize"])}"); + + if (state?["Running"]?.GetValue() == true) + { + var stats = Json(http, $"containers/{id}/stats?stream=false"); + var memory = stats["memory_stats"]; + Console.WriteLine($"Memoria ora: {Mib(memory?["usage"])}, limite visto dal contenitore {Mib(memory?["limit"])}, massimo {Mib(memory?["max_usage"])}"); + } + + Console.WriteLine("-- registro"); + using var response = http.GetAsync(new Uri($"containers/{id}/logs?stdout=1&stderr=1×tamps=1", UriKind.Relative)).GetAwaiter().GetResult(); + response.EnsureSuccessStatusCode(); + var bytes = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult(); + var raw = response.Content.Headers.ContentType?.MediaType == "application/vnd.docker.raw-stream"; + Console.Write(raw ? Encoding.UTF8.GetString(bytes) : Demultiplex(bytes)); + Console.WriteLine("-- fine del registro"); + } + + // Without a terminal the log arrives in frames: one byte for the stream, three zeros, four bytes of length. + private static string Demultiplex(byte[] bytes) + { + var text = new StringBuilder(); + var at = 0; + while (at + 8 <= bytes.Length) + { + var length = (bytes[at + 4] << 24) | (bytes[at + 5] << 16) | (bytes[at + 6] << 8) | bytes[at + 7]; + var size = Math.Min(length, bytes.Length - at - 8); + text.Append(Encoding.UTF8.GetString(bytes, at + 8, size)); + at += 8 + length; + } + + return text.ToString(); + } + + private static JsonNode Json(HttpClient http, string path) + { + using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(path, UriKind.Relative)); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + using var response = http.Send(request); + response.EnsureSuccessStatusCode(); + using var stream = response.Content.ReadAsStream(); + return JsonNode.Parse(stream) ?? new JsonObject(); + } + + private static string Mib(JsonNode? bytes) + { + var value = bytes?.GetValue() ?? 0; + return value == 0 ? "nessuno" : (value / 1048576.0).ToString("0", CultureInfo.InvariantCulture) + " MiB"; + } +}