Making Ctrl+C the default behaviour

also making the timeout customizable, plus documentation
pull/37/merge
Kohsuke Kawaguchi 2014-01-08 21:27:53 -08:00
parent 1ed045b817
commit ae1bc43cff
4 changed files with 33 additions and 34 deletions

36
Main.cs
View File

@ -337,32 +337,24 @@ namespace winsw
StopProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
}
try
var proc = Process.GetProcessById(pid);
WriteEvent("Send SIGINT " + process.Id);
bool successful = SigIntHelper.SendSIGINTToProcess(proc,descriptor.StopTimeout);
if (successful)
{
var proc = Process.GetProcessById(pid);
if (descriptor.SendSIGINT)
WriteEvent("SIGINT to" + process.Id + " successful");
}
else
{
try
{
WriteEvent("Send SIGINT " + process.Id);
bool successful = SigIntHelper.SendSIGINTToProcess(proc);
if (successful)
{
WriteEvent("SIGINT to" + process.Id + " successful");
}
else
{
WriteEvent("SIGINT to " + process.Id + " failed - Killing as fallback");
proc.Kill();
}
}
else
{
WriteEvent("ProcessKill " + process.Id);
WriteEvent("SIGINT to " + process.Id + " failed - Killing as fallback");
proc.Kill();
}
}
catch (ArgumentException)
{
// Process already exited.
catch (ArgumentException)
{
// Process already exited.
}
}
}

View File

@ -185,6 +185,13 @@ When you use the `<stopargument>`, you must use `<startargument>` instead of `<a
<stopexecutable>catalina.sh</stopexecutable>
<stopargument>stop</stopargument>
### stoptimeout
When the service is requested to stop, winsw first attempts to <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms683155(v=vs.85).aspx">send Ctrl+C signal to the process</a>, then wait for up to 15 seconds for the process to exit by itself gracefully. A process failing to do that (or if the process does not have a console), then winsw resorts to calling <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686714(v=vs.85).aspx">TerminateProcess</a> API to kill the service instantly.
This optional element allows you to change this "15 seconds" value, so that you can control how long winsw gives the service to shut itself down. See `<onfailure>` below for how to specify time duration:
<stoptimeout>10sec</stoptimeout>
### env
This optional element can be specified multiple times if necessary to specify environment variables to be set for the child process. The syntax is:
@ -253,4 +260,4 @@ It is possible to specify the useraccount (and password) that the service will r
### Working directory
Some services need to run with a working directory specified. To do this, specify a `<workingdirectory>` element like this:
<workingdirectory>C:\application</workingdirectory>
<workingdirectory>C:\application</workingdirectory>

View File

@ -562,14 +562,14 @@ namespace winsw
}
/// <summary>
/// True if the service can interact with the desktop.
/// Time to wait for the service to gracefully shutdown before we forcibly kill it
/// </summary>
public bool SendSIGINT
{
get
{
return dom.SelectSingleNode("//sendsigint") != null;
}
}
public TimeSpan StopTimeout
{
get
{
return SingleTimeSpanElement(dom, "stoptimeout", TimeSpan.FromSeconds(15));
}
}
}
}

View File

@ -41,7 +41,7 @@ namespace winsw
/// </summary>
/// <param name="process">The process to attach to and send the SIGINT</param>
/// <returns>True if the process shut down successfully to the SIGINT, false if it did not.</returns>
public static bool SendSIGINTToProcess(Process process)
public static bool SendSIGINTToProcess(Process process, TimeSpan shutdownTimeout)
{
if (AttachConsole((uint)process.Id))
{
@ -49,7 +49,7 @@ namespace winsw
SetConsoleCtrlHandler(null, true);
GenerateConsoleCtrlEvent(CtrlTypes.CTRL_C_EVENT, 0);
process.WaitForExit(15000);
process.WaitForExit(shutdownTimeout.TotalMilliseconds);
return process.HasExited;
}
@ -59,4 +59,4 @@ namespace winsw
}
}
}
}
}