2013-09-11 06:33:47 +00:00
|
|
|
using System;
|
2015-01-16 00:30:09 +00:00
|
|
|
using System.Threading;
|
2013-02-23 20:09:44 +00:00
|
|
|
using NLog;
|
2013-09-11 06:33:47 +00:00
|
|
|
using NzbDrone.Common;
|
2015-01-16 00:30:09 +00:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-09-11 06:33:47 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.ProgressMessaging;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
2013-09-14 06:36:07 +00:00
|
|
|
namespace NzbDrone.Core.Messaging.Commands
|
2013-02-19 02:37:16 +00:00
|
|
|
{
|
2015-01-16 00:30:09 +00:00
|
|
|
public class CommandExecutor : IHandle<ApplicationStartedEvent>,
|
|
|
|
IHandle<ApplicationShutdownRequested>
|
2013-02-19 02:37:16 +00:00
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
private const int THREAD_LIMIT = 3;
|
|
|
|
|
2013-02-23 20:09:44 +00:00
|
|
|
private readonly Logger _logger;
|
2013-05-08 05:47:15 +00:00
|
|
|
private readonly IServiceFactory _serviceFactory;
|
2015-01-16 00:30:09 +00:00
|
|
|
private readonly IManageCommandQueue _commandQueueManager;
|
2013-09-14 06:36:07 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
2015-01-16 00:30:09 +00:00
|
|
|
private static CancellationTokenSource _cancellationTokenSource;
|
2013-09-11 06:33:47 +00:00
|
|
|
|
2015-01-16 00:30:09 +00:00
|
|
|
public CommandExecutor(IServiceFactory serviceFactory,
|
|
|
|
IManageCommandQueue commandQueueManager,
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
Logger logger)
|
|
|
|
{
|
2013-02-23 20:09:44 +00:00
|
|
|
_logger = logger;
|
2013-05-08 05:47:15 +00:00
|
|
|
_serviceFactory = serviceFactory;
|
2015-01-16 00:30:09 +00:00
|
|
|
_commandQueueManager = commandQueueManager;
|
2013-09-14 06:36:07 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
2014-04-12 20:49:41 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 00:30:09 +00:00
|
|
|
private void ExecuteCommands()
|
2013-08-31 03:08:19 +00:00
|
|
|
{
|
2015-01-16 00:30:09 +00:00
|
|
|
try
|
2014-06-08 07:50:29 +00:00
|
|
|
{
|
2015-01-16 00:30:09 +00:00
|
|
|
foreach (var command in _commandQueueManager.Queue(_cancellationTokenSource.Token))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-01-05 23:32:17 +00:00
|
|
|
ExecuteCommand((dynamic)command.Body, command);
|
2015-01-16 00:30:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2017-01-05 23:32:17 +00:00
|
|
|
_logger.Error(ex, "Error occurred while executing task {0}", command.Name);
|
2015-01-16 00:30:09 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-08 07:50:29 +00:00
|
|
|
}
|
2015-01-16 00:30:09 +00:00
|
|
|
catch (ThreadAbortException ex)
|
2014-06-08 07:50:29 +00:00
|
|
|
{
|
2017-10-08 05:14:26 +00:00
|
|
|
_logger.Error(ex, "Thread aborted");
|
2015-01-16 00:30:09 +00:00
|
|
|
Thread.ResetAbort();
|
2014-06-08 07:50:29 +00:00
|
|
|
}
|
2018-04-22 21:12:57 +00:00
|
|
|
catch (OperationCanceledException)
|
2017-10-08 05:20:51 +00:00
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
_logger.Trace("Stopped one command execution pipeline");
|
2017-10-08 05:20:51 +00:00
|
|
|
}
|
2015-03-25 23:46:41 +00:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2017-01-05 23:32:17 +00:00
|
|
|
_logger.Error(ex, "Unknown error in thread");
|
2015-03-25 23:46:41 +00:00
|
|
|
}
|
2013-08-31 03:08:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
private void ExecuteCommand<TCommand>(TCommand command, CommandModel commandModel)
|
|
|
|
where TCommand : Command
|
2013-08-31 03:08:19 +00:00
|
|
|
{
|
2020-03-06 01:57:11 +00:00
|
|
|
IExecute<TCommand> handler = null;
|
2013-07-19 05:05:07 +00:00
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
try
|
|
|
|
{
|
2020-03-06 01:57:11 +00:00
|
|
|
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
|
|
|
handler = (IExecute<TCommand>)_serviceFactory.Build(handlerContract);
|
|
|
|
|
|
|
|
_logger.Trace("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
|
|
|
|
2015-01-16 00:30:09 +00:00
|
|
|
_commandQueueManager.Start(commandModel);
|
|
|
|
BroadcastCommandUpdate(commandModel);
|
2013-09-11 06:33:47 +00:00
|
|
|
|
2015-06-03 18:17:50 +00:00
|
|
|
if (ProgressMessageContext.CommandModel == null)
|
2013-09-04 05:02:26 +00:00
|
|
|
{
|
2015-06-03 18:17:50 +00:00
|
|
|
ProgressMessageContext.CommandModel = commandModel;
|
2013-09-04 05:02:26 +00:00
|
|
|
}
|
2013-08-30 16:18:12 +00:00
|
|
|
|
2015-01-16 00:30:09 +00:00
|
|
|
handler.Execute(command);
|
2015-01-25 20:10:49 +00:00
|
|
|
|
2017-09-28 02:20:37 +00:00
|
|
|
_commandQueueManager.Complete(commandModel, command.CompletionMessage ?? commandModel.Message);
|
2015-01-16 00:30:09 +00:00
|
|
|
}
|
|
|
|
catch (CommandFailedException ex)
|
|
|
|
{
|
2015-06-02 07:18:34 +00:00
|
|
|
_commandQueueManager.SetMessage(commandModel, "Failed");
|
2015-01-16 00:30:09 +00:00
|
|
|
_commandQueueManager.Fail(commandModel, ex.Message, ex);
|
|
|
|
throw;
|
2013-05-08 05:47:15 +00:00
|
|
|
}
|
2015-01-16 00:30:09 +00:00
|
|
|
catch (Exception ex)
|
2013-05-08 05:47:15 +00:00
|
|
|
{
|
2015-01-16 00:30:09 +00:00
|
|
|
_commandQueueManager.SetMessage(commandModel, "Failed");
|
|
|
|
_commandQueueManager.Fail(commandModel, "Failed", ex);
|
2013-05-08 05:47:15 +00:00
|
|
|
throw;
|
|
|
|
}
|
2013-09-14 06:20:10 +00:00
|
|
|
finally
|
|
|
|
{
|
2015-01-16 00:30:09 +00:00
|
|
|
BroadcastCommandUpdate(commandModel);
|
|
|
|
|
|
|
|
_eventAggregator.PublishEvent(new CommandExecutedEvent(commandModel));
|
2013-09-14 23:34:21 +00:00
|
|
|
|
2015-06-03 18:17:50 +00:00
|
|
|
if (ProgressMessageContext.CommandModel == commandModel)
|
2013-09-14 06:20:10 +00:00
|
|
|
{
|
2015-06-03 18:17:50 +00:00
|
|
|
ProgressMessageContext.CommandModel = null;
|
2013-09-14 06:20:10 +00:00
|
|
|
}
|
2013-05-08 05:47:15 +00:00
|
|
|
|
2020-03-06 01:57:11 +00:00
|
|
|
if (handler != null)
|
|
|
|
{
|
|
|
|
_logger.Trace("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, commandModel.Duration.ToString());
|
|
|
|
}
|
|
|
|
}
|
2013-04-24 01:56:00 +00:00
|
|
|
}
|
2017-01-05 23:32:17 +00:00
|
|
|
|
2015-01-16 00:30:09 +00:00
|
|
|
private void BroadcastCommandUpdate(CommandModel command)
|
2013-09-14 23:43:03 +00:00
|
|
|
{
|
2015-01-16 00:30:09 +00:00
|
|
|
if (command.Body.SendUpdatesToClient)
|
2013-09-14 23:43:03 +00:00
|
|
|
{
|
|
|
|
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
|
|
|
|
}
|
|
|
|
}
|
2015-01-16 00:30:09 +00:00
|
|
|
|
|
|
|
public void Handle(ApplicationStartedEvent message)
|
|
|
|
{
|
|
|
|
_cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
for (int i = 0; i < THREAD_LIMIT; i++)
|
|
|
|
{
|
|
|
|
var thread = new Thread(ExecuteCommands);
|
|
|
|
thread.Start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(ApplicationShutdownRequested message)
|
|
|
|
{
|
|
|
|
_logger.Info("Shutting down task execution");
|
|
|
|
_cancellationTokenSource.Cancel(true);
|
|
|
|
}
|
2013-02-19 02:37:16 +00:00
|
|
|
}
|
2013-03-05 06:25:05 +00:00
|
|
|
}
|