-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make .NET server stop if Tauri app exits ungracefully
- Loading branch information
1 parent
0c49f21
commit 677061e
Showing
7 changed files
with
242 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Diagnostics; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace NetPad; | ||
|
||
/// <summary> | ||
/// Used to exit this program when the parent host process that started it exits. | ||
/// </summary> | ||
public static class ParentProcessTracker | ||
{ | ||
private static bool _initialized; | ||
private static IHost? _thisHost; | ||
|
||
public static void ExitWhenParentProcessExists(int parentPid) | ||
{ | ||
if (_initialized) | ||
{ | ||
throw new InvalidOperationException("Already initialized"); | ||
} | ||
|
||
Process? parentProcess = null; | ||
|
||
try | ||
{ | ||
parentProcess = Process.GetProcessById(parentPid); | ||
parentProcess.EnableRaisingEvents = true; | ||
} | ||
catch | ||
{ | ||
// ignore | ||
} | ||
|
||
if (parentProcess != null) | ||
{ | ||
parentProcess.Exited += (_, _) => | ||
{ | ||
try | ||
{ | ||
_thisHost?.StopAsync(TimeSpan.FromSeconds(10)); | ||
} | ||
catch | ||
{ | ||
// ignore | ||
} | ||
finally | ||
{ | ||
Environment.Exit((int)ProgramExitCode.Success); | ||
} | ||
}; | ||
|
||
_initialized = true; | ||
} | ||
else | ||
{ | ||
Console.WriteLine($"Parent process with ID '{parentPid}' is not running"); | ||
Environment.Exit((int)ProgramExitCode.ParentProcessIsNotRunning); | ||
} | ||
} | ||
|
||
public static void SetThisHost(IHost host) | ||
{ | ||
if (!_initialized) | ||
{ | ||
throw new InvalidOperationException("Not initialized"); | ||
} | ||
|
||
_thisHost = host; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Linq; | ||
using NetPad.Apps.Shells; | ||
using NetPad.Apps.Shells.Electron; | ||
using NetPad.Apps.Shells.Tauri; | ||
using NetPad.Apps.Shells.Web; | ||
|
||
namespace NetPad; | ||
|
||
public class ProgramArgs | ||
{ | ||
public ProgramArgs(string[] args) | ||
{ | ||
Raw = args; | ||
|
||
RunMode = args.Contains("--swagger") ? RunMode.SwaggerGen : RunMode.Normal; | ||
|
||
var parentPidArg = Array.IndexOf(args, "--parent-pid"); | ||
if (parentPidArg >= 0 && parentPidArg + 1 < args.Length) | ||
{ | ||
if (int.TryParse(args[parentPidArg + 1], out var parentPid)) | ||
{ | ||
ParentPid = parentPid; | ||
} | ||
else | ||
{ | ||
Console.WriteLine($"Invalid parent pid: {args[parentPidArg + 1]}"); | ||
Environment.Exit((int)ProgramExitCode.InvalidParentProcessPid); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The raw args passed to the program. | ||
/// </summary> | ||
public string[] Raw { get; } | ||
|
||
/// <summary> | ||
/// The pid of the process that started this program. | ||
/// </summary> | ||
public int? ParentPid { get; } | ||
|
||
/// <summary> | ||
/// The mode to run the program in. | ||
/// </summary> | ||
public RunMode RunMode { get; } | ||
|
||
public IShell CreateShell() | ||
{ | ||
if (Raw.Any(a => a.ContainsIgnoreCase("/ELECTRONPORT"))) | ||
{ | ||
return new ElectronShell(); | ||
} | ||
|
||
if (Raw.Any(a => a.EqualsIgnoreCase("--tauri"))) | ||
{ | ||
return new TauriShell(); | ||
} | ||
|
||
return new WebBrowserShell(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace NetPad; | ||
|
||
public enum ProgramExitCode | ||
{ | ||
Success = 0, | ||
UnexpectedError = 1, | ||
SwaggerGenError = 2, | ||
PortUnavailable = 3, | ||
InvalidParentProcessPid = 4, | ||
ParentProcessIsNotRunning = 5, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace NetPad; | ||
|
||
public enum RunMode | ||
{ | ||
Normal, | ||
SwaggerGen | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.