From 780e374122b3e8f76e25ce63742cf50dd7c40d0d Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 30 Aug 2013 09:18:12 -0700 Subject: [PATCH] Added ProgressMessaging through nlog --- NzbDrone.Api/NancyBootstrapper.cs | 5 +- .../Messaging/MessageAggregator.cs | 14 ++-- .../ParserTests/ParserFixture.cs | 1 + .../Lifecycle/ApplicationStartedEvent.cs | 1 - NzbDrone.Core/NzbDrone.Core.csproj | 3 + .../NewProgressMessageEvent.cs | 18 ++++ .../ProgressMessaging/ProgressMessage.cs | 14 ++++ .../ProgressMessagingTarget.cs | 82 +++++++++++++++++++ UI/Router.js | 2 +- 9 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 NzbDrone.Core/ProgressMessaging/NewProgressMessageEvent.cs create mode 100644 NzbDrone.Core/ProgressMessaging/ProgressMessage.cs create mode 100644 NzbDrone.Core/ProgressMessaging/ProgressMessagingTarget.cs diff --git a/NzbDrone.Api/NancyBootstrapper.cs b/NzbDrone.Api/NancyBootstrapper.cs index 5f65c80e2..ed1463a23 100644 --- a/NzbDrone.Api/NancyBootstrapper.cs +++ b/NzbDrone.Api/NancyBootstrapper.cs @@ -9,6 +9,7 @@ using NzbDrone.Api.Extensions.Pipelines; using NzbDrone.Common.Messaging; using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Lifecycle; +using NzbDrone.Core.ProgressMessaging; using TinyIoC; namespace NzbDrone.Api @@ -28,14 +29,12 @@ namespace NzbDrone.Api { _logger.Info("Starting NzbDrone API"); - RegisterPipelines(pipelines); container.Resolve().Register(); container.Resolve().Register(pipelines); container.Resolve().PublishEvent(new ApplicationStartedEvent()); - ApplicationPipelines.OnError.AddItemToEndOfPipeline(container.Resolve().HandleException); } @@ -47,10 +46,8 @@ namespace NzbDrone.Api { registerNancyPipeline.Register(pipelines); } - } - protected override TinyIoCContainer GetApplicationContainer() { return _tinyIoCContainer; diff --git a/NzbDrone.Common/Messaging/MessageAggregator.cs b/NzbDrone.Common/Messaging/MessageAggregator.cs index 84d16012d..0be1a0951 100644 --- a/NzbDrone.Common/Messaging/MessageAggregator.cs +++ b/NzbDrone.Common/Messaging/MessageAggregator.cs @@ -87,30 +87,32 @@ namespace NzbDrone.Common.Messaging _logger.Debug("{0} -> {1}", command.GetType().Name, handler.GetType().Name); var sw = Stopwatch.StartNew(); - TrackedCommand queuedCommand = null; + TrackedCommand trackedCommand = null; try { - queuedCommand = _trackCommands.TrackIfNew(command); + trackedCommand = _trackCommands.TrackIfNew(command); - if (queuedCommand == null) + if (trackedCommand == null) { _logger.Info("Command is already in progress: {0}", command.GetType().Name); return; } + MappedDiagnosticsContext.Set("CommandId", trackedCommand.Command.CommandId); + PublishEvent(new CommandStartedEvent(command)); handler.Execute(command); sw.Stop(); - _trackCommands.Completed(queuedCommand, sw.Elapsed); + _trackCommands.Completed(trackedCommand, sw.Elapsed); PublishEvent(new CommandCompletedEvent(command)); } catch (Exception e) { - if (queuedCommand != null) + if (trackedCommand != null) { - _trackCommands.Failed(queuedCommand, e); + _trackCommands.Failed(trackedCommand, e); } PublishEvent(new CommandFailedEvent(command, e)); diff --git a/NzbDrone.Core.Test/ParserTests/ParserFixture.cs b/NzbDrone.Core.Test/ParserTests/ParserFixture.cs index 192ff7380..ac797bba8 100644 --- a/NzbDrone.Core.Test/ParserTests/ParserFixture.cs +++ b/NzbDrone.Core.Test/ParserTests/ParserFixture.cs @@ -80,6 +80,7 @@ namespace NzbDrone.Core.Test.ParserTests [TestCase("Top_Gear.19x06.720p_HDTV_x264-FoV", "Top Gear", 19, 6)] [TestCase("Portlandia.S03E10.Alexandra.720p.WEB-DL.AAC2.0.H.264-CROM.mkv", "Portlandia", 3, 10)] [TestCase("(Game of Thrones s03 e - \"Game of Thrones Season 3 Episode 10\"", "Game of Thrones", 3, 10)] + [TestCase("House.Hunters.International.S05E607.720p.hdtv.x264", "House.Hunters.International", 5, 607)] public void ParseTitle_single(string postTitle, string title, int seasonNumber, int episodeNumber) { var result = Parser.Parser.ParseTitle(postTitle); diff --git a/NzbDrone.Core/Lifecycle/ApplicationStartedEvent.cs b/NzbDrone.Core/Lifecycle/ApplicationStartedEvent.cs index 985986b05..f66622dd3 100644 --- a/NzbDrone.Core/Lifecycle/ApplicationStartedEvent.cs +++ b/NzbDrone.Core/Lifecycle/ApplicationStartedEvent.cs @@ -6,5 +6,4 @@ namespace NzbDrone.Core.Lifecycle { } - } \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 3f95a1ab6..6a13f8b82 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -219,6 +219,9 @@ + + + diff --git a/NzbDrone.Core/ProgressMessaging/NewProgressMessageEvent.cs b/NzbDrone.Core/ProgressMessaging/NewProgressMessageEvent.cs new file mode 100644 index 000000000..0b2905312 --- /dev/null +++ b/NzbDrone.Core/ProgressMessaging/NewProgressMessageEvent.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NzbDrone.Common.Messaging; + +namespace NzbDrone.Core.ProgressMessaging +{ + public class NewProgressMessageEvent : IEvent + { + public ProgressMessage ProgressMessage { get; set; } + + public NewProgressMessageEvent(ProgressMessage progressMessage) + { + ProgressMessage = progressMessage; + } + } +} diff --git a/NzbDrone.Core/ProgressMessaging/ProgressMessage.cs b/NzbDrone.Core/ProgressMessaging/ProgressMessage.cs new file mode 100644 index 000000000..876087efb --- /dev/null +++ b/NzbDrone.Core/ProgressMessaging/ProgressMessage.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Core.ProgressMessaging +{ + public class ProgressMessage + { + public DateTime Time { get; set; } + public String CommandId { get; set; } + public String Message { get; set; } + } +} diff --git a/NzbDrone.Core/ProgressMessaging/ProgressMessagingTarget.cs b/NzbDrone.Core/ProgressMessaging/ProgressMessagingTarget.cs new file mode 100644 index 000000000..247fa7686 --- /dev/null +++ b/NzbDrone.Core/ProgressMessaging/ProgressMessagingTarget.cs @@ -0,0 +1,82 @@ +using System; +using NLog.Config; +using NLog; +using NLog.Layouts; +using NLog.Targets; +using NzbDrone.Common.Messaging; +using NzbDrone.Core.Lifecycle; + +namespace NzbDrone.Core.ProgressMessaging +{ + + public class ProgressMessagingTarget : TargetWithLayout, IHandle, IHandle + { + private readonly IMessageAggregator _messageAggregator; + public LoggingRule Rule { get; set; } + + public ProgressMessagingTarget(IMessageAggregator messageAggregator) + { + _messageAggregator = messageAggregator; + } + + public void Register() + { + Layout = new SimpleLayout("${callsite:className=false:fileName=false:includeSourcePath=false:methodName=true}"); + + Rule = new LoggingRule("*", this); + Rule.EnableLoggingForLevel(LogLevel.Info); + + LogManager.Configuration.AddTarget("ProgressMessagingLogger", this); + LogManager.Configuration.LoggingRules.Add(Rule); + LogManager.ConfigurationReloaded += OnLogManagerOnConfigurationReloaded; + LogManager.ReconfigExistingLoggers(); + } + + public void UnRegister() + { + LogManager.ConfigurationReloaded -= OnLogManagerOnConfigurationReloaded; + LogManager.Configuration.RemoveTarget("ProgressMessagingLogger"); + LogManager.Configuration.LoggingRules.Remove(Rule); + LogManager.ReconfigExistingLoggers(); + Dispose(); + } + + private void OnLogManagerOnConfigurationReloaded(object sender, LoggingConfigurationReloadedEventArgs args) + { + Register(); + } + + protected override void Write(LogEventInfo logEvent) + { + var commandId = MappedDiagnosticsContext.Get("CommandId"); + + if (String.IsNullOrWhiteSpace(commandId)) + { + return; + } + + var message = new ProgressMessage(); + message.Time = logEvent.TimeStamp; + message.CommandId = commandId; + message.Message = logEvent.FormattedMessage; + + _messageAggregator.PublishEvent(new NewProgressMessageEvent(message)); + } + + public void Handle(ApplicationStartedEvent message) + { + if (!LogManager.Configuration.LoggingRules.Contains(Rule)) + { + Register(); + } + } + + public void Handle(ApplicationShutdownRequested message) + { + if (LogManager.Configuration.LoggingRules.Contains(Rule)) + { + UnRegister(); + } + } + } +} \ No newline at end of file diff --git a/UI/Router.js b/UI/Router.js index 76513c471..984bf94ec 100644 --- a/UI/Router.js +++ b/UI/Router.js @@ -42,7 +42,7 @@ require( RouterBinder.bind(App.Router); App.navbarRegion.show(new NavbarView()); $('body').addClass('started'); - }) + }); }); return App.Router;