|
1 | 1 | using GChan.Models.Trackers;
|
2 | 2 | using GChan.Properties;
|
| 3 | +using NetTopologySuite.Triangulate.QuadEdge; |
3 | 4 | using NLog;
|
4 | 5 | using System;
|
5 | 6 | using System.Collections.Concurrent;
|
| 7 | +using System.Collections.Generic; |
6 | 8 | using System.Threading;
|
7 | 9 | using System.Threading.Tasks;
|
| 10 | +using System.Windows.Documents; |
8 | 11 |
|
9 | 12 | #nullable enable
|
10 | 13 |
|
@@ -78,22 +81,44 @@ public async Task WorkAsync()
|
78 | 81 | /// </summary>
|
79 | 82 | private IProcessable? MaybeDequeue()
|
80 | 83 | {
|
81 |
| - while (true) |
| 84 | + var deferredProcessables = new List<IProcessable>(); |
| 85 | + |
| 86 | + try |
82 | 87 | {
|
83 |
| - if (queue.TryDequeue(out var processable)) |
| 88 | + while (true) |
84 | 89 | {
|
85 |
| - if (processable.ShouldProcess) |
| 90 | + if (queue.TryDequeue(out var processable)) |
86 | 91 | {
|
87 |
| - logger.Trace("Dequeued processable {0}.", processable); |
88 |
| - return processable; |
| 92 | + if (processable.ShouldProcess) |
| 93 | + { |
| 94 | + if (processable.ReadyToProcessAt == null || processable.ReadyToProcessAt <= DateTimeOffset.Now) |
| 95 | + { |
| 96 | + logger.Trace("Dequeued processable {0}.", processable); |
| 97 | + return processable; |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + deferredProcessables.Add(processable); |
| 102 | + logger.Trace("Deferring processable {0}. Ready to process at {1}.", processable, processable.ReadyToProcessAt); |
| 103 | + } |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + logger.Trace("Discarding dequeued processable {0}.", processable); |
| 108 | + } |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + logger.Trace("Processable queue empty."); |
| 113 | + return null; |
89 | 114 | }
|
90 |
| - |
91 |
| - logger.Trace("Discarding dequeued processable {0}.", processable); |
92 | 115 | }
|
93 |
| - else |
| 116 | + } |
| 117 | + finally |
| 118 | + { |
| 119 | + foreach (var deferredProcessable in deferredProcessables) |
94 | 120 | {
|
95 |
| - logger.Trace("Processable queue empty."); |
96 |
| - return null; |
| 121 | + queue.Enqueue(deferredProcessable); |
97 | 122 | }
|
98 | 123 | }
|
99 | 124 | }
|
|
0 commit comments