1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-21 01:49:57 +02:00
Sonarr/NzbDrone.Core/Messaging/Commands/Tracking/CommandTrackingService.cs

85 lines
2.3 KiB
C#
Raw Normal View History

2013-09-11 09:33:47 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Common.Cache;
namespace NzbDrone.Core.Messaging.Commands.Tracking
2013-09-11 09:33:47 +03:00
{
public interface ITrackCommands
{
Command GetById(int id);
Command GetById(string id);
void Completed(Command trackedCommand);
void Failed(Command trackedCommand, Exception e);
IEnumerable<Command> RunningCommands();
Command FindExisting(Command command);
void Store(Command command);
void Start(Command command);
}
public class CommandTrackingService : ITrackCommands, IExecute<TrackedCommandCleanupCommand>
{
private readonly ICached<Command> _cache;
public CommandTrackingService(ICacheManger cacheManger)
{
_cache = cacheManger.GetCache<Command>(GetType());
}
public Command GetById(int id)
{
return _cache.Find(id.ToString());
}
public Command GetById(string id)
{
return _cache.Find(id);
}
public void Start(Command command)
{
command.Start();
}
public void Completed(Command trackedCommand)
{
trackedCommand.Completed();
}
public void Failed(Command trackedCommand, Exception e)
{
trackedCommand.Failed(e);
}
public IEnumerable<Command> RunningCommands()
{
return _cache.Values.Where(c => c.State == CommandStatus.Running);
}
public Command FindExisting(Command command)
{
return RunningCommands().SingleOrDefault(t => CommandEqualityComparer.Instance.Equals(t, command));
2013-09-11 09:33:47 +03:00
}
public void Store(Command command)
{
if (command.GetType() == typeof(TrackedCommandCleanupCommand))
{
return;
}
_cache.Set(command.Id.ToString(), command);
}
public void Execute(TrackedCommandCleanupCommand message)
{
var old = _cache.Values.Where(c => c.State != CommandStatus.Running && c.StateChangeTime < DateTime.UtcNow.AddMinutes(-5));
foreach (var trackedCommand in old)
{
_cache.Remove(trackedCommand.Id.ToString());
}
}
}
}