1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

Prevent ProgressMessageTarget from ever reading the command from the database.

This commit is contained in:
Taloth Saldono 2015-06-03 20:17:50 +02:00
parent 6d18b37a94
commit 866f971d41
6 changed files with 52 additions and 39 deletions

View File

@ -62,7 +62,7 @@ public void Handle(CommandUpdatedEvent message)
{ {
if (message.Command.Body.SendUpdatesToClient) if (message.Command.Body.SendUpdatesToClient)
{ {
BroadcastResourceChange(ModelAction.Updated, message.Command.Id); BroadcastResourceChange(ModelAction.Updated, message.Command.InjectTo<CommandResource>());
} }
} }
} }

View File

@ -69,9 +69,9 @@ private void ExecuteCommand<TCommand>(TCommand command, CommandModel commandMode
_commandQueueManager.Start(commandModel); _commandQueueManager.Start(commandModel);
BroadcastCommandUpdate(commandModel); BroadcastCommandUpdate(commandModel);
if (!MappedDiagnosticsContext.Contains("CommandId")) if (ProgressMessageContext.CommandModel == null)
{ {
MappedDiagnosticsContext.Set("CommandId", commandModel.Id.ToString()); ProgressMessageContext.CommandModel = commandModel;
} }
handler.Execute(command); handler.Execute(command);
@ -96,9 +96,9 @@ private void ExecuteCommand<TCommand>(TCommand command, CommandModel commandMode
_eventAggregator.PublishEvent(new CommandExecutedEvent(commandModel)); _eventAggregator.PublishEvent(new CommandExecutedEvent(commandModel));
if (MappedDiagnosticsContext.Get("CommandId") == commandModel.Id.ToString()) if (ProgressMessageContext.CommandModel == commandModel)
{ {
MappedDiagnosticsContext.Remove("CommandId"); ProgressMessageContext.CommandModel = null;
} }
} }

View File

@ -80,8 +80,8 @@ public CommandModel Push<TCommand>(TCommand command, CommandPriority priority =
_logger.Trace("Inserting new command: {0}", commandModel.Name); _logger.Trace("Inserting new command: {0}", commandModel.Name);
_repo.Insert(commandModel); _repo.Insert(commandModel);
_commandQueue.Add(commandModel);
_commandCache.Set(commandModel.Id.ToString(), commandModel); _commandCache.Set(commandModel.Id.ToString(), commandModel);
_commandQueue.Add(commandModel);
return commandModel; return commandModel;
} }

View File

@ -717,6 +717,7 @@
<Compile Include="Profiles\Delay\DelayProfileService.cs" /> <Compile Include="Profiles\Delay\DelayProfileService.cs" />
<Compile Include="Profiles\Delay\DelayProfileTagInUseValidator.cs" /> <Compile Include="Profiles\Delay\DelayProfileTagInUseValidator.cs" />
<Compile Include="Profiles\ProfileRepository.cs" /> <Compile Include="Profiles\ProfileRepository.cs" />
<Compile Include="ProgressMessaging\ProgressMessageContext.cs" />
<Compile Include="Qualities\Revision.cs" /> <Compile Include="Qualities\Revision.cs" />
<Compile Include="RemotePathMappings\RemotePathMapping.cs" /> <Compile Include="RemotePathMappings\RemotePathMapping.cs" />
<Compile Include="RemotePathMappings\RemotePathMappingRepository.cs" /> <Compile Include="RemotePathMappings\RemotePathMappingRepository.cs" />

View File

@ -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;
}
}
}

View File

@ -15,8 +15,6 @@ public class ProgressMessageTarget : Target, IHandle<ApplicationStartedEvent>
private readonly IManageCommandQueue _commandQueueManager; private readonly IManageCommandQueue _commandQueueManager;
private static LoggingRule _rule; private static LoggingRule _rule;
private const string REENTRY_LOCK = "ProgressMessagingLock";
public ProgressMessageTarget(IEventAggregator eventAggregator, IManageCommandQueue commandQueueManager) public ProgressMessageTarget(IEventAggregator eventAggregator, IManageCommandQueue commandQueueManager)
{ {
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
@ -25,29 +23,20 @@ public ProgressMessageTarget(IEventAggregator eventAggregator, IManageCommandQue
protected override void Write(LogEventInfo logEvent) protected override void Write(LogEventInfo logEvent)
{ {
if (!ReentryPreventionCheck()) return; var command = ProgressMessageContext.CommandModel;
var command = GetCurrentCommand(); if (!IsClientMessage(logEvent, command)) return;
if (IsClientMessage(logEvent, command)) if (!ProgressMessageContext.LockReentrancy()) return;
try
{ {
_commandQueueManager.SetMessage(command, logEvent.FormattedMessage); _commandQueueManager.SetMessage(command, logEvent.FormattedMessage);
_eventAggregator.PublishEvent(new CommandUpdatedEvent(command)); _eventAggregator.PublishEvent(new CommandUpdatedEvent(command));
} }
finally
MappedDiagnosticsContext.Remove(REENTRY_LOCK);
}
private CommandModel GetCurrentCommand()
{ {
var commandId = MappedDiagnosticsContext.Get("CommandId"); ProgressMessageContext.UnlockReentrancy();
if (String.IsNullOrWhiteSpace(commandId))
{
return null;
} }
return _commandQueueManager.Get(Convert.ToInt32(commandId));
} }
private bool IsClientMessage(LogEventInfo logEvent, CommandModel command) private bool IsClientMessage(LogEventInfo logEvent, CommandModel command)
@ -60,20 +49,6 @@ private bool IsClientMessage(LogEventInfo logEvent, CommandModel command)
return logEvent.Properties.ContainsKey("Status"); 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) public void Handle(ApplicationStartedEvent message)
{ {
_rule = new LoggingRule("*", LogLevel.Trace, this); _rule = new LoggingRule("*", LogLevel.Trace, this);