Skip to content

Commit

Permalink
Handle orphaned dashboard instances
Browse files Browse the repository at this point in the history
  • Loading branch information
SaifAqqad committed Jul 6, 2024
1 parent 8ab969f commit 8d91186
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/AspireRunner.AspNetCore/AspireRunner.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.1.5</Version>
<Version>1.1.6</Version>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>

Expand Down
64 changes: 46 additions & 18 deletions src/AspireRunner.Core/AspireDashboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,27 @@ public void Start()

using (_instanceLock.Acquire(timeout: TimeSpan.FromSeconds(InstanceLockTimeout)))
{
switch (Options.Runner.SingleInstanceHandling)
var instance = TryGetRunningInstance();
if (!IsProcessRunning(instance.Runner) && IsProcessRunning(instance.Dashboard))
{
case SingleInstanceHandling.ReplaceExisting:
{
TryGetRunningInstance()?.Kill(true);
break;
}
case SingleInstanceHandling.WarnAndExit:
// orphaned instance, kill it
instance.Dashboard!.Kill(true);
}

if (IsProcessRunning(instance.Dashboard))
{
switch (Options.Runner.SingleInstanceHandling)
{
var runningInstance = TryGetRunningInstance();
if (runningInstance != null)
case SingleInstanceHandling.ReplaceExisting:
{
_logger.LogWarning("Another instance of the Aspire Dashboard is already running, Process Id = {PID}", runningInstance.Id);
instance.Dashboard?.Kill(true);
break;
}
case SingleInstanceHandling.WarnAndExit:
{
_logger.LogWarning("Another instance of the Aspire Dashboard is already running, Process Id = {PID}", instance.Dashboard!.Id);
return;
}

break;
}
}

Expand Down Expand Up @@ -233,24 +237,48 @@ private Task LaunchBrowserAsync(string url)

private void PersistInstance()
{
File.WriteAllText(Path.Combine(_runnerFolder, InstanceFile), _dashboardProcess!.Id.ToString());
var instanceFilePath = Path.Combine(_runnerFolder, InstanceFile);
File.WriteAllText(instanceFilePath, $"{_dashboardProcess!.Id}:{Environment.ProcessId}");
}

private Process? TryGetRunningInstance()
private (Process? Dashboard, Process? Runner) TryGetRunningInstance()
{
var instanceFile = Path.Combine(_runnerFolder, InstanceFile);
if (!File.Exists(instanceFile) || !int.TryParse(File.ReadAllText(instanceFile), out var pid))
var instanceFilePath = Path.Combine(_runnerFolder, InstanceFile);
if (!File.Exists(instanceFilePath))
{
return null;
return default;
}

var instanceInfo = File.ReadAllText(instanceFilePath);
if (string.IsNullOrWhiteSpace(instanceInfo))
{
return default;
}

var pids = instanceInfo.Split(':', 2);
_ = int.TryParse(pids[0], out var dashboardPid);
_ = int.TryParse(pids.ElementAtOrDefault(1), out var runnerPid);

var runner = runnerPid > 0 && TryGetProcess(runnerPid) is { } rp ? rp : null;
var dashboard = dashboardPid > 0 && TryGetProcess(dashboardPid) is { ProcessName: "dotnet" } dp ? dp : null;

return (dashboard, runner);
}

private static Process? TryGetProcess(int pid)
{
try
{
return Process.GetProcessById(pid) is { ProcessName: "dotnet" } p ? p : null;
return Process.GetProcessById(pid);
}
catch
{
return null;
}
}

private static bool IsProcessRunning(Process? process)
{
return process?.HasExited is false;
}
}
2 changes: 1 addition & 1 deletion src/AspireRunner.Core/AspireDashboardManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task<AspireDashboard> GetDashboardAsync(AspireDashboardOptions opti
throw new ApplicationException("The Aspire Dashboard is not installed");
}

_logger.LogWarning("The Aspire Dashboard is not installed, download1ing the latest compatible version...");
_logger.LogWarning("The Aspire Dashboard is not installed, downloading the latest compatible version...");
var latestVersion = await FetchLatestVersionAsync(installedRuntimes, preferredVersion);

var downloadedVersion = await InstallAsync(installedRuntimes, latestVersion);
Expand Down
2 changes: 1 addition & 1 deletion src/AspireRunner.Core/AspireRunner.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.1.5</Version>
<Version>1.1.6</Version>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RuntimeIdentifiers>win;osx;linux</RuntimeIdentifiers>
<langversion>latest</langversion>
Expand Down
2 changes: 1 addition & 1 deletion src/AspireRunner.Tool/AspireRunner.Tool.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.1.5</Version>
<Version>1.1.6</Version>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RuntimeIdentifiers>win;osx;linux</RuntimeIdentifiers>
Expand Down

0 comments on commit 8d91186

Please sign in to comment.