TrackedCommands are cleaned up automatically

TrackedCommandCleanupCommand is not tracked
This commit is contained in:
Mark McDowall 2013-08-30 10:27:17 -07:00
parent 780e374122
commit 56cd80d24a
7 changed files with 64 additions and 14 deletions

View File

@ -39,7 +39,7 @@ namespace NzbDrone.Api.Commands
private Response GetAllCommands() private Response GetAllCommands()
{ {
return _trackCommands.AllTracked.AsResponse(); return _trackCommands.AllTracked().AsResponse();
} }
} }
} }

View File

@ -61,6 +61,19 @@ namespace NzbDrone.Common.Cache
return value.Object; return value.Object;
} }
public T Remove(string key)
{
CacheItem value;
_store.TryRemove(key, out value);
if (value == null)
{
return default(T);
}
return value.Object;
}
public T Get(string key, Func<T> function, TimeSpan? lifeTime = null) public T Get(string key, Func<T> function, TimeSpan? lifeTime = null)
{ {
Ensure.That(() => key).IsNotNullOrWhiteSpace(); Ensure.That(() => key).IsNotNullOrWhiteSpace();
@ -81,7 +94,6 @@ namespace NzbDrone.Common.Cache
return value; return value;
} }
public void Clear() public void Clear()
{ {
_store.Clear(); _store.Clear();

View File

@ -13,6 +13,7 @@ namespace NzbDrone.Common.Cache
void Set(string key, T value, TimeSpan? lifetime = null); void Set(string key, T value, TimeSpan? lifetime = null);
T Get(string key, Func<T> function, TimeSpan? lifeTime = null); T Get(string key, Func<T> function, TimeSpan? lifeTime = null);
T Find(string key); T Find(string key);
T Remove(string key);
ICollection<T> Values { get; } ICollection<T> Values { get; }
} }

View File

@ -10,11 +10,11 @@ namespace NzbDrone.Common.Messaging.Tracking
TrackedCommand TrackIfNew(ICommand command); TrackedCommand TrackIfNew(ICommand command);
TrackedCommand Completed(TrackedCommand trackedCommand, TimeSpan runtime); TrackedCommand Completed(TrackedCommand trackedCommand, TimeSpan runtime);
TrackedCommand Failed(TrackedCommand trackedCommand, Exception e); TrackedCommand Failed(TrackedCommand trackedCommand, Exception e);
ICollection<TrackedCommand> AllTracked { get; } List<TrackedCommand> AllTracked();
Boolean ExistingCommand(ICommand command); Boolean ExistingCommand(ICommand command);
} }
public class TrackCommands : ITrackCommands public class TrackCommands : ITrackCommands, IExecute<TrackedCommandCleanupCommand>
{ {
private readonly ICached<TrackedCommand> _cache; private readonly ICached<TrackedCommand> _cache;
@ -31,7 +31,7 @@ namespace NzbDrone.Common.Messaging.Tracking
} }
var trackedCommand = new TrackedCommand(command, CommandState.Running); var trackedCommand = new TrackedCommand(command, CommandState.Running);
_cache.Set(command.CommandId, trackedCommand); Store(trackedCommand);
return trackedCommand; return trackedCommand;
} }
@ -42,7 +42,7 @@ namespace NzbDrone.Common.Messaging.Tracking
trackedCommand.State = CommandState.Completed; trackedCommand.State = CommandState.Completed;
trackedCommand.Runtime = runtime; trackedCommand.Runtime = runtime;
_cache.Set(trackedCommand.Command.CommandId, trackedCommand); Store(trackedCommand);
return trackedCommand; return trackedCommand;
} }
@ -53,26 +53,43 @@ namespace NzbDrone.Common.Messaging.Tracking
trackedCommand.State = CommandState.Failed; trackedCommand.State = CommandState.Failed;
trackedCommand.Exception = e; trackedCommand.Exception = e;
_cache.Set(trackedCommand.Command.CommandId, trackedCommand); Store(trackedCommand);
return trackedCommand; return trackedCommand;
} }
public ICollection<TrackedCommand> AllTracked public List<TrackedCommand> AllTracked()
{ {
get return _cache.Values.ToList();
{
return _cache.Values;
}
} }
public bool ExistingCommand(ICommand command) public bool ExistingCommand(ICommand command)
{ {
var running = AllTracked.Where(i => i.Type == command.GetType().FullName && i.State == CommandState.Running); var running = AllTracked().Where(i => i.Type == command.GetType().FullName && i.State == CommandState.Running);
var result = running.Select(r => r.Command).Contains(command, new CommandEqualityComparer()); var result = running.Select(r => r.Command).Contains(command, new CommandEqualityComparer());
return result; return result;
} }
private void Store(TrackedCommand trackedCommand)
{
if (trackedCommand.Command.GetType() == typeof(TrackedCommandCleanupCommand))
{
return;
}
_cache.Set(trackedCommand.Command.CommandId, trackedCommand);
}
public void Execute(TrackedCommandCleanupCommand message)
{
var old = AllTracked().Where(c => c.StateChangeTime < DateTime.UtcNow.AddMinutes(-15));
foreach (var trackedCommand in old)
{
_cache.Remove(trackedCommand.Command.CommandId);
}
}
} }
} }

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Common.Messaging.Tracking
{
public class TrackedCommandCleanupCommand : ICommand
{
public string CommandId { get; private set; }
public TrackedCommandCleanupCommand()
{
CommandId = HashUtil.GenerateCommandId();
}
}
}

