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)
{ {

50
Main.cs
View File

@ -319,15 +319,55 @@ namespace winsw
private void StopProcessAndChildren(int pid) private void StopProcessAndChildren(int pid)
{ {
var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid); var childPids = GetChildPids(pid);
foreach (var mo in searcher.Get())
if (descriptor.StopParentProcessFirst)
{ {
StopProcessAndChildren(Convert.ToInt32(mo["ProcessID"])); 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 childPids = new List<int>();
foreach (var mo in searcher.Get())
{
var childProcessId = mo["ProcessID"];
WriteEvent("Found child process: " + childProcessId + " Name: " + mo["Name"]);
childPids.Add(Convert.ToInt32(childProcessId));
}
return childPids;
}
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;
} }
var proc = Process.GetProcessById(pid);
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

@ -53,10 +53,8 @@ namespace winsw
return process.HasExited; return process.HasExited;
} }
else
{
return false; return false;
} }
} }
}
} }

View File

@ -5,6 +5,8 @@ using System.Xml;
namespace winswTests namespace winswTests
{ {
using System;
[TestFixture] [TestFixture]
public class ServiceDescriptorTests public class ServiceDescriptorTests
{ {
@ -72,5 +74,84 @@ 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"));
}
} }
} }