From 9cfdcf4ae7256f168689158f0078ad176bc83fb2 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Fri, 31 Mar 2017 15:28:02 +0200 Subject: [PATCH] [JENKINS-42744] - Allow ignoring the WINSW_SERVICE_ID env variable (test-only for now) --- .../RunawayProcessKillerExtension.cs | 21 ++++++++++++++++--- .../Extensions/RunawayProcessKillerTest.cs | 5 ++++- src/Test/winswTests/Util/ConfigXmlBuilder.cs | 1 + 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Plugins/RunawayProcessKiller/RunawayProcessKillerExtension.cs b/src/Plugins/RunawayProcessKiller/RunawayProcessKillerExtension.cs index 562b17a..94abf59 100644 --- a/src/Plugins/RunawayProcessKiller/RunawayProcessKillerExtension.cs +++ b/src/Plugins/RunawayProcessKiller/RunawayProcessKillerExtension.cs @@ -27,6 +27,12 @@ namespace winsw.Plugins.RunawayProcessKiller /// public bool StopParentProcessFirst { get; private set; } + /// + /// If true, the runaway process will be checked for the WinSW environment variable before termination. + /// This option is not documented AND not supposed to be used by users. + /// + public bool CheckWinSWEnvironmentVariable { get; private set; } + public override String DisplayName { get { return "Runaway Process Killer"; } } private String ServiceId { get; set; } @@ -38,11 +44,12 @@ namespace winsw.Plugins.RunawayProcessKiller // Default initializer } - public RunawayProcessKillerExtension(String pidfile, int stopTimeoutMs = 5000, bool stopParentFirst = false) + public RunawayProcessKillerExtension(String pidfile, int stopTimeoutMs = 5000, bool stopParentFirst = false, bool checkWinSWEnvironmentVariable = true) { this.Pidfile = pidfile; this.StopTimeout = TimeSpan.FromMilliseconds(5000); this.StopParentProcessFirst = stopParentFirst; + this.CheckWinSWEnvironmentVariable = checkWinSWEnvironmentVariable; } public override void Configure(ServiceDescriptor descriptor, XmlNode node) @@ -53,6 +60,9 @@ namespace winsw.Plugins.RunawayProcessKiller StopTimeout = TimeSpan.FromMilliseconds(Int32.Parse(XmlHelper.SingleElement(node, "stopTimeout", false))); StopParentProcessFirst = Boolean.Parse(XmlHelper.SingleElement(node, "stopParentFirst", false)); ServiceId = descriptor.Id; + //TODO: Consider making it documented + var checkWinSWEnvironmentVariable = XmlHelper.SingleElement(node, "checkWinSWEnvironmentVariable", true); + CheckWinSWEnvironmentVariable = checkWinSWEnvironmentVariable != null ? Boolean.Parse(checkWinSWEnvironmentVariable) : true; } /// @@ -113,7 +123,7 @@ namespace winsw.Plugins.RunawayProcessKiller { affiliatedServiceId = previousProcessEnvVars[expectedEnvVarName]; } - else + else if (CheckWinSWEnvironmentVariable) { Logger.Warn("The process " + pid + " has no " + expectedEnvVarName + " environment variable defined. " + "The process has not been started by WinSW, hence it won't be terminated."); @@ -125,9 +135,14 @@ namespace winsw.Plugins.RunawayProcessKiller } return; } + else + { + // We just skip this check + affiliatedServiceId = null; + } // Check the service ID value - if (!ServiceId.Equals(affiliatedServiceId)) + if (CheckWinSWEnvironmentVariable && !ServiceId.Equals(affiliatedServiceId)) { Logger.Warn("The process " + pid + " has been started by Windows service with ID='" + affiliatedServiceId + "'. " + "It is another service (current service id is '" + ServiceId + "'), hence the process won't be terminated."); diff --git a/src/Test/winswTests/Extensions/RunawayProcessKillerTest.cs b/src/Test/winswTests/Extensions/RunawayProcessKillerTest.cs index efa56cc..31f950a 100644 --- a/src/Test/winswTests/Extensions/RunawayProcessKillerTest.cs +++ b/src/Test/winswTests/Extensions/RunawayProcessKillerTest.cs @@ -89,8 +89,11 @@ namespace winswTests.Extensions try { // Generate extension and ensure that the roundtrip is correct + //TODO: checkWinSWEnvironmentVariable should be true, but it does not work due to proc.StartInfo.EnvironmentVariables var pidfile = Path.Combine(tmpDir, "process.pid"); - var sd = ConfigXmlBuilder.create(id: winswId).WithRunawayProcessKiller(new RunawayProcessKillerExtension(pidfile), extensionId).ToServiceDescriptor(); + var sd = ConfigXmlBuilder.create(id: winswId) + .WithRunawayProcessKiller(new RunawayProcessKillerExtension(pidfile, checkWinSWEnvironmentVariable: false), extensionId) + .ToServiceDescriptor(); WinSWExtensionManager manager = new WinSWExtensionManager(sd); manager.LoadExtensions(); var extension = manager.Extensions[extensionId] as RunawayProcessKillerExtension; diff --git a/src/Test/winswTests/Util/ConfigXmlBuilder.cs b/src/Test/winswTests/Util/ConfigXmlBuilder.cs index 9416a29..9fc75f4 100644 --- a/src/Test/winswTests/Util/ConfigXmlBuilder.cs +++ b/src/Test/winswTests/Util/ConfigXmlBuilder.cs @@ -113,6 +113,7 @@ namespace winswTests.Util str.AppendFormat(" {0}\n", ext.Pidfile); str.AppendFormat(" {0}\n", ext.StopTimeout.TotalMilliseconds); str.AppendFormat(" {0}\n", ext.StopParentProcessFirst); + str.AppendFormat(" {0}\n", ext.CheckWinSWEnvironmentVariable); str.Append( " \n"); ExtensionXmls.Add(str.ToString());