1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

Fixed: Unnecessary idle cpu usage

ref #4386
This commit is contained in:
Taloth Saldono 2021-03-19 02:48:09 +01:00
parent fa8b2f48e7
commit 5a69801877
2 changed files with 39 additions and 4 deletions

View File

@ -23,6 +23,8 @@ public void Add(CommandModel item)
lock (_mutex) lock (_mutex)
{ {
_items.Add(item); _items.Add(item);
Monitor.PulseAll(_mutex);
} }
} }
@ -68,6 +70,8 @@ public void RemoveMany(IEnumerable<CommandModel> commands)
{ {
_items.Remove(command); _items.Remove(command);
} }
Monitor.PulseAll(_mutex);
} }
} }
public bool RemoveIfQueued(int id) public bool RemoveIfQueued(int id)
@ -82,6 +86,8 @@ public bool RemoveIfQueued(int id)
{ {
_items.Remove(command); _items.Remove(command);
rval = true; rval = true;
Monitor.PulseAll(_mutex);
} }
} }
@ -101,18 +107,43 @@ public IEnumerable<CommandModel> GetConsumingEnumerable()
public IEnumerable<CommandModel> GetConsumingEnumerable(CancellationToken cancellationToken) public IEnumerable<CommandModel> GetConsumingEnumerable(CancellationToken cancellationToken)
{ {
cancellationToken.Register(PulseAllConsumers);
while (!cancellationToken.IsCancellationRequested) while (!cancellationToken.IsCancellationRequested)
{ {
if (TryGet(out var command)) CommandModel command = null;
lock (_mutex)
{
if (cancellationToken.IsCancellationRequested)
{
break;
}
if (!TryGet(out command))
{
Monitor.Wait(_mutex);
continue;
}
}
if (command != null)
{ {
yield return command; yield return command;
} }
Thread.Sleep(10);
} }
} }
public bool TryGet(out CommandModel item) public void PulseAllConsumers()
{
// Signal all consumers to reevaluate cancellation token
lock (_mutex)
{
Monitor.PulseAll(_mutex);
}
}
private bool TryGet(out CommandModel item)
{ {
var rval = true; var rval = true;
item = default(CommandModel); item = default(CommandModel);

View File

@ -183,6 +183,8 @@ public void Start(CommandModel command)
public void Complete(CommandModel command, string message) public void Complete(CommandModel command, string message)
{ {
Update(command, CommandStatus.Completed, message); Update(command, CommandStatus.Completed, message);
_commandQueue.PulseAllConsumers();
} }
public void Fail(CommandModel command, string message, Exception e) public void Fail(CommandModel command, string message, Exception e)
@ -190,6 +192,8 @@ public void Fail(CommandModel command, string message, Exception e)
command.Exception = e.ToString(); command.Exception = e.ToString();
Update(command, CommandStatus.Failed, message); Update(command, CommandStatus.Failed, message);
_commandQueue.PulseAllConsumers();
} }
public void Requeue() public void Requeue()