mirror of
https://github.com/lidarr/lidarr.git
synced 2025-07-16 10:03:51 -07:00
Commands are stored in memory and prevents duplicate jobs
This commit is contained in:
parent
a86c131761
commit
c917cdc0cc
16 changed files with 244 additions and 13 deletions
59
NzbDrone.Common/Messaging/Manager/CommandManager.cs
Normal file
59
NzbDrone.Common/Messaging/Manager/CommandManager.cs
Normal file
|
@ -0,0 +1,59 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Common.Messaging.Events;
|
||||
|
||||
namespace NzbDrone.Common.Messaging.Manager
|
||||
{
|
||||
public interface IManageCommands
|
||||
{
|
||||
ICollection<CommandManagerItem> Items { get; }
|
||||
Boolean ExistingItem(ICommand command);
|
||||
}
|
||||
|
||||
public class CommandManager : IManageCommands,
|
||||
IHandle<CommandStartedEvent>,
|
||||
IHandle<CommandCompletedEvent>,
|
||||
IHandle<CommandFailedEvent>
|
||||
{
|
||||
private readonly ICached<CommandManagerItem> _cache;
|
||||
|
||||
public CommandManager(ICacheManger cacheManger)
|
||||
{
|
||||
_cache = cacheManger.GetCache<CommandManagerItem>(GetType());
|
||||
}
|
||||
|
||||
public void Handle(CommandStartedEvent message)
|
||||
{
|
||||
_cache.Set(message.Command.CommandId, new CommandManagerItem(message.Command, CommandState.Running));
|
||||
}
|
||||
|
||||
public void Handle(CommandCompletedEvent message)
|
||||
{
|
||||
_cache.Set(message.Command.CommandId, new CommandManagerItem(message.Command, CommandState.Completed));
|
||||
}
|
||||
|
||||
public void Handle(CommandFailedEvent message)
|
||||
{
|
||||
_cache.Set(message.Command.CommandId, new CommandManagerItem(message.Command, CommandState.Failed));
|
||||
}
|
||||
|
||||
public ICollection<CommandManagerItem> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
return _cache.Values;
|
||||
}
|
||||
}
|
||||
|
||||
public bool ExistingItem(ICommand command)
|
||||
{
|
||||
var running = Items.Where(i => i.Type == command.GetType().FullName && i.State == CommandState.Running);
|
||||
|
||||
var result = running.Select(r => r.Command).Contains(command, new CommandEqualityComparer());
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue