The guard (SPEC 9.3, 9.4) is now the container's main process and the parent of the engine. It listens on a named pipe for the engine's heartbeat and startup steps, and steps in after two minutes of silence, ten minutes of a startup that does not move, or an exit without notice. It stops the engine, cancels our purchases and sales left open on demo accounts, puts back the stops the positions need from the state file the engine writes every ten seconds, writes a note, warns the user and restarts the engine without any injected fault. The third forced restart within an hour writes the STOP file. The container's stop is passed on to the engine, which closes in order. At restart the engine adopts the guard's stops before reconciling, cancels the orders it left open and plans again the exits already decided, so the rules of SPEC 5.6 apply to them. The acceptance test blocks the main loop right after a sale leaves: in process on a demo-like broker, and with real processes in the drill, which also runs inside the Docker image (tasks ci and the CI workflow). The guard uses the ASP.NET shared runtime so both programs share /app. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
261 lines
9.4 KiB
C#
261 lines
9.4 KiB
C#
using System.Diagnostics;
|
|
using System.Globalization;
|
|
|
|
// Project commands (ADR 0003): the same steps on Windows and Linux, without make.
|
|
// Run through tasks.ps1 / tasks.sh, which build this tool apart from the solution.
|
|
|
|
var root = FindRoot();
|
|
var command = args.Length > 0 ? args[0].ToLowerInvariant() : "help";
|
|
var rest = args.Skip(1).ToArray();
|
|
|
|
return command switch
|
|
{
|
|
"check" => Check(),
|
|
"test" => Test(Environment.GetEnvironmentVariable("ANUBIS_TEST_DB") ?? "sqlite", rest),
|
|
"dev" => Run("dotnet", ["watch", "--project", "src/Anubis.Server", "run", .. rest]),
|
|
"build" => BuildImage("dev"),
|
|
"ci" => Ci(),
|
|
"hooks" => Step("Attivazione dell'hook pre-commit (.githooks)", "git", ["config", "core.hooksPath", ".githooks"]) ?? 0,
|
|
"seed-demo" => Run("dotnet", ["run", "--project", "src/Anubis.Server", "-c", "Release", "--", "--demo", .. rest]),
|
|
"screenshots" => Screenshots(rest),
|
|
"desktop-win" => NotYet("desktop-win", 10),
|
|
_ => Help(),
|
|
};
|
|
|
|
// Locked restore, formatting, build with analyzers as errors, tests on the engine in ANUBIS_TEST_DB.
|
|
int Check()
|
|
{
|
|
var engine = Environment.GetEnvironmentVariable("ANUBIS_TEST_DB") ?? "sqlite";
|
|
return Step("Ripristino dei pacchetti dai file di blocco", "dotnet", ["restore", "Anubis.slnx", "--locked-mode"])
|
|
?? Step("Controllo della formattazione", "dotnet", ["format", "Anubis.slnx", "--verify-no-changes", "--no-restore"])
|
|
?? Step("Compilazione con gli analizzatori", "dotnet", ["build", "Anubis.slnx", "--no-restore", "-c", "Release"])
|
|
?? Test(engine, ["--no-build", "-c", "Release"]);
|
|
}
|
|
|
|
int Test(string engine, string[] extra)
|
|
{
|
|
var env = new Dictionary<string, string> { ["ANUBIS_TEST_DB"] = engine, ["TESTINGPLATFORM_TELEMETRY_OPTOUT"] = "1" };
|
|
return Step($"Test su {engine}", "dotnet",
|
|
["test", "Anubis.slnx", "--filter", "TestCategory!=LiveDemo", "--logger", "console;verbosity=normal", .. extra], env) ?? 0;
|
|
}
|
|
|
|
// What the remote CI does, run locally: check on SQLite, tests on PostgreSQL, image build, the guard drill in the image.
|
|
int Ci()
|
|
{
|
|
var failed = Check();
|
|
if (failed != 0)
|
|
{
|
|
return failed;
|
|
}
|
|
|
|
var ownPostgres = Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_HOST") is null;
|
|
if (ownPostgres)
|
|
{
|
|
failed = Step("Avvio di PostgreSQL per i test", "docker", ["compose", "up", "-d", "--wait", "postgres"]) ?? 0;
|
|
if (failed != 0)
|
|
{
|
|
return failed;
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
failed = Test("postgres", ["--no-build", "-c", "Release"]);
|
|
}
|
|
finally
|
|
{
|
|
if (ownPostgres)
|
|
{
|
|
Step("Arresto di PostgreSQL", "docker", ["compose", "stop", "postgres"]);
|
|
}
|
|
}
|
|
|
|
failed = failed != 0 ? failed : BuildImage("ci");
|
|
return failed != 0 ? failed : GuardDrill("ci");
|
|
}
|
|
|
|
// The guard drill inside the image (issue #48): the real guard and the real engine, a sale blocked halfway, the restart.
|
|
int GuardDrill(string tag) =>
|
|
Step("Esercitazione della guardia nell'immagine", "docker", ["run", "--rm", $"gitea.encke-hake.ts.net/balbosoft/anubis:{tag}", "drill"]) ?? 0;
|
|
|
|
int BuildImage(string tag) =>
|
|
Step("Costruzione dell'immagine Docker", "docker",
|
|
["build", "-f", "deploy/docker/Dockerfile", "-t", $"gitea.encke-hake.ts.net/balbosoft/anubis:{tag}", "."]) ?? 0;
|
|
|
|
// Demo data photographed at 1440 and 390 pixels, light and dark, to compare with docs/design (SPEC 13.1). Uses the
|
|
// Edge or Chrome already on the computer, driven through its DevTools protocol: nothing to install (ADR 0007).
|
|
int Screenshots(string[] pages)
|
|
{
|
|
var browser = FindBrowser();
|
|
if (browser is null)
|
|
{
|
|
Console.Error.WriteLine("Serve Microsoft Edge o Google Chrome installato per le fotografie.");
|
|
return 2;
|
|
}
|
|
|
|
var failed = Step("Compilazione del server", "dotnet", ["build", "src/Anubis.Server", "-c", "Release"]);
|
|
if (failed is not null)
|
|
{
|
|
return failed.Value;
|
|
}
|
|
|
|
var port = FreePort();
|
|
var url = "http://127.0.0.1:" + port.ToString(CultureInfo.InvariantCulture);
|
|
var server = new ProcessStartInfo("dotnet") { WorkingDirectory = root, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true };
|
|
foreach (var argument in new[] { Path.Combine("src", "Anubis.Server", "bin", "Release", "net10.0", "Anubis.Server.dll"), "--demo", "--urls", url })
|
|
{
|
|
server.ArgumentList.Add(argument);
|
|
}
|
|
|
|
using var process = Process.Start(server)!;
|
|
process.OutputDataReceived += (_, _) => { };
|
|
process.ErrorDataReceived += (_, _) => { };
|
|
process.BeginOutputReadLine();
|
|
process.BeginErrorReadLine();
|
|
try
|
|
{
|
|
using var http = new System.Net.Http.HttpClient();
|
|
var ready = false;
|
|
for (var i = 0; i < 60 && !ready; i++)
|
|
{
|
|
try
|
|
{
|
|
ready = http.GetAsync(new Uri(url + "/health")).GetAwaiter().GetResult().IsSuccessStatusCode;
|
|
}
|
|
catch (System.Net.Http.HttpRequestException)
|
|
{
|
|
}
|
|
|
|
if (!ready)
|
|
{
|
|
Thread.Sleep(500);
|
|
}
|
|
}
|
|
|
|
if (!ready)
|
|
{
|
|
Console.Error.WriteLine("Il server dimostrativo non risponde.");
|
|
return 1;
|
|
}
|
|
|
|
var folder = Path.Combine(root, "artifacts", "screenshots");
|
|
Directory.CreateDirectory(folder);
|
|
var targets = pages.Length > 0 ? pages : ["/", "/conti", "/diario", "/impostazioni"];
|
|
using var shots = Shots.Start(browser);
|
|
foreach (var page in targets)
|
|
{
|
|
var name = page == "/" ? "panoramica" : page.Trim('/').Replace('/', '-');
|
|
foreach (var (width, height, dark) in new[] { (1440, 1400, false), (390, 2200, false), (1440, 1400, true) })
|
|
{
|
|
var file = Path.Combine(folder, $"{name}-{width.ToString(CultureInfo.InvariantCulture)}{(dark ? "-scuro" : "")}.png");
|
|
shots.Take(url + page, width, height, dark, file);
|
|
Console.WriteLine($" {file}");
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
finally
|
|
{
|
|
process.Kill(entireProcessTree: true);
|
|
}
|
|
}
|
|
|
|
static string? FindBrowser()
|
|
{
|
|
string[] candidates =
|
|
[
|
|
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Microsoft", "Edge", "Application", "msedge.exe"),
|
|
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Microsoft", "Edge", "Application", "msedge.exe"),
|
|
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Google", "Chrome", "Application", "chrome.exe"),
|
|
"/usr/bin/chromium", "/usr/bin/chromium-browser", "/usr/bin/google-chrome", "/usr/bin/microsoft-edge",
|
|
];
|
|
return candidates.FirstOrDefault(File.Exists);
|
|
}
|
|
|
|
static int FreePort()
|
|
{
|
|
var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
|
|
listener.Start();
|
|
var port = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;
|
|
listener.Stop();
|
|
return port;
|
|
}
|
|
|
|
int NotYet(string name, int phase)
|
|
{
|
|
Console.Error.WriteLine($"'tasks {name}' arriva nella Fase {phase.ToString(CultureInfo.InvariantCulture)}.");
|
|
return 2;
|
|
}
|
|
|
|
int Help()
|
|
{
|
|
Console.WriteLine("""
|
|
Comandi di Anubis (ADR 0003):
|
|
tasks check ripristino bloccato, formattazione, compilazione, test
|
|
tasks test solo i test (motore da ANUBIS_TEST_DB, predefinito sqlite)
|
|
tasks dev avvia il server e lo riavvia a ogni modifica
|
|
tasks build costruisce l'immagine Docker
|
|
tasks ci come la CI: check, test su PostgreSQL, immagine
|
|
tasks hooks attiva l'hook che esegue check prima di ogni commit
|
|
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 desktop-win installatore Windows (Fase 10)
|
|
""");
|
|
return command == "help" ? 0 : 1;
|
|
}
|
|
|
|
// Runs one step and returns its exit code only when it failed, so steps chain with ??.
|
|
int? Step(string title, string file, string[] arguments, Dictionary<string, string>? env = null)
|
|
{
|
|
Console.WriteLine();
|
|
Console.WriteLine($"== {title}");
|
|
var code = Run(file, arguments, env);
|
|
if (code != 0)
|
|
{
|
|
Console.Error.WriteLine($"!! {title}: non riuscito (codice {code.ToString(CultureInfo.InvariantCulture)})");
|
|
return code;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
int Run(string file, string[] arguments, Dictionary<string, string>? env = null)
|
|
{
|
|
var info = new ProcessStartInfo(file) { WorkingDirectory = root, UseShellExecute = false };
|
|
foreach (var argument in arguments)
|
|
{
|
|
info.ArgumentList.Add(argument);
|
|
}
|
|
|
|
foreach (var (key, value) in env ?? [])
|
|
{
|
|
info.Environment[key] = value;
|
|
}
|
|
|
|
try
|
|
{
|
|
using var process = Process.Start(info)!;
|
|
process.WaitForExit();
|
|
return process.ExitCode;
|
|
}
|
|
catch (System.ComponentModel.Win32Exception ex)
|
|
{
|
|
Console.Error.WriteLine($"Impossibile avviare '{file}': {ex.Message}");
|
|
return 127;
|
|
}
|
|
}
|
|
|
|
static string FindRoot()
|
|
{
|
|
for (var dir = new DirectoryInfo(Environment.CurrentDirectory); dir is not null; dir = dir.Parent)
|
|
{
|
|
if (File.Exists(Path.Combine(dir.FullName, "Anubis.slnx")))
|
|
{
|
|
return dir.FullName;
|
|
}
|
|
}
|
|
|
|
throw new InvalidOperationException("Anubis.slnx non trovato: lancia i comandi dalla cartella del repository.");
|
|
}
|