1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-26 01:27:00 +00:00

Added ProgressMessaging through nlog

This commit is contained in:
Mark McDowall 2013-08-30 09:18:12 -07:00
parent 3c632743a1
commit 780e374122
9 changed files with 128 additions and 12 deletions

View file

@ -9,6 +9,7 @@
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 @@ protected override void ApplicationStartup(TinyIoCContainer container, IPipeline
{
_logger.Info("Starting NzbDrone API");
RegisterPipelines(pipelines);
container.Resolve<DatabaseTarget>().Register();
container.Resolve<IEnableBasicAuthInNancy>().Register(pipelines);
container.Resolve<IMessageAggregator>().PublishEvent(new ApplicationStartedEvent());
ApplicationPipelines.OnError.AddItemToEndOfPipeline(container.Resolve<NzbDroneErrorPipeline>().HandleException);
}
@ -47,10 +46,8 @@ private void RegisterPipelines(IPipelines pipelines)
{
registerNancyPipeline.Register(pipelines);
}
}
protected override TinyIoCContainer GetApplicationContainer()
{
return _tinyIoCContainer;

View file

@ -87,30 +87,32 @@ public void PublishCommand<TCommand>(TCommand command) where TCommand : class, I
_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));

View file

@ -80,6 +80,7 @@ public class ParserFixture : CoreTest
[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);

View file

@ -6,5 +6,4 @@ public class ApplicationStartedEvent : IEvent
{
}
}

View file

@ -219,6 +219,9 @@
<Compile Include="Instrumentation\Commands\DeleteLogFilesCommand.cs" />
<Compile Include="Instrumentation\Commands\TrimLogCommand.cs" />
<Compile Include="Instrumentation\DeleteLogFilesService.cs" />
<Compile Include="ProgressMessaging\NewProgressMessageEvent.cs" />
<Compile Include="ProgressMessaging\ProgressMessage.cs" />
<Compile Include="ProgressMessaging\ProgressMessagingTarget.cs" />
<Compile Include="Instrumentation\SetLoggingLevel.cs" />
<Compile Include="Jobs\TaskManager.cs" />
<Compile Include="Lifecycle\ApplicationShutdownRequested.cs" />

View file

@ -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;
}
}
}

View file

@ -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; }
}
}

View file

@ -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<ApplicationStartedEvent>, IHandle<ApplicationShutdownRequested>
{
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();
}
}
}
}

View file

@ -42,7 +42,7 @@ require(
RouterBinder.bind(App.Router);
App.navbarRegion.show(new NavbarView());
$('body').addClass('started');
})
});
});
return App.Router;