using AipGateway.API.Application.Configurations; using Microsoft.AspNetCore.Connections; using Serilog; using System.Diagnostics; using System.Threading.Channels; namespace AipGateway.AIP.Service.WindowsService { public class AipServiceMain : BackgroundService { private readonly ILogger _log; private readonly AipSettings _aipSettings; private static List _AipProcessList = new List(); private static WebApplicationBuilder? _builder; public AipServiceMain(ILogger log, AipSettings aipSettings) { _log = log; _aipSettings = aipSettings; InitializeSubProcess(); } private void InitializeSubProcess() { var currentProcessPath = Process.GetCurrentProcess().MainModule!.FileName; int defPort = _aipSettings.Port; int bindings = _aipSettings.Bindings; for (int ii = 0; ii < bindings; ii++) { int Port = defPort + (ii+1); var startInfo = new ProcessStartInfo { FileName = currentProcessPath, Arguments = $"{Port} {_aipSettings.ProcessId}", CreateNoWindow = true, UseShellExecute = false }; AipProcessInfomation aipProcessInfo = new AipProcessInfomation { ProcessName = currentProcessPath, CreateAt = DateTime.Now, Port = Port, process = Process.Start(startInfo) }; if (aipProcessInfo.process == null) { _log.LogError("Aip Process Start Failed: {0} ==> Port: {1}.", currentProcessPath, Port); } else { _log.LogInformation("Aip Process Start {0} ==> Port: {1}, PID: {2}.", currentProcessPath, Port, aipProcessInfo.process.Id); } _AipProcessList.Add(aipProcessInfo); } } private Process? StartSubProcess(int port) { var currentProcessPath = Process.GetCurrentProcess().MainModule!.FileName; var startInfo = new ProcessStartInfo { FileName = currentProcessPath, Arguments = $"{port}", CreateNoWindow = true, UseShellExecute = false }; return Process.Start(startInfo); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { stoppingToken.Register(() => _log.LogInformation("ServiceA is stopping.")); while (!stoppingToken.IsCancellationRequested) { foreach (AipProcessInfomation aipProcess in _AipProcessList) { Process? runProcess = aipProcess.process; if (runProcess == null) { _log.LogError("Aip Process Start Failed: {0} ==> Port: {1}.", aipProcess.ProcessName, aipProcess.Port); aipProcess.process = StartSubProcess(aipProcess.Port); } else { var process = Process.GetProcessById(runProcess.Id); if (process == null) { _log.LogError("Aip Process Terminated: {0} ==> Port: {1}.", aipProcess.ProcessName, aipProcess.Port); aipProcess.process = StartSubProcess(aipProcess.Port); } } } await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); } } public override void Dispose() { _log.LogError("Aip Service Main terminated."); foreach (AipProcessInfomation aipProcess in _AipProcessList) { Process? runProcess = aipProcess.process; if (runProcess != null) { var process = Process.GetProcessById(runProcess.Id); if (process != null) { _log.LogInformation("Aip Process Kill: {0}, {1}", runProcess.Id, aipProcess.Port); process.Kill(); } } } base.Dispose(); } public static void Start(AipSettings aipSettings) { //_builder = } } }