-
Notifications
You must be signed in to change notification settings - Fork 1
/
JobStateRestController.java
306 lines (272 loc) · 10.4 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
/*
* Copyright 2011-2020 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.ServletRequest;
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.appng.api.Environment;
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.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.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.Autowired;
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 final JobRecordService jobRecordService;
private final Scheduler scheduler;
private final Site site;
private final Application app;
// private @Autowired Environment env;
// private @Autowired HttpServletRequest request;
private @Value("${" + PropertyConstants.BEARER_TOKEN + "}") String bearerToken;
private @Value("${skipAuth:false}") boolean skipAuth;
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
public ServletRequest getRequest() {
return null;
}
@Lookup
public Environment getEnvironment() {
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) {
for (String siteName : new TreeSet<>(RequestUtil.getSiteNames(getEnvironment()))) {
Site site = RequestUtil.getSiteByName(getEnvironment(), siteName);
if (site.isActive()) {
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> records = jobRecordService.getJobRecords(site.getName(), application,
jobKey.getName(), startedAfter, now, null, null, new PageRequest(0, pageSize));
if (hasTimeUnit) {
jobState.setStartedAfter(toLocalTime(startedAfter));
}
jobState.setTotalRecords((int) records.getTotalElements());
if (withRecords) {
jobState.setRecords(records.map(r -> toRecord(r)).getContent());
}
if (hasTimeUnit) {
if (thresholdError > 0 && records.getTotalElements() < thresholdError) {
jobState.setState(JobState.StateEnum.ERROR);
} else if (thresholdWarn > 0 && records.getTotalElements() < thresholdWarn) {
jobState.setState(JobState.StateEnum.WARN);
} else {
jobState.setState(JobState.StateEnum.OK);
}
} else {
jobState.setState(JobState.StateEnum.UNDEFINED);
}
}
} 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(toLocalTime(r.getStartTime()));
jobRecord.setEnd(toLocalTime(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 toLocalTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime();
}
boolean isAuthorized() {
if (skipAuth) {
return true;
}
if (StringUtils.isBlank(bearerToken)) {
return false;
}
List<String> auths = EnumerationUtils
.toList(((HttpServletRequest) getRequest()).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());
}
}