2013-09-10 23:33:47 -07:00
|
|
|
using System;
|
2013-05-12 19:52:55 -07:00
|
|
|
using System.Linq;
|
2013-03-04 22:25:05 -08:00
|
|
|
using System.Threading.Tasks;
|
2013-02-23 12:09:44 -08:00
|
|
|
using NLog;
|
2013-09-10 23:33:47 -07:00
|
|
|
using NzbDrone.Common;
|
2013-05-12 19:52:55 -07:00
|
|
|
using NzbDrone.Common.EnsureThat;
|
|
|
|
using NzbDrone.Common.Serializer;
|
2013-07-11 23:10:34 -07:00
|
|
|
using NzbDrone.Common.TPL;
|
2013-09-13 23:36:07 -07:00
|
|
|
using NzbDrone.Core.Messaging.Commands.Tracking;
|
2013-09-10 23:33:47 -07:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.ProgressMessaging;
|
2013-02-18 18:37:16 -08:00
|
|
|
|
2013-09-13 23:36:07 -07:00
|
|
|
namespace NzbDrone.Core.Messaging.Commands
|
2013-02-18 18:37:16 -08:00
|
|
|
{
|
2013-09-13 23:36:07 -07:00
|
|
|
public class CommandExecutor : ICommandExecutor
|
2013-02-18 18:37:16 -08:00
|
|
|
{
|
2013-02-23 12:09:44 -08:00
|
|
|
private readonly Logger _logger;
|
2013-05-07 22:47:15 -07:00
|
|
|
private readonly IServiceFactory _serviceFactory;
|
2013-08-28 21:43:26 -07:00
|
|
|
private readonly ITrackCommands _trackCommands;
|
2013-09-13 23:36:07 -07:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2013-05-29 18:35:26 -07:00
|
|
|
private readonly TaskFactory _taskFactory;
|
2013-02-18 18:37:16 -08:00
|
|
|
|
2013-09-13 23:36:07 -07:00
|
|
|
public CommandExecutor(Logger logger, IServiceFactory serviceFactory, ITrackCommands trackCommands, IEventAggregator eventAggregator)
|
2013-02-18 18:37:16 -08:00
|
|
|
{
|
2013-09-10 23:33:47 -07:00
|
|
|
var scheduler = new LimitedConcurrencyLevelTaskScheduler(3);
|
|
|
|
|
2013-02-23 12:09:44 -08:00
|
|
|
_logger = logger;
|
2013-05-07 22:47:15 -07:00
|
|
|
_serviceFactory = serviceFactory;
|
2013-08-28 21:43:26 -07:00
|
|
|
_trackCommands = trackCommands;
|
2013-09-13 23:36:07 -07:00
|
|
|
_eventAggregator = eventAggregator;
|
2013-05-29 18:35:26 -07:00
|
|
|
_taskFactory = new TaskFactory(scheduler);
|
2013-02-18 18:37:16 -08:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
public void PublishCommand<TCommand>(TCommand command) where TCommand : Command
|
2013-04-23 18:56:00 -07:00
|
|
|
{
|
2013-05-12 19:52:55 -07:00
|
|
|
Ensure.That(() => command).IsNotNull();
|
|
|
|
|
2013-08-30 20:08:19 -07:00
|
|
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
if (_trackCommands.FindExisting(command) != null)
|
2013-08-30 20:08:19 -07:00
|
|
|
{
|
2013-09-10 23:33:47 -07:00
|
|
|
_logger.Debug("Command is already in progress: {0}", command.GetType().Name);
|
2013-08-30 20:08:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
_trackCommands.Store(command);
|
|
|
|
|
|
|
|
ExecuteCommand<TCommand>(command);
|
2013-08-30 20:08:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void PublishCommand(string commandTypeName)
|
|
|
|
{
|
|
|
|
dynamic command = GetCommand(commandTypeName);
|
|
|
|
PublishCommand(command);
|
|
|
|
}
|
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
public Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command
|
2013-08-30 20:08:19 -07:00
|
|
|
{
|
|
|
|
Ensure.That(() => command).IsNotNull();
|
2013-05-07 22:47:15 -07:00
|
|
|
|
2013-04-26 19:03:34 -07:00
|
|
|
_logger.Trace("Publishing {0}", command.GetType().Name);
|
2013-05-07 22:47:15 -07:00
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
var existingCommand = _trackCommands.FindExisting(command);
|
2013-08-30 20:08:19 -07:00
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
if (existingCommand != null)
|
2013-08-30 20:08:19 -07:00
|
|
|
{
|
2013-09-10 23:33:47 -07:00
|
|
|
_logger.Debug("Command is already in progress: {0}", command.GetType().Name);
|
|
|
|
return existingCommand;
|
2013-08-30 20:08:19 -07:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
_trackCommands.Store(command);
|
|
|
|
|
|
|
|
_taskFactory.StartNew(() => ExecuteCommand<TCommand>(command)
|
2013-08-30 20:08:19 -07:00
|
|
|
, TaskCreationOptions.PreferFairness)
|
|
|
|
.LogExceptions();
|
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
return command;
|
2013-08-30 20:08:19 -07:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
public Command PublishCommandAsync(string commandTypeName)
|
2013-08-30 20:08:19 -07:00
|
|
|
{
|
|
|
|
dynamic command = GetCommand(commandTypeName);
|
|
|
|
return PublishCommandAsync(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
private dynamic GetCommand(string commandTypeName)
|
|
|
|
{
|
2013-09-10 23:33:47 -07:00
|
|
|
var commandType = _serviceFactory.GetImplementations(typeof(Command))
|
2013-08-30 20:08:19 -07:00
|
|
|
.Single(c => c.FullName.Equals(commandTypeName, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
|
|
return Json.Deserialize("{}", commandType);
|
|
|
|
}
|
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
private void ExecuteCommand<TCommand>(Command command) where TCommand : Command
|
2013-08-30 20:08:19 -07:00
|
|
|
{
|
|
|
|
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
2013-05-20 19:49:08 -07:00
|
|
|
var handler = (IExecute<TCommand>)_serviceFactory.Build(handlerContract);
|
2013-05-07 22:47:15 -07:00
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
_logger.Trace("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
2013-07-18 22:05:07 -07:00
|
|
|
|
2013-05-07 22:47:15 -07:00
|
|
|
try
|
|
|
|
{
|
2013-09-10 23:33:47 -07:00
|
|
|
_trackCommands.Start(command);
|
2013-09-13 23:36:07 -07:00
|
|
|
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
|
2013-09-10 23:33:47 -07:00
|
|
|
|
|
|
|
if (!MappedDiagnosticsContext.Contains("CommandId") && command.SendUpdatesToClient)
|
2013-09-03 22:02:26 -07:00
|
|
|
{
|
2013-09-10 23:33:47 -07:00
|
|
|
MappedDiagnosticsContext.Set("CommandId", command.Id.ToString());
|
2013-09-03 22:02:26 -07:00
|
|
|
}
|
2013-08-30 09:18:12 -07:00
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
handler.Execute((TCommand)command);
|
|
|
|
_trackCommands.Completed(command);
|
2013-05-07 22:47:15 -07:00
|
|
|
}
|
2013-05-20 19:49:08 -07:00
|
|
|
catch (Exception e)
|
2013-05-07 22:47:15 -07:00
|
|
|
{
|
2013-09-10 23:33:47 -07:00
|
|
|
_trackCommands.Failed(command, e);
|
2013-05-07 22:47:15 -07:00
|
|
|
throw;
|
|
|
|
}
|
2013-09-13 23:20:10 -07:00
|
|
|
finally
|
|
|
|
{
|
2013-09-14 16:34:21 -07:00
|
|
|
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
|
|
|
|
_eventAggregator.PublishEvent(new CommandExecutedEvent(command));
|
|
|
|
|
2013-09-13 23:20:10 -07:00
|
|
|
if (MappedDiagnosticsContext.Get("CommandId") == command.Id.ToString())
|
|
|
|
{
|
|
|
|
MappedDiagnosticsContext.Remove("CommandId");
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 22:47:15 -07:00
|
|
|
|
2013-09-10 23:33:47 -07:00
|
|
|
_logger.Trace("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, command.Runtime.ToString(""));
|
2013-04-23 18:56:00 -07:00
|
|
|
}
|
2013-02-18 18:37:16 -08:00
|
|
|
}
|
2013-03-04 22:25:05 -08:00
|
|
|
}
|