2013-09-11 09:33:47 +03:00
|
|
|
using System;
|
2013-05-13 05:52:55 +03:00
|
|
|
using System.Linq;
|
2013-03-05 09:25:05 +03:00
|
|
|
using System.Threading.Tasks;
|
2013-02-23 23:09:44 +03:00
|
|
|
using NLog;
|
2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.Common;
|
2013-05-13 05:52:55 +03:00
|
|
|
using NzbDrone.Common.EnsureThat;
|
2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-05-13 05:52:55 +03:00
|
|
|
using NzbDrone.Common.Serializer;
|
2013-07-12 09:10:34 +03:00
|
|
|
using NzbDrone.Common.TPL;
|
2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Tracking;
|
|
|
|
using NzbDrone.Core.ProgressMessaging;
|
2013-02-19 05:37:16 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
namespace NzbDrone.Core.Messaging
|
2013-02-19 05:37:16 +03:00
|
|
|
{
|
2013-04-24 04:56:00 +03:00
|
|
|
public class MessageAggregator : IMessageAggregator
|
2013-02-19 05:37:16 +03:00
|
|
|
{
|
2013-02-23 23:09:44 +03:00
|
|
|
private readonly Logger _logger;
|
2013-05-08 08:47:15 +03:00
|
|
|
private readonly IServiceFactory _serviceFactory;
|
2013-08-29 07:43:26 +03:00
|
|
|
private readonly ITrackCommands _trackCommands;
|
2013-05-30 04:35:26 +03:00
|
|
|
private readonly TaskFactory _taskFactory;
|
2013-02-19 05:37:16 +03:00
|
|
|
|
2013-08-29 07:43:26 +03:00
|
|
|
public MessageAggregator(Logger logger, IServiceFactory serviceFactory, ITrackCommands trackCommands)
|
2013-02-19 05:37:16 +03:00
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
var scheduler = new LimitedConcurrencyLevelTaskScheduler(3);
|
|
|
|
|
2013-02-23 23:09:44 +03:00
|
|
|
_logger = logger;
|
2013-05-08 08:47:15 +03:00
|
|
|
_serviceFactory = serviceFactory;
|
2013-08-29 07:43:26 +03:00
|
|
|
_trackCommands = trackCommands;
|
2013-05-30 04:35:26 +03:00
|
|
|
_taskFactory = new TaskFactory(scheduler);
|
2013-02-19 05:37:16 +03:00
|
|
|
}
|
|
|
|
|
2013-05-13 05:52:55 +03:00
|
|
|
public void PublishEvent<TEvent>(TEvent @event) where TEvent : class ,IEvent
|
2013-02-19 05:37:16 +03:00
|
|
|
{
|
2013-05-13 05:52:55 +03:00
|
|
|
Ensure.That(() => @event).IsNotNull();
|
|
|
|
|
2013-05-11 23:06:57 +03:00
|
|
|
var eventName = GetEventName(@event.GetType());
|
|
|
|
|
|
|
|
_logger.Trace("Publishing {0}", eventName);
|
2013-02-19 05:37:16 +03:00
|
|
|
|
2013-03-05 09:25:05 +03:00
|
|
|
//call synchronous handlers first.
|
2013-05-08 08:47:15 +03:00
|
|
|
foreach (var handler in _serviceFactory.BuildAll<IHandle<TEvent>>())
|
2013-02-19 05:37:16 +03:00
|
|
|
{
|
2013-04-27 05:03:34 +03:00
|
|
|
try
|
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.Trace("{0} -> {1}", eventName, handler.GetType().Name);
|
2013-04-27 05:03:34 +03:00
|
|
|
handler.Handle(@event);
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.Trace("{0} <- {1}", eventName, handler.GetType().Name);
|
2013-04-27 05:03:34 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2013-05-11 23:06:57 +03:00
|
|
|
_logger.ErrorException(string.Format("{0} failed while processing [{1}]", handler.GetType().Name, eventName), e);
|
2013-04-27 05:03:34 +03:00
|
|
|
}
|
2013-03-05 09:25:05 +03:00
|
|
|
}
|
|
|
|
|
2013-05-08 08:47:15 +03:00
|
|
|
foreach (var handler in _serviceFactory.BuildAll<IHandleAsync<TEvent>>())
|
2013-03-05 09:25:05 +03:00
|
|
|
{
|
|
|
|
var handlerLocal = handler;
|
2013-05-30 04:35:26 +03:00
|
|
|
|
|
|
|
_taskFactory.StartNew(() =>
|
2013-04-27 05:03:34 +03:00
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.Trace("{0} ~> {1}", eventName, handlerLocal.GetType().Name);
|
2013-04-27 05:03:34 +03:00
|
|
|
handlerLocal.HandleAsync(@event);
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.Trace("{0} <~ {1}", eventName, handlerLocal.GetType().Name);
|
2013-07-12 09:10:34 +03:00
|
|
|
}, TaskCreationOptions.PreferFairness)
|
|
|
|
.LogExceptions();
|
2013-02-19 05:37:16 +03:00
|
|
|
}
|
|
|
|
}
|
2013-04-24 04:56:00 +03:00
|
|
|
|
2013-05-11 23:06:57 +03: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-09-11 09:33:47 +03:00
|
|
|
public void PublishCommand<TCommand>(TCommand command) where TCommand : Command
|
2013-04-24 04:56:00 +03:00
|
|
|
{
|
2013-05-13 05:52:55 +03:00
|
|
|
Ensure.That(() => command).IsNotNull();
|
|
|
|
|
2013-08-31 06:08:19 +03:00
|
|
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
if (_trackCommands.FindExisting(command) != null)
|
2013-08-31 06:08:19 +03:00
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.Debug("Command is already in progress: {0}", command.GetType().Name);
|
2013-08-31 06:08:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
_trackCommands.Store(command);
|
|
|
|
|
|
|
|
ExecuteCommand<TCommand>(command);
|
2013-08-31 06:08:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void PublishCommand(string commandTypeName)
|
|
|
|
{
|
|
|
|
dynamic command = GetCommand(commandTypeName);
|
|
|
|
PublishCommand(command);
|
|
|
|
}
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
public Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command
|
2013-08-31 06:08:19 +03:00
|
|
|
{
|
|
|
|
Ensure.That(() => command).IsNotNull();
|
2013-05-08 08:47:15 +03:00
|
|
|
|
2013-04-27 05:03:34 +03:00
|
|
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
2013-05-08 08:47:15 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
var existingCommand = _trackCommands.FindExisting(command);
|
2013-08-31 06:08:19 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
if (existingCommand != null)
|
2013-08-31 06:08:19 +03:00
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.Debug("Command is already in progress: {0}", command.GetType().Name);
|
|
|
|
return existingCommand;
|
2013-08-31 06:08:19 +03:00
|
|
|
}
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
_trackCommands.Store(command);
|
|
|
|
|
|
|
|
_taskFactory.StartNew(() => ExecuteCommand<TCommand>(command)
|
2013-08-31 06:08:19 +03:00
|
|
|
, TaskCreationOptions.PreferFairness)
|
|
|
|
.LogExceptions();
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
return command;
|
2013-08-31 06:08:19 +03:00
|
|
|
}
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
public Command PublishCommandAsync(string commandTypeName)
|
2013-08-31 06:08:19 +03:00
|
|
|
{
|
|
|
|
dynamic command = GetCommand(commandTypeName);
|
|
|
|
return PublishCommandAsync(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
private dynamic GetCommand(string commandTypeName)
|
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
var commandType = _serviceFactory.GetImplementations(typeof(Command))
|
2013-08-31 06:08:19 +03:00
|
|
|
.Single(c => c.FullName.Equals(commandTypeName, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
|
|
return Json.Deserialize("{}", commandType);
|
|
|
|
}
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
private void ExecuteCommand<TCommand>(Command command) where TCommand : Command
|
2013-08-31 06:08:19 +03:00
|
|
|
{
|
|
|
|
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
2013-05-21 05:49:08 +03:00
|
|
|
var handler = (IExecute<TCommand>)_serviceFactory.Build(handlerContract);
|
2013-05-08 08:47:15 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.Trace("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
2013-07-19 08:05:07 +03:00
|
|
|
|
2013-05-08 08:47:15 +03:00
|
|
|
try
|
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
_trackCommands.Start(command);
|
|
|
|
PublishEvent(new CommandUpdatedEvent(command));
|
|
|
|
|
|
|
|
if (!MappedDiagnosticsContext.Contains("CommandId") && command.SendUpdatesToClient)
|
2013-09-04 08:02:26 +03:00
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
MappedDiagnosticsContext.Set("CommandId", command.Id.ToString());
|
2013-09-04 08:02:26 +03:00
|
|
|
}
|
2013-08-30 19:18:12 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
handler.Execute((TCommand)command);
|
|
|
|
_trackCommands.Completed(command);
|
|
|
|
PublishEvent(new CommandUpdatedEvent(command));
|
2013-08-29 07:43:26 +03:00
|
|
|
|
2013-05-08 08:47:15 +03:00
|
|
|
}
|
2013-05-21 05:49:08 +03:00
|
|
|
catch (Exception e)
|
2013-05-08 08:47:15 +03:00
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
_trackCommands.Failed(command, e);
|
|
|
|
PublishEvent(new CommandUpdatedEvent(command));
|
2013-05-08 08:47:15 +03:00
|
|
|
throw;
|
|
|
|
}
|
2013-09-14 09:20:10 +03:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (MappedDiagnosticsContext.Get("CommandId") == command.Id.ToString())
|
|
|
|
{
|
|
|
|
MappedDiagnosticsContext.Remove("CommandId");
|
|
|
|
}
|
|
|
|
}
|
2013-05-08 08:47:15 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
PublishEvent(new CommandExecutedEvent(command));
|
|
|
|
PublishEvent(new CommandUpdatedEvent(command));
|
|
|
|
|
|
|
|
_logger.Trace("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, command.Runtime.ToString(""));
|
2013-04-24 04:56:00 +03:00
|
|
|
}
|
2013-02-19 05:37:16 +03:00
|
|
|
}
|
2013-03-05 09:25:05 +03:00
|
|
|
}
|