Refactoring - minor cleanup of Main.cs

Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
pull/75/head
Oleg Nenashev 2015-01-31 12:18:25 +03:00
parent 6fcebfa0e2
commit 9291ac911c
1 changed files with 57 additions and 57 deletions

114
Main.cs
View File

@ -15,10 +15,10 @@ namespace winsw
{ {
public class WrapperService : ServiceBase, EventLogger public class WrapperService : ServiceBase, EventLogger
{ {
private SERVICE_STATUS wrapperServiceStatus; private SERVICE_STATUS _wrapperServiceStatus;
private readonly Process process = new Process(); private readonly Process _process = new Process();
private readonly ServiceDescriptor descriptor; private readonly ServiceDescriptor _descriptor;
private Dictionary<string, string> _envs; private Dictionary<string, string> _envs;
/// <summary> /// <summary>
@ -30,8 +30,8 @@ namespace winsw
public WrapperService() public WrapperService()
{ {
descriptor = new ServiceDescriptor(); _descriptor = new ServiceDescriptor();
ServiceName = descriptor.Id; ServiceName = _descriptor.Id;
CanShutdown = true; CanShutdown = true;
CanStop = true; CanStop = true;
CanPauseAndContinue = false; CanPauseAndContinue = false;
@ -45,7 +45,7 @@ namespace winsw
/// </summary> /// </summary>
private void HandleFileCopies() private void HandleFileCopies()
{ {
var file = descriptor.BasePath + ".copies"; var file = _descriptor.BasePath + ".copies";
if (!File.Exists(file)) if (!File.Exists(file))
return; // nothing to handle return; // nothing to handle
@ -115,16 +115,16 @@ namespace winsw
/// </summary> /// </summary>
private void HandleLogfiles() private void HandleLogfiles()
{ {
string logDirectory = descriptor.LogDirectory; string logDirectory = _descriptor.LogDirectory;
if (!Directory.Exists(logDirectory)) if (!Directory.Exists(logDirectory))
{ {
Directory.CreateDirectory(logDirectory); Directory.CreateDirectory(logDirectory);
} }
LogHandler logAppender = descriptor.LogHandler; LogHandler logAppender = _descriptor.LogHandler;
logAppender.EventLogger = this; logAppender.EventLogger = this;
logAppender.log(process.StandardOutput.BaseStream, process.StandardError.BaseStream); logAppender.log(_process.StandardOutput.BaseStream, _process.StandardError.BaseStream);
} }
public void LogEvent(String message) public void LogEvent(String message)
@ -177,7 +177,7 @@ namespace winsw
private void WriteEvent(String message) private void WriteEvent(String message)
{ {
string logfilename = Path.Combine(descriptor.LogDirectory, descriptor.BaseName + ".wrapper.log"); string logfilename = Path.Combine(_descriptor.LogDirectory, _descriptor.BaseName + ".wrapper.log");
StreamWriter log = new StreamWriter(logfilename, true); StreamWriter log = new StreamWriter(logfilename, true);
log.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + message); log.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + message);
@ -187,7 +187,7 @@ namespace winsw
protected override void OnStart(string[] _) protected override void OnStart(string[] _)
{ {
_envs = descriptor.EnvironmentVariables; _envs = _descriptor.EnvironmentVariables;
foreach (string key in _envs.Keys) foreach (string key in _envs.Keys)
{ {
LogEvent("envar " + key + '=' + _envs[key]); LogEvent("envar " + key + '=' + _envs[key]);
@ -196,7 +196,7 @@ namespace winsw
HandleFileCopies(); HandleFileCopies();
// handle downloads // handle downloads
foreach (Download d in descriptor.Downloads) foreach (Download d in _descriptor.Downloads)
{ {
LogEvent("Downloading: " + d.From+ " to "+d.To); LogEvent("Downloading: " + d.From+ " to "+d.To);
try try
@ -211,26 +211,26 @@ namespace winsw
} }
} }
string startarguments = descriptor.Startarguments; string startarguments = _descriptor.Startarguments;
if (startarguments == null) if (startarguments == null)
{ {
startarguments = descriptor.Arguments; startarguments = _descriptor.Arguments;
} }
else else
{ {
startarguments += " " + descriptor.Arguments; startarguments += " " + _descriptor.Arguments;
} }
LogEvent("Starting " + descriptor.Executable + ' ' + startarguments); LogEvent("Starting " + _descriptor.Executable + ' ' + startarguments);
WriteEvent("Starting " + descriptor.Executable + ' ' + startarguments); WriteEvent("Starting " + _descriptor.Executable + ' ' + startarguments);
StartProcess(process, startarguments, descriptor.Executable); StartProcess(_process, startarguments, _descriptor.Executable);
// send stdout and stderr to its respective output file. // send stdout and stderr to its respective output file.
HandleLogfiles(); HandleLogfiles();
process.StandardInput.Close(); // nothing for you to read! _process.StandardInput.Close(); // nothing for you to read!
} }
protected override void OnShutdown() protected override void OnShutdown()
@ -267,17 +267,17 @@ namespace winsw
/// </summary> /// </summary>
private void StopIt() private void StopIt()
{ {
string stoparguments = descriptor.Stoparguments; string stoparguments = _descriptor.Stoparguments;
LogEvent("Stopping " + descriptor.Id); LogEvent("Stopping " + _descriptor.Id);
WriteEvent("Stopping " + descriptor.Id); WriteEvent("Stopping " + _descriptor.Id);
_orderlyShutdown = true; _orderlyShutdown = true;
if (stoparguments == null) if (stoparguments == null)
{ {
try try
{ {
WriteEvent("ProcessKill " + process.Id); WriteEvent("ProcessKill " + _process.Id);
StopProcessAndChildren(process.Id); StopProcessAndChildren(_process.Id);
} }
catch (InvalidOperationException) catch (InvalidOperationException)
{ {
@ -288,37 +288,37 @@ namespace winsw
{ {
SignalShutdownPending(); SignalShutdownPending();
stoparguments += " " + descriptor.Arguments; stoparguments += " " + _descriptor.Arguments;
Process stopProcess = new Process(); Process stopProcess = new Process();
String executable = descriptor.StopExecutable; String executable = _descriptor.StopExecutable;
if (executable == null) if (executable == null)
{ {
executable = descriptor.Executable; executable = _descriptor.Executable;
} }
StartProcess(stopProcess, stoparguments, executable); StartProcess(stopProcess, stoparguments, executable);
WriteEvent("WaitForProcessToExit "+process.Id+"+"+stopProcess.Id); WriteEvent("WaitForProcessToExit "+_process.Id+"+"+stopProcess.Id);
WaitForProcessToExit(process); WaitForProcessToExit(_process);
WaitForProcessToExit(stopProcess); WaitForProcessToExit(stopProcess);
SignalShutdownComplete(); SignalShutdownComplete();
} }
if (_systemShuttingdown && descriptor.BeepOnShutdown) if (_systemShuttingdown && _descriptor.BeepOnShutdown)
{ {
Console.Beep(); Console.Beep();
} }
WriteEvent("Finished " + descriptor.Id); WriteEvent("Finished " + _descriptor.Id);
} }
private void StopProcessAndChildren(int pid) private void StopProcessAndChildren(int pid)
{ {
var childPids = GetChildPids(pid); var childPids = GetChildPids(pid);
if (descriptor.StopParentProcessFirst) if (_descriptor.StopParentProcessFirst)
{ {
StopProcess(pid); StopProcess(pid);
foreach (var childPid in childPids) foreach (var childPid in childPids)
@ -364,7 +364,7 @@ namespace winsw
} }
WriteEvent("Send SIGINT " + pid); WriteEvent("Send SIGINT " + pid);
bool successful = SigIntHelper.SendSIGINTToProcess(proc, descriptor.StopTimeout); bool successful = SigIntHelper.SendSIGINTToProcess(proc, _descriptor.StopTimeout);
if (successful) if (successful)
{ {
WriteEvent("SIGINT to" + pid + " successful"); WriteEvent("SIGINT to" + pid + " successful");
@ -391,7 +391,7 @@ namespace winsw
{ {
// WriteEvent("WaitForProcessToExit [start]"); // WriteEvent("WaitForProcessToExit [start]");
while (!processoWait.WaitForExit(descriptor.SleepTime.Milliseconds)) while (!processoWait.WaitForExit(_descriptor.SleepTime.Milliseconds))
{ {
SignalShutdownPending(); SignalShutdownPending();
// WriteEvent("WaitForProcessToExit [repeat]"); // WriteEvent("WaitForProcessToExit [repeat]");
@ -408,28 +408,28 @@ namespace winsw
private void SignalShutdownPending() private void SignalShutdownPending()
{ {
IntPtr handle = ServiceHandle; IntPtr handle = ServiceHandle;
wrapperServiceStatus.checkPoint++; _wrapperServiceStatus.checkPoint++;
wrapperServiceStatus.waitHint = descriptor.WaitHint.Milliseconds; _wrapperServiceStatus.waitHint = _descriptor.WaitHint.Milliseconds;
// WriteEvent("SignalShutdownPending " + wrapperServiceStatus.checkPoint + ":" + wrapperServiceStatus.waitHint); // WriteEvent("SignalShutdownPending " + wrapperServiceStatus.checkPoint + ":" + wrapperServiceStatus.waitHint);
wrapperServiceStatus.currentState = (int)State.SERVICE_STOP_PENDING; _wrapperServiceStatus.currentState = (int)State.SERVICE_STOP_PENDING;
Advapi32.SetServiceStatus(handle, ref wrapperServiceStatus); Advapi32.SetServiceStatus(handle, ref _wrapperServiceStatus);
} }
private void SignalShutdownComplete() private void SignalShutdownComplete()
{ {
IntPtr handle = ServiceHandle; IntPtr handle = ServiceHandle;
wrapperServiceStatus.checkPoint++; _wrapperServiceStatus.checkPoint++;
// WriteEvent("SignalShutdownComplete " + wrapperServiceStatus.checkPoint + ":" + wrapperServiceStatus.waitHint); // WriteEvent("SignalShutdownComplete " + wrapperServiceStatus.checkPoint + ":" + wrapperServiceStatus.waitHint);
wrapperServiceStatus.currentState = (int)State.SERVICE_STOPPED; _wrapperServiceStatus.currentState = (int)State.SERVICE_STOPPED;
Advapi32.SetServiceStatus(handle, ref wrapperServiceStatus); Advapi32.SetServiceStatus(handle, ref _wrapperServiceStatus);
} }
private void StartProcess(Process process, string arguments, String executable) private void StartProcess(Process processToStart, string arguments, String executable)
{ {
var ps = process.StartInfo; var ps = processToStart.StartInfo;
ps.FileName = executable; ps.FileName = executable;
ps.Arguments = arguments; ps.Arguments = arguments;
ps.WorkingDirectory = descriptor.WorkingDirectory; ps.WorkingDirectory = _descriptor.WorkingDirectory;
ps.CreateNoWindow = false; ps.CreateNoWindow = false;
ps.UseShellExecute = false; ps.UseShellExecute = false;
ps.RedirectStandardInput = true; // this creates a pipe for stdin to the new process, instead of having it inherit our stdin. ps.RedirectStandardInput = true; // this creates a pipe for stdin to the new process, instead of having it inherit our stdin.
@ -440,34 +440,34 @@ namespace winsw
Environment.SetEnvironmentVariable(key, _envs[key]); Environment.SetEnvironmentVariable(key, _envs[key]);
// ps.EnvironmentVariables[key] = envs[key]; // bugged (lower cases all variable names due to StringDictionary being used, see http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=326163) // ps.EnvironmentVariables[key] = envs[key]; // bugged (lower cases all variable names due to StringDictionary being used, see http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=326163)
process.Start(); processToStart.Start();
WriteEvent("Started " + process.Id); WriteEvent("Started " + processToStart.Id);
var priority = descriptor.Priority; var priority = _descriptor.Priority;
if (priority != ProcessPriorityClass.Normal) if (priority != ProcessPriorityClass.Normal)
process.PriorityClass = priority; processToStart.PriorityClass = priority;
// monitor the completion of the process // monitor the completion of the process
StartThread(delegate StartThread(delegate
{ {
string msg = process.Id + " - " + process.StartInfo.FileName + " " + process.StartInfo.Arguments; string msg = processToStart.Id + " - " + processToStart.StartInfo.FileName + " " + processToStart.StartInfo.Arguments;
process.WaitForExit(); processToStart.WaitForExit();
try try
{ {
if (_orderlyShutdown) if (_orderlyShutdown)
{ {
LogEvent("Child process [" + msg + "] terminated with " + process.ExitCode, EventLogEntryType.Information); LogEvent("Child process [" + msg + "] terminated with " + processToStart.ExitCode, EventLogEntryType.Information);
} }
else else
{ {
LogEvent("Child process [" + msg + "] finished with " + process.ExitCode, EventLogEntryType.Warning); LogEvent("Child process [" + msg + "] finished with " + processToStart.ExitCode, EventLogEntryType.Warning);
// if we finished orderly, report that to SCM. // if we finished orderly, report that to SCM.
// by not reporting unclean shutdown, we let Windows SCM to decide if it wants to // by not reporting unclean shutdown, we let Windows SCM to decide if it wants to
// restart the service automatically // restart the service automatically
if (process.ExitCode == 0) if (processToStart.ExitCode == 0)
SignalShutdownComplete(); SignalShutdownComplete();
Environment.Exit(process.ExitCode); Environment.Exit(processToStart.ExitCode);
} }
} }
catch (InvalidOperationException ioe) catch (InvalidOperationException ioe)
@ -477,7 +477,7 @@ namespace winsw
try try
{ {
process.Dispose(); processToStart.Dispose();
} }
catch (InvalidOperationException ioe) catch (InvalidOperationException ioe)
{ {
@ -510,6 +510,7 @@ namespace winsw
throw new WmiException(ReturnValue.NoSuchService); throw new WmiException(ReturnValue.NoSuchService);
} }
// ReSharper disable once InconsistentNaming
public static void Run(string[] _args) public static void Run(string[] _args)
{ {
if (_args.Length > 0) if (_args.Length > 0)
@ -530,8 +531,7 @@ namespace winsw
// and among other things it makes it difficult for the caller // and among other things it makes it difficult for the caller
// to read stdout/stderr. Thus redirection becomes handy. // to read stdout/stderr. Thus redirection becomes handy.
var f = new FileStream(args[1], FileMode.Create); var f = new FileStream(args[1], FileMode.Create);
var w = new StreamWriter(f); var w = new StreamWriter(f) {AutoFlush = true};
w.AutoFlush = true;
Console.SetOut(w); Console.SetOut(w);
Console.SetError(w); Console.SetError(w);