-
Notifications
You must be signed in to change notification settings - Fork 1
/
JobStateRestController.java
342 lines (307 loc) · 12.7 KB
/
JobStateRestController.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright 2011-2022 the original author or authors.
*
* Licensed 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 org.appng.application.scheduler.service;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.EnumerationUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.appng.api.Environment;
import org.appng.api.Request;
import org.appng.api.RequestUtil;
import org.appng.api.ScheduledJobResult.ExecutionResult;
import org.appng.api.SiteProperties;
import org.appng.api.model.Application;
import org.appng.api.model.Site;
import org.appng.api.model.Site.SiteState;
import org.appng.api.support.environment.DefaultEnvironment;
import org.appng.application.scheduler.Constants;
import org.appng.application.scheduler.PropertyConstants;
import org.appng.core.controller.HttpHeaders;
import org.appng.core.domain.JobExecutionRecord;
import org.appng.scheduler.openapi.JobStateApi;
import org.appng.scheduler.openapi.model.Job;
import org.appng.scheduler.openapi.model.JobRecord;
import org.appng.scheduler.openapi.model.JobState;
import org.appng.scheduler.openapi.model.JobState.StateNameEnum;
import org.appng.scheduler.openapi.model.Jobs;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* {@link RestController} providing a REST API to query for {@link JobState}s
*
* @author Matthias Müller
*/
@Slf4j
@RestController
@RequiredArgsConstructor
public class JobStateRestController implements JobStateApi {
private static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
private final JobRecordService jobRecordService;
private final Scheduler scheduler;
private final Site site;
private final Application app;
private @Value("${" + PropertyConstants.BEARER_TOKEN + "}") String bearerToken;
private @Value("${skipAuth:false}") boolean skipAuth;
private @Value("${checkFirstRun:true}") boolean checkFirstRun;
public enum TimeUnit {
YEAR, MONTH, WEEK, DAY, HOUR, MINUTE;
public Date getStartDate(Date now) {
switch (this) {
case MINUTE:
return DateUtils.addMinutes(now, -1);
case HOUR:
return DateUtils.addHours(now, -1);
case WEEK:
return DateUtils.addWeeks(now, -1);
case MONTH:
return DateUtils.addMonths(now, -1);
case YEAR:
return DateUtils.addYears(now, -1);
default:
return DateUtils.addDays(now, -1);
}
}
}
@Lookup
protected Request getRequest() {
return null;
}
@Override
public ResponseEntity<Jobs> getJobs(
@RequestParam(value = "jobdata", required = false, defaultValue = "false") Boolean addJobdata,
@RequestParam(value = "all", required = false, defaultValue = "false") Boolean addAll,
@RequestParam(value = "thresholds", required = false) Boolean thresholds) {
if (!isAuthorized()) {
return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
}
List<Job> jobList = Lists.newArrayList();
if (addAll) {
Environment env = getRequest().getEnvironment();
for (String siteName : new TreeSet<>(RequestUtil.getSiteNames(env))) {
Site site = RequestUtil.getSiteByName(env, siteName);
if (site.isActive() && SiteState.STARTED.equals(site.getState())) {
try {
Application schedulerApp = site.getApplication(app.getName());
if (null != schedulerApp) {
addJobs(addJobdata, thresholds, jobList, site, schedulerApp);
}
} catch (SchedulerException e) {
log.error("error while retrieving jobs for site " + site.getName(), e);
}
}
}
} else {
try {
addJobs(addJobdata, thresholds, jobList, site, app);
} catch (SchedulerException e) {
log.error("error while retrieving jobs for site " + site.getName(), e);
}
}
return ResponseEntity.ok(new Jobs().jobs(jobList));
}
protected void addJobs(Boolean addJobdata, Boolean thresholds, List<Job> jobList, Site site,
Application schedulerApp) throws SchedulerException {
Scheduler siteScheduler = schedulerApp.getBean(Scheduler.class);
Set<JobKey> jobKeys = siteScheduler.getJobKeys(GroupMatcher.jobGroupEquals(site.getName()));
for (JobKey jobKey : jobKeys) {
JobDetail jobDetail = siteScheduler.getJobDetail(jobKey);
JobDataMap jobDataMap = jobDetail.getJobDataMap();
Boolean thresholdsPresent = jobDataMap.containsKey(Constants.THRESHOLD_TIMEUNIT)
&& (jobDataMap.containsKey(Constants.THRESHOLD_WARN)
|| jobDataMap.containsKey(Constants.THRESHOLD_ERROR));
if (null == thresholds || thresholdsPresent.equals(thresholds)) {
Job job = new Job();
job.setSite(site.getName());
String name = jobKey.getName();
String[] splittedName = name.split("_");
job.setApplication(splittedName[0]);
job.setJob(splittedName[1]);
String servicePath = site.getProperties().getString(SiteProperties.SERVICE_PATH);
String detail = String.format("%s%s/%s/appng-scheduler/rest/jobState/%s/%s", site.getDomain(),
servicePath, site.getName(), job.getApplication(), job.getJob());
job.setSelf(detail);
if (addJobdata) {
job.setJobData(jobDataMap.getWrappedMap());
}
job.setThresholdsPresent(thresholdsPresent);
jobList.add(job);
}
}
}
@Override
public ResponseEntity<JobState> getJobState(
// @formatter:off
@PathVariable("application") String application,
@PathVariable("job") String job,
@RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
@RequestParam(value = "records", required = false, defaultValue = "false") Boolean records
// @formatter:on
) {
if (!isAuthorized()) {
return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
}
JobState jobState = getState(application, job, pageSize, records);
if (null != jobState) {
return ResponseEntity.ok(jobState);
}
return ResponseEntity.notFound().build();
}
private JobState getState(String application, String job, Integer pageSize, boolean withRecords) {
JobState jobState = null;
try {
String jobName = job.startsWith(application) ? job : application + "_" + job;
JobKey jobKey = new JobKey(jobName, site.getName());
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
if (null != jobDetail) {
jobState = new JobState();
jobState.setSite(site.getName());
jobState.setApplication(application);
jobState.setJob(jobName);
JobDataMap jobDataMap = jobDetail.getJobDataMap();
jobState.setJobData(jobDataMap.getWrappedMap());
int thresholdWarn = -1;
int thresholdError = -1;
if (jobDataMap.containsKey(Constants.THRESHOLD_WARN)) {
thresholdWarn = jobDataMap.getInt(Constants.THRESHOLD_WARN);
}
if (jobDataMap.containsKey(Constants.THRESHOLD_ERROR)) {
thresholdError = jobDataMap.getInt(Constants.THRESHOLD_ERROR);
}
TimeUnit timeunit = null;
if (jobDataMap.containsKey(Constants.THRESHOLD_TIMEUNIT)) {
timeunit = TimeUnit.valueOf(jobDataMap.getString(Constants.THRESHOLD_TIMEUNIT).toUpperCase());
}
Date now = new Date();
boolean hasTimeUnit = null != timeunit;
Date startedAfter = hasTimeUnit ? timeunit.getStartDate(now) : null;
Page<JobExecutionRecord> okRecords = jobRecordService.getJobRecords(site.getName(), application,
jobKey.getName(), startedAfter, now, ExecutionResult.SUCCESS.name(), null,
new PageRequest(0, 1));
Page<JobExecutionRecord> failedRecords = jobRecordService.getJobRecords(site.getName(), application,
jobKey.getName(), startedAfter, now, ExecutionResult.FAIL.name(), null, new PageRequest(0, 1));
if (hasTimeUnit) {
jobState.setStartedAfter(toOffsetDateTime(startedAfter));
}
int totalSuccess = (int) okRecords.getTotalElements();
int totalFailed = (int) failedRecords.getTotalElements();
int total = totalSuccess + totalFailed;
jobState.setTotalFailed(totalFailed);
jobState.setTotalSuccess(totalSuccess);
jobState.setTotalRecords(total);
if (withRecords) {
Page<JobExecutionRecord> allRecords = jobRecordService.getJobRecords(site.getName(), application,
jobKey.getName(), startedAfter, now, null, null, new PageRequest(0, pageSize));
jobState.setRecords(allRecords.map(r -> toRecord(r)).getContent());
}
String message = "Tresholds and/or time unit have not been definded.";
boolean hasErrorTreshold = thresholdError > 0;
boolean hasWarnTreshold = thresholdWarn > 0;
StateNameEnum state = StateNameEnum.OK;
StateNameEnum logState = state;
int treshold;
String operand = "less than";
if (hasTimeUnit && (hasWarnTreshold || hasErrorTreshold)) {
JobExecutionRecord firstRun = jobRecordService.getFirstRun(site.getName(), application,
jobKey.getName());
if (checkFirstRun && null != firstRun && firstRun.getStartTime().after(startedAfter)) {
message = String.format(
"Job first run at %s, so not enough data to validate tresholds based on %s.",
DATE_FORMAT.format(firstRun.getStartTime()), DATE_FORMAT.format(startedAfter));
} else {
String messageFormat = "The job failed %s time(s) and succeeded %s time(s) during the last %s, which is %s the %s treshold of %s.";
if (hasErrorTreshold && totalSuccess < thresholdError) {
logState = state = StateNameEnum.ERROR;
treshold = thresholdError;
} else if (hasWarnTreshold && totalSuccess < thresholdWarn) {
logState = state = StateNameEnum.WARN;
treshold = thresholdWarn;
} else {
treshold = hasWarnTreshold ? thresholdWarn : (hasErrorTreshold ? thresholdError : -1);
logState = hasWarnTreshold ? StateNameEnum.WARN
: (hasErrorTreshold ? StateNameEnum.ERROR : StateNameEnum.OK);
operand = "greater than/equal to";
}
message = String.format(messageFormat, totalFailed, totalSuccess, timeunit, operand, logState,
treshold);
}
}
jobState.setStateName(state);
jobState.setState(state.ordinal());
jobState.setMessage(message);
}
} catch (SchedulerException e) {
log.error("error while retrieving job", e);
}
return jobState;
}
private JobRecord toRecord(JobExecutionRecord r) {
JobRecord jobRecord = new JobRecord();
jobRecord.setId(r.getId());
jobRecord.setStart(toOffsetDateTime(r.getStartTime()));
jobRecord.setEnd(toOffsetDateTime(r.getEndTime()));
jobRecord.setRunOnce(r.isRunOnce());
jobRecord.setDuration(r.getDuration().intValue());
jobRecord.setStacktrace(r.getStacktraces());
ExecutionResult execResult = ExecutionResult.valueOf(r.getResult());
jobRecord.setState(
ExecutionResult.SUCCESS.equals(execResult) ? JobRecord.StateEnum.OK : JobRecord.StateEnum.ERROR);
return jobRecord;
}
private OffsetDateTime toOffsetDateTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime();
}
boolean isAuthorized() {
if (skipAuth) {
return true;
}
if (StringUtils.isBlank(bearerToken)) {
return false;
}
DefaultEnvironment env = (DefaultEnvironment) getRequest().getEnvironment();
HttpServletRequest servletRequest = env.getServletRequest();
List<String> auths = EnumerationUtils.toList(servletRequest.getHeaders(HttpHeaders.AUTHORIZATION));
return null != auths && auths.contains("Bearer " + bearerToken);
}
protected List<String> getCleanedList(String value) {
return Arrays.asList(StringUtils.trimToEmpty(value).split(",")).stream().map(StringUtils::trim)
.collect(Collectors.toList());
}
}