-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBackgroundTaskQueue.cs
More file actions
115 lines (97 loc) · 3.42 KB
/
BackgroundTaskQueue.cs
File metadata and controls
115 lines (97 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright © 2024 Travelonium AB
*
* This file is part of Arcadeia.
*
* Arcadeia is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Arcadeia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Arcadeia. If not, see <https://www.gnu.org/licenses/>.
*
*/
using System.Collections.Concurrent;
namespace Arcadeia
{
public class BackgroundTaskQueue(ILogger<BackgroundTaskQueue> logger) : IBackgroundTaskQueue
{
private readonly SemaphoreSlim _signal = new(0);
private readonly ILogger<BackgroundTaskQueue> _logger = logger;
private readonly ConcurrentDictionary<string, Timer> _timers = new();
private readonly ConcurrentQueue<Tuple<string, Func<CancellationToken, Task>>> _tasks = new();
public IEnumerable<string> Tasks
{
get
{
return _tasks.ToArray().Select(task => task.Item1);
}
}
public void Queue(string key, Func<CancellationToken, Task> task)
{
ArgumentNullException.ThrowIfNull(task, nameof(task));
// Ensure the task is not already queued
foreach (var entry in _tasks)
{
if (entry.Item1 == key)
{
return;
}
}
if (_timers.TryGetValue(key, out var timer))
{
timer.Dispose();
_timers.Remove(key, out _);
}
_timers.TryAdd(key, new Timer((_) =>
{
_tasks.Enqueue(new Tuple<string, Func<CancellationToken, Task>>(key, task));
_logger.LogInformation("Queued: {}", key);
_signal.Release();
}, null, 15000, Timeout.Infinite));
}
public async Task<Func<CancellationToken, Task>> DequeueAsync(CancellationToken cancellationToken)
{
// Wait for a signal while respecting the caller's cancellation token
await _signal.WaitAsync(cancellationToken);
if (_tasks.TryDequeue(out var task))
{
_logger.LogInformation("Dequeued: {TaskName}", task.Item1);
return task.Item2;
}
else
{
_logger.LogWarning("Dequeue Failed: No Tasks Available.");
throw new InvalidOperationException("Failed to dequeue a task because the queue is empty.");
}
}
public void Clear()
{
// Clear all tasks in the queue
while (_tasks.TryDequeue(out var task))
{
_logger.LogInformation("Discarded: {TaskName}", task.Item1);
}
// Dispose of and remove all associated timers
foreach (var key in _timers.Keys)
{
if (_timers.TryRemove(key, out var timer))
{
timer.Dispose();
}
}
// Reset the semaphore to avoid releasing unnecessary signals
while (_signal.CurrentCount > 0)
{
_signal.Wait(0);
}
_logger.LogInformation("Background Task Queue Cleared.");
}
}
}