mirror of
https://github.com/logos-storage/logos-storage-nim-cs-dist-tests.git
synced 2026-01-05 23:13:08 +00:00
cleanup on startup
This commit is contained in:
parent
b1197975a1
commit
50e58dc799
@ -9,8 +9,17 @@ namespace CodexPlugin
|
||||
{
|
||||
private readonly IPluginTools pluginTools;
|
||||
private readonly ProcessControlMap processControlMap;
|
||||
private readonly NumberSource numberSource = new NumberSource(1);
|
||||
private readonly FreePortFinder freePortFinder = new FreePortFinder();
|
||||
private readonly static NumberSource numberSource = new NumberSource(1);
|
||||
private readonly static FreePortFinder freePortFinder = new FreePortFinder();
|
||||
private readonly static object _lock = new object();
|
||||
private readonly static string dataParentDir = "codex_disttest_datadirs";
|
||||
private readonly static CodexExePath codexExePath = new CodexExePath();
|
||||
|
||||
static BinaryCodexStarter()
|
||||
{
|
||||
StopAllCodexProcesses();
|
||||
DeleteParentDataDir();
|
||||
}
|
||||
|
||||
public BinaryCodexStarter(IPluginTools pluginTools, ProcessControlMap processControlMap)
|
||||
{
|
||||
@ -20,15 +29,21 @@ namespace CodexPlugin
|
||||
|
||||
public ICodexInstance[] BringOnline(CodexSetup codexSetup)
|
||||
{
|
||||
LogSeparator();
|
||||
Log($"Starting {codexSetup.Describe()}...");
|
||||
lock (_lock)
|
||||
{
|
||||
LogSeparator();
|
||||
Log($"Starting {codexSetup.Describe()}...");
|
||||
|
||||
return StartCodexBinaries(codexSetup, codexSetup.NumberOfNodes);
|
||||
return StartCodexBinaries(codexSetup, codexSetup.NumberOfNodes);
|
||||
}
|
||||
}
|
||||
|
||||
public void Decommission()
|
||||
{
|
||||
processControlMap.StopAll();
|
||||
lock (_lock)
|
||||
{
|
||||
processControlMap.StopAll();
|
||||
}
|
||||
}
|
||||
|
||||
private ICodexInstance[] StartCodexBinaries(CodexStartupConfig startupConfig, int numberOfNodes)
|
||||
@ -45,9 +60,11 @@ namespace CodexPlugin
|
||||
private ICodexInstance StartBinary(CodexStartupConfig config)
|
||||
{
|
||||
var name = "codex_" + numberSource.GetNextNumber();
|
||||
var dataDir = $"datadir_{numberSource.GetNextNumber()}";
|
||||
var dataDir = Path.Combine(dataParentDir, $"datadir_{numberSource.GetNextNumber()}");
|
||||
var pconfig = new CodexProcessConfig(name, freePortFinder, dataDir);
|
||||
var factory = new CodexProcessRecipe(pconfig);
|
||||
Log(pconfig);
|
||||
|
||||
var factory = new CodexProcessRecipe(pconfig, codexExePath);
|
||||
var recipe = factory.Initialize(config);
|
||||
|
||||
var startInfo = new ProcessStartInfo(
|
||||
@ -69,7 +86,7 @@ namespace CodexPlugin
|
||||
name: name,
|
||||
imageName: "binary",
|
||||
startUtc: DateTime.UtcNow,
|
||||
discoveryEndpoint: new Address("Disc", local, pconfig.DiscPort),
|
||||
discoveryEndpoint: new Address("Disc", pconfig.LocalIpAddrs.ToString(), pconfig.DiscPort),
|
||||
apiEndpoint: new Address("Api", "http://" + local, pconfig.ApiPort),
|
||||
listenEndpoint: new Address("Listen", local, pconfig.ListenPort),
|
||||
ethAccount: null,
|
||||
@ -87,9 +104,44 @@ namespace CodexPlugin
|
||||
Log("----------------------------------------------------------------------------");
|
||||
}
|
||||
|
||||
private void Log(CodexProcessConfig pconfig)
|
||||
{
|
||||
Log(
|
||||
"NodeConfig:Name=" + pconfig.Name +
|
||||
"ApiPort=" + pconfig.ApiPort +
|
||||
"DiscPort=" + pconfig.DiscPort +
|
||||
"ListenPort=" + pconfig.ListenPort +
|
||||
"DataDir=" + pconfig.DataDir
|
||||
);
|
||||
}
|
||||
|
||||
private void Log(string message)
|
||||
{
|
||||
pluginTools.GetLog().Log(message);
|
||||
}
|
||||
|
||||
private static void DeleteParentDataDir()
|
||||
{
|
||||
if (Directory.Exists(dataParentDir))
|
||||
{
|
||||
Directory.Delete(dataParentDir, true);
|
||||
}
|
||||
}
|
||||
|
||||
private static void StopAllCodexProcesses()
|
||||
{
|
||||
var processes = Process.GetProcesses();
|
||||
var codexes = processes.Where(p =>
|
||||
p.ProcessName.ToLowerInvariant() == "codex" &&
|
||||
p.MainModule != null &&
|
||||
p.MainModule.FileName == codexExePath.Get()
|
||||
).ToArray();
|
||||
|
||||
foreach (var c in codexes)
|
||||
{
|
||||
c.Kill();
|
||||
c.WaitForExit();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,6 +54,7 @@ namespace CodexPlugin
|
||||
|
||||
if (waitTillStopped)
|
||||
{
|
||||
process.WaitForExit();
|
||||
foreach (var t in streamTasks) t.Wait();
|
||||
}
|
||||
|
||||
|
||||
18
ProjectPlugins/CodexPlugin/CodexExePath.cs
Normal file
18
ProjectPlugins/CodexPlugin/CodexExePath.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace CodexPlugin
|
||||
{
|
||||
public class CodexExePath
|
||||
{
|
||||
private readonly string path = Path.Combine(
|
||||
"d:",
|
||||
"Dev",
|
||||
"nim-codex",
|
||||
"build",
|
||||
"codex.exe"
|
||||
);
|
||||
|
||||
public string Get()
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using Utils;
|
||||
using Nethereum.Util;
|
||||
|
||||
namespace CodexPlugin
|
||||
@ -26,6 +25,10 @@ namespace CodexPlugin
|
||||
ListenPort = freePortFinder.GetNextFreePort();
|
||||
Name = name;
|
||||
DataDir = dataDir;
|
||||
var host = Dns.GetHostEntry(Dns.GetHostName());
|
||||
var addrs = host.AddressList.Where(a => a.AddressFamily == AddressFamily.InterNetwork).ToList();
|
||||
|
||||
LocalIpAddrs = addrs.First();
|
||||
}
|
||||
|
||||
public int ApiPort { get; }
|
||||
@ -33,15 +36,18 @@ namespace CodexPlugin
|
||||
public int ListenPort { get; }
|
||||
public string Name { get; }
|
||||
public string DataDir { get; }
|
||||
public IPAddress LocalIpAddrs { get; }
|
||||
}
|
||||
|
||||
public class CodexProcessRecipe
|
||||
{
|
||||
private readonly CodexProcessConfig pc;
|
||||
private readonly CodexExePath codexExePath;
|
||||
|
||||
public CodexProcessRecipe(CodexProcessConfig pc)
|
||||
public CodexProcessRecipe(CodexProcessConfig pc, CodexExePath codexExePath)
|
||||
{
|
||||
this.pc = pc;
|
||||
this.codexExePath = codexExePath;
|
||||
}
|
||||
|
||||
public ProcessRecipe Initialize(CodexStartupConfig config)
|
||||
@ -57,11 +63,7 @@ namespace CodexPlugin
|
||||
AddArg("--log-level", config.LogLevelWithTopics());
|
||||
|
||||
// This makes the node announce itself to its local IP address.
|
||||
var host = Dns.GetHostEntry(Dns.GetHostName());
|
||||
var addrs = host.AddressList.Where(a => a.AddressFamily == AddressFamily.InterNetwork).ToList();
|
||||
var ipaddrs = addrs.First();
|
||||
|
||||
AddArg("--nat", $"extip:{ipaddrs.ToStringInvariant()}");
|
||||
AddArg("--nat", $"extip:{pc.LocalIpAddrs.ToStringInvariant()}");
|
||||
|
||||
AddArg("--listen-addrs", $"/ip4/0.0.0.0/tcp/{pc.ListenPort}");
|
||||
|
||||
@ -139,13 +141,7 @@ namespace CodexPlugin
|
||||
private ProcessRecipe Create()
|
||||
{
|
||||
return new ProcessRecipe(
|
||||
cmd: Path.Combine(
|
||||
"d:",
|
||||
"Dev",
|
||||
"nim-codex",
|
||||
"build",
|
||||
"codex.exe"
|
||||
),
|
||||
cmd: codexExePath.Get(),
|
||||
args: args.ToArray());
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user