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.
133 lines
4.6 KiB
133 lines
4.6 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;
|
||
|
using NzbDrone.Common.Serializer;
|
||
11 years ago
|
using NzbDrone.Common.TPL;
|
||
11 years ago
|
using NzbDrone.Core.Messaging.Commands.Tracking;
|
||
11 years ago
|
using NzbDrone.Core.Messaging.Events;
|
||
|
using NzbDrone.Core.ProgressMessaging;
|
||
12 years ago
|
|
||
11 years ago
|
namespace NzbDrone.Core.Messaging.Commands
|
||
12 years ago
|
{
|
||
11 years ago
|
public class CommandExecutor : ICommandExecutor
|
||
12 years ago
|
{
|
||
12 years ago
|
private readonly Logger _logger;
|
||
12 years ago
|
private readonly IServiceFactory _serviceFactory;
|
||
11 years ago
|
private readonly ITrackCommands _trackCommands;
|
||
11 years ago
|
private readonly IEventAggregator _eventAggregator;
|
||
12 years ago
|
private readonly TaskFactory _taskFactory;
|
||
12 years ago
|
|
||
11 years ago
|
public CommandExecutor(Logger logger, IServiceFactory serviceFactory, ITrackCommands trackCommands, IEventAggregator eventAggregator)
|
||
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;
|
||
11 years ago
|
_eventAggregator = eventAggregator;
|
||
12 years ago
|
_taskFactory = new TaskFactory(scheduler);
|
||
12 years ago
|
}
|
||
|
|
||
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);
|
||
11 years ago
|
|
||
12 years ago
|
try
|
||
|
{
|
||
11 years ago
|
_trackCommands.Start(command);
|
||
11 years ago
|
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
|
||
11 years ago
|
|
||
|
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);
|
||
12 years ago
|
}
|
||
12 years ago
|
catch (Exception e)
|
||
12 years ago
|
{
|
||
11 years ago
|
_trackCommands.Failed(command, e);
|
||
12 years ago
|
throw;
|
||
|
}
|
||
11 years ago
|
finally
|
||
|
{
|
||
|
if (MappedDiagnosticsContext.Get("CommandId") == command.Id.ToString())
|
||
|
{
|
||
|
MappedDiagnosticsContext.Remove("CommandId");
|
||
|
}
|
||
11 years ago
|
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
|
||
|
_eventAggregator.PublishEvent(new CommandExecutedEvent(command));
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
11 years ago
|
_logger.Trace("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, command.Runtime.ToString(""));
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|