forked from ebx/ebx-jobqueue-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueuedJobCommand.java
295 lines (265 loc) · 8.98 KB
/
QueuedJobCommand.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
295
/*
* 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.commands;
import com.echobox.jobqueue.CoreJobType;
import com.echobox.jobqueue.JobQueue;
import com.echobox.jobqueue.PersistentJobCommandOnQueue;
import com.echobox.jobqueue.PersistentQueuedJobCommandQueue;
import com.echobox.jobqueue.QueuedJobId;
import com.echobox.jobqueue.commands.behaviour.JobCommandInterruptionStrategy;
import com.echobox.jobqueue.commands.status.FutureJobCommandSuccess;
import com.echobox.jobqueue.context.JobCommandExecutionContext;
import com.echobox.jobqueue.events.JobCommandEvent;
import com.echobox.jobqueue.events.JobCommandStatsGatherer;
import com.echobox.jobqueue.status.JobProgressReport;
import com.echobox.jobqueue.status.JobStatus;
import com.echobox.jobqueue.status.JobSuccess;
import com.echobox.jobqueue.status.JobUnknownStatus;
import com.echobox.jobqueue.status.ProgressStatsUnavailable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.concurrent.Future;
/**
* JobCommand decorator which wraps a JobCommand we wish to queue instead of executing locally
*
* @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
* @param <J> The type of JobCommand which is being queued
*
* @author Michael Lavelle
*/
public class QueuedJobCommand<C extends JobCommandExecutionContext<C, Q, I>,
Q extends Serializable, I extends Serializable, J extends
JobCommand<C>>
extends CoordinatorJobCommand<C> {
/**
* Default serialization id
*/
private static final long serialVersionUID = 1L;
/**
* A logging instance for this class
*/
private static Logger logger = LoggerFactory.getLogger(QueuedJobCommand.class);
/**
* The queue we are sending the job commands to.
*/
private PersistentQueuedJobCommandQueue<C, Q, I> jobQueue;
/**
* The job we are queuing
*/
private J queuedJob;
/**
* The type of queue
*/
private Q queueType;
/**
* The id
*/
private QueuedJobId<Q, I> queuedJobId;
/**
* Instantiates a new Queued job command.
*
* @param jobQueue The jobQueue we will send the queuedJob to
* @param queueType The type of queue we will send the queuedJob to
* @param queuedJob The job we wish to queue
* @param jobCreationTimeUnix the job creation time unix
*/
public QueuedJobCommand(PersistentQueuedJobCommandQueue<C, Q, I> jobQueue, Q queueType,
J queuedJob, long jobCreationTimeUnix) {
super(new CoreJobCommandType(CoreJobType.QUEUED_JOB), jobCreationTimeUnix);
this.jobQueue = jobQueue;
this.queuedJob = queuedJob;
this.queueType = queueType;
if (JobCommandStatsGatherer.isEnabled()) {
queuedJob.addCompletionListener(
new JobCommandStatsGatherer<>(queueType, queuedJob.getJobCreationTimeUnix()));
}
}
/**
* Constructor for creating a queuedJob command where we already have the objectId
*
* @param jobQueue The jobQueue we will send the queuedJob to
* @param queuedJob The job we wish to queue
* @param jobCreationTimeUnix the job creation time unix
* @param queuedJobId the queued job id
*/
public QueuedJobCommand(PersistentQueuedJobCommandQueue<C, Q, I> jobQueue, J queuedJob,
long jobCreationTimeUnix, QueuedJobId<Q, I> queuedJobId) {
super(new CoreJobCommandType(CoreJobType.QUEUED_JOB), jobCreationTimeUnix);
this.jobQueue = jobQueue;
this.queuedJob = queuedJob;
this.queueType = queuedJobId.getQueueType();
this.queuedJobId = queuedJobId;
}
/**
* Gets queue type.
*
* @return The queueType
*/
public Q getQueueType() {
return queueType;
}
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.JobCommand#isAsynchronousExecution()
*/
/**
* By default this JobCommand is asynchronous by nature of the fact that execution adds the
* JobCommand to a JobCommandQueue for decoupled execution by consumers
*/
@Override
protected boolean isAsynchronousExecution() {
return true;
}
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.JobCommand#executeOrRetry(com.echobox.jobqueue.context.
* JobCommandExecutionContext,
* long)
*/
/**
* The default behaviour of this type of job is asynchronous - the JobCommand is added to
* the JobCommandQueue and a handle to the future JobSuccess status is returned, which
* also notifies of any exceptions occurring that prevent JobSuccess being achieved
*/
@Override
protected Future<JobSuccess> doExecute(C executionContext,
long defaultMaxFutureWaitTimeoutSeconds) throws Exception {
if (queuedJobId == null) {
queuedJobId = jobQueue.addJobToQueue(this, queueType);
} else {
boolean reset = jobQueue.resetJob(queuedJobId);
if (!reset) {
logger.warn(this.getLogMessage(executionContext, JobCommandEvent.JOB_ERROR,
"Call to reset job " + "wasn't successful"));
}
}
return new FutureJobCommandSuccess<C>(this, executionContext,
defaultMaxFutureWaitTimeoutSeconds);
}
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.CoordinatorJobCommand#getDelegatedCompletionStatus()
*/
@Override
protected JobStatus getDelegatedStatus() {
if (this.getQueuedJobId() == null) {
return new JobUnknownStatus();
} else {
JobStatus status = jobQueue.determineJobStatus(queuedJobId);
// When the queued job completes ensure to call it's completion listeners, these have
// already been called on the consumer side but this allows for the producer and consumer
// to have different actions on complete.
if (status.isCompleted()) {
final PersistentJobCommandOnQueue<C, Q, I> completedJob = jobQueue.getJob(queuedJobId);
if (completedJob != null) {
final long completionTimeUnix = completedJob.getJob().getCompletedUnixTime();
if (status.isCompletedWithoutError()) {
queuedJob.setSuccessfulCompletionUnixTime(completionTimeUnix);
} else if (status.isCompletedWithError()) {
queuedJob.setUnsuccessfulCompletionUnixTime(completionTimeUnix, status.getException());
}
} else {
logger.warn(String.format("Unable to read completed job with id: %s", queuedJobId));
}
}
return status;
}
}
/**
* Gets queued job id.
*
* @return The id
*/
public QueuedJobId<Q, I> getQueuedJobId() {
return queuedJobId;
}
/**
* Gets queued job.
*
* @return The job we are queuing
*/
public J getQueuedJob() {
return queuedJob;
}
/**
* Gets job queue.
*
* @return The queue that we use to queue these jobs
*/
public JobQueue<Q, I, QueuedJobCommand<C, Q, I, ?>, DequeuedJobCommand<C, Q, I>> getJobQueue() {
return jobQueue;
}
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.JobCommand#cleanUp(com.echobox.jobqueue.
* JobCommandExecutionContext)
*/
@Override
public void cleanUp(C executionContext) throws Exception {
if (queuedJobId != null) {
jobQueue.deleteJob(queuedJobId);
queuedJobId = null;
}
}
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.JobCommand#isCompletionStatusLoggingEnabled()
*/
@Override
public boolean isCompletionStatusLoggingEnabled(C context) {
return queuedJob.isCompletionStatusLoggingEnabled(context);
}
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.JobCommand#createInterruptStrategy()
*/
@Override
public JobCommandInterruptionStrategy createInterruptStrategy() {
return JobCommandInterruptionStrategy.NONE;
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.commands.JobCommand#getProgressReport()
*/
@Override
public JobProgressReport getProgressReport() {
return new ProgressStatsUnavailable();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.getClass().getSimpleName() + ":" + getJobType() + ":" + queuedJob.toString();
}
@Override
public void resetCompletionStatus() {
super.resetCompletionStatus();
// Ensure to also reset decorated job
queuedJob.resetCompletionStatus();
}
}