View File

@ -96,6 +96,7 @@
<Compile Include="Messaging\Tracking\TrackedCommand.cs" /> <Compile Include="Messaging\Tracking\TrackedCommand.cs" />
<Compile Include="Messaging\Events\CommandStartedEvent.cs" /> <Compile Include="Messaging\Events\CommandStartedEvent.cs" />
<Compile Include="Messaging\CommandEqualityComparer.cs" /> <Compile Include="Messaging\CommandEqualityComparer.cs" />
<Compile Include="Messaging\Tracking\TrackedCommandCleanupCommand.cs" />
<Compile Include="PathEqualityComparer.cs" /> <Compile Include="PathEqualityComparer.cs" />
<Compile Include="Services.cs" /> <Compile Include="Services.cs" />
<Compile Include="TPL\LimitedConcurrencyLevelTaskScheduler.cs" /> <Compile Include="TPL\LimitedConcurrencyLevelTaskScheduler.cs" />

View File

@ -4,6 +4,7 @@ using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Messaging; using NzbDrone.Common.Messaging;
using NzbDrone.Common.Messaging.Events; using NzbDrone.Common.Messaging.Events;
using NzbDrone.Common.Messaging.Tracking;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Configuration.Events; using NzbDrone.Core.Configuration.Events;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
@ -48,7 +49,8 @@ namespace NzbDrone.Core.Jobs
new ScheduledTask{ Interval = 12*60, TypeName = typeof(RefreshSeriesCommand).FullName}, new ScheduledTask{ Interval = 12*60, TypeName = typeof(RefreshSeriesCommand).FullName},
new ScheduledTask{ Interval = 1, TypeName = typeof(DownloadedEpisodesScanCommand).FullName}, new ScheduledTask{ Interval = 1, TypeName = typeof(DownloadedEpisodesScanCommand).FullName},
new ScheduledTask{ Interval = 60, TypeName = typeof(ApplicationUpdateCommand).FullName}, new ScheduledTask{ Interval = 60, TypeName = typeof(ApplicationUpdateCommand).FullName},
new ScheduledTask{ Interval = 1*60, TypeName = typeof(TrimLogCommand).FullName} new ScheduledTask{ Interval = 1*60, TypeName = typeof(TrimLogCommand).FullName},
new ScheduledTask{ Interval = 5, TypeName = typeof(TrackedCommandCleanupCommand).FullName}
}; };
var currentTasks = _scheduledTaskRepository.All(); var currentTasks = _scheduledTaskRepository.All();