2013-03-03 23:26:41 +01:00
|
|
|
|
using System;
|
2013-05-08 07:47:15 +02:00
|
|
|
|
using System.Reflection;
|
2013-03-05 07:25:05 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2013-02-23 21:09:44 +01:00
|
|
|
|
using NLog;
|
2013-05-11 01:53:50 +02:00
|
|
|
|
using NzbDrone.Common.Composition;
|
2013-02-19 03:37:16 +01:00
|
|
|
|
|
2013-04-24 03:56:00 +02:00
|
|
|
|
namespace NzbDrone.Common.Messaging
|
2013-02-19 03:37:16 +01:00
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
[Singleton]
|
2013-04-24 03:56:00 +02:00
|
|
|
|
public class MessageAggregator : IMessageAggregator
|
2013-02-19 03:37:16 +01:00
|
|
|
|
{
|
2013-02-23 21:09:44 +01:00
|
|
|
|
private readonly Logger _logger;
|
2013-05-08 07:47:15 +02:00
|
|
|
|
private readonly IServiceFactory _serviceFactory;
|
2013-02-19 03:37:16 +01:00
|
|
|
|
|
2013-05-08 07:47:15 +02:00
|
|
|
|
public MessageAggregator(Logger logger, IServiceFactory serviceFactory)
|
2013-02-19 03:37:16 +01:00
|
|
|
|
{
|
2013-02-23 21:09:44 +01:00
|
|
|
|
_logger = logger;
|
2013-05-08 07:47:15 +02:00
|
|
|
|
_serviceFactory = serviceFactory;
|
2013-02-19 03:37:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 04:03:34 +02:00
|
|
|
|
public void PublishEvent<TEvent>(TEvent @event) where TEvent : IEvent
|
2013-02-19 03:37:16 +01:00
|
|
|
|
{
|
2013-05-11 22:06:57 +02:00
|
|
|
|
var eventName = GetEventName(@event.GetType());
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Publishing {0}", eventName);
|
2013-02-19 03:37:16 +01:00
|
|
|
|
|
2013-03-05 07:25:05 +01:00
|
|
|
|
//call synchronous handlers first.
|
2013-05-08 07:47:15 +02:00
|
|
|
|
foreach (var handler in _serviceFactory.BuildAll<IHandle<TEvent>>())
|
2013-02-19 03:37:16 +01:00
|
|
|
|
{
|
2013-04-27 04:03:34 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-05-11 22:06:57 +02:00
|
|
|
|
_logger.Debug("{0} -> {1}", eventName, handler.GetType().Name);
|
2013-04-27 04:03:34 +02:00
|
|
|
|
handler.Handle(@event);
|
2013-05-11 22:06:57 +02:00
|
|
|
|
_logger.Debug("{0} <- {1}", eventName, handler.GetType().Name);
|
2013-04-27 04:03:34 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-05-11 22:06:57 +02:00
|
|
|
|
_logger.ErrorException(string.Format("{0} failed while processing [{1}]", handler.GetType().Name, eventName), e);
|
2013-04-27 04:03:34 +02:00
|
|
|
|
}
|
2013-03-05 07:25:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-08 07:47:15 +02:00
|
|
|
|
foreach (var handler in _serviceFactory.BuildAll<IHandleAsync<TEvent>>())
|
2013-03-05 07:25:05 +01:00
|
|
|
|
{
|
|
|
|
|
var handlerLocal = handler;
|
|
|
|
|
Task.Factory.StartNew(() =>
|
2013-04-27 04:03:34 +02:00
|
|
|
|
{
|
2013-05-11 22:06:57 +02:00
|
|
|
|
_logger.Debug("{0} ~> {1}", eventName, handlerLocal.GetType().Name);
|
2013-04-27 04:03:34 +02:00
|
|
|
|
handlerLocal.HandleAsync(@event);
|
2013-05-11 22:06:57 +02:00
|
|
|
|
_logger.Debug("{0} <~ {1}", eventName, handlerLocal.GetType().Name);
|
2013-04-27 04:03:34 +02:00
|
|
|
|
});
|
2013-02-19 03:37:16 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-24 03:56:00 +02:00
|
|
|
|
|
2013-04-27 04:03:34 +02:00
|
|
|
|
|
2013-05-11 22:06:57 +02: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-04-27 04:03:34 +02:00
|
|
|
|
public void PublishCommand<TCommand>(TCommand command) where TCommand : ICommand
|
2013-04-24 03:56:00 +02:00
|
|
|
|
{
|
2013-05-08 07:47:15 +02:00
|
|
|
|
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
|
|
|
|
|
2013-04-27 04:03:34 +02:00
|
|
|
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
2013-05-08 07:47:15 +02:00
|
|
|
|
|
|
|
|
|
var handler = _serviceFactory.Build(handlerContract);
|
|
|
|
|
|
2013-04-27 04:03:34 +02:00
|
|
|
|
_logger.Debug("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
2013-05-11 22:06:57 +02:00
|
|
|
|
|
2013-05-08 07:47:15 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-05-12 01:38:41 +02:00
|
|
|
|
handlerContract.GetMethod("Execute").Invoke(handler, new object[] {command});
|
|
|
|
|
PublishEvent(new CommandCompletedEvent(command));
|
2013-05-08 07:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
catch (TargetInvocationException e)
|
|
|
|
|
{
|
2013-05-12 01:38:41 +02:00
|
|
|
|
PublishEvent(new CommandFailedEvent(command, e));
|
|
|
|
|
|
2013-05-08 07:47:15 +02:00
|
|
|
|
if (e.InnerException != null)
|
|
|
|
|
{
|
|
|
|
|
throw e.InnerException;
|
|
|
|
|
}
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-05-12 01:38:41 +02:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
PublishEvent(new CommandExecutedEvent(command));
|
|
|
|
|
}
|
2013-05-08 07:47:15 +02:00
|
|
|
|
|
2013-04-27 04:03:34 +02:00
|
|
|
|
_logger.Debug("{0} <- {1}", command.GetType().Name, handler.GetType().Name);
|
2013-04-24 03:56:00 +02:00
|
|
|
|
}
|
2013-02-19 03:37:16 +01:00
|
|
|
|
}
|
2013-03-05 07:25:05 +01:00
|
|
|
|
}
|