DynamicJobManager.java
5.71 KB
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
package com.ecar.apm.quartz;
import org.apache.commons.lang.StringUtils;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ecar.apm.context.BeanProvider;
import com.ecar.apm.job.HttpMonitoringJob;
import com.ecar.apm.model.HttpSequence;
import com.ecar.apm.service.HttpRequestService;
/**
* @author Shengzhao Li
*/
public class DynamicJobManager {
private static final Logger LOGGER = LoggerFactory.getLogger(DynamicJobManager.class);
private static final String MONITORING_INSTANCE_JOB_NAME_PREFIX = "monitoring-instance-";
public static String generateMonitoringInstanceJobName(String key) {
return MONITORING_INSTANCE_JOB_NAME_PREFIX + key;
}
private transient HttpRequestService httpRequestService = BeanProvider.getBean(HttpRequestService.class);
private HttpSequence instance;
public DynamicJobManager(HttpSequence instance) {
this.instance = instance;
}
public boolean enable() {
//final ApplicationInstance instance = instanceRepository.findByGuid(guid, ApplicationInstance.class);
if (instance.isEnabled()) {
LOGGER.debug("<{}> Instance[guid={}] already enabled, ignore it", username(), instance.getGuid());
return false;
}
final boolean addSuccessful = startupMonitoringJob(instance);
if (!addSuccessful) {
LOGGER.debug("<{}> NOTE: Add MonitoringJob[jobName={}] failed", username(), instance.getJobName());
return false;
}
//update
instance.setEnabled(true);
httpRequestService.updateEnabled(instance);
LOGGER.debug("<{}> Update ApplicationInstance[guid={}] enabled=true,jobName={}", username(), instance.getGuid(), instance.getJobName());
return true;
}
private boolean startupMonitoringJob(HttpSequence instance) {
final String jobName = getAndSetJobName(instance);
DynamicJob job = new DynamicJob(jobName)
.cronExpression(instance.getFrequency().getCronExpression())
.target(HttpMonitoringJob.class)
.addJobData(HttpMonitoringJob.APPLICATION_INSTANCE_GUID, instance.getGuid());
return executeStartup(instance, job);
}
private boolean executeStartup(HttpSequence instance, DynamicJob job) {
boolean result = false;
try {
if (DynamicSchedulerFactory.existJob(job)) {
result = DynamicSchedulerFactory.resumeJob(job);
LOGGER.debug("<{}> Resume [{}] by ApplicationInstance[guid={},instanceName={}] result: {}", username(), job, instance.getGuid(), instance.getName(), result);
} else {
result = DynamicSchedulerFactory.registerJob(job);
LOGGER.debug("<{}> Register [{}] by ApplicationInstance[guid={},instanceName={}] result: {}", username(), job, instance.getGuid(), instance.getName(), result);
}
} catch (SchedulerException e) {
LOGGER.error("<{}> Register [" + job + "] failed", username(), e);
}
return result;
}
private String getAndSetJobName(HttpSequence instance) {
String jobName = instance.getJobName();
if (StringUtils.isEmpty(jobName)) {
jobName = generateMonitoringInstanceJobName(instance.getGuid());
instance.setJobName(jobName);
}
return jobName;
}
private String username() {
return null;
//return SecurityUtils.currentUsername();
}
public boolean delete() {
if (instance.isEnabled()) {
LOGGER.debug("<{}> Forbid delete enabled ApplicationInstance[guid={}]", username(), instance.getGuid());
return false;
}
httpRequestService.deleteHttpLog(instance.getGuid());
checkAndRemoveJob(instance);
//logic delete
instance.setArchived(true);
httpRequestService.archivedHttpData(instance.getGuid());
LOGGER.debug("<{}> Archive ApplicationInstance[guid={}] and FrequencyMonitorLogs,MonitoringReminderLogs", username(), instance.getGuid());
return true;
}
private void checkAndRemoveJob(HttpSequence instance) {
DynamicJob job = new DynamicJob(getAndSetJobName(instance));
try {
if (DynamicSchedulerFactory.existJob(job)) {
final boolean result = DynamicSchedulerFactory.removeJob(job);
LOGGER.debug("<{}> Remove DynamicJob[{}] result [{}]", username(), job, result);
}
} catch (SchedulerException e) {
LOGGER.error("<{}> Remove [" + job + "] failed", username(), e);
}
}
/* * 1. Remove the job
* 2. update instance to enabled=false
**/
public boolean kill() {
if (!instance.isEnabled()) {
LOGGER.debug("<{}> Expect ApplicationInstance[guid={}] enabled=true,but it is false, illegal status",
username(), instance.getGuid());
return false;
}
if (!pauseJob(instance)) {
LOGGER.debug("<{}> Pause Job[name={}] failed", username(), instance.getJobName());
return false;
}
//update
instance.setEnabled(false);
httpRequestService.updateEnabled(instance);
LOGGER.debug("<{}> Update ApplicationInstance[guid={}] enabled=false", username(), instance.getGuid());
return true;
}
private boolean pauseJob(HttpSequence instance) {
DynamicJob job = new DynamicJob(getAndSetJobName(instance));
try {
return DynamicSchedulerFactory.pauseJob(job);
} catch (SchedulerException e) {
LOGGER.error("<{}> Pause [" + job + "] failed", username(), e);
return false;
}
}
}