2013-03-03 22:26:41 +00:00
|
|
|
|
using System;
|
2013-07-19 05:05:07 +00:00
|
|
|
|
using System.Diagnostics;
|
2013-05-13 02:52:55 +00:00
|
|
|
|
using System.Linq;
|
2013-03-05 06:25:05 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2013-02-23 20:09:44 +00:00
|
|
|
|
using NLog;
|
2013-05-13 02:52:55 +00:00
|
|
|
|
using NzbDrone.Common.EnsureThat;
|
2013-08-28 06:51:42 +00:00
|
|
|
|
using NzbDrone.Common.Messaging.Events;
|
2013-08-29 04:43:26 +00:00
|
|
|
|
using NzbDrone.Common.Messaging.Tracking;
|
2013-05-13 02:52:55 +00:00
|
|
|
|
using NzbDrone.Common.Serializer;
|
2013-07-12 06:10:34 +00:00
|
|
|
|
using NzbDrone.Common.TPL;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
|
2013-04-24 01:56:00 +00:00
|
|
|
|
namespace NzbDrone.Common.Messaging
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-04-24 01:56:00 +00:00
|
|
|
|
public class MessageAggregator : IMessageAggregator
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-02-23 20:09:44 +00:00
|
|
|
|
private readonly Logger _logger;
|
2013-05-08 05:47:15 +00:00
|
|
|
|
private readonly IServiceFactory _serviceFactory;
|
2013-08-29 04:43:26 +00:00
|
|
|
|
private readonly ITrackCommands _trackCommands;
|
2013-05-30 01:35:26 +00:00
|
|
|
|
private readonly TaskFactory _taskFactory;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
|
2013-08-29 04:43:26 +00:00
|
|
|
|
public MessageAggregator(Logger logger, IServiceFactory serviceFactory, ITrackCommands trackCommands)
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-02-23 20:09:44 +00:00
|
|
|
|
_logger = logger;
|
2013-05-08 05:47:15 +00:00
|
|
|
|
_serviceFactory = serviceFactory;
|
2013-08-29 04:43:26 +00:00
|
|
|
|
_trackCommands = trackCommands;
|
2013-05-30 01:35:26 +00:00
|
|
|
|
var scheduler = new LimitedConcurrencyLevelTaskScheduler(2);
|
|
|
|
|
_taskFactory = new TaskFactory(scheduler);
|
2013-02-19 02:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 02:52:55 +00:00
|
|
|
|
public void PublishEvent<TEvent>(TEvent @event) where TEvent : class ,IEvent
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-05-13 02:52:55 +00:00
|
|
|
|
Ensure.That(() => @event).IsNotNull();
|
|
|
|
|
|
2013-05-11 20:06:57 +00:00
|
|
|
|
var eventName = GetEventName(@event.GetType());
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Publishing {0}", eventName);
|
2013-02-19 02:37:16 +00:00
|
|
|
|
|
2013-03-05 06:25:05 +00:00
|
|
|
|
//call synchronous handlers first.
|
2013-05-08 05:47:15 +00:00
|
|
|
|
foreach (var handler in _serviceFactory.BuildAll<IHandle<TEvent>>())
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-04-27 02:03:34 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-05-11 20:06:57 +00:00
|
|
|
|
_logger.Debug("{0} -> {1}", eventName, handler.GetType().Name);
|
2013-04-27 02:03:34 +00:00
|
|
|
|
handler.Handle(@event);
|
2013-05-11 20:06:57 +00:00
|
|
|
|
_logger.Debug("{0} <- {1}", eventName, handler.GetType().Name);
|
2013-04-27 02:03:34 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-05-11 20:06:57 +00:00
|
|
|
|
_logger.ErrorException(string.Format("{0} failed while processing [{1}]", handler.GetType().Name, eventName), e);
|
2013-04-27 02:03:34 +00:00
|
|
|
|
}
|
2013-03-05 06:25:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
foreach (var handler in _serviceFactory.BuildAll<IHandleAsync<TEvent>>())
|
2013-03-05 06:25:05 +00:00
|
|
|
|
{
|
|
|
|
|
var handlerLocal = handler;
|
2013-05-30 01:35:26 +00:00
|
|
|
|
|
|
|
|
|
_taskFactory.StartNew(() =>
|
2013-04-27 02:03:34 +00:00
|
|
|
|
{
|
2013-05-11 20:06:57 +00:00
|
|
|
|
_logger.Debug("{0} ~> {1}", eventName, handlerLocal.GetType().Name);
|
2013-04-27 02:03:34 +00:00
|
|
|
|
handlerLocal.HandleAsync(@event);
|
2013-05-11 20:06:57 +00:00
|
|
|
|
_logger.Debug("{0} <~ {1}", eventName, handlerLocal.GetType().Name);
|
2013-07-12 06:10:34 +00:00
|
|
|
|
}, TaskCreationOptions.PreferFairness)
|
|
|
|
|
.LogExceptions();
|
2013-02-19 02:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-24 01:56:00 +00:00
|
|
|
|
|
2013-05-11 20:06:57 +00:00
|
|
|
|
private static string GetEventName(Type eventType)
|
|
|
|
|
{
|
|
|
|
|
if (!eventType.IsGenericType)
|
|
|
|
|
{
|
|
|
|
|
return eventType.Name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Format("{0}<{1}>", eventType.Name.Remove(eventType.Name.IndexOf('`')), eventType.GetGenericArguments()[0].Name);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 02:52:55 +00:00
|
|
|
|
public void PublishCommand<TCommand>(TCommand command) where TCommand : class, ICommand
|
2013-04-24 01:56:00 +00:00
|
|
|
|
{
|
2013-05-13 02:52:55 +00:00
|
|
|
|
Ensure.That(() => command).IsNotNull();
|
|
|
|
|
|
2013-08-31 03:08:19 +00:00
|
|
|
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
|
|
|
|
|
|
|
|
|
var trackedCommand = _trackCommands.TrackIfNew(command);
|
|
|
|
|
|
|
|
|
|
if (trackedCommand == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Command is already in progress: {0}", command.GetType().Name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExecuteCommand<TCommand>(trackedCommand);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void PublishCommand(string commandTypeName)
|
|
|
|
|
{
|
|
|
|
|
dynamic command = GetCommand(commandTypeName);
|
|
|
|
|
PublishCommand(command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TrackedCommand PublishCommandAsync<TCommand>(TCommand command) where TCommand : class, ICommand
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(() => command).IsNotNull();
|
2013-05-08 05:47:15 +00:00
|
|
|
|
|
2013-04-27 02:03:34 +00:00
|
|
|
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
2013-05-08 05:47:15 +00:00
|
|
|
|
|
2013-08-31 03:08:19 +00:00
|
|
|
|
var existingCommand = _trackCommands.TrackNewOrGet(command);
|
|
|
|
|
|
|
|
|
|
if (existingCommand.Existing)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Command is already in progress: {0}", command.GetType().Name);
|
|
|
|
|
return existingCommand.TrackedCommand;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_taskFactory.StartNew(() => ExecuteCommand<TCommand>(existingCommand.TrackedCommand)
|
|
|
|
|
, TaskCreationOptions.PreferFairness)
|
|
|
|
|
.LogExceptions();
|
|
|
|
|
|
|
|
|
|
return existingCommand.TrackedCommand;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TrackedCommand PublishCommandAsync(string commandTypeName)
|
|
|
|
|
{
|
|
|
|
|
dynamic command = GetCommand(commandTypeName);
|
|
|
|
|
return PublishCommandAsync(command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private dynamic GetCommand(string commandTypeName)
|
|
|
|
|
{
|
|
|
|
|
var commandType = _serviceFactory.GetImplementations(typeof(ICommand))
|
|
|
|
|
.Single(c => c.FullName.Equals(commandTypeName, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
|
|
|
|
return Json.Deserialize("{}", commandType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ExecuteCommand<TCommand>(TrackedCommand trackedCommand) where TCommand : class, ICommand
|
|
|
|
|
{
|
|
|
|
|
var command = (TCommand)trackedCommand.Command;
|
|
|
|
|
|
|
|
|
|
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
2013-05-21 02:49:08 +00:00
|
|
|
|
var handler = (IExecute<TCommand>)_serviceFactory.Build(handlerContract);
|
2013-05-08 05:47:15 +00:00
|
|
|
|
|
2013-04-27 02:03:34 +00:00
|
|
|
|
_logger.Debug("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
2013-05-11 20:06:57 +00:00
|
|
|
|
|
2013-07-19 05:05:07 +00:00
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-08-30 16:18:12 +00:00
|
|
|
|
MappedDiagnosticsContext.Set("CommandId", trackedCommand.Command.CommandId);
|
|
|
|
|
|
2013-08-31 03:08:19 +00:00
|
|
|
|
PublishEvent(new CommandStartedEvent(trackedCommand));
|
2013-05-21 02:49:08 +00:00
|
|
|
|
handler.Execute(command);
|
2013-07-19 05:05:07 +00:00
|
|
|
|
sw.Stop();
|
2013-08-29 04:43:26 +00:00
|
|
|
|
|
2013-08-30 16:18:12 +00:00
|
|
|
|
_trackCommands.Completed(trackedCommand, sw.Elapsed);
|
2013-08-31 03:08:19 +00:00
|
|
|
|
PublishEvent(new CommandCompletedEvent(trackedCommand));
|
2013-05-08 05:47:15 +00:00
|
|
|
|
}
|
2013-05-21 02:49:08 +00:00
|
|
|
|
catch (Exception e)
|
2013-05-08 05:47:15 +00:00
|
|
|
|
{
|
2013-08-31 03:08:19 +00:00
|
|
|
|
_trackCommands.Failed(trackedCommand, e);
|
|
|
|
|
PublishEvent(new CommandFailedEvent(trackedCommand, e));
|
2013-05-08 05:47:15 +00:00
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-05-11 23:38:41 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
2013-08-31 03:08:19 +00:00
|
|
|
|
PublishEvent(new CommandExecutedEvent(trackedCommand));
|
2013-05-11 23:38:41 +00:00
|
|
|
|
}
|
2013-05-08 05:47:15 +00:00
|
|
|
|
|
2013-07-19 05:05:07 +00:00
|
|
|
|
_logger.Debug("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, sw.Elapsed.ToString(""));
|
2013-04-24 01:56:00 +00:00
|
|
|
|
}
|
2013-02-19 02:37:16 +00:00
|
|
|
|
}
|
2013-03-05 06:25:05 +00:00
|
|
|
|
}
|