parent
909439f615
commit
64181ebdff
@ -0,0 +1,10 @@
|
||||
namespace NzbDrone.Core.Messaging.Commands
|
||||
{
|
||||
public interface ICommandExecutor
|
||||
{
|
||||
void PublishCommand<TCommand>(TCommand command) where TCommand : Command;
|
||||
void PublishCommand(string commandTypeName);
|
||||
Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command;
|
||||
Command PublishCommandAsync(string commandTypeName);
|
||||
}
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
|
||||
namespace NzbDrone.Core.Messaging
|
||||
namespace NzbDrone.Core.Messaging.Commands
|
||||
{
|
||||
public interface IExecute<TCommand> : IProcessMessage<TCommand> where TCommand : Command
|
||||
{
|
@ -1,4 +1,4 @@
|
||||
namespace NzbDrone.Core.Messaging.Tracking
|
||||
namespace NzbDrone.Core.Messaging.Commands.Tracking
|
||||
{
|
||||
public enum CommandStatus
|
||||
{
|
@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
|
||||
namespace NzbDrone.Core.Messaging.Tracking
|
||||
namespace NzbDrone.Core.Messaging.Commands.Tracking
|
||||
{
|
||||
public class ExistingCommand
|
||||
{
|
@ -0,0 +1,7 @@
|
||||
namespace NzbDrone.Core.Messaging.Commands.Tracking
|
||||
{
|
||||
public class TrackedCommandCleanupCommand : Command
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
using NzbDrone.Common;
|
||||
using NzbDrone.Common.EnsureThat;
|
||||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Common.TPL;
|
||||
|
||||
namespace NzbDrone.Core.Messaging.Events
|
||||
{
|
||||
public class EventAggregator : IEventAggregator
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
private readonly IServiceFactory _serviceFactory;
|
||||
private readonly TaskFactory _taskFactory;
|
||||
|
||||
public EventAggregator(Logger logger, IServiceFactory serviceFactory)
|
||||
{
|
||||
var scheduler = new LimitedConcurrencyLevelTaskScheduler(3);
|
||||
|
||||
_logger = logger;
|
||||
_serviceFactory = serviceFactory;
|
||||
_taskFactory = new TaskFactory(scheduler);
|
||||
}
|
||||
|
||||
public void PublishEvent<TEvent>(TEvent @event) where TEvent : class ,IEvent
|
||||
{
|
||||
Ensure.That(() => @event).IsNotNull();
|
||||
|
||||
var eventName = GetEventName(@event.GetType());
|
||||
|
||||
_logger.Trace("Publishing {0}", eventName);
|
||||
|
||||
//call synchronous handlers first.
|
||||
foreach (var handler in _serviceFactory.BuildAll<IHandle<TEvent>>())
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.Trace("{0} -> {1}", eventName, handler.GetType().Name);
|
||||
handler.Handle(@event);
|
||||
_logger.Trace("{0} <- {1}", eventName, handler.GetType().Name);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.ErrorException(string.Format("{0} failed while processing [{1}]", handler.GetType().Name, eventName), e);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var handler in _serviceFactory.BuildAll<IHandleAsync<TEvent>>())
|
||||
{
|
||||
var handlerLocal = handler;
|
||||
|
||||
_taskFactory.StartNew(() =>
|
||||
{
|
||||
_logger.Trace("{0} ~> {1}", eventName, handlerLocal.GetType().Name);
|
||||
handlerLocal.HandleAsync(@event);
|
||||
_logger.Trace("{0} <~ {1}", eventName, handlerLocal.GetType().Name);
|
||||
}, TaskCreationOptions.PreferFairness)
|
||||
.LogExceptions();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using NzbDrone.Common.Messaging;
|
||||
|
||||
namespace NzbDrone.Core.Messaging.Events
|
||||
{
|
||||
public interface IEventAggregator
|
||||
{
|
||||
void PublishEvent<TEvent>(TEvent @event) where TEvent : class, IEvent;
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
using NzbDrone.Common.Messaging;
|
||||
|
||||
namespace NzbDrone.Core.Messaging
|
||||
namespace NzbDrone.Core.Messaging.Events
|
||||
{
|
||||
public interface IHandle<TEvent> : IProcessMessage<TEvent> where TEvent : IEvent
|
||||
{
|
@ -1,17 +0,0 @@
|
||||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
|
||||
namespace NzbDrone.Core.Messaging
|
||||
{
|
||||
/// <summary>
|
||||
/// Enables loosely-coupled publication of events.
|
||||
/// </summary>
|
||||
public interface IMessageAggregator
|
||||
{
|
||||
void PublishEvent<TEvent>(TEvent @event) where TEvent : class, IEvent;
|
||||
void PublishCommand<TCommand>(TCommand command) where TCommand : Command;
|
||||
void PublishCommand(string commandTypeName);
|
||||
Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command;
|
||||
Command PublishCommandAsync(string commandTypeName);
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
using System;
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
|
||||
namespace NzbDrone.Core.Messaging
|
||||
{
|
||||
public static class MessageExtensions
|
||||
{
|
||||
public static string GetExecutorName(this Type commandType)
|
||||
{
|
||||
if (!typeof(Command).IsAssignableFrom(commandType))
|
||||
{
|
||||
throw new ArgumentException("commandType must implement ICommand");
|
||||
}
|
||||
|
||||
return string.Format("I{0}Executor", commandType.Name);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
|
||||
namespace NzbDrone.Core.Messaging.Tracking
|
||||
{
|
||||
public class TrackedCommandCleanupCommand : Command
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue