Skip to content

Commit

Permalink
Improved process.
Browse files Browse the repository at this point in the history
  • Loading branch information
ffMathy committed Feb 24, 2023
1 parent 7a46d31 commit 48b34a8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 70 deletions.
19 changes: 2 additions & 17 deletions src/FluffySpoon.Ngrok.AspNet/NgrokHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class NgrokHostedService : INgrokHostedService
private readonly IHostApplicationLifetime _lifetime;
private readonly INgrokService _service;
private readonly ILogger<NgrokHostedService> _logger;

private Task? _stopTask;

public NgrokHostedService(
IServer server,
Expand Down Expand Up @@ -48,24 +46,11 @@ public async Task StartAsync(CancellationToken cancellationToken)
.First();
_service.StartAsync(address, combinedCancellationToken);
});

_lifetime.ApplicationStopped.Register(() =>
{
_logger.LogDebug("Application has stopped - will stop Ngrok");
StopProcessAsync(combinedCancellationToken);
});
}

private Task StopProcessAsync(CancellationToken combinedCancellationToken)
{
if (_stopTask != null)
return _stopTask;

return _stopTask = _service.StopAsync(combinedCancellationToken);
}

public Task StopAsync(CancellationToken cancellationToken)
{
return StopProcessAsync(cancellationToken);
_logger.LogDebug("Application has stopped - will stop Ngrok");
return _service.StopAsync(cancellationToken);
}
}
4 changes: 2 additions & 2 deletions src/FluffySpoon.Ngrok/INgrokProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public interface INgrokProcess
{
void Start();
void Stop();
Task StartAsync();
Task StopAsync();
}
80 changes: 31 additions & 49 deletions src/FluffySpoon.Ngrok/NgrokProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public class NgrokProcess : INgrokProcess
private readonly NgrokOptions _options;
private readonly ILogger<NgrokProcess> _logger;

private Process? _process;

public NgrokProcess(
NgrokOptions options,
ILogger<NgrokProcess> logger)
Expand All @@ -20,54 +18,48 @@ public NgrokProcess(
_logger = logger;
}

public void Start()
public async Task StartAsync()
{
var processInformation = GetProcessStartInfo();

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(existingProcesses.First());

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

return;
}
await KillExistingProcessesAsync();

_logger.LogInformation("Starting Ngrok process");

var process = Process.Start(processInformation);
SetProcess(process);
var processInformation = GetProcessStartInfo();
using var process =
Process.Start(processInformation) ??
throw new InvalidOperationException("Could not start process");
}

private void SetProcess(Process? process)
private ProcessWindowStyle GetProcessWindowStyle()
{
if (process == null)
return;

process.EnableRaisingEvents = true;
process.ErrorDataReceived += ProcessErrorDataReceived;

_process = process;
return _options.ShowNgrokWindow ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;
}

private void ProcessErrorDataReceived(object? sender, DataReceivedEventArgs e)
private async Task KillExistingProcessesAsync()
{
if (string.IsNullOrWhiteSpace(e.Data))
var existingProcesses = Process
.GetProcessesByName(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Ngrok" : "ngrok")
.ToArray();
if (!existingProcesses.Any())
return;

_logger.LogError("{Error}", e.Data);
}
try
{
_logger.LogDebug("Killing existing ngrok processes");

private ProcessWindowStyle GetProcessWindowStyle()
{
return _options.ShowNgrokWindow ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;
foreach (var existingProcess in existingProcesses)
{
existingProcess.Kill();
await existingProcess.WaitForExitAsync();
}
}
finally
{
foreach (var existingProcess in existingProcesses)
{
existingProcess.Dispose();
}
}
}

private ProcessStartInfo GetProcessStartInfo()
Expand All @@ -84,18 +76,8 @@ private ProcessStartInfo GetProcessStartInfo()
return processStartInfo;
}

public void Stop()
public async Task StopAsync()
{
_logger.LogInformation("Stopping ngrok process");

if (_process == null)
return;

_process.ErrorDataReceived -= ProcessErrorDataReceived;

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

_process = null;
await KillExistingProcessesAsync();
}
}
4 changes: 2 additions & 2 deletions src/FluffySpoon.Ngrok/NgrokService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task InitializeAsync(CancellationToken cancellationToken = default)
_isInitialized = true;

await _downloader.DownloadExecutableAsync(cancellationToken);
_process.Start();
await _process.StartAsync();
}

public async Task<TunnelResponse> StartAsync(
Expand Down Expand Up @@ -96,8 +96,8 @@ public async Task StopAsync(CancellationToken cancellationToken)
var hooks = _hooks.ToArray();
var activeTunnels = _activeTunnels.ToArray();

_process.Stop();
_activeTunnels.Clear();
await _process.StopAsync();

await Task.WhenAll(activeTunnels
.Select(tunnel => Task.WhenAll(hooks
Expand Down

0 comments on commit 48b34a8

Please sign in to comment.