Add functionality to shutdown Kusto process when parent process exits (#1609)

This commit is contained in:
Cory Rivera
2022-07-29 15:26:26 -07:00
committed by GitHub
parent 11dd29d8a0
commit 40df024dbc
7 changed files with 67 additions and 31 deletions

View File

@@ -23,7 +23,7 @@
<When Condition="'$(BUILD_DOTNET_TOOL)' == 'true'">
<PropertyGroup>
<PackageId>Microsoft.SqlServer.SqlToolsServiceLayer.Tool</PackageId>
<PackageVersion>1.0.0</PackageVersion>
<PackageVersion>1.1.0</PackageVersion>
<PackageDescription>.NET client SQL Tools Service application, usable as a dotnet tool. This package is intended to be used by internal applications only and should not be referenced directly.</PackageDescription>
<PackAsTool>true</PackAsTool>
<ToolCommandName>$(AssemblyName)</ToolCommandName>

View File

@@ -9,7 +9,6 @@ using Microsoft.SqlTools.ServiceLayer.SqlContext;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.Utility;
using System.Diagnostics;
using System.Threading;
namespace Microsoft.SqlTools.ServiceLayer
{
@@ -57,9 +56,7 @@ namespace Microsoft.SqlTools.ServiceLayer
// 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();
ProcessExitTimer.Start(commandOptions.ParentProcessId.Value);
}
serviceHost.WaitForExit();
@@ -83,20 +80,5 @@ namespace Microsoft.SqlTools.ServiceLayer
sqlClientListener?.Dispose();
}
}
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

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