Merge pull request #53 from oleg-nenashev/PR_47_refactoring

PR #47 - merged version
pull/47/merge
Oleg Nenashev 2014-11-21 13:17:52 +03:00
commit b93e08ee8b
7 changed files with 245 additions and 71 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
bin bin
obj obj
*.suo *.suo
/UpgradeLog.htm
/winsw.csproj.DotSettings.user
/winsw.csproj.user

View File

@ -163,6 +163,22 @@ namespace winsw
private string pattern; private string pattern;
private int period; private int period;
public string Pattern
{
get
{
return pattern;
}
}
public int Period
{
get
{
return period;
}
}
public TimeBasedRollingLogAppender(string logDirectory, string baseName, string pattern, int period) public TimeBasedRollingLogAppender(string logDirectory, string baseName, string pattern, int period)
: base(logDirectory, baseName) : base(logDirectory, baseName)
{ {
@ -246,6 +262,22 @@ namespace winsw
private int sizeThreshold; private int sizeThreshold;
private int filesToKeep; private int filesToKeep;
public int SizeTheshold
{
get
{
return sizeThreshold;
}
}
public int FilesToKeep
{
get
{
return filesToKeep;
}
}
public SizeBasedRollingLogAppender(string logDirectory, string baseName, int sizeThreshold, int filesToKeep) public SizeBasedRollingLogAppender(string logDirectory, string baseName, int sizeThreshold, int filesToKeep)
: base(logDirectory, baseName) : base(logDirectory, baseName)
{ {

46
Main.cs
View File

@ -318,16 +318,56 @@ namespace winsw
} }
private void StopProcessAndChildren(int pid) private void StopProcessAndChildren(int pid)
{
var childPids = GetChildPids(pid);
if (descriptor.StopParentProcessFirst)
{
StopProcess(pid);
foreach (var childPid in childPids)
{
StopProcessAndChildren(childPid);
}
}
else
{
foreach (var childPid in childPids)
{
StopProcessAndChildren(childPid);
}
StopProcess(pid);
}
}
private List<int> GetChildPids(int pid)
{ {
var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid); var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
var childPids = new List<int>();
foreach (var mo in searcher.Get()) foreach (var mo in searcher.Get())
{ {
StopProcessAndChildren(Convert.ToInt32(mo["ProcessID"])); var childProcessId = mo["ProcessID"];
WriteEvent("Found child process: " + childProcessId + " Name: " + mo["Name"]);
childPids.Add(Convert.ToInt32(childProcessId));
} }
return childPids;
}
var proc = Process.GetProcessById(pid); private void StopProcess(int pid)
{
WriteEvent("Stopping process " + pid);
Process proc;
try
{
proc = Process.GetProcessById(pid);
}
catch (ArgumentException)
{
WriteEvent("Process " + pid + " is already stopped");
return;
}
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");

View File

@ -292,3 +292,9 @@ Possible values are `idle`, `belownormal`, `normal`, `abovenormal`, `high`, `rea
<priority>idle</priority> <priority>idle</priority>
Specifying a priority higher than normal has unintended consequences. See <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.processpriorityclass(v=vs.110).aspx">MSDN discussion</a> for details. This feature is intended primarily to launch a process in a lower priority so as not to interfere with the computer's interactive usage. Specifying a priority higher than normal has unintended consequences. See <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.processpriorityclass(v=vs.110).aspx">MSDN discussion</a> for details. This feature is intended primarily to launch a process in a lower priority so as not to interfere with the computer's interactive usage.
###stopparentprocessfirst
Optionally specify the order of service shutdown. If true, the parent process is shutdown first. This is useful when the main process is a console, which can respond to Ctrol+C command and will gracefully shutdown child processes
```
<stopparentprocessfirst>true</stopparentprocessfirst>
```

View File

@ -28,13 +28,13 @@ namespace winsw
/// ///
/// This string is "c:\abc\def\ghi" when the configuration XML is "c:\abc\def\ghi.xml" /// This string is "c:\abc\def\ghi" when the configuration XML is "c:\abc\def\ghi.xml"
/// </summary> /// </summary>
public readonly string BasePath; public string BasePath { get; set; }
/// <summary> /// <summary>
/// The file name portion of the configuration file. /// The file name portion of the configuration file.
/// ///
/// In the above example, this would be "ghi". /// In the above example, this would be "ghi".
/// </summary> /// </summary>
public readonly string BaseName; public string BaseName { get; set; }
public virtual string ExecutablePath public virtual string ExecutablePath
{ {
@ -580,7 +580,21 @@ namespace winsw
{ {
get get
{ {
return SingleTimeSpanElement(dom, "stoptimeout", TimeSpan.FromSeconds(15)); return SingleTimeSpanElement(dom.FirstChild, "stoptimeout", TimeSpan.FromSeconds(15));
}
}
public bool StopParentProcessFirst
{
get
{
var value = SingleElement("stopparentprocessfirst", true);
bool result;
if (bool.TryParse(value, out result))
{
return result;
}
return false;
} }
} }

View File

@ -51,12 +51,10 @@ namespace winsw
process.WaitForExit((int)shutdownTimeout.TotalMilliseconds); process.WaitForExit((int)shutdownTimeout.TotalMilliseconds);
return process.HasExited; return process.HasExited;
}
else
{
return false;
} }
return false;
} }
} }
} }

View File

@ -1,63 +1,65 @@
using NUnit.Framework; using NUnit.Framework;
using winsw; using winsw;
using System.Diagnostics; using System.Diagnostics;
using System.Xml; using System.Xml;
namespace winswTests namespace winswTests
{ {
[TestFixture] using System;
public class ServiceDescriptorTests
{ [TestFixture]
public class ServiceDescriptorTests
private ServiceDescriptor extendedServiceDescriptor; {
private const string ExpectedWorkingDirectory = @"Z:\Path\SubPath"; private ServiceDescriptor extendedServiceDescriptor;
private const string Username = "User";
private const string Password = "Password"; private const string ExpectedWorkingDirectory = @"Z:\Path\SubPath";
private const string Domain = "Domain"; private const string Username = "User";
private const string Password = "Password";
[SetUp] private const string Domain = "Domain";
public void SetUp()
{ [SetUp]
const string SeedXml = "<service>" public void SetUp()
+ "<id>service.exe</id>" {
+ "<name>Service</name>" const string SeedXml = "<service>"
+ "<description>The service.</description>" + "<id>service.exe</id>"
+ "<executable>node.exe</executable>" + "<name>Service</name>"
+ "<arguments>My Arguments</arguments>" + "<description>The service.</description>"
+ "<logmode>rotate</logmode>" + "<executable>node.exe</executable>"
+ "<serviceaccount>" + "<arguments>My Arguments</arguments>"
+ "<domain>" + Domain + "</domain>" + "<logmode>rotate</logmode>"
+ "<user>" + Username + "</user>" + "<serviceaccount>"
+ "<password>" + Password + "</password>" + "<domain>" + Domain + "</domain>"
+ "</serviceaccount>" + "<user>" + Username + "</user>"
+ "<workingdirectory>" + "<password>" + Password + "</password>"
+ ExpectedWorkingDirectory + "</serviceaccount>"
+ "</workingdirectory>" + "<workingdirectory>"
+ @"<logpath>C:\logs</logpath>" + ExpectedWorkingDirectory
+ "</workingdirectory>"
+ @"<logpath>C:\logs</logpath>"
+ "</service>"; + "</service>";
extendedServiceDescriptor = ServiceDescriptor.FromXML(SeedXml); extendedServiceDescriptor = ServiceDescriptor.FromXML(SeedXml);
} }
[Test] [Test]
public void VerifyWorkingDirectory() public void VerifyWorkingDirectory()
{ {
System.Diagnostics.Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + extendedServiceDescriptor.WorkingDirectory); System.Diagnostics.Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + extendedServiceDescriptor.WorkingDirectory);
Assert.That(extendedServiceDescriptor.WorkingDirectory, Is.EqualTo(ExpectedWorkingDirectory)); Assert.That(extendedServiceDescriptor.WorkingDirectory, Is.EqualTo(ExpectedWorkingDirectory));
} }
[Test] [Test]
public void VerifyUsername() public void VerifyUsername()
{ {
System.Diagnostics.Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + extendedServiceDescriptor.WorkingDirectory); System.Diagnostics.Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + extendedServiceDescriptor.WorkingDirectory);
Assert.That(extendedServiceDescriptor.ServiceAccountUser, Is.EqualTo(Domain + "\\" + Username)); Assert.That(extendedServiceDescriptor.ServiceAccountUser, Is.EqualTo(Domain + "\\" + Username));
} }
[Test] [Test]
public void VerifyPassword() public void VerifyPassword()
{ {
System.Diagnostics.Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + extendedServiceDescriptor.WorkingDirectory); System.Diagnostics.Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + extendedServiceDescriptor.WorkingDirectory);
Assert.That(extendedServiceDescriptor.ServiceAccountPassword, Is.EqualTo(Password)); Assert.That(extendedServiceDescriptor.ServiceAccountPassword, Is.EqualTo(Password));
} }
[Test] [Test]
@ -71,6 +73,85 @@ namespace winswTests
sd = ServiceDescriptor.FromXML("<service><id>test</id></service>"); sd = ServiceDescriptor.FromXML("<service><id>test</id></service>");
Assert.That(sd.Priority, Is.EqualTo(ProcessPriorityClass.Normal)); Assert.That(sd.Priority, Is.EqualTo(ProcessPriorityClass.Normal));
} }
}
} [Test]
public void StopParentProcessFirstIsFalseByDefault()
{
Assert.False(extendedServiceDescriptor.StopParentProcessFirst);
}
[Test]
public void CanParseStopParentProcessFirst()
{
const string SeedXml = "<service>"
+ "<stopparentprocessfirst>true</stopparentprocessfirst>"
+ "</service>";
var serviceDescriptor = ServiceDescriptor.FromXML(SeedXml);
Assert.True(serviceDescriptor.StopParentProcessFirst);
}
[Test]
public void CanParseStopTimeout()
{
const string SeedXml = "<service>"
+ "<stoptimeout>60sec</stoptimeout>"
+ "</service>";
var serviceDescriptor = ServiceDescriptor.FromXML(SeedXml);
Assert.That(serviceDescriptor.StopTimeout, Is.EqualTo(TimeSpan.FromSeconds(60)));
}
[Test]
public void CanParseStopTimeoutFromMinutes()
{
const string SeedXml = "<service>"
+ "<stoptimeout>10min</stoptimeout>"
+ "</service>";
var serviceDescriptor = ServiceDescriptor.FromXML(SeedXml);
Assert.That(serviceDescriptor.StopTimeout, Is.EqualTo(TimeSpan.FromMinutes(10)));
}
[Test]
public void LogModeRollBySize()
{
const string SeedXml = "<service>"
+ "<logpath>c:\\</logpath>"
+ "<log mode=\"roll-by-size\">"
+ "<sizeThreshold>112</sizeThreshold>"
+ "<keepFiles>113</keepFiles>"
+ "</log>"
+ "</service>";
var serviceDescriptor = ServiceDescriptor.FromXML(SeedXml);
serviceDescriptor.BaseName = "service";
var logHandler = serviceDescriptor.LogHandler as SizeBasedRollingLogAppender;
Assert.NotNull(logHandler);
Assert.That(logHandler.SizeTheshold, Is.EqualTo(112 * 1024));
Assert.That(logHandler.FilesToKeep, Is.EqualTo(113));
}
[Test]
public void LogModeRollByTime()
{
const string SeedXml = "<service>"
+ "<logpath>c:\\</logpath>"
+ "<log mode=\"roll-by-time\">"
+ "<period>7</period>"
+ "<pattern>log pattern</pattern>"
+ "</log>"
+ "</service>";
var serviceDescriptor = ServiceDescriptor.FromXML(SeedXml);
serviceDescriptor.BaseName = "service";
var logHandler = serviceDescriptor.LogHandler as TimeBasedRollingLogAppender;
Assert.NotNull(logHandler);
Assert.That(logHandler.Period, Is.EqualTo(7));
Assert.That(logHandler.Pattern, Is.EqualTo("log pattern"));
}
}
}