From 05142d2a8f2a58069f67aed5d1d55d290a2d133b Mon Sep 17 00:00:00 2001 From: Eric Fontana Date: Fri, 21 Nov 2014 09:06:13 -0500 Subject: [PATCH] Re-worked Windows Event shutdown to use Thread.Abort to enable shuting down in a quicker fashion. --- .../Properties/AssemblyInfo.cs | 4 +- TimberWinR/Inputs/IISW3CInputListener.cs | 1 + TimberWinR/Inputs/InputListener.cs | 3 ++ TimberWinR/Inputs/LogsListener.cs | 1 + TimberWinR/Inputs/StdinListener.cs | 3 +- TimberWinR/Inputs/TcpInputListener.cs | 2 + TimberWinR/Inputs/UdpInputListener.cs | 7 +++- TimberWinR/Inputs/W3CInputListener.cs | 1 + TimberWinR/Inputs/WindowsEvtInputListener.cs | 41 ++++++++++++++++--- TimberWinR/Manager.cs | 2 + chocolateyUninstall.ps1.template | 2 +- timberwinr.nuspec.template | 4 +- 12 files changed, 58 insertions(+), 13 deletions(-) diff --git a/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs b/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs index d5c99bf..4032b8e 100644 --- a/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs +++ b/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.11.0")] -[assembly: AssemblyFileVersion("1.3.11.0")] +[assembly: AssemblyVersion("1.3.12.0")] +[assembly: AssemblyFileVersion("1.3.12.0")] diff --git a/TimberWinR/Inputs/IISW3CInputListener.cs b/TimberWinR/Inputs/IISW3CInputListener.cs index df94f0c..4ea48eb 100644 --- a/TimberWinR/Inputs/IISW3CInputListener.cs +++ b/TimberWinR/Inputs/IISW3CInputListener.cs @@ -41,6 +41,7 @@ namespace TimberWinR.Inputs public override void Shutdown() { + LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType); base.Shutdown(); } diff --git a/TimberWinR/Inputs/InputListener.cs b/TimberWinR/Inputs/InputListener.cs index b2b36b3..ea46dcf 100644 --- a/TimberWinR/Inputs/InputListener.cs +++ b/TimberWinR/Inputs/InputListener.cs @@ -59,9 +59,12 @@ namespace TimberWinR.Inputs public void Finished() { FinishedEvent.Set(); + LogManager.GetCurrentClassLogger().Info("Finished Shutdown {0}", InputType); } public virtual void Shutdown() { + LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType); + FinishedEvent.WaitOne(); try { diff --git a/TimberWinR/Inputs/LogsListener.cs b/TimberWinR/Inputs/LogsListener.cs index befe2a0..99ae321 100644 --- a/TimberWinR/Inputs/LogsListener.cs +++ b/TimberWinR/Inputs/LogsListener.cs @@ -53,6 +53,7 @@ namespace TimberWinR.Inputs public override void Shutdown() { + LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType); base.Shutdown(); } diff --git a/TimberWinR/Inputs/StdinListener.cs b/TimberWinR/Inputs/StdinListener.cs index 731cb86..e71fa3d 100644 --- a/TimberWinR/Inputs/StdinListener.cs +++ b/TimberWinR/Inputs/StdinListener.cs @@ -28,7 +28,8 @@ namespace TimberWinR.Inputs } public override void Shutdown() - { + { + LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType); base.Shutdown(); } diff --git a/TimberWinR/Inputs/TcpInputListener.cs b/TimberWinR/Inputs/TcpInputListener.cs index d09c18d..249b981 100644 --- a/TimberWinR/Inputs/TcpInputListener.cs +++ b/TimberWinR/Inputs/TcpInputListener.cs @@ -52,6 +52,8 @@ namespace TimberWinR.Inputs public override void Shutdown() { + LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType); + this._tcpListenerV4.Stop(); this._tcpListenerV6.Stop(); diff --git a/TimberWinR/Inputs/UdpInputListener.cs b/TimberWinR/Inputs/UdpInputListener.cs index 9cbaf3d..a67b2b3 100644 --- a/TimberWinR/Inputs/UdpInputListener.cs +++ b/TimberWinR/Inputs/UdpInputListener.cs @@ -60,7 +60,9 @@ namespace TimberWinR.Inputs public override void Shutdown() - { + { + LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType); + _udpListener.Close(); Finished(); base.Shutdown(); } @@ -84,7 +86,8 @@ namespace TimberWinR.Inputs } catch (Exception ex) { - LogManager.GetCurrentClassLogger().Error(ex); + if (!CancelToken.IsCancellationRequested) + LogManager.GetCurrentClassLogger().Error(ex); } Finished(); diff --git a/TimberWinR/Inputs/W3CInputListener.cs b/TimberWinR/Inputs/W3CInputListener.cs index 66b3531..01af584 100644 --- a/TimberWinR/Inputs/W3CInputListener.cs +++ b/TimberWinR/Inputs/W3CInputListener.cs @@ -41,6 +41,7 @@ namespace TimberWinR.Inputs public override void Shutdown() { + LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType); base.Shutdown(); } diff --git a/TimberWinR/Inputs/WindowsEvtInputListener.cs b/TimberWinR/Inputs/WindowsEvtInputListener.cs index ce3155a..c4be9b5 100644 --- a/TimberWinR/Inputs/WindowsEvtInputListener.cs +++ b/TimberWinR/Inputs/WindowsEvtInputListener.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Security.AccessControl; using System.Text; using System.Threading; @@ -25,22 +26,31 @@ namespace TimberWinR.Inputs private int _pollingIntervalInSeconds = 1; private TimberWinR.Parser.WindowsEvent _arguments; private long _receivedMessages; + private List _tasks { get; set; } public WindowsEvtInputListener(TimberWinR.Parser.WindowsEvent arguments, CancellationToken cancelToken) : base(cancelToken, "Win32-Eventlog") { _arguments = arguments; _pollingIntervalInSeconds = arguments.Interval; + _tasks = new List(); foreach (string eventHive in _arguments.Source.Split(',')) { string hive = eventHive.Trim(); - Task.Factory.StartNew(() => EventWatcher(eventHive)); + var thread = new Thread(new ParameterizedThreadStart(EventWatcher)); + _tasks.Add(thread); + thread.Start(eventHive); } } public override void Shutdown() { + LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType); + foreach(Thread t in _tasks) + { + t.Abort(); + } base.Shutdown(); } @@ -64,8 +74,10 @@ namespace TimberWinR.Inputs return json; } - private void EventWatcher(string location) + private void EventWatcher(object ploc) { + string location = ploc.ToString(); + LogQuery oLogQuery = new LogQuery(); LogManager.GetCurrentClassLogger().Info("WindowsEvent Input Listener Ready"); @@ -147,13 +159,32 @@ namespace TimberWinR.Inputs rs = null; } } + catch (System.Threading.ThreadAbortException tex) + { + Thread.ResetAbort(); + break; + } catch (Exception ex) { - LogManager.GetCurrentClassLogger().Error(ex); + LogManager.GetCurrentClassLogger().Error(ex); } - Thread.CurrentThread.Priority = ThreadPriority.Normal; - System.Threading.Thread.Sleep(_pollingIntervalInSeconds * 1000); + try + { + Thread.CurrentThread.Priority = ThreadPriority.Normal; + System.Threading.Thread.Sleep(_pollingIntervalInSeconds * 1000); + } + catch (System.Threading.ThreadAbortException tex) + { + Thread.ResetAbort(); + break; + } + catch (Exception ex) + { + LogManager.GetCurrentClassLogger().Error(ex); + } + + } Finished(); diff --git a/TimberWinR/Manager.cs b/TimberWinR/Manager.cs index cec594d..8196c57 100644 --- a/TimberWinR/Manager.cs +++ b/TimberWinR/Manager.cs @@ -48,6 +48,8 @@ namespace TimberWinR foreach (InputListener listener in Listeners) listener.Shutdown(); + + LogManager.GetCurrentClassLogger().Info("Completed ShutDown"); } diff --git a/chocolateyUninstall.ps1.template b/chocolateyUninstall.ps1.template index 9a45797..726a813 100644 --- a/chocolateyUninstall.ps1.template +++ b/chocolateyUninstall.ps1.template @@ -1,7 +1,7 @@ $packageName = 'TimberWinR-${version}' # arbitrary name for the package, used in messages $installerType = 'msi' #only one of these: exe, msi, msu $url = 'http://www.ericfontana.com/TimberWinR/TimberWinR-${version}.0.msi' # download url -$silentArgs = '{0084CE8C-1F06-4B74-9EB4-C3067850B173} /quiet' +$silentArgs = '{ABD20B9E-8628-40AD-AB11-5BD4356F9D94} /quiet' $validExitCodes = @(0) #please insert other valid exit codes here, exit codes for ms http://msdn.microsoft.com/en-us/library/aa368542(VS.85).aspx UnInstall-ChocolateyPackage "$packageName" "$installerType" "$silentArgs" "$url" -validExitCodes $validExitCodes diff --git a/timberwinr.nuspec.template b/timberwinr.nuspec.template index 2414128..1b23055 100644 --- a/timberwinr.nuspec.template +++ b/timberwinr.nuspec.template @@ -13,9 +13,9 @@ https://github.com/efontana/TimberWinR TimberWinR admin + http://www.ericfontana.com/timberwinr.jpg http://www.apache.org/licenses/LICENSE-2.0.html - false - + false