-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWorkerJobCommand.java
162 lines (142 loc) · 4.83 KB
/
WorkerJobCommand.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
/*
* 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.JobType;
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.status.JobProgressReport;
import com.echobox.jobqueue.status.JobStatus;
import com.echobox.jobqueue.status.JobSuccess;
import com.echobox.jobqueue.status.ProgressStatsUnavailable;
import com.echobox.time.UnixTime;
import org.slf4j.Logger;
import java.util.concurrent.Future;
/**
* WorkerJobCommands by default execute synchronously, and set their completion status
* according to whether they *doWork* successfully.
*
* @param <C> The type of JobCommandExecutionContext in which we execute commands
*
* @author Michael Lavelle
*/
public abstract class WorkerJobCommand<C extends JobCommandExecutionContext<C, ?, ?>>
extends JobCommand<C> {
/**
* Default serialization id
*/
private static final long serialVersionUID = 1L;
/**
* Instantiates a new Worker job command.
*
* @param jobType The type of this Job
* @param jobCreationTimeUnix The unix time when the job was created
*/
public WorkerJobCommand(JobType<?> jobType, long jobCreationTimeUnix) {
super(jobType, jobCreationTimeUnix);
}
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.JobCommand#isAsynchronousExecution()
*/
@Override
protected boolean isAsynchronousExecution() {
return false;
}
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.JobCommand#doExecute(com.echobox.jobqueue.
* JobCommandExecutionContext,
* long)
*/
@Override
protected Future<JobSuccess> doExecute(C executionContext,
long defaultMaxFutureWaitTimeoutSeconds) throws Exception {
try {
if (isCompletionStatusLoggingEnabled(executionContext)) {
getLogger()
.debug(getLogMessage(executionContext, JobCommandEvent.JOB_STARTED, "Executing job"));
}
doWork(executionContext);
if (isCompletionStatusLoggingEnabled(executionContext)) {
getLogger().debug(
getLogMessage(executionContext, JobCommandEvent.JOB_COMPLETED_SUCCESSFULLY,
"Job Completed Successfully"));
}
setSuccessfulCompletionUnixTime(UnixTime.now());
return new FutureJobCommandSuccess<C>(this, executionContext,
defaultMaxFutureWaitTimeoutSeconds);
} catch (Exception e) {
getLogger().error(
getLogMessage(executionContext, JobCommandEvent.JOB_COMPLETED_UNSUCCESSFULLY,
"Job Completed Unsuccessfully"), e);
setUnsuccessfulCompletionUnixTime(UnixTime.now(), e);
throw e;
}
}
/**
* Do work.
*
* @param executionContext The execution context we perform the work within
* @throws Exception Any exception thrown performing the work
*/
protected abstract void doWork(C executionContext) throws Exception;
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.JobCommand#cleanUp(com.echobox.jobqueue.context.
* JobCommandExecutionContext)
*/
@Override
public void cleanUp(C executionContext) throws Exception {
// No-op by default
}
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.JobCommand#createInterruptStrategy()
*/
@Override
public JobCommandInterruptionStrategy createInterruptStrategy() {
return JobCommandInterruptionStrategy.NONE;
}
/*
* (non-Javadoc)
*
* @see com.echobox.jobqueue.commands.JobCommand#determineJobStatus()
*/
@Override
public final JobStatus determineJobStatus() {
return getJobCompletionStatus();
}
/* (non-Javadoc)
* @see com.echobox.jobqueue.commands.JobCommand#getProgressReport()
*/
@Override
public JobProgressReport getProgressReport() {
return new ProgressStatsUnavailable();
}
/**
* Gets logger.
*
* @return a logger for the specific subclass of WorkerJobCommand
*/
protected abstract Logger getLogger();
}