-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathRecurringJobEntity.cs
203 lines (166 loc) · 7.53 KB
/
RecurringJobEntity.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
200
201
202
203
// This file is part of Hangfire.
// Copyright © 2019 Sergey Odinokov.
//
// Hangfire is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3
// of the License, or any later version.
//
// Hangfire 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with Hangfire. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Globalization;
using Cronos;
using Hangfire.Annotations;
using Hangfire.Common;
using Hangfire.Storage;
namespace Hangfire
{
internal class RecurringJobEntity
{
private readonly IDictionary<string, string> _recurringJob;
public RecurringJobEntity(
[NotNull] string recurringJobId,
[NotNull] IDictionary<string, string> recurringJob,
[NotNull] ITimeZoneResolver timeZoneResolver,
DateTime now)
{
if (timeZoneResolver == null) throw new ArgumentNullException(nameof(timeZoneResolver));
_recurringJob = recurringJob ?? throw new ArgumentNullException(nameof(recurringJob));
RecurringJobId = recurringJobId ?? throw new ArgumentNullException(nameof(recurringJobId));
if (recurringJob.TryGetValue("Queue", out var queue) && !String.IsNullOrWhiteSpace(queue))
{
Queue = queue;
}
TimeZone = recurringJob.TryGetValue("TimeZoneId", out var timeZoneId) && !String.IsNullOrWhiteSpace(timeZoneId)
? timeZoneResolver.GetTimeZoneById(timeZoneId)
: TimeZoneInfo.Utc;
if (recurringJob.TryGetValue("Cron", out var cron) && !String.IsNullOrWhiteSpace(cron))
{
Cron = cron;
}
if (recurringJob.TryGetValue("Job", out var job) && !String.IsNullOrWhiteSpace(job))
{
Job = InvocationData.DeserializePayload(job).DeserializeJob();
}
if (recurringJob.TryGetValue("LastJobId", out var lastJobId) && !String.IsNullOrWhiteSpace(lastJobId))
{
LastJobId = lastJobId;
}
if (recurringJob.TryGetValue("LastExecution", out var lastExecution) && !String.IsNullOrWhiteSpace(lastExecution))
{
LastExecution = JobHelper.DeserializeDateTime(lastExecution);
}
if (recurringJob.TryGetValue("NextExecution", out var nextExecution) && !String.IsNullOrWhiteSpace(nextExecution))
{
NextExecution = JobHelper.DeserializeDateTime(nextExecution);
}
if (recurringJob.TryGetValue("CreatedAt", out var createdAt) && !String.IsNullOrWhiteSpace(createdAt))
{
CreatedAt = JobHelper.DeserializeDateTime(createdAt);
}
else
{
CreatedAt = now;
}
if (recurringJob.TryGetValue("V", out var v) && !String.IsNullOrWhiteSpace(v))
{
Version = int.Parse(v, CultureInfo.InvariantCulture);
}
}
public string RecurringJobId { get; }
public string Queue { get; set; }
public string Cron { get; set; }
public TimeZoneInfo TimeZone { get; set; }
public Job Job { get; set; }
public DateTime CreatedAt { get; }
public DateTime? NextExecution { get; }
public DateTime? LastExecution { get; set; }
public string LastJobId { get; set; }
public int? Version { get; set; }
public DateTime? GetNextExecution()
{
var parts = Cron.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
var format = CronFormat.Standard;
try
{
if (parts.Length == 6)
{
format |= CronFormat.IncludeSeconds;
}
else if (parts.Length != 5)
{
throw new CronFormatException(
$"Wrong number of parts in the `{Cron}` cron expression, you can only use 5 or 6 (with seconds) part-based expressions.");
}
return CronExpression.Parse(Cron, format).GetNextOccurrence(
LastExecution ?? CreatedAt.AddSeconds(-1),
TimeZone,
inclusive: false);
}
catch (Exception ex)
{
throw new ArgumentException("CRON expression is invalid. Please see the inner exception for details.", "cronExpression", ex);
}
}
public bool IsChanged(out IReadOnlyDictionary<string, string> changedFields, out DateTime? nextExecution)
{
changedFields = GetChangedFields(out nextExecution);
return changedFields.Count > 0 || nextExecution != NextExecution;
}
public IReadOnlyDictionary<string, string> GetChangedFields(out DateTime? nextExecution)
{
var result = new Dictionary<string, string>();
if ((_recurringJob.TryGetValue("Queue", out var queue) ? queue : null) != Queue)
{
result.Add("Queue", Queue);
}
if ((_recurringJob.TryGetValue("Cron", out var cron) ? cron : null) != Cron)
{
result.Add("Cron", Cron);
}
if ((_recurringJob.TryGetValue("TimeZoneId", out var timeZoneId) ? timeZoneId : null) != TimeZone.Id)
{
result.Add("TimeZoneId", TimeZone.Id);
}
var serializedJob = InvocationData.SerializeJob(Job).SerializePayload();
if ((_recurringJob.TryGetValue("Job", out var job) ? job : null) != serializedJob)
{
result.Add("Job", serializedJob);
}
var serializedCreatedAt = JobHelper.SerializeDateTime(CreatedAt);
if ((_recurringJob.TryGetValue("CreatedAt", out var createdAt) ? createdAt : null) != serializedCreatedAt)
{
result.Add("CreatedAt", serializedCreatedAt);
}
var serializedLastExecution = LastExecution.HasValue ? JobHelper.SerializeDateTime(LastExecution.Value) : null;
if ((_recurringJob.TryGetValue("LastExecution", out var lastExecution) ? lastExecution : null) !=
serializedLastExecution)
{
result.Add("LastExecution", serializedLastExecution ?? String.Empty);
}
nextExecution = GetNextExecution();
var serializedNextExecution = nextExecution.HasValue ? JobHelper.SerializeDateTime(nextExecution.Value) : null;
if ((_recurringJob.TryGetValue("NextExecution", out var nextExecutionValue) ? nextExecutionValue : null) !=
serializedNextExecution)
{
result.Add("NextExecution", serializedNextExecution ?? String.Empty);
}
if ((_recurringJob.TryGetValue("LastJobId", out var lastJobId) ? lastJobId : null) != LastJobId)
{
result.Add("LastJobId", LastJobId ?? String.Empty);
}
if (!_recurringJob.ContainsKey("V"))
{
result.Add("V", "2");
}
return result;
}
}
}