/
JobManager.java
245 lines (224 loc) · 8.37 KB
/
JobManager.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
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* 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 org.jumpmind.symmetric.job;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.jumpmind.db.sql.ISqlTransaction;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.SymmetricException;
import org.jumpmind.symmetric.model.JobDefinition;
import org.jumpmind.symmetric.service.impl.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
/*
* @see IJobManager
*/
public class JobManager extends AbstractService implements IJobManager {
private static final Logger log = LoggerFactory.getLogger(JobManager.class);
private List<IJob> jobs;
private ThreadPoolTaskScheduler taskScheduler;
private ISymmetricEngine engine;
private JobCreator jobCreator = new JobCreator();
private boolean started = false;
public JobManager(ISymmetricEngine engine) {
super(engine.getParameterService(), engine.getSymmetricDialect());
this.engine = engine;
this.taskScheduler = new ThreadPoolTaskScheduler();
setSqlMap(new JobManagerSqlMap(engine.getSymmetricDialect().getPlatform(), createSqlReplacementTokens()));
this.taskScheduler.setThreadNamePrefix(String.format("%s-job-", engine.getParameterService().getEngineName()));
this.taskScheduler.setPoolSize(20);
this.taskScheduler.initialize();
}
@Override
public void init() {
this.stopJobs();
List<JobDefinition> jobDefitions = loadJobs(engine);
BuiltInJobs builtInJobs = new BuiltInJobs();
jobDefitions = builtInJobs.syncBuiltInJobs(jobDefitions, engine, taskScheduler); // TODO save built in jobs
this.jobs = new ArrayList<IJob>();
for (JobDefinition jobDefinition : jobDefitions) {
IJob job = jobCreator.createJob(jobDefinition, engine, taskScheduler);
if (job != null) {
jobs.add(job);
}
}
}
protected List<JobDefinition> loadJobs(ISymmetricEngine engine) {
return sqlTemplate.query(getSql("loadCustomJobs"), new JobMapper());
}
@Override
public boolean isStarted() {
return started;
}
@Override
public IJob getJob(String name) {
for (IJob job : jobs) {
if (job.getName().equalsIgnoreCase(name)) {
return job;
}
}
return null;
}
/*
* Start the jobs if they are configured to be started
*/
@Override
public synchronized void startJobs() {
for (IJob job : jobs) {
if (isAutoStartConfigured(job) && isJobApplicableToNodeGroup(job)) {
job.start();
} else {
log.info("Job {} not configured for auto start", job.getName());
}
}
started = true;
}
@Override
public boolean isJobApplicableToNodeGroup(IJob job) {
String nodeGroupId = job.getJobDefinition().getNodeGroupId();
if (StringUtils.isEmpty(nodeGroupId) || nodeGroupId.equals("ALL")) {
return true;
}
return engine.getParameterService().getNodeGroupId().equals(nodeGroupId);
}
protected boolean isAutoStartConfigured(IJob job) {
String autoStartValue = null;
if (job.getDeprecatedStartParameter() != null) {
autoStartValue = engine.getParameterService().getString(job.getDeprecatedStartParameter());
}
if (StringUtils.isEmpty(autoStartValue)) {
autoStartValue = engine.getParameterService().getString(job.getJobDefinition().getStartParameter());
if (StringUtils.isEmpty(autoStartValue)) {
autoStartValue = String.valueOf(job.getJobDefinition().isDefaultAutomaticStartup());
}
}
return "1".equals(autoStartValue) || Boolean.parseBoolean(autoStartValue);
}
@Override
public synchronized void stopJobs() {
if (jobs != null) {
for (IJob job : jobs) {
job.stop();
}
Thread.interrupted();
started = false;
}
}
@Override
public synchronized void destroy() {
stopJobs();
if (taskScheduler != null) {
taskScheduler.shutdown();
}
}
@Override
public List<IJob> getJobs() {
List<IJob> sortedJobs = sortJobs(jobs);
return sortedJobs;
}
protected List<IJob> sortJobs(List<IJob> jobs) {
List<IJob> jobsSorted = new ArrayList<>();
if (jobs != null) {
jobsSorted.addAll(jobs);
Collections.sort(jobsSorted, new Comparator<IJob>() {
@Override
public int compare(IJob job1, IJob job2) {
Integer job1Started = job1.isStarted() ? 1 : 0;
Integer job2Started = job2.isStarted() ? 1 : 0;
if (job1Started.equals(job2Started)) {
return -job1.getJobDefinition().getJobType().compareTo(job2.getJobDefinition().getJobType());
} else {
return -job1Started.compareTo(job2Started);
}
}
});
}
return jobsSorted;
}
@Override
public void restartJobs() {
this.init();
this.startJobs();
}
@Override
public void restartJob(String name) {
IJob job = getJob(name);
if (job != null) {
job.stop();
if (job instanceof AbstractJob) {
job.getJobDefinition().setDefaultAutomaticStartup(((AbstractJob) job).getDefaults().isEnabled());
}
if (isAutoStartConfigured(job) && isJobApplicableToNodeGroup(job)) {
job.start();
}
}
}
@Override
public void saveJob(JobDefinition job) {
Object[] args = { job.getDescription(), job.getJobType().toString(),
job.getJobExpression(), job.isDefaultAutomaticStartup() ? 1 : 0, job.getDefaultSchedule(),
job.getNodeGroupId(), job.getCreateBy(), job.getLastUpdateBy(), job.getJobName() };
if (sqlTemplate.update(getSql("updateJobSql"), args) <= 0) {
sqlTemplate.update(getSql("insertJobSql"), args);
}
}
@Override
public void saveJobAsCopy(JobDefinition job) {
String newName = job.getJobName();
List<String> names = jobs.stream().map(IJob::getName).collect(Collectors.toList());
String suffix = "";
for (int i = 2; names.contains(newName + suffix); i++) {
suffix = "_" + i;
}
job.setJobName(newName + suffix);
saveJob(job);
}
@Override
public void editJob(String oldName, JobDefinition job) {
ISqlTransaction transaction = null;
try {
transaction = sqlTemplate.startSqlTransaction();
removeJob(oldName);
saveJob(job);
transaction.commit();
} catch (Exception ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} finally {
close(transaction);
}
}
@Override
public void removeJob(String name) {
Object[] args = { name };
if (sqlTemplate.update(getSql("deleteJobSql"), args) == 1) {
} else {
throw new SymmetricException("Failed to remove job " + name + ". Note that BUILT_IN jobs cannot be removed.");
}
}
}