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()
{
return _trackCommands.AllTracked.AsResponse();
return _trackCommands.AllTracked().AsResponse();
}
}
}

View File

@ -61,6 +61,19 @@ namespace NzbDrone.Common.Cache
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)
{
Ensure.That(() => key).IsNotNullOrWhiteSpace();
@ -81,7 +94,6 @@ namespace NzbDrone.Common.Cache
return value;
}
public void Clear()
{
_store.Clear();

View File

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

View File

@ -10,11 +10,11 @@ namespace NzbDrone.Common.Messaging.Tracking
TrackedCommand TrackIfNew(ICommand command);
TrackedCommand Completed(TrackedCommand trackedCommand, TimeSpan runtime);
TrackedCommand Failed(TrackedCommand trackedCommand, Exception e);
ICollection<TrackedCommand> AllTracked { get; }
List<TrackedCommand> AllTracked();
Boolean ExistingCommand(ICommand command);
}
public class TrackCommands : ITrackCommands
public class TrackCommands : ITrackCommands, IExecute<TrackedCommandCleanupCommand>
{
private readonly ICached<TrackedCommand> _cache;
@ -31,7 +31,7 @@ namespace NzbDrone.Common.Messaging.Tracking
}
var trackedCommand = new TrackedCommand(command, CommandState.Running);
_cache.Set(command.CommandId, trackedCommand);
Store(trackedCommand);
return trackedCommand;
}
@ -42,7 +42,7 @@ namespace NzbDrone.Common.Messaging.Tracking
trackedCommand.State = CommandState.Completed;
trackedCommand.Runtime = runtime;
_cache.Set(trackedCommand.Command.CommandId, trackedCommand);
Store(trackedCommand);
return trackedCommand;
}
@ -53,26 +53,43 @@ namespace NzbDrone.Common.Messaging.Tracking
trackedCommand.State = CommandState.Failed;
trackedCommand.Exception = e;
_cache.Set(trackedCommand.Command.CommandId, trackedCommand);
Store(trackedCommand);
return trackedCommand;
}
public ICollection<TrackedCommand> AllTracked
public List<TrackedCommand> AllTracked()
{
get
{
return _cache.Values;
}
return _cache.Values.ToList();
}
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());
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\Events\CommandStartedEvent.cs" />
<Compile Include="Messaging\CommandEqualityComparer.cs" />
<Compile Include="Messaging\Tracking\TrackedCommandCleanupCommand.cs" />
<Compile Include="PathEqualityComparer.cs" />
<Compile Include="Services.cs" />
<Compile Include="TPL\LimitedConcurrencyLevelTaskScheduler.cs" />

View File

@ -4,6 +4,7 @@ using System.Linq;
using NLog;
using NzbDrone.Common.Messaging;
using NzbDrone.Common.Messaging.Events;
using NzbDrone.Common.Messaging.Tracking;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Configuration.Events;
using NzbDrone.Core.Indexers;
@ -48,7 +49,8 @@ namespace NzbDrone.Core.Jobs
new ScheduledTask{ Interval = 12*60, TypeName = typeof(RefreshSeriesCommand).FullName},
new ScheduledTask{ Interval = 1, TypeName = typeof(DownloadedEpisodesScanCommand).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();