1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-27 01:57:21 +00:00

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 @@ private Response RunCommand(CommandResource resource)
private Response GetAllCommands()
{
return _trackCommands.AllTracked.AsResponse();
return _trackCommands.AllTracked().AsResponse();
}
}
}

View file

@ -61,6 +61,19 @@ public T Find(string key)
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 @@ public T Get(string key, Func<T> function, TimeSpan? lifeTime = null)
return value;
}
public void Clear()
{
_store.Clear();

View file

@ -13,6 +13,7 @@ public interface ICached<T> : ICached
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 @@ public interface ITrackCommands
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 @@ public TrackedCommand TrackIfNew(ICommand command)
}
var trackedCommand = new TrackedCommand(command, CommandState.Running);
_cache.Set(command.CommandId, trackedCommand);
Store(trackedCommand);
return trackedCommand;
}
@ -42,7 +42,7 @@ public TrackedCommand Completed(TrackedCommand trackedCommand, TimeSpan runtime)
trackedCommand.State = CommandState.Completed;
trackedCommand.Runtime = runtime;
_cache.Set(trackedCommand.Command.CommandId, trackedCommand);
Store(trackedCommand);
return trackedCommand;
}
@ -53,26 +53,43 @@ public TrackedCommand Failed(TrackedCommand trackedCommand, Exception e)
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 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 @@ public void Handle(ApplicationStartedEvent message)
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();