2013-03-03 22:26:41 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-02-19 02:37:16 +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-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-04-24 01:56:00 +00:00
|
|
|
|
private readonly Func<IEnumerable<IProcessMessage>> _handlers;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
|
2013-04-24 01:56:00 +00:00
|
|
|
|
public MessageAggregator(Logger logger, Func<IEnumerable<IProcessMessage>> handlers)
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-02-23 20:09:44 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_handlers = handlers;
|
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-03-03 22:26:41 +00:00
|
|
|
|
foreach (var handler in _handlers().OfType<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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var handler in _handlers().OfType<IHandleAsync<TEvent>>())
|
|
|
|
|
{
|
|
|
|
|
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-04-27 02:03:34 +00:00
|
|
|
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
|
|
|
|
var handler = _handlers().OfType<IExecute<TCommand>>().Single();
|
|
|
|
|
_logger.Debug("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
|
|
|
|
handler.Execute(command);
|
|
|
|
|
_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
|
|
|
|
}
|