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
|
|
|
{
|
2019-12-22 22:08:53 +00:00
|
|
|
private const int THREAD_UPPER_BOUND = 10;
|
|
|
|
private const int THREAD_LOWER_BOUND = 2;
|
|
|
|
private const int THREAD_LIMIT = 2;
|
|
|
|
|
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
|
|
|
|
{
|
2019-12-22 22:08:53 +00:00
|
|
|
ExecuteCommand((dynamic)command.Body, command);
|
2015-01-16 00:30:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2019-09-29 02:39:09 +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
|
|
|
}
|
2019-08-31 02:50:03 +00:00
|
|
|
catch (OperationCanceledException)
|
2019-06-14 03:54:25 +00:00
|
|
|
{
|
|
|
|
_logger.Trace("Stopped one command execution pipeline");
|
|
|
|
}
|
2015-03-25 23:46:41 +00:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2019-09-29 02:39:09 +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
|
|
|
}
|
|
|
|
|
2019-12-22 22:08:53 +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-05-03 14:59:37 +00:00
|
|
|
handler = (IExecute<TCommand>)_serviceFactory.Build(typeof(IExecute<TCommand>));
|
2020-03-06 01:57:11 +00:00
|
|
|
|
|
|
|
_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
|
|
|
|
2019-06-14 03:54:25 +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
|
|
|
}
|
2019-06-14 03:54:25 +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();
|
|
|
|
|
2018-08-07 21:20:58 +00:00
|
|
|
var envLimit = Environment.GetEnvironmentVariable("THREAD_LIMIT") ?? $"{THREAD_LIMIT}";
|
2023-05-23 10:52:39 +00:00
|
|
|
var threadLimit = THREAD_LIMIT;
|
|
|
|
if (int.TryParse(envLimit, out var parsedLimit))
|
2019-12-22 22:08:53 +00:00
|
|
|
{
|
|
|
|
threadLimit = parsedLimit;
|
|
|
|
}
|
2018-08-07 21:20:58 +00:00
|
|
|
|
|
|
|
threadLimit = Math.Max(THREAD_LOWER_BOUND, threadLimit);
|
|
|
|
threadLimit = Math.Min(THREAD_UPPER_BOUND, threadLimit);
|
2019-12-22 21:24:11 +00:00
|
|
|
|
2018-08-07 21:20:58 +00:00
|
|
|
_logger.Info("Starting {} threads for tasks.", threadLimit);
|
|
|
|
|
2023-05-23 10:52:39 +00:00
|
|
|
for (var i = 0; i < threadLimit + 1; i++)
|
2015-01-16 00:30:09 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|