forked from ebx/ebx-jobqueue-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistentQueuedJobCommandQueueImpl.java
294 lines (252 loc) · 11 KB
/
PersistentQueuedJobCommandQueueImpl.java
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.echobox.jobqueue;
import com.echobox.jobqueue.commands.DequeuedJobCommand;
import com.echobox.jobqueue.commands.JobCommand;
import com.echobox.jobqueue.commands.QueuedJobCommand;
import com.echobox.jobqueue.config.QueueConfig;
import com.echobox.jobqueue.config.QueueConfigFactory;
import com.echobox.jobqueue.context.JobCommandExecutionContext;
import com.echobox.jobqueue.status.JobFailedStatus;
import com.echobox.jobqueue.status.JobStatus;
import com.echobox.jobqueue.status.JobSuccess;
import com.echobox.jobqueue.status.JobUnknownStatus;
import com.echobox.time.UnixTime;
import org.javatuples.Pair;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeoutException;
/**
* A type of PersistentQueuedJobCommandQueue implementation which is backed by a
* PersistentJobCommandQueue - a simpler interface to implement. Jobs on the queue as are stored
* as instances of PersistentJobCommandOnQueue
*
* @param <C> The type of JobCommandExecutionContext in which we execute commands
* @param <Q> The type of the queue-type identifier
* @param <I> The type of unique identifier for each job on the queue
*
* @author Michael Lavelle
*/
public class PersistentQueuedJobCommandQueueImpl<C extends
JobCommandExecutionContext<C, Q, I>, Q extends Serializable, I extends Serializable>
implements PersistentQueuedJobCommandQueue<C, Q, I> {
private PersistentJobCommandQueue<C, Q, I> persistentJobCommandQueue;
private QueueConfigFactory<Q> queueConfigFactory;
/**
* Instantiates a new PersistentQueuedJobCommandQueueImpl which delegates to the
* provided PersistentJobCommandQueue implementation ( a simpler interface to
* implement)
*
* @param persistentJobCommandQueue The persistent JobCommand queue
* @param queueConfigFactory the queue config factory
*/
public PersistentQueuedJobCommandQueueImpl(PersistentJobCommandQueue<C, Q, I>
persistentJobCommandQueue,
QueueConfigFactory<Q> queueConfigFactory) {
this.persistentJobCommandQueue = persistentJobCommandQueue;
this.queueConfigFactory = queueConfigFactory;
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobCommandOnQueueRepository#setJobSuccessfulCompletion(
* com.echobox.jobqueue.QueuedJobId, java.lang.String, long)
*/
@Override
public boolean setJobSuccessfulCompletion(QueuedJobId<Q, I> queuedJobId, String consumerId,
long completedUnixTime) {
return persistentJobCommandQueue.setJobSuccessfulCompletion(queuedJobId, consumerId,
completedUnixTime);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobCommandOnQueueRepository#setJobUnsuccessfulCompletion(
* com.echobox.jobqueue.QueuedJobId, java.lang.Exception, java.lang.String, long)
*/
@Override
public boolean setJobUnsuccessfulCompletion(QueuedJobId<Q, I> queuedJobId, Exception exception,
String consumerId, long completedUnixTime) {
return persistentJobCommandQueue
.setJobUnsuccessfulCompletion(queuedJobId, exception, consumerId, completedUnixTime);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.PersistentQueuedJobCommandQueue#determineJobStatus(
* com.echobox.jobqueue.QueuedJobId)
*/
@Override
public JobStatus determineJobStatus(QueuedJobId<Q, I> queuedJobId) {
PersistentJobOnQueue<Q, I, JobCommand<C>> queuedJob =
persistentJobCommandQueue.getJob(queuedJobId);
if (queuedJob == null) {
return new JobFailedStatus(
new IllegalStateException("Job with id:" + queuedJobId + " doesn't exist on the queue"));
} else {
// Work out current completion status flags
boolean completed = queuedJob.getJob().getJobCompletionStatus().isCompleted();
boolean hasError = queuedJob.getJob().getExceptionMessage() != null;
boolean isConsumed = queuedJob.getConsumerId() != null;
QueueConfig queueConfig = queueConfigFactory.getQueueConfig(queuedJob.getId().getQueueType());
// Set last heartbeat time to be the consumption time if the consumption time
// was in the last (heartbeat interval * 2) seconds
Long lastHeartbeatTimeUnix = queuedJob.getLastHeartbeatTimeUnix();
if (queuedJob != null && lastHeartbeatTimeUnix == null
&& queuedJob.getConsumptionTimeUnix() != null && queuedJob.getConsumptionTimeUnix()
> UnixTime.now() - queueConfig.getHeartbeatPulsePeriodSecs() * 2) {
lastHeartbeatTimeUnix = queuedJob.getConsumptionTimeUnix();
}
// Require a heartbeat with the last 2 * heartbeatPulsePeriodSecs seconds
long twoHeartbeatDurationSeconds = queueConfig.getHeartbeatPulsePeriodSecs() * 2;
long requiredHeartbeatAfterUnixTime = UnixTime.now() - twoHeartbeatDurationSeconds;
if (completed) {
// If the job is completed, return the appropriate status
if (hasError) {
return new JobFailedStatus(
new RuntimeException(queuedJob.getJob().getExceptionMessage()));
} else {
return new JobSuccess();
}
} else if (isConsumed) {
// If the job has been consumed, but not completed always check the last heartbeat time
if (lastHeartbeatTimeUnix == null
|| lastHeartbeatTimeUnix < requiredHeartbeatAfterUnixTime) {
Exception noHeartbeatException = new TimeoutException(
"No heartbeat in the last:" + twoHeartbeatDurationSeconds
+ " seconds for queued job with id:" + queuedJobId);
// Treat the job as effectively completed
setJobUnsuccessfulCompletion(queuedJobId, noHeartbeatException, null,
UnixTime.now());
return new JobFailedStatus(noHeartbeatException);
}
}
}
return new JobUnknownStatus();
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.PersistentQueuedJobCommandQueue#resetJob(
* com.echobox.jobqueue.QueuedJobId)
*/
@Override
public boolean resetJob(QueuedJobId<Q, I> queuedJobId) {
return persistentJobCommandQueue.resetJob(queuedJobId);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobCommandOnQueueRepository#deleteJob(com.echobox.jobqueue.
* QueuedJobId)
*/
@Override
public boolean deleteJob(QueuedJobId<Q, I> queuedJobId) {
return persistentJobCommandQueue.deleteJob(queuedJobId);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobCommandOnQueueRepository#deleteAllUnconsumedJobs(
* java.io.Serializable)
*/
@Override
public boolean deleteAllUnconsumedJobs(Q queueType) {
return persistentJobCommandQueue.deleteAllUnconsumedJobs(queueType);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobCommandOnQueueRepository#reserveAllUnconsumedJobs(
* java.io.Serializable, java.lang.String)
*/
@Override
public boolean reserveAllUnconsumedJobs(Q queueType, String producerId) {
return persistentJobCommandQueue.reserveAllUnconsumedJobs(queueType, producerId);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobQueue#getNextJob(java.lang.String, java.io.Serializable)
*/
@Override
public Pair<QueuedJobId<Q, I>, DequeuedJobCommand<C, Q, I>> getNextJob(String consumerId,
Q queueType) {
Pair<QueuedJobId<Q, I>, PersistentJobCommandOnQueue<C, Q, I>> queuedJobNoSQLDataPair =
persistentJobCommandQueue.getNextJob(consumerId, queueType);
if (queuedJobNoSQLDataPair != null) {
PersistentJobOnQueue<Q, I, JobCommand<C>> queuedJobNoSQLData =
queuedJobNoSQLDataPair.getValue1();
DequeuedJobCommand<C, Q, I> dequeuedJobCommand =
new DequeuedJobCommand<>(this, queuedJobNoSQLData.getJob(),
queuedJobNoSQLData.getJob().getJobCreationTimeUnix(), queuedJobNoSQLData.getId(),
consumerId);
return new Pair<>(queuedJobNoSQLData.getId(),
dequeuedJobCommand);
}
return null;
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobCommandOnQueueRepository#updateHeartbeatTime(
* com.echobox.jobqueue.QueuedJobId, java.lang.String, long)
*/
@Override
public boolean updateHeartbeatTime(QueuedJobId<Q, I> queuedJobId, String consumerId,
long heartbeatUnixTime) {
return persistentJobCommandQueue.updateHeartbeatTime(queuedJobId, consumerId,
heartbeatUnixTime);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobCommandOnQueueRepository#getJob(com.echobox.jobqueue.QueuedJobId)
*/
@Override
public PersistentJobCommandOnQueue<C, Q, I> getJob(QueuedJobId<Q, I> queuedJobId) {
return persistentJobCommandQueue.getJob(queuedJobId);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobCommandOnQueueRepository#deleteAllJobs(java.io.Serializable)
*/
@Override
public boolean deleteAllJobs(Q queueType) {
return persistentJobCommandQueue.deleteAllJobs(queueType);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobQueue#getJobCount(java.io.Serializable)
*/
@Override
public long getJobCount(Q queueType) {
return persistentJobCommandQueue.getJobCount(queueType);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobQueue#addJobToQueue(com.echobox.jobqueue.Job,
* java.io.Serializable)
*/
@Override
public QueuedJobId<Q, I> addJobToQueue(QueuedJobCommand<C, Q, I, ?> queuedJobCommand,
Q queueType) {
return persistentJobCommandQueue.addJobToQueue(queuedJobCommand.getQueuedJob(), queueType);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobQueue#addJobToQueueWithId(com.echobox.jobqueue.Job,
* com.echobox.jobqueue.QueuedJobId)
*/
@Override
public QueuedJobId<Q, I> addJobToQueueWithId(QueuedJobCommand<C, Q, I, ?> queuedJobCommand,
QueuedJobId<Q, I> queuedJobId) {
return persistentJobCommandQueue.addJobToQueueWithId(queuedJobCommand, queuedJobId);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobCommandOnQueueRepository#saveJob(
* com.echobox.jobqueue.PersistentJobCommandOnQueue)
*/
@Override
public QueuedJobId<Q, I> saveJob(
PersistentJobCommandOnQueue<C, Q, I> persistentJobCommandOnQueue) {
return persistentJobCommandQueue.saveJob(persistentJobCommandOnQueue);
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.JobCommandOnQueueRepository#getAllConsumedJobs(java.io.Serializable)
*/
@Override
public List<PersistentJobCommandOnQueue<C, Q, I>> getAllConsumedJobs(Q queueType) {
return persistentJobCommandQueue.getAllConsumedJobs(queueType);
}
}