forked from brthor/Gofer.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskSchedule.cs
186 lines (153 loc) · 6.06 KB
/
TaskSchedule.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
using System;
using System.Threading.Tasks;
using Gofer.NET.Utils;
using NCrontab;
using Newtonsoft.Json;
namespace Gofer.NET
{
public class TaskSchedule
{
public string LockKey => $"{nameof(TaskSchedule)}::{TaskKey}::ScheduleLock";
private string LastRunValueKey => $"{nameof(TaskSchedule)}::{TaskKey}::LastRunValue";
public bool IsRecurring { get; }
public string TaskKey { get; }
private readonly DateTime _startTime;
private readonly TaskInfo _taskInfo;
private readonly TimeSpan? _intervalOrOffsetFromNow;
private readonly TaskQueue _taskQueue;
private readonly DateTimeOffset? _scheduledTimeAsDateTimeOffset;
private readonly DateTime? _scheduledTime;
private readonly string _crontab;
public TaskSchedule() { }
public TaskSchedule(
TaskInfo taskInfo,
TimeSpan interval,
TaskQueue taskQueue,
bool isRecurring, string taskId) : this(taskInfo, taskQueue, isRecurring, taskId)
{
_intervalOrOffsetFromNow = interval;
}
public TaskSchedule(
TaskInfo taskInfo,
DateTimeOffset scheduledTimeAsDateTimeOffset,
TaskQueue taskQueue,
bool isRecurring, string taskId) : this(taskInfo, taskQueue, isRecurring, taskId)
{
_scheduledTimeAsDateTimeOffset = scheduledTimeAsDateTimeOffset;
}
public TaskSchedule(
TaskInfo taskInfo,
DateTime scheduledTime,
TaskQueue taskQueue,
bool isRecurring, string taskId) : this(taskInfo, taskQueue, isRecurring, taskId)
{
_scheduledTime = scheduledTime;
}
public TaskSchedule(
TaskInfo taskInfo,
string crontab,
TaskQueue taskQueue, string taskKey) : this(taskInfo, taskQueue, true, taskKey)
{
ValidateCrontab(crontab);
_crontab = crontab;
}
private TaskSchedule(TaskInfo taskInfo, TaskQueue taskQueue, bool isRecurring, string taskKey)
{
_taskInfo = taskInfo;
_startTime = DateTime.UtcNow;
_taskQueue = taskQueue;
TaskKey = taskKey;
IsRecurring = isRecurring;
}
/// <summary>
/// Returns true if the task is run.
/// </summary>
public async Task<bool> RunIfScheduleReached()
{
var lastRunTime = await GetLastRunTime(LastRunValueKey);
// If we've already run before, and aren't recurring, dont run again.
if (lastRunTime.HasValue && !IsRecurring)
{
// True is returned so the task removal from schedule is effectively propagated among workers.
return true;
}
if (TaskShouldExecuteBasedOnSchedule(lastRunTime ?? _startTime))
{
await SetLastRunTime();
LogScheduledTaskRun();
await _taskQueue.Enqueue(_taskInfo);
return true;
}
return false;
}
private bool TaskShouldExecuteBasedOnSchedule(DateTime lastRunTime)
{
if (_intervalOrOffsetFromNow.HasValue)
{
var difference = DateTime.UtcNow - lastRunTime;
return difference >= _intervalOrOffsetFromNow;
}
if (_scheduledTimeAsDateTimeOffset.HasValue)
{
var utcScheduledTime = _scheduledTimeAsDateTimeOffset.Value.ToUniversalTime();
return DateTime.UtcNow >= utcScheduledTime;
}
if (_scheduledTime.HasValue)
{
var utcScheduledTime = _scheduledTime.Value.ToUniversalTime();
return DateTime.UtcNow >= utcScheduledTime;
}
if (_crontab != null)
{
var crontabSchedule = CrontabSchedule.Parse(_crontab, new CrontabSchedule.ParseOptions() { IncludingSeconds = true });
var nextOccurence = crontabSchedule.GetNextOccurrence(lastRunTime);
return DateTime.UtcNow >= nextOccurence;
}
throw new Exception("Invalid scheduling mechanism used. This is a code bug, should not happen.");
}
/// <summary>
/// Not Thread safe. Use External locking.
/// </summary>
private async Task<DateTime?> GetLastRunTime(string lastRunValueKey)
{
var jsonString = await _taskQueue.Backend.GetString(lastRunValueKey);
if (string.IsNullOrEmpty(jsonString))
{
return null;
}
return JsonConvert.DeserializeObject<DateTime>(jsonString);
}
/// <summary>
/// Not thread safe. Use external locking.
/// </summary>
private async Task SetLastRunTime()
{
await _taskQueue.Backend.SetString(LastRunValueKey, JsonConvert.SerializeObject(DateTime.UtcNow));
}
private void LogScheduledTaskRun()
{
var intervalString = _intervalOrOffsetFromNow?.ToString() ??
_scheduledTimeAsDateTimeOffset?.ToString() ?? _scheduledTime?.ToString() ?? _crontab;
Console.WriteLine($"Queueing Scheduled Task for run with interval: {intervalString}");
}
private void ValidateCrontab(string crontab)
{
try
{
var schedule = CrontabSchedule.Parse(crontab, new CrontabSchedule.ParseOptions{IncludingSeconds = true});
schedule.GetNextOccurrence(DateTime.UtcNow);
}
catch (Exception ex)
{
throw new Exception("Crontab is invalid. See the inner exception for details.", ex);
}
}
/// <summary>
/// Used to prevent overlap between tasks added at different times but sharing a name.
/// </summary>
public async Task ClearLastRunTime()
{
await _taskQueue.Backend.DeleteKey(LastRunValueKey);
}
}
}