Skip to content

Commit

Permalink
Kill redundant processes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ffMathy committed Feb 22, 2023
1 parent b20becb commit cfb00c0
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/FluffySpoon.Ngrok/NgrokProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ public void Start()
{
var processInformation = GetProcessStartInfo();

var existingProcess = Process.GetProcessesByName(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
"Ngrok" :
"ngrok");
if (existingProcess.Any())
var existingProcesses = Process
.GetProcessesByName(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Ngrok" : "ngrok")
.ToArray();
if (existingProcesses.Any())
{
_logger.LogDebug("Ngrok process ({ProcessName}) is already running", processInformation.FileName);
SetProcess(existingProcess.First());
SetProcess(existingProcesses.First());

foreach (var existingProcess in existingProcesses.Skip(1))
{
existingProcess.Kill();
}

return;
}

Expand All @@ -55,7 +61,7 @@ private void ProcessErrorDataReceived(object? sender, DataReceivedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Data))
return;

_logger.LogError("{Error}", e.Data);
}

Expand All @@ -67,7 +73,7 @@ private ProcessWindowStyle GetProcessWindowStyle()
private ProcessStartInfo GetProcessStartInfo()
{
var processStartInfo = new ProcessStartInfo(
NgrokDownloader.GetExecutableFileName(),
NgrokDownloader.GetExecutableFileName(),
"start --none")
{
CreateNoWindow = true,
Expand All @@ -82,14 +88,14 @@ public void Stop()
{
_logger.LogInformation("Stopping ngrok process");

if (_process == null)
if (_process == null)
return;

_process.ErrorDataReceived -= ProcessErrorDataReceived;

_process.Kill();
_process.WaitForExit();

_process = null;
}
}

0 comments on commit cfb00c0

Please sign in to comment.