Add extension point for tracking process startup and termination in extensions

pull/133/head
Oleg Nenashev 2016-11-26 20:43:39 +01:00
parent 929b87c383
commit 535e8429e0
4 changed files with 71 additions and 1 deletions

View File

@ -269,6 +269,7 @@ namespace winsw
WriteEvent("Starting " + _descriptor.Executable + ' ' + startarguments); WriteEvent("Starting " + _descriptor.Executable + ' ' + startarguments);
StartProcess(_process, startarguments, _descriptor.Executable); StartProcess(_process, startarguments, _descriptor.Executable);
ExtensionManager.FireOnProcessStarted(_process);
// send stdout and stderr to its respective output file. // send stdout and stderr to its respective output file.
HandleLogfiles(); HandleLogfiles();
@ -321,6 +322,7 @@ namespace winsw
{ {
WriteEvent("ProcessKill " + _process.Id); WriteEvent("ProcessKill " + _process.Id);
StopProcessAndChildren(_process.Id); StopProcessAndChildren(_process.Id);
ExtensionManager.FireOnProcessTerminated(_process);
} }
catch (InvalidOperationException) catch (InvalidOperationException)
{ {

View File

@ -23,5 +23,15 @@ namespace winsw.Extensions
{ {
// Do nothing // Do nothing
} }
public virtual void OnProcessStarted(System.Diagnostics.Process process)
{
// Do nothing
}
public virtual void OnProcessTerminated(System.Diagnostics.Process process)
{
// Do nothing
}
} }
} }

View File

@ -9,7 +9,8 @@ namespace winsw.Extensions
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// All implementations should provide the default empty constructor. /// All implementations should provide the default empty constructor.
/// The initialization will be performed by Init methods /// The initialization will be performed by Init methods.
/// Binary comparibility of the class is not guaranteed in WinSW 2.
/// </remarks> /// </remarks>
public interface IWinSWExtension public interface IWinSWExtension
{ {
@ -37,6 +38,22 @@ namespace winsw.Extensions
/// <exception cref="ExtensionException">Any error during execution</exception> /// <exception cref="ExtensionException">Any error during execution</exception>
void OnStart(IEventWriter logger); void OnStart(IEventWriter logger);
/// <summary>
/// Handler, which is being invoked once the child process is started.
/// </summary>
/// <param name="process">Process</param>
/// <param name="logger">Logger</param>
/// <exception cref="ExtensionException">Any error during execution</exception>
void OnProcessStarted(System.Diagnostics.Process process);
/// <summary>
/// Handler, which is being invoked once the child process is terminated.
/// </summary>
/// <param name="process">Process</param>
/// <param name="logger">Logger</param>
/// <exception cref="ExtensionException">Any error during execution</exception>
void OnProcessTerminated(System.Diagnostics.Process process);
/// <summary> /// <summary>
/// Stop handler. Called during stop of the service /// Stop handler. Called during stop of the service
/// </summary> /// </summary>

View File

@ -4,6 +4,7 @@ using System.Xml;
using System.Reflection; using System.Reflection;
using System.Diagnostics; using System.Diagnostics;
using winsw.Util; using winsw.Util;
using log4net;
namespace winsw.Extensions namespace winsw.Extensions
{ {
@ -12,6 +13,8 @@ namespace winsw.Extensions
public Dictionary<string, IWinSWExtension> Extensions { private set; get; } public Dictionary<string, IWinSWExtension> Extensions { private set; get; }
public ServiceDescriptor ServiceDescriptor { private set; get; } public ServiceDescriptor ServiceDescriptor { private set; get; }
private static readonly ILog Log = LogManager.GetLogger(typeof(WinSWExtensionManager));
public WinSWExtensionManager(ServiceDescriptor serviceDescriptor) public WinSWExtensionManager(ServiceDescriptor serviceDescriptor)
{ {
ServiceDescriptor = serviceDescriptor; ServiceDescriptor = serviceDescriptor;
@ -42,6 +45,44 @@ namespace winsw.Extensions
} }
} }
/// <summary>
/// Handler, which is being invoked once the child process is started.
/// </summary>
/// <param name="process">Process</param>
public void FireOnProcessStarted(System.Diagnostics.Process process)
{
foreach (var ext in Extensions)
{
try
{
ext.Value.OnProcessStarted(process);
}
catch (ExtensionException ex)
{
Log.Error("onProcessStarted() handler failed for " + ext.Value.DisplayName, ex);
}
}
}
/// <summary>
/// Handler, which is being invoked once the child process is terminated.
/// </summary>
/// <param name="process">Process</param>
public void FireOnProcessTerminated(System.Diagnostics.Process process)
{
foreach (var ext in Extensions)
{
try
{
ext.Value.OnProcessTerminated(process);
}
catch (ExtensionException ex)
{
Log.Error("onProcessTerminated() handler failed for " + ext.Value.DisplayName, ex);
}
}
}
//TODO: Implement loading of external extensions. Current version supports internal hack //TODO: Implement loading of external extensions. Current version supports internal hack
#region Extension load management #region Extension load management