2013-08-29 04:43:26 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Messaging.Tracking
|
|
|
|
|
{
|
|
|
|
|
public interface ITrackCommands
|
|
|
|
|
{
|
|
|
|
|
TrackedCommand TrackIfNew(ICommand command);
|
|
|
|
|
TrackedCommand Completed(TrackedCommand trackedCommand, TimeSpan runtime);
|
|
|
|
|
TrackedCommand Failed(TrackedCommand trackedCommand, Exception e);
|
2013-08-30 17:27:17 +00:00
|
|
|
|
List<TrackedCommand> AllTracked();
|
2013-08-29 04:43:26 +00:00
|
|
|
|
Boolean ExistingCommand(ICommand command);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 17:27:17 +00:00
|
|
|
|
public class TrackCommands : ITrackCommands, IExecute<TrackedCommandCleanupCommand>
|
2013-08-29 04:43:26 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly ICached<TrackedCommand> _cache;
|
|
|
|
|
|
|
|
|
|
public TrackCommands(ICacheManger cacheManger)
|
|
|
|
|
{
|
|
|
|
|
_cache = cacheManger.GetCache<TrackedCommand>(GetType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TrackedCommand TrackIfNew(ICommand command)
|
|
|
|
|
{
|
|
|
|
|
if (ExistingCommand(command))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var trackedCommand = new TrackedCommand(command, CommandState.Running);
|
2013-08-30 17:27:17 +00:00
|
|
|
|
Store(trackedCommand);
|
2013-08-29 04:43:26 +00:00
|
|
|
|
|
|
|
|
|
return trackedCommand;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TrackedCommand Completed(TrackedCommand trackedCommand, TimeSpan runtime)
|
|
|
|
|
{
|
|
|
|
|
trackedCommand.StateChangeTime = DateTime.UtcNow;
|
|
|
|
|
trackedCommand.State = CommandState.Completed;
|
|
|
|
|
trackedCommand.Runtime = runtime;
|
|
|
|
|
|
2013-08-30 17:27:17 +00:00
|
|
|
|
Store(trackedCommand);
|
2013-08-29 04:43:26 +00:00
|
|
|
|
|
|
|
|
|
return trackedCommand;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TrackedCommand Failed(TrackedCommand trackedCommand, Exception e)
|
|
|
|
|
{
|
|
|
|
|
trackedCommand.StateChangeTime = DateTime.UtcNow;
|
|
|
|
|
trackedCommand.State = CommandState.Failed;
|
|
|
|
|
trackedCommand.Exception = e;
|
|
|
|
|
|
2013-08-30 17:27:17 +00:00
|
|
|
|
Store(trackedCommand);
|
2013-08-29 04:43:26 +00:00
|
|
|
|
|
|
|
|
|
return trackedCommand;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 17:27:17 +00:00
|
|
|
|
public List<TrackedCommand> AllTracked()
|
2013-08-29 04:43:26 +00:00
|
|
|
|
{
|
2013-08-30 17:27:17 +00:00
|
|
|
|
return _cache.Values.ToList();
|
2013-08-29 04:43:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ExistingCommand(ICommand command)
|
|
|
|
|
{
|
2013-08-30 17:27:17 +00:00
|
|
|
|
var running = AllTracked().Where(i => i.Type == command.GetType().FullName && i.State == CommandState.Running);
|
2013-08-29 04:43:26 +00:00
|
|
|
|
|
|
|
|
|
var result = running.Select(r => r.Command).Contains(command, new CommandEqualityComparer());
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-08-30 17:27:17 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-29 04:43:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|