shutdown support

git-svn-id: https://svn.kenai.com/svn/winsw~subversion/trunk@24 c8b2a3fe-9b5b-6a51-a37e-dc31b0e308fa
remotes/git-svn
jjasper4 2009-01-14 21:40:30 +00:00
parent 8fc1277776
commit 7f8ceb3f45
2 changed files with 708 additions and 653 deletions

75
Main.cs
View File

@ -305,14 +305,17 @@ namespace winsw
/// so don't try to kill us when the child exits. /// so don't try to kill us when the child exits.
/// </summary> /// </summary>
private bool orderlyShutdown; private bool orderlyShutdown;
private bool systemShuttingdown;
public WrapperService() public WrapperService()
{ {
this.descriptor = new ServiceDescriptor(); this.descriptor = new ServiceDescriptor();
this.ServiceName = descriptor.Id; this.ServiceName = descriptor.Id;
this.CanShutdown = true;
this.CanStop = true; this.CanStop = true;
this.CanPauseAndContinue = false; this.CanPauseAndContinue = false;
this.AutoLog = true; this.AutoLog = true;
this.systemShuttingdown = false;
} }
/// <summary> /// <summary>
@ -349,11 +352,11 @@ namespace winsw
string line; string line;
while ((line = tr.ReadLine()) != null) while ((line = tr.ReadLine()) != null)
{ {
EventLog.WriteEntry("Handling copy: " + line); LogEvent("Handling copy: " + line);
string[] tokens = line.Split('>'); string[] tokens = line.Split('>');
if (tokens.Length > 2) if (tokens.Length > 2)
{ {
EventLog.WriteEntry("Too many delimiters in " + line); LogEvent("Too many delimiters in " + line);
continue; continue;
} }
@ -377,7 +380,7 @@ namespace winsw
} }
catch (IOException e) catch (IOException e)
{ {
EventLog.WriteEntry("Failed to copy :" + sourceFileName + " to " + destFileName + " because " + e.Message); LogEvent("Failed to copy :" + sourceFileName + " to " + destFileName + " because " + e.Message);
} }
} }
@ -413,12 +416,46 @@ namespace winsw
new Thread(delegate() { CopyStream(process.StandardError, new StreamWriter(new FileStream(errorLogfilename, fileMode))); }).Start(); new Thread(delegate() { CopyStream(process.StandardError, new StreamWriter(new FileStream(errorLogfilename, fileMode))); }).Start();
} }
private void LogEvent(String message)
{
if (systemShuttingdown)
{
/* NOP - cannot call EventLog because of shutdown. */
}
else
{
EventLog.WriteEntry(message);
}
}
private void LogEvent(String message, EventLogEntryType type)
{
if (systemShuttingdown)
{
/* NOP - cannot call EventLog because of shutdown. */
}
else
{
EventLog.WriteEntry(message, type);
}
}
private void WriteEvent(String message)
{
string logfilename = Path.Combine(descriptor.LogDirectory, descriptor.BaseName + ".log");
StreamWriter log = new StreamWriter(logfilename, true);
log.WriteLine(message);
log.Flush();
log.Close();
}
protected override void OnStart(string[] args) protected override void OnStart(string[] args)
{ {
envs = descriptor.EnvironmentVariables; envs = descriptor.EnvironmentVariables;
foreach (string key in envs.Keys) foreach (string key in envs.Keys)
{ {
EventLog.WriteEntry("envar " + key + '=' + envs[key]); LogEvent("envar " + key + '=' + envs[key]);
} }
HandleFileCopies(); HandleFileCopies();
@ -434,7 +471,7 @@ namespace winsw
startarguments += " " + descriptor.Arguments; startarguments += " " + descriptor.Arguments;
} }
EventLog.WriteEntry("Starting " + descriptor.Executable + ' ' + startarguments); LogEvent("Starting " + descriptor.Executable + ' ' + startarguments);
StartProcess(process, startarguments, descriptor.Executable); StartProcess(process, startarguments, descriptor.Executable);
@ -444,10 +481,28 @@ namespace winsw
process.StandardInput.Close(); // nothing for you to read! process.StandardInput.Close(); // nothing for you to read!
} }
protected override void OnShutdown()
{
try
{
this.systemShuttingdown = true;
StopIt();
}
catch (Exception ex)
{
WriteEvent("Shutdown exception:"+ex.Message);
}
}
protected override void OnStop() protected override void OnStop()
{
StopIt();
}
private void StopIt()
{ {
string stoparguments = descriptor.Stoparguments; string stoparguments = descriptor.Stoparguments;
EventLog.WriteEntry("Stopping " + descriptor.Id); LogEvent("Stopping " + descriptor.Id);
orderlyShutdown = true; orderlyShutdown = true;
if (stoparguments == null) if (stoparguments == null)
@ -506,17 +561,17 @@ namespace winsw
{ {
if (orderlyShutdown) if (orderlyShutdown)
{ {
EventLog.WriteEntry("Child process [" + msg + "] terminated with " + process.ExitCode, EventLogEntryType.Information); LogEvent("Child process [" + msg + "] terminated with " + process.ExitCode, EventLogEntryType.Information);
} }
else else
{ {
EventLog.WriteEntry("Child process [" + msg + "] terminated with " + process.ExitCode, EventLogEntryType.Warning); LogEvent("Child process [" + msg + "] terminated with " + process.ExitCode, EventLogEntryType.Warning);
Environment.Exit(process.ExitCode); Environment.Exit(process.ExitCode);
} }
} }
catch (InvalidOperationException ioe) catch (InvalidOperationException ioe)
{ {
EventLog.WriteEntry("WaitForExit " + ioe.Message); LogEvent("WaitForExit " + ioe.Message);
} }
try try
@ -525,7 +580,7 @@ namespace winsw
} }
catch (InvalidOperationException ioe) catch (InvalidOperationException ioe)
{ {
EventLog.WriteEntry("Dispose " + ioe.Message); LogEvent("Dispose " + ioe.Message);
} }
}).Start(); }).Start();
} }

