diff --git a/src/NzbDrone.Api/Commands/CommandModule.cs b/src/NzbDrone.Api/Commands/CommandModule.cs index 1f273ccfc..fd9414d53 100644 --- a/src/NzbDrone.Api/Commands/CommandModule.cs +++ b/src/NzbDrone.Api/Commands/CommandModule.cs @@ -62,7 +62,7 @@ namespace NzbDrone.Api.Commands { if (message.Command.Body.SendUpdatesToClient) { - BroadcastResourceChange(ModelAction.Updated, message.Command.Id); + BroadcastResourceChange(ModelAction.Updated, message.Command.InjectTo()); } } } diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs index ce84f54b1..0deb5c9df 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs @@ -69,9 +69,9 @@ namespace NzbDrone.Core.Messaging.Commands _commandQueueManager.Start(commandModel); BroadcastCommandUpdate(commandModel); - if (!MappedDiagnosticsContext.Contains("CommandId")) + if (ProgressMessageContext.CommandModel == null) { - MappedDiagnosticsContext.Set("CommandId", commandModel.Id.ToString()); + ProgressMessageContext.CommandModel = commandModel; } handler.Execute(command); @@ -96,9 +96,9 @@ namespace NzbDrone.Core.Messaging.Commands _eventAggregator.PublishEvent(new CommandExecutedEvent(commandModel)); - if (MappedDiagnosticsContext.Get("CommandId") == commandModel.Id.ToString()) + if (ProgressMessageContext.CommandModel == commandModel) { - MappedDiagnosticsContext.Remove("CommandId"); + ProgressMessageContext.CommandModel = null; } } diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs b/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs index 23b0cf53d..d45547b8f 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs @@ -78,10 +78,10 @@ namespace NzbDrone.Core.Messaging.Commands }; _logger.Trace("Inserting new command: {0}", commandModel.Name); - + _repo.Insert(commandModel); - _commandQueue.Add(commandModel); _commandCache.Set(commandModel.Id.ToString(), commandModel); + _commandQueue.Add(commandModel); return commandModel; } diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index d78b6c363..1818ed1e5 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -717,6 +717,7 @@ + diff --git a/src/NzbDrone.Core/ProgressMessaging/ProgressMessageContext.cs b/src/NzbDrone.Core/ProgressMessaging/ProgressMessageContext.cs new file mode 100644 index 000000000..685fc3a53 --- /dev/null +++ b/src/NzbDrone.Core/ProgressMessaging/ProgressMessageContext.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NzbDrone.Core.Messaging.Commands; + +namespace NzbDrone.Core.ProgressMessaging +{ + public static class ProgressMessageContext + { + [ThreadStatic] + private static CommandModel _commandModel; + + [ThreadStatic] + private static bool _reentrancyLock; + + public static CommandModel CommandModel + { + get { return _commandModel; } + set { _commandModel = value; } + } + + public static bool LockReentrancy() + { + if (_reentrancyLock) + return false; + + _reentrancyLock = true; + return true; + } + + public static void UnlockReentrancy() + { + _reentrancyLock = false; + } + } +} diff --git a/src/NzbDrone.Core/ProgressMessaging/ProgressMessageTarget.cs b/src/NzbDrone.Core/ProgressMessaging/ProgressMessageTarget.cs index a1181c454..be870adc1 100644 --- a/src/NzbDrone.Core/ProgressMessaging/ProgressMessageTarget.cs +++ b/src/NzbDrone.Core/ProgressMessaging/ProgressMessageTarget.cs @@ -15,8 +15,6 @@ namespace NzbDrone.Core.ProgressMessaging private readonly IManageCommandQueue _commandQueueManager; private static LoggingRule _rule; - private const string REENTRY_LOCK = "ProgressMessagingLock"; - public ProgressMessageTarget(IEventAggregator eventAggregator, IManageCommandQueue commandQueueManager) { _eventAggregator = eventAggregator; @@ -25,29 +23,20 @@ namespace NzbDrone.Core.ProgressMessaging protected override void Write(LogEventInfo logEvent) { - if (!ReentryPreventionCheck()) return; + var command = ProgressMessageContext.CommandModel; - var command = GetCurrentCommand(); - - if (IsClientMessage(logEvent, command)) + if (!IsClientMessage(logEvent, command)) return; + + if (!ProgressMessageContext.LockReentrancy()) return; + try { _commandQueueManager.SetMessage(command, logEvent.FormattedMessage); _eventAggregator.PublishEvent(new CommandUpdatedEvent(command)); } - - MappedDiagnosticsContext.Remove(REENTRY_LOCK); - } - - private CommandModel GetCurrentCommand() - { - var commandId = MappedDiagnosticsContext.Get("CommandId"); - - if (String.IsNullOrWhiteSpace(commandId)) + finally { - return null; + ProgressMessageContext.UnlockReentrancy(); } - - return _commandQueueManager.Get(Convert.ToInt32(commandId)); } private bool IsClientMessage(LogEventInfo logEvent, CommandModel command) @@ -60,20 +49,6 @@ namespace NzbDrone.Core.ProgressMessaging return logEvent.Properties.ContainsKey("Status"); } - private bool ReentryPreventionCheck() - { - var reentryLock = MappedDiagnosticsContext.Get(REENTRY_LOCK); - var commandId = MappedDiagnosticsContext.Get("CommandId"); - - if (reentryLock.IsNullOrWhiteSpace() || reentryLock != commandId) - { - MappedDiagnosticsContext.Set(REENTRY_LOCK, MappedDiagnosticsContext.Get("CommandId")); - return true; - } - - return false; - } - public void Handle(ApplicationStartedEvent message) { _rule = new LoggingRule("*", LogLevel.Trace, this);