[JENKINS-42744] - Allow ignoring the WINSW_SERVICE_ID env variable (test-only for now)

pull/202/head
Oleg Nenashev 2017-03-31 15:28:02 +02:00
parent bca9bafc66
commit 9cfdcf4ae7
3 changed files with 23 additions and 4 deletions

View File

@ -27,6 +27,12 @@ namespace winsw.Plugins.RunawayProcessKiller
/// </summary>
public bool StopParentProcessFirst { get; private set; }
/// <summary>
/// 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.
/// </summary>
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;
}
/// <summary>
@ -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.");

View File

@ -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;

View File

@ -113,6 +113,7 @@ namespace winswTests.Util
str.AppendFormat(" <pidfile>{0}</pidfile>\n", ext.Pidfile);
str.AppendFormat(" <stopTimeout>{0}</stopTimeout>\n", ext.StopTimeout.TotalMilliseconds);
str.AppendFormat(" <stopParentFirst>{0}</stopParentFirst>\n", ext.StopParentProcessFirst);
str.AppendFormat(" <checkWinSWEnvironmentVariable>{0}</checkWinSWEnvironmentVariable>\n", ext.CheckWinSWEnvironmentVariable);
str.Append( " </extension>\n");
ExtensionXmls.Add(str.ToString());