ソースを参照

Add extension point for tracking process startup and termination in extensions

Oleg Nenashev 9 年 前
コミット
535e8429e0

+ 2 - 0
src/Core/ServiceWrapper/Main.cs

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

+ 10 - 0
src/Core/WinSWCore/Extensions/AbstractWinSWExtension.cs

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

+ 18 - 1
src/Core/WinSWCore/Extensions/IWinSWExtension.cs

@@ -9,7 +9,8 @@ namespace winsw.Extensions
     /// </summary>
     /// <remarks>
     /// 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>
     public interface IWinSWExtension
     {
@@ -37,6 +38,22 @@ namespace winsw.Extensions
         /// <exception cref="ExtensionException">Any error during execution</exception>
         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>
         /// Stop handler. Called during stop of the service
         /// </summary>

+ 41 - 0
src/Core/WinSWCore/Extensions/WinSWExtensionManager.cs

@@ -4,6 +4,7 @@ using System.Xml;
 using System.Reflection;
 using System.Diagnostics;
 using winsw.Util;
+using log4net;
 
 namespace winsw.Extensions
 {
@@ -12,6 +13,8 @@ namespace winsw.Extensions
         public Dictionary<string, IWinSWExtension> Extensions { private set; get; }
         public ServiceDescriptor ServiceDescriptor { private set; get; }
 
+        private static readonly ILog Log = LogManager.GetLogger(typeof(WinSWExtensionManager));
+
         public WinSWExtensionManager(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
         #region Extension load management