View File

@ -1,6 +1,6 @@
 
Microsoft Visual Studio Solution File, Format Version 10.00 Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008 # Visual C# Express 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "winsw", "winsw.csproj", "{0DE77F55-ADE5-43C1-999A-0BC81153B039}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "winsw", "winsw.csproj", "{0DE77F55-ADE5-43C1-999A-0BC81153B039}"
EndProject EndProject
Global Global
@ -15,8 +15,8 @@ Global
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0DE77F55-ADE5-43C1-999A-0BC81153B039}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {0DE77F55-ADE5-43C1-999A-0BC81153B039}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0DE77F55-ADE5-43C1-999A-0BC81153B039}.Debug|Any CPU.Build.0 = Debug|Any CPU {0DE77F55-ADE5-43C1-999A-0BC81153B039}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DE77F55-ADE5-43C1-999A-0BC81153B039}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU {0DE77F55-ADE5-43C1-999A-0BC81153B039}.Debug|Mixed Platforms.ActiveCfg = Release|Any CPU
{0DE77F55-ADE5-43C1-999A-0BC81153B039}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU {0DE77F55-ADE5-43C1-999A-0BC81153B039}.Debug|Mixed Platforms.Build.0 = Release|Any CPU
{0DE77F55-ADE5-43C1-999A-0BC81153B039}.Debug|Win32.ActiveCfg = Debug|Any CPU {0DE77F55-ADE5-43C1-999A-0BC81153B039}.Debug|Win32.ActiveCfg = Debug|Any CPU
{0DE77F55-ADE5-43C1-999A-0BC81153B039}.Release|Any CPU.ActiveCfg = Release|Any CPU {0DE77F55-ADE5-43C1-999A-0BC81153B039}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0DE77F55-ADE5-43C1-999A-0BC81153B039}.Release|Any CPU.Build.0 = Release|Any CPU {0DE77F55-ADE5-43C1-999A-0BC81153B039}.Release|Any CPU.Build.0 = Release|Any CPU