-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDefaultCommandQueue.cs
199 lines (174 loc) · 5.93 KB
/
DefaultCommandQueue.cs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// ==========================================================================
// Notifo.io
// ==========================================================================
// Copyright (c) Sebastian Stehle
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Notifo.SDK.Extensions;
using Notifo.SDK.Resources;
namespace Notifo.SDK.CommandQueue
{
internal sealed class DefaultCommandQueue : ICommandQueue
{
private readonly ICommandStore commandStore;
private readonly ICommandTrigger[] commandTriggers;
private readonly int maxRetries;
private readonly TimeSpan timeout;
private readonly Task task;
private readonly BlockingCollection<QueuedCommand> queue = new BlockingCollection<QueuedCommand>();
private readonly Queue<QueuedCommand> retryQueue = new Queue<QueuedCommand>();
public event EventHandler<NotificationLogEventArgs> OnLog;
public DefaultCommandQueue(
ICommandStore commandStore,
ICommandTrigger[] commandTriggers,
int maxRetries,
TimeSpan timeout)
{
this.commandStore = commandStore;
this.commandTriggers = commandTriggers;
this.maxRetries = maxRetries;
this.timeout = timeout;
task = Task.Run(RunAsync);
}
public async Task CompleteAsync(
CancellationToken ct)
{
Trigger();
foreach (var trigger in commandTriggers.OfType<IDisposable>())
{
trigger.Dispose();
}
await task.WithCancellation(ct);
}
public void Dispose()
{
queue.CompleteAdding();
foreach (var trigger in commandTriggers.OfType<IDisposable>())
{
trigger.Dispose();
}
task.Wait();
}
public void Run(ICommand command)
{
var queudCommand = new QueuedCommand
{
Command = command,
CommandId = Guid.NewGuid()
};
lock (retryQueue)
{
foreach (var existing in retryQueue)
{
if (existing.Command.Merge(command))
{
return;
}
}
retryQueue.Enqueue(queudCommand);
}
Trigger();
_ = StoreAsync(queudCommand);
}
private async Task StoreAsync(QueuedCommand queudCommand)
{
try
{
await commandStore.StoreAsync(queudCommand).AsTask();
}
catch (Exception ex)
{
OnLog?.Invoke(this, new NotificationLogEventArgs(NotificationLogType.Error, this, Strings.CommandError, null, ex));
}
}
public void Trigger()
{
lock (retryQueue)
{
if (retryQueue.TryDequeue(out var dequeued))
{
queue.Add(dequeued);
}
}
}
private async Task RunAsync()
{
try
{
var pendingCommands = await commandStore.GetCommandsAsync();
foreach (var command in pendingCommands)
{
retryQueue.Enqueue(command);
}
}
catch (Exception ex)
{
OnLog?.Invoke(this, new NotificationLogEventArgs(NotificationLogType.Error, this, Strings.CommandError, null, ex));
}
foreach (var trigger in commandTriggers)
{
trigger.Start(this);
}
try
{
foreach (var enqueued in queue.GetConsumingEnumerable())
{
try
{
if (!Debugger.IsAttached)
{
using (var cts = new CancellationTokenSource(timeout))
{
await enqueued.Command.ExecuteAsync(cts.Token);
}
}
else
{
await enqueued.Command.ExecuteAsync(default(CancellationToken));
}
// We have completed the command successfully, so we can remove it here.
try
{
await commandStore.RemoveAsync(enqueued.CommandId);
}
catch (Exception ex)
{
OnLog?.Invoke(this, new NotificationLogEventArgs(NotificationLogType.Error, this, Strings.CommandError, null, ex));
}
// We have just completed a command, so it is very likely that the next one will be successful as well.
Trigger();
}
catch
{
if (enqueued.Retries < maxRetries)
{
enqueued.Retries++;
lock (retryQueue)
{
retryQueue.Enqueue(enqueued);
}
}
}
}
}
catch
{
try
{
queue.CompleteAdding();
}
catch
{
return;
}
}
}
}
}