-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathAsyncTimer.cs
More file actions
108 lines (91 loc) · 3.33 KB
/
AsyncTimer.cs
File metadata and controls
108 lines (91 loc) · 3.33 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
namespace ServiceControl.Infrastructure.BackgroundTasks;
using System;
using System.Threading;
using System.Threading.Tasks;
public enum TimerJobExecutionResult
{
ScheduleNextExecution,
ExecuteImmediately,
DoNotContinueExecuting
}
public class TimerJob
{
public TimerJob(Func<CancellationToken, Task<TimerJobExecutionResult>> callback, TimeSpan due, TimeSpan interval, Action<Exception> errorCallback)
{
tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
task = Task.Run(async () =>
{
try
{
await Task.Delay(due, token).ConfigureAwait(false);
var consecutiveFailures = 0;
while (!token.IsCancellationRequested)
{
try
{
var result = await callback(token).ConfigureAwait(false);
consecutiveFailures = 0;
if (result == TimerJobExecutionResult.DoNotContinueExecuting)
{
tokenSource.Cancel();
}
else if (result == TimerJobExecutionResult.ScheduleNextExecution)
{
await Task.Delay(interval, token).ConfigureAwait(false);
}
//Otherwise execute immediately
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
break;
}
catch (Exception ex)
{
consecutiveFailures++;
const int MaxDelayDurationInSeconds = 60;
var delayInSeconds = consecutiveFailures * 10;
var backoffDelay = TimeSpan.FromSeconds(int.Min(MaxDelayDurationInSeconds, delayInSeconds));
await Task.Delay(backoffDelay, token).ConfigureAwait(false);
errorCallback(ex);
}
}
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
// no-op
}
}, CancellationToken.None);
}
public async Task Stop(CancellationToken cancellationToken)
{
if (tokenSource == null)
{
return;
}
await tokenSource.CancelAsync().ConfigureAwait(false);
tokenSource.Dispose();
if (task == null)
{
return;
}
try
{
await task.WaitAsync(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException) when (tokenSource.IsCancellationRequested)
{
//NOOP
}
}
readonly Task task;
readonly CancellationTokenSource tokenSource;
}
public interface IAsyncTimer
{
TimerJob Schedule(Func<CancellationToken, Task<TimerJobExecutionResult>> callback, TimeSpan due, TimeSpan interval, Action<Exception> errorCallback);
}
public class AsyncTimer : IAsyncTimer
{
public TimerJob Schedule(Func<CancellationToken, Task<TimerJobExecutionResult>> callback, TimeSpan due, TimeSpan interval, Action<Exception> errorCallback) => new(callback, due, interval, errorCallback);
}