2013-03-03 22:26:41 +00:00
|
|
|
|
using System;
|
2013-05-08 05:47:15 +00:00
|
|
|
|
using System.Reflection;
|
2013-03-05 06:25:05 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2013-02-23 20:09:44 +00:00
|
|
|
|
using NLog;
|
2013-05-10 23:53:50 +00:00
|
|
|
|
using NzbDrone.Common.Composition;
|
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-05-10 23:53:50 +00:00
|
|
|
|
[Singleton]
|
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-02-19 02:37:16 +00:00
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
public MessageAggregator(Logger logger, IServiceFactory serviceFactory)
|
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-02-19 02:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 02:03:34 +00:00
|
|
|
|
public void PublishEvent<TEvent>(TEvent @event) where TEvent : IEvent
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-04-27 02:03:34 +00:00
|
|
|
|
_logger.Trace("Publishing {0}", @event.GetType().Name);
|
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
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("{0} -> {1}", @event.GetType().Name, handler.GetType().Name);
|
|
|
|
|
handler.Handle(@event);
|
|
|
|
|
_logger.Debug("{0} <- {1}", @event.GetType().Name, handler.GetType().Name);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException(string.Format("{0} failed while processing [{1}]", handler.GetType().Name, @event.GetType().Name), e);
|
|
|
|
|
}
|
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;
|
|
|
|
|
Task.Factory.StartNew(() =>
|
2013-04-27 02:03:34 +00:00
|
|
|
|
{
|
|
|
|
|
_logger.Debug("{0} ~> {1}", @event.GetType().Name, handlerLocal.GetType().Name);
|
|
|
|
|
handlerLocal.HandleAsync(@event);
|
|
|
|
|
_logger.Debug("{0} <~ {1}", @event.GetType().Name, handlerLocal.GetType().Name);
|
|
|
|
|
});
|
2013-02-19 02:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-24 01:56:00 +00:00
|
|
|
|
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
|
|
|
|
public void PublishCommand<TCommand>(TCommand command) where TCommand : ICommand
|
2013-04-24 01:56:00 +00:00
|
|
|
|
{
|
2013-05-08 05:47:15 +00:00
|
|
|
|
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
|
|
|
|
|
2013-04-27 02:03:34 +00:00
|
|
|
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
2013-05-08 05:47:15 +00:00
|
|
|
|
|
|
|
|
|
var handler = _serviceFactory.Build(handlerContract);
|
|
|
|
|
|
2013-04-27 02:03:34 +00:00
|
|
|
|
_logger.Debug("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
2013-05-08 05:47:15 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
handlerContract.GetMethod("Execute").Invoke(handler, new object[] { command });
|
|
|
|
|
}
|
|
|
|
|
catch (TargetInvocationException e)
|
|
|
|
|
{
|
|
|
|
|
if (e.InnerException != null)
|
|
|
|
|
{
|
|
|
|
|
throw e.InnerException;
|
|
|
|
|
}
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 02:03:34 +00:00
|
|
|
|
_logger.Debug("{0} <- {1}", command.GetType().Name, handler.GetType().Name);
|
2013-04-24 01:56:00 +00:00
|
|
|
|
}
|
2013-02-19 02:37:16 +00:00
|
|
|
|
}
|
2013-03-05 06:25:05 +00:00
|
|
|
|
}
|