Add a parent process ID argument to ServiceLayer so that we can exit gracefully if the parent process exits. (#1103)

This commit is contained in:
Cory Rivera
2020-10-29 13:00:23 -07:00
committed by GitHub
parent 563593eba7
commit 8c805a72a9
2 changed files with 34 additions and 2 deletions

View File

@@ -6,8 +6,8 @@ using Microsoft.SqlTools.ServiceLayer.Hosting;
using Microsoft.SqlTools.ServiceLayer.SqlContext; using Microsoft.SqlTools.ServiceLayer.SqlContext;
using Microsoft.SqlTools.ServiceLayer.Utility; using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.Utility; using Microsoft.SqlTools.Utility;
using System.IO;
using System.Diagnostics; using System.Diagnostics;
using System.Threading;
namespace Microsoft.SqlTools.ServiceLayer namespace Microsoft.SqlTools.ServiceLayer
{ {
@@ -46,6 +46,14 @@ namespace Microsoft.SqlTools.ServiceLayer
SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails); SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails);
ServiceHost serviceHost = HostLoader.CreateAndStartServiceHost(sqlToolsContext); ServiceHost serviceHost = HostLoader.CreateAndStartServiceHost(sqlToolsContext);
// If this service was started by another process, then it should shutdown when that parent process does.
if (commandOptions.ParentProcessId != null)
{
var parentProcess = Process.GetProcessById(commandOptions.ParentProcessId.Value);
var statusThread = new Thread(() => CheckParentStatusLoop(parentProcess));
statusThread.Start();
}
serviceHost.WaitForExit(); serviceHost.WaitForExit();
} }
catch (Exception ex) catch (Exception ex)
@@ -66,5 +74,20 @@ namespace Microsoft.SqlTools.ServiceLayer
Logger.Close(); Logger.Close();
} }
} }
private static void CheckParentStatusLoop(Process parent)
{
Logger.Write(TraceEventType.Information, $"Starting thread to check status of parent process. Parent PID: {parent.Id}");
while (true)
{
if (parent.HasExited)
{
var processName = Process.GetCurrentProcess().ProcessName;
Logger.Write(TraceEventType.Information, $"Terminating {processName} process because parent process has exited. Parent PID: {parent.Id}");
Environment.Exit(0);
}
Thread.Sleep(5000);
}
}
} }
} }

View File

@@ -13,7 +13,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
{ {
internal const string ServiceLayerServiceName = "MicrosoftSqlToolsServiceLayer.exe"; internal const string ServiceLayerServiceName = "MicrosoftSqlToolsServiceLayer.exe";
private static readonly string[] serviceLayerCommandArgs = { "-d", "--developers" }; private static readonly string[] serviceLayerCommandArgs = { "-d", "--developers", "--parent-pid" };
/** /**
* List of contributors to this project, used as part of the onboarding process. * List of contributors to this project, used as part of the onboarding process.
@@ -23,6 +23,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
"Charles-Gagnon" "Charles-Gagnon"
}; };
public int? ParentProcessId { get; private set; }
public ServiceLayerCommandOptions(string[] args) : base(args.Where(arg => !serviceLayerCommandArgs.Contains(arg)).ToArray(), ServiceLayerServiceName) public ServiceLayerCommandOptions(string[] args) : base(args.Where(arg => !serviceLayerCommandArgs.Contains(arg)).ToArray(), ServiceLayerServiceName)
{ {
for (int i = 0; i < args.Length; ++i) for (int i = 0; i < args.Length; ++i)
@@ -41,6 +43,13 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
Console.WriteLine(string.Join(Environment.NewLine, contributors.Select(contributor => $"\t{contributor}"))); Console.WriteLine(string.Join(Environment.NewLine, contributors.Select(contributor => $"\t{contributor}")));
this.ShouldExit = true; this.ShouldExit = true;
break; break;
case "--parent-pid":
string nextArg = args[++i];
if (Int32.TryParse(nextArg, out int parsedInt))
{
ParentProcessId = parsedInt;
}
break;
} }
} }
} }