-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueuedJob.cs
151 lines (125 loc) · 4.34 KB
/
QueuedJob.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
using System;
using System.Linq;
using System.Threading;
using Composite;
using Hangfire.Annotations;
using Hangfire.CompositeC1.Types;
using Hangfire.Logging;
using Hangfire.Storage;
namespace Hangfire.CompositeC1
{
public class QueuedJob : IFetchedJob
{
private static readonly object SyncRoot = new object();
private readonly ILog _logger = LogProvider.GetCurrentClassLogger();
private readonly CompositeC1Storage _storage;
private readonly Timer _timer;
private bool _disposed;
private bool _removedFromQueue;
private bool _requeued;
private readonly Guid _id;
private readonly DateTime? _fetchedAt;
private readonly string _queue;
public string JobId { get; }
public QueuedJob([NotNull] CompositeC1Storage storage,
Guid id,
[NotNull] string jobId,
[NotNull] string queue,
[NotNull] DateTime? fetchedAt)
{
Verify.ArgumentNotNull(storage, nameof(storage));
Verify.ArgumentNotNull(jobId, nameof(jobId));
Verify.ArgumentNotNull(queue, nameof(queue));
Verify.ArgumentNotNull(fetchedAt, nameof(fetchedAt));
_storage = storage;
_id = id;
_queue = queue;
_fetchedAt = fetchedAt;
JobId = jobId;
var keepAliveInterval = TimeSpan.FromSeconds(storage.Options.SlidingInvisibilityTimeout.TotalSeconds / 5);
_timer = new Timer(ExecuteKeepAliveQuery, null, keepAliveInterval, keepAliveInterval);
}
public void RemoveFromQueue()
{
lock (SyncRoot)
{
if (!_fetchedAt.HasValue)
{
return;
}
_storage.UseConnection(connection =>
{
var queuedJob = connection.Get<IJobQueue>().SingleOrDefault(q => q.Id == _id && q.Queue == _queue && q.FetchedAt == _fetchedAt);
if (queuedJob != null)
{
connection.Delete(queuedJob);
}
});
_removedFromQueue = true;
}
}
public void Requeue()
{
lock (SyncRoot)
{
if (!_fetchedAt.HasValue)
{
return;
}
_storage.UseConnection(connection =>
{
var queuedJob = connection.Get<IJobQueue>().SingleOrDefault(q => q.Id == _id && q.FetchedAt == _fetchedAt);
if (queuedJob != null)
{
queuedJob.FetchedAt = null;
connection.Update(queuedJob);
}
});
_requeued = true;
}
}
public void Dispose()
{
if (_disposed)
{
return;
}
_disposed = true;
_timer?.Dispose();
lock (SyncRoot)
{
if (!_removedFromQueue && !_requeued)
{
Requeue();
}
}
}
private void ExecuteKeepAliveQuery(object obj)
{
lock (SyncRoot)
{
if (_requeued || _removedFromQueue)
{
return;
}
try
{
_storage.UseConnection(connection =>
{
var queuedJob = connection.Get<IJobQueue>().SingleOrDefault(q => q.Id == _id);
if (queuedJob != null)
{
queuedJob.FetchedAt = DateTime.UtcNow;
connection.Update(queuedJob);
}
});
_logger.Trace($"Keep-alive query for message {_id} sent");
}
catch (Exception ex)
{
_logger.DebugException($"Unable to execute keep-alive query for message {_id}", ex);
}
}
}
}
}