cleanup process and datadir after test stop

This commit is contained in:
ThatBen 2025-01-28 11:21:47 +01:00
parent e4512438ed
commit b1197975a1
No known key found for this signature in database
GPG Key ID: 62C543548433D43E
7 changed files with 55 additions and 27 deletions

View File

@ -26,6 +26,11 @@ namespace CodexPlugin
return StartCodexBinaries(codexSetup, codexSetup.NumberOfNodes);
}
public void Decommission()
{
processControlMap.StopAll();
}
private ICodexInstance[] StartCodexBinaries(CodexStartupConfig startupConfig, int numberOfNodes)
{
var result = new List<ICodexInstance>();
@ -39,11 +44,12 @@ namespace CodexPlugin
private ICodexInstance StartBinary(CodexStartupConfig config)
{
var portMap = new CodexPortMap(freePortFinder);
var factory = new CodexProcessRecipe(portMap, numberSource);
var name = "codex_" + numberSource.GetNextNumber();
var dataDir = $"datadir_{numberSource.GetNextNumber()}";
var pconfig = new CodexProcessConfig(name, freePortFinder, dataDir);
var factory = new CodexProcessRecipe(pconfig);
var recipe = factory.Initialize(config);
var name = "codex_" + numberSource.GetNextNumber();
var startInfo = new ProcessStartInfo(
fileName: recipe.Cmd,
arguments: recipe.Args
@ -63,14 +69,14 @@ namespace CodexPlugin
name: name,
imageName: "binary",
startUtc: DateTime.UtcNow,
discoveryEndpoint: new Address("Disc", local, portMap.DiscPort),
apiEndpoint: new Address("Api", "http://" + local, portMap.ApiPort),
listenEndpoint: new Address("Listen", local, portMap.ListenPort),
discoveryEndpoint: new Address("Disc", local, pconfig.DiscPort),
apiEndpoint: new Address("Api", "http://" + local, pconfig.ApiPort),
listenEndpoint: new Address("Listen", local, pconfig.ListenPort),
ethAccount: null,
metricsEndpoint: null
);
var pc = new BinaryProcessControl(process, name);
var pc = new BinaryProcessControl(process, pconfig);
processControlMap.Add(instance, pc);
return instance;

View File

@ -6,17 +6,17 @@ namespace CodexPlugin
{
public class BinaryProcessControl : IProcessControl
{
private Process process;
private readonly string nodeName;
private bool running;
private readonly Process process;
private readonly CodexProcessConfig config;
private readonly List<string> logLines = new List<string>();
private readonly List<Task> streamTasks = new List<Task>();
private bool running;
public BinaryProcessControl(Process process, string nodeName)
public BinaryProcessControl(Process process, CodexProcessConfig config)
{
running = true;
this.process = process;
this.nodeName = nodeName;
this.config = config;
streamTasks.Add(Task.Run(() => ReadProcessStream(process.StandardOutput)));
streamTasks.Add(Task.Run(() => ReadProcessStream(process.StandardError)));
}
@ -32,13 +32,14 @@ namespace CodexPlugin
public void DeleteDataDirFolder()
{
throw new NotImplementedException();
if (!Directory.Exists(config.DataDir)) throw new Exception("datadir not found");
Directory.Delete(config.DataDir, true);
}
public IDownloadedLog DownloadLog(LogFile file)
{
foreach (var line in logLines) file.WriteRaw(line);
return new DownloadedLog(file, nodeName);
return new DownloadedLog(file, config.Name);
}
public bool HasCrashed()
@ -50,7 +51,13 @@ namespace CodexPlugin
{
running = false;
process.Kill();
foreach (var t in streamTasks) t.Wait();
if (waitTillStopped)
{
foreach (var t in streamTasks) t.Wait();
}
DeleteDataDirFolder();
}
}
}

View File

@ -36,6 +36,7 @@ namespace CodexPlugin
public void Decommission()
{
codexStarter.Decommission();
}
public ICodexInstance[] DeployCodexNodes(int numberOfNodes, Action<ICodexSetup> setup)

View File

@ -17,42 +17,43 @@ namespace CodexPlugin
public string[] Args { get; }
}
public class CodexPortMap
public class CodexProcessConfig
{
public CodexPortMap(FreePortFinder freePortFinder)
public CodexProcessConfig(string name, FreePortFinder freePortFinder, string dataDir)
{
ApiPort = freePortFinder.GetNextFreePort();
DiscPort = freePortFinder.GetNextFreePort();
ListenPort = freePortFinder.GetNextFreePort();
Name = name;
DataDir = dataDir;
}
public int ApiPort { get; }
public int DiscPort { get; }
public int ListenPort { get; }
public string Name { get; }
public string DataDir { get; }
}
public class CodexProcessRecipe
{
private readonly CodexPortMap portMap;
private readonly NumberSource numberSource;
private readonly CodexProcessConfig pc;
public CodexProcessRecipe(CodexPortMap portMap, NumberSource numberSource)
public CodexProcessRecipe(CodexProcessConfig pc)
{
this.portMap = portMap;
this.numberSource = numberSource;
this.pc = pc;
}
public ProcessRecipe Initialize(CodexStartupConfig config)
{
args.Clear();
AddArg("--api-port", portMap.ApiPort);
AddArg("--api-port", pc.ApiPort);
AddArg("--api-bindaddr", "0.0.0.0");
var dataDir = $"datadir_{numberSource.GetNextNumber()}";
AddArg("--data-dir", dataDir);
AddArg("--data-dir", pc.DataDir);
AddArg("--disc-port", portMap.DiscPort);
AddArg("--disc-port", pc.DiscPort);
AddArg("--log-level", config.LogLevelWithTopics());
// This makes the node announce itself to its local IP address.
@ -62,7 +63,7 @@ namespace CodexPlugin
AddArg("--nat", $"extip:{ipaddrs.ToStringInvariant()}");
AddArg("--listen-addrs", $"/ip4/0.0.0.0/tcp/{portMap.ListenPort}");
AddArg("--listen-addrs", $"/ip4/0.0.0.0/tcp/{pc.ListenPort}");
if (!string.IsNullOrEmpty(config.BootstrapSpr))
{

View File

@ -43,6 +43,10 @@ namespace CodexPlugin
return containers.Select(CreateInstance).ToArray();
}
public void Decommission()
{
}
private StartupConfig CreateStartupConfig(CodexSetup codexSetup)
{
var startupConfig = new StartupConfig();

View File

@ -5,5 +5,6 @@ namespace CodexPlugin
public interface ICodexStarter
{
ICodexInstance[] BringOnline(CodexSetup codexSetup);
void Decommission();
}
}

View File

@ -25,5 +25,13 @@ namespace CodexPlugin
{
return processControlMap[instance.Name];
}
public void StopAll()
{
var pcs = processControlMap.Values.ToArray();
processControlMap.Clear();
foreach (var c in pcs) c.Stop(waitTillStopped: true);
}
}
}