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.
Prowlarr/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs

133 lines
4.6 KiB

11 years ago
using System;
using System.Linq;
using System.Threading.Tasks;
using NLog;
11 years ago
using NzbDrone.Common;
using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.Serializer;
using NzbDrone.Common.TPL;
using NzbDrone.Core.Messaging.Commands.Tracking;
11 years ago
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.ProgressMessaging;
namespace NzbDrone.Core.Messaging.Commands
{
public class CommandExecutor : ICommandExecutor
{
private readonly Logger _logger;
private readonly IServiceFactory _serviceFactory;
private readonly ITrackCommands _trackCommands;
private readonly IEventAggregator _eventAggregator;
private readonly TaskFactory _taskFactory;
public CommandExecutor(Logger logger, IServiceFactory serviceFactory, ITrackCommands trackCommands, IEventAggregator eventAggregator)
{
11 years ago
var scheduler = new LimitedConcurrencyLevelTaskScheduler(3);
_logger = logger;
_serviceFactory = serviceFactory;
_trackCommands = trackCommands;
_eventAggregator = eventAggregator;
_taskFactory = new TaskFactory(scheduler);
}
11 years ago
public void PublishCommand<TCommand>(TCommand command) where TCommand : Command
{
Ensure.That(() => command).IsNotNull();
_logger.Trace("Publishing {0}", command.GetType().Name);
11 years ago
if (_trackCommands.FindExisting(command) != null)
{
11 years ago
_logger.Debug("Command is already in progress: {0}", command.GetType().Name);
return;
}
11 years ago
_trackCommands.Store(command);
ExecuteCommand<TCommand>(command);
}
public void PublishCommand(string commandTypeName)
{
dynamic command = GetCommand(commandTypeName);
PublishCommand(command);
}
11 years ago
public Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command
{
Ensure.That(() => command).IsNotNull();
_logger.Trace("Publishing {0}", command.GetType().Name);
11 years ago
var existingCommand = _trackCommands.FindExisting(command);
11 years ago
if (existingCommand != null)
{
11 years ago
_logger.Debug("Command is already in progress: {0}", command.GetType().Name);
return existingCommand;
}
11 years ago
_trackCommands.Store(command);
_taskFactory.StartNew(() => ExecuteCommand<TCommand>(command)
, TaskCreationOptions.PreferFairness)
.LogExceptions();
11 years ago
return command;
}
11 years ago
public Command PublishCommandAsync(string commandTypeName)
{
dynamic command = GetCommand(commandTypeName);
return PublishCommandAsync(command);
}
private dynamic GetCommand(string commandTypeName)
{
11 years ago
var commandType = _serviceFactory.GetImplementations(typeof(Command))
.Single(c => c.FullName.Equals(commandTypeName, StringComparison.InvariantCultureIgnoreCase));
return Json.Deserialize("{}", commandType);
}
11 years ago
private void ExecuteCommand<TCommand>(Command command) where TCommand : Command
{
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
var handler = (IExecute<TCommand>)_serviceFactory.Build(handlerContract);
11 years ago
_logger.Trace("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
try
{
11 years ago
_trackCommands.Start(command);
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
11 years ago
if (!MappedDiagnosticsContext.Contains("CommandId") && command.SendUpdatesToClient)
{
11 years ago
MappedDiagnosticsContext.Set("CommandId", command.Id.ToString());
}
11 years ago
handler.Execute((TCommand)command);
_trackCommands.Completed(command);
}
catch (Exception e)
{
11 years ago
_trackCommands.Failed(command, e);
throw;
}
finally
{
if (MappedDiagnosticsContext.Get("CommandId") == command.Id.ToString())
{
MappedDiagnosticsContext.Remove("CommandId");
}
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
_eventAggregator.PublishEvent(new CommandExecutedEvent(command));
}
11 years ago
_logger.Trace("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, command.Runtime.ToString(""));
}
}
}