You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
6.1 KiB
177 lines
6.1 KiB
11 years ago
|
using System;
|
||
12 years ago
|
using System.Linq;
|
||
12 years ago
|
using System.Threading.Tasks;
|
||
12 years ago
|
using NLog;
|
||
11 years ago
|
using NzbDrone.Common;
|
||
12 years ago
|
using NzbDrone.Common.EnsureThat;
|
||
11 years ago
|
using NzbDrone.Common.Messaging;
|
||
12 years ago
|
using NzbDrone.Common.Serializer;
|
||
12 years ago
|
using NzbDrone.Common.TPL;
|
||
11 years ago
|
using NzbDrone.Core.Messaging.Commands;
|
||
|
using NzbDrone.Core.Messaging.Events;
|
||
|
using NzbDrone.Core.Messaging.Tracking;
|
||
|
using NzbDrone.Core.ProgressMessaging;
|
||
12 years ago
|
|
||
11 years ago
|
namespace NzbDrone.Core.Messaging
|
||
12 years ago
|
{
|
||
12 years ago
|
public class MessageAggregator : IMessageAggregator
|
||
12 years ago
|
{
|
||
12 years ago
|
private readonly Logger _logger;
|
||
12 years ago
|
private readonly IServiceFactory _serviceFactory;
|
||
11 years ago
|
private readonly ITrackCommands _trackCommands;
|
||
12 years ago
|
private readonly TaskFactory _taskFactory;
|
||
12 years ago
|
|
||
11 years ago
|
public MessageAggregator(Logger logger, IServiceFactory serviceFactory, ITrackCommands trackCommands)
|
||
12 years ago
|
{
|
||
11 years ago
|
var scheduler = new LimitedConcurrencyLevelTaskScheduler(3);
|
||
|
|
||
12 years ago
|
_logger = logger;
|
||
12 years ago
|
_serviceFactory = serviceFactory;
|
||
11 years ago
|
_trackCommands = trackCommands;
|
||
12 years ago
|
_taskFactory = new TaskFactory(scheduler);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
public void PublishEvent<TEvent>(TEvent @event) where TEvent : class ,IEvent
|
||
12 years ago
|
{
|
||
12 years ago
|
Ensure.That(() => @event).IsNotNull();
|
||
|
|
||
12 years ago
|
var eventName = GetEventName(@event.GetType());
|
||
|
|
||
|
_logger.Trace("Publishing {0}", eventName);
|
||
12 years ago
|
|
||
12 years ago
|
//call synchronous handlers first.
|
||
12 years ago
|
foreach (var handler in _serviceFactory.BuildAll<IHandle<TEvent>>())
|
||
12 years ago
|
{
|
||
12 years ago
|
try
|
||
|
{
|
||
11 years ago
|
_logger.Trace("{0} -> {1}", eventName, handler.GetType().Name);
|
||
12 years ago
|
handler.Handle(@event);
|
||
11 years ago
|
_logger.Trace("{0} <- {1}", eventName, handler.GetType().Name);
|
||
12 years ago
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
12 years ago
|
_logger.ErrorException(string.Format("{0} failed while processing [{1}]", handler.GetType().Name, eventName), e);
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
foreach (var handler in _serviceFactory.BuildAll<IHandleAsync<TEvent>>())
|
||
12 years ago
|
{
|
||
|
var handlerLocal = handler;
|
||
12 years ago
|
|
||
|
_taskFactory.StartNew(() =>
|
||
12 years ago
|
{
|
||
11 years ago
|
_logger.Trace("{0} ~> {1}", eventName, handlerLocal.GetType().Name);
|
||
12 years ago
|
handlerLocal.HandleAsync(@event);
|
||
11 years ago
|
_logger.Trace("{0} <~ {1}", eventName, handlerLocal.GetType().Name);
|
||
12 years ago
|
}, TaskCreationOptions.PreferFairness)
|
||
|
.LogExceptions();
|
||
12 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
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);
|
||
|
}
|
||
|
|
||
11 years ago
|
public void PublishCommand<TCommand>(TCommand command) where TCommand : Command
|
||
12 years ago
|
{
|
||
12 years ago
|
Ensure.That(() => command).IsNotNull();
|
||
|
|
||
11 years ago
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
||
|
|
||
11 years ago
|
if (_trackCommands.FindExisting(command) != null)
|
||
11 years ago
|
{
|
||
11 years ago
|
_logger.Debug("Command is already in progress: {0}", command.GetType().Name);
|
||
11 years ago
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
_trackCommands.Store(command);
|
||
|
|
||
|
ExecuteCommand<TCommand>(command);
|
||
11 years ago
|
}
|
||
|
|
||
|
public void PublishCommand(string commandTypeName)
|
||
|
{
|
||
|
dynamic command = GetCommand(commandTypeName);
|
||
|
PublishCommand(command);
|
||
|
}
|
||
|
|
||
11 years ago
|
public Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command
|
||
11 years ago
|
{
|
||
|
Ensure.That(() => command).IsNotNull();
|
||
12 years ago
|
|
||
12 years ago
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
||
12 years ago
|
|
||
11 years ago
|
var existingCommand = _trackCommands.FindExisting(command);
|
||
11 years ago
|
|
||
11 years ago
|
if (existingCommand != null)
|
||
11 years ago
|
{
|
||
11 years ago
|
_logger.Debug("Command is already in progress: {0}", command.GetType().Name);
|
||
|
return existingCommand;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
_trackCommands.Store(command);
|
||
|
|
||
|
_taskFactory.StartNew(() => ExecuteCommand<TCommand>(command)
|
||
11 years ago
|
, TaskCreationOptions.PreferFairness)
|
||
|
.LogExceptions();
|
||
|
|
||
11 years ago
|
return command;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
public Command PublishCommandAsync(string commandTypeName)
|
||
11 years ago
|
{
|
||
|
dynamic command = GetCommand(commandTypeName);
|
||
|
return PublishCommandAsync(command);
|
||
|
}
|
||
|
|
||
|
private dynamic GetCommand(string commandTypeName)
|
||
|
{
|
||
11 years ago
|
var commandType = _serviceFactory.GetImplementations(typeof(Command))
|
||
11 years ago
|
.Single(c => c.FullName.Equals(commandTypeName, StringComparison.InvariantCultureIgnoreCase));
|
||
|
|
||
|
return Json.Deserialize("{}", commandType);
|
||
|
}
|
||
|
|
||
11 years ago
|
private void ExecuteCommand<TCommand>(Command command) where TCommand : Command
|
||
11 years ago
|
{
|
||
|
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
||
12 years ago
|
var handler = (IExecute<TCommand>)_serviceFactory.Build(handlerContract);
|
||
12 years ago
|
|
||
11 years ago
|
_logger.Trace("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
||
12 years ago
|
|
||
12 years ago
|
try
|
||
|
{
|
||
11 years ago
|
_trackCommands.Start(command);
|
||
|
PublishEvent(new CommandUpdatedEvent(command));
|
||
|
|
||
|
if (!MappedDiagnosticsContext.Contains("CommandId") && command.SendUpdatesToClient)
|
||
11 years ago
|
{
|
||
11 years ago
|
MappedDiagnosticsContext.Set("CommandId", command.Id.ToString());
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
handler.Execute((TCommand)command);
|
||
|
_trackCommands.Completed(command);
|
||
|
PublishEvent(new CommandUpdatedEvent(command));
|
||
11 years ago
|
|
||
12 years ago
|
}
|
||
12 years ago
|
catch (Exception e)
|
||
12 years ago
|
{
|
||
11 years ago
|
_trackCommands.Failed(command, e);
|
||
|
PublishEvent(new CommandUpdatedEvent(command));
|
||
12 years ago
|
throw;
|
||
|
}
|
||
|
|
||
11 years ago
|
PublishEvent(new CommandExecutedEvent(command));
|
||
|
PublishEvent(new CommandUpdatedEvent(command));
|
||
|
|
||
|
_logger.Trace("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, command.Runtime.ToString(""));
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|