|
| 1 | +using System.Diagnostics; |
| 2 | + |
| 3 | +namespace PulseTestClient; |
| 4 | + |
| 5 | +public static class ProcessOrchestrator |
| 6 | +{ |
| 7 | + public static async Task<int> RunAsync(ClientOptions options, int botsPerProcess, CancellationToken ct) |
| 8 | + { |
| 9 | + int totalBots = options.BotCount; |
| 10 | + int processCount = (totalBots + botsPerProcess - 1) / botsPerProcess; |
| 11 | + |
| 12 | + Console.WriteLine($"Spawning {totalBots} bots across {processCount} processes ({botsPerProcess} per process).."); |
| 13 | + |
| 14 | + // Account creation must be sequential across all bots |
| 15 | + for (var i = 0; i < totalBots; i++) |
| 16 | + { |
| 17 | + var accountName = $"{options.AccountPrefix}-{i}"; |
| 18 | + Console.WriteLine($"[{accountName}] Ensuring account exists.."); |
| 19 | + await MetaForge.RunCommandAsync($"account create {accountName} --skip-update-check --skip-auto-login", ct); |
| 20 | + } |
| 21 | + |
| 22 | + var processes = new List<Process>(); |
| 23 | + |
| 24 | + for (var p = 0; p < processCount; p++) |
| 25 | + { |
| 26 | + int offset = p * botsPerProcess; |
| 27 | + int count = Math.Min(botsPerProcess, totalBots - offset); |
| 28 | + |
| 29 | + string childArgs = BuildChildArgs(options, offset, count, totalBots); |
| 30 | + |
| 31 | + var process = new Process |
| 32 | + { |
| 33 | + StartInfo = new ProcessStartInfo |
| 34 | + { |
| 35 | + FileName = Environment.ProcessPath!, |
| 36 | + Arguments = childArgs, |
| 37 | + UseShellExecute = false, |
| 38 | + RedirectStandardOutput = true, |
| 39 | + RedirectStandardError = true, |
| 40 | + }, |
| 41 | + }; |
| 42 | + |
| 43 | + process.OutputDataReceived += (_, e) => |
| 44 | + { |
| 45 | + if (e.Data != null) Console.WriteLine(e.Data); |
| 46 | + }; |
| 47 | + |
| 48 | + process.ErrorDataReceived += (_, e) => |
| 49 | + { |
| 50 | + if (e.Data != null) Console.Error.WriteLine(e.Data); |
| 51 | + }; |
| 52 | + |
| 53 | + process.Start(); |
| 54 | + process.BeginOutputReadLine(); |
| 55 | + process.BeginErrorReadLine(); |
| 56 | + processes.Add(process); |
| 57 | + |
| 58 | + Console.WriteLine($"[orchestrator] Process {p} started (PID {process.Id}): bots {offset}..{offset + count - 1}"); |
| 59 | + } |
| 60 | + |
| 61 | + Console.WriteLine($"[orchestrator] All {processCount} processes running. Press q+Enter or Ctrl+C to stop."); |
| 62 | + |
| 63 | + // Watch for quit |
| 64 | + _ = Task.Run(() => |
| 65 | + { |
| 66 | + while (!ct.IsCancellationRequested) |
| 67 | + { |
| 68 | + string? line = Console.ReadLine(); |
| 69 | + |
| 70 | + if (line is "q" or "Q" or "quit") |
| 71 | + { |
| 72 | + Console.WriteLine("[orchestrator] Quit requested, stopping child processes.."); |
| 73 | + SignalStop(); |
| 74 | + } |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + // When parent is cancelled, signal children |
| 79 | + ct.Register(SignalStop); |
| 80 | + |
| 81 | + // Wait for all children |
| 82 | + Task[] tasks = processes.Select(p => p.WaitForExitAsync(CancellationToken.None)).ToArray(); |
| 83 | + await Task.WhenAll(tasks); |
| 84 | + |
| 85 | + // Clean up the stop file after all children have exited |
| 86 | + CleanupStopFile(); |
| 87 | + |
| 88 | + int failed = processes.Count(p => p.ExitCode != 0); |
| 89 | + |
| 90 | + if (failed > 0) |
| 91 | + Console.WriteLine($"[orchestrator] {failed}/{processCount} processes exited with errors."); |
| 92 | + else |
| 93 | + Console.WriteLine($"[orchestrator] All {processCount} processes exited cleanly."); |
| 94 | + |
| 95 | + return failed > 0 ? 1 : 0; |
| 96 | + } |
| 97 | + |
| 98 | + private static string BuildChildArgs(ClientOptions options, int offset, int count, int totalBots) |
| 99 | + { |
| 100 | + var parts = new List<string> |
| 101 | + { |
| 102 | + $"--account={options.AccountPrefix}", |
| 103 | + $"--bot-count={count}", |
| 104 | + $"--bot-offset={offset}", |
| 105 | + $"--total-bot-count={totalBots}", |
| 106 | + $"--ip={options.ServerIp}", |
| 107 | + $"--port={options.ServerPort}", |
| 108 | + $"--pos-x={options.PositionX}", |
| 109 | + $"--pos-y={options.PositionY}", |
| 110 | + $"--pos-z={options.PositionZ}", |
| 111 | + $"--spawn-radius={options.SpawnRadius}", |
| 112 | + $"--dispersion-radius={options.DispersionRadius}", |
| 113 | + $"--rotate-speed={options.RotateSpeed}", |
| 114 | + }; |
| 115 | + |
| 116 | + return string.Join(' ', parts); |
| 117 | + } |
| 118 | + |
| 119 | + private static void SignalStop() |
| 120 | + { |
| 121 | + string stopFile = Path.Combine(Path.GetTempPath(), "dcl-pulse-test-client.stop"); |
| 122 | + |
| 123 | + try { File.WriteAllText(stopFile, ""); } |
| 124 | + catch |
| 125 | + { /* best effort */ |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static void CleanupStopFile() |
| 130 | + { |
| 131 | + string stopFile = Path.Combine(Path.GetTempPath(), "dcl-pulse-test-client.stop"); |
| 132 | + |
| 133 | + try { File.Delete(stopFile); } |
| 134 | + catch |
| 135 | + { /* best effort */ |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments