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

@@ -66,6 +66,13 @@ namespace Microsoft.SqlTools.Hosting.Utility
case "-parallel-message-processing":
ParallelMessageProcessing = true;
break;
case "-parent-pid":
string nextArg = args[++i];
if (Int32.TryParse(nextArg, out int parsedInt))
{
ParentProcessId = parsedInt;
}
break;
default:
ErrorMessage += string.Format("Unknown argument \"{0}\"" + Environment.NewLine, argName);
break;
@@ -140,6 +147,12 @@ namespace Microsoft.SqlTools.Hosting.Utility
/// </summary>
public bool ParallelMessageProcessing { get; private set; } = false;
/// <summary>
/// The ID of the process that started this service. This is used to check when the parent
/// process exits so that the service process can exit at the same time.
/// </summary>
public int? ParentProcessId { get; private set; }
public virtual void SetLocale(string locale)
{
try

View File

@@ -0,0 +1,44 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Diagnostics;
using System.Threading;
namespace Microsoft.SqlTools.Utility
{
public static class ProcessExitTimer
{
/// <summary>
/// Starts a thread that checks if the provided parent process has exited each time the provided interval has elapsed.
/// Once the parent process has exited the process that started the timer also exits.
/// </summary>
/// <param name="parentProcessId">The ID of the parent process to monitor.</param>
/// <param name="intervalMs">The time interval in milliseconds for when to poll the parent process.</param>
/// <returns>The ID of the thread running the timer.</returns>
public static int Start(int parentProcessId, int intervalMs = 10000)
{
var statusThread = new Thread(() => CheckParentStatusLoop(parentProcessId, intervalMs));
statusThread.Start();
return statusThread.ManagedThreadId;
}
private static void CheckParentStatusLoop(int parentProcessId, int intervalMs)
{
var parent = Process.GetProcessById(parentProcessId);
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(intervalMs);
}
}
}
}