应用启动时启动任务调度
This commit is contained in:
parent
2afce7f966
commit
c0faacae36
|
@ -19,5 +19,7 @@ public class SaveAPITestRequest {
|
|||
|
||||
private List<Scenario> scenarioDefinition;
|
||||
|
||||
private String userId;
|
||||
|
||||
private Schedule schedule;
|
||||
}
|
||||
|
|
|
@ -108,7 +108,7 @@ public class APIReportService {
|
|||
report.setCreateTime(System.currentTimeMillis());
|
||||
report.setUpdateTime(System.currentTimeMillis());
|
||||
report.setStatus(APITestStatus.Running.name());
|
||||
report.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
|
||||
report.setUserId(test.getUserId());
|
||||
apiTestReportMapper.insert(report);
|
||||
|
||||
return report.getId();
|
||||
|
|
|
@ -141,7 +141,11 @@ public class APITestService {
|
|||
byte[] bytes = fileService.loadFileAsBytes(file.getFileId());
|
||||
InputStream is = new ByteArrayInputStream(bytes);
|
||||
|
||||
String reportId = apiReportService.create(get(request.getId()));
|
||||
APITestResult apiTest = get(request.getId());
|
||||
if (SessionUtils.getUser() == null) {
|
||||
apiTest.setUserId(request.getUserId());
|
||||
}
|
||||
String reportId = apiReportService.create(apiTest);
|
||||
changeStatus(request.getId(), APITestStatus.Running);
|
||||
|
||||
jMeterService.run(request.getId(), is);
|
||||
|
@ -239,10 +243,12 @@ public class APITestService {
|
|||
Schedule schedule = new Schedule();
|
||||
schedule.setResourceId(request.getResourceId());
|
||||
schedule.setEnable(request.getEnable());
|
||||
schedule.setJob(ApiTestJob.class.getName());
|
||||
schedule.setValue(request.getValue().trim());
|
||||
schedule.setGroup(ScheduleGroup.API_TEST.name());
|
||||
schedule.setKey(request.getResourceId());
|
||||
schedule.setType(ScheduleType.CRON.name());
|
||||
schedule.setUserId(SessionUtils.getUser().getId());
|
||||
return schedule;
|
||||
}
|
||||
|
||||
|
@ -252,7 +258,7 @@ public class APITestService {
|
|||
if (enable != null && enable && StringUtils.isNotBlank(cronExpression)) {
|
||||
try {
|
||||
QuartzManager.addOrUpdateCronJob(ApiTestJob.getJobKey(request.getResourceId()),
|
||||
ApiTestJob.getTriggerKey(request.getResourceId()), ApiTestJob.class, cronExpression, QuartzManager.getDefaultJobDataMap(request.getResourceId(), cronExpression));
|
||||
ApiTestJob.getTriggerKey(request.getResourceId()), ApiTestJob.class, cronExpression, QuartzManager.getDefaultJobDataMap(request.getResourceId(), cronExpression, SessionUtils.getUser().getId()));
|
||||
} catch (SchedulerException e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
MSException.throwException("定时任务开启异常");
|
||||
|
|
|
@ -15,10 +15,14 @@ public class Schedule implements Serializable {
|
|||
|
||||
private String group;
|
||||
|
||||
private String job;
|
||||
|
||||
private Boolean enable;
|
||||
|
||||
private String resourceId;
|
||||
|
||||
private String userId;
|
||||
|
||||
private String customData;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
|
|
@ -454,6 +454,76 @@ public class ScheduleExample {
|
|||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobIsNull() {
|
||||
addCriterion("job is null");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobIsNotNull() {
|
||||
addCriterion("job is not null");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobEqualTo(String value) {
|
||||
addCriterion("job =", value, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobNotEqualTo(String value) {
|
||||
addCriterion("job <>", value, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobGreaterThan(String value) {
|
||||
addCriterion("job >", value, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobGreaterThanOrEqualTo(String value) {
|
||||
addCriterion("job >=", value, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobLessThan(String value) {
|
||||
addCriterion("job <", value, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobLessThanOrEqualTo(String value) {
|
||||
addCriterion("job <=", value, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobLike(String value) {
|
||||
addCriterion("job like", value, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobNotLike(String value) {
|
||||
addCriterion("job not like", value, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobIn(List<String> values) {
|
||||
addCriterion("job in", values, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobNotIn(List<String> values) {
|
||||
addCriterion("job not in", values, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobBetween(String value1, String value2) {
|
||||
addCriterion("job between", value1, value2, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andJobNotBetween(String value1, String value2) {
|
||||
addCriterion("job not between", value1, value2, "job");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andEnableIsNull() {
|
||||
addCriterion("`enable` is null");
|
||||
return (Criteria) this;
|
||||
|
@ -583,6 +653,76 @@ public class ScheduleExample {
|
|||
addCriterion("resource_id not between", value1, value2, "resourceId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdIsNull() {
|
||||
addCriterion("user_id is null");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdIsNotNull() {
|
||||
addCriterion("user_id is not null");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdEqualTo(String value) {
|
||||
addCriterion("user_id =", value, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdNotEqualTo(String value) {
|
||||
addCriterion("user_id <>", value, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdGreaterThan(String value) {
|
||||
addCriterion("user_id >", value, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdGreaterThanOrEqualTo(String value) {
|
||||
addCriterion("user_id >=", value, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdLessThan(String value) {
|
||||
addCriterion("user_id <", value, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdLessThanOrEqualTo(String value) {
|
||||
addCriterion("user_id <=", value, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdLike(String value) {
|
||||
addCriterion("user_id like", value, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdNotLike(String value) {
|
||||
addCriterion("user_id not like", value, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdIn(List<String> values) {
|
||||
addCriterion("user_id in", values, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdNotIn(List<String> values) {
|
||||
addCriterion("user_id not in", values, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdBetween(String value1, String value2) {
|
||||
addCriterion("user_id between", value1, value2, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
|
||||
public Criteria andUserIdNotBetween(String value1, String value2) {
|
||||
addCriterion("user_id not between", value1, value2, "userId");
|
||||
return (Criteria) this;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Criteria extends GeneratedCriteria {
|
||||
|
|
|
@ -7,8 +7,10 @@
|
|||
<result column="type" jdbcType="VARCHAR" property="type" />
|
||||
<result column="value" jdbcType="VARCHAR" property="value" />
|
||||
<result column="group" jdbcType="VARCHAR" property="group" />
|
||||
<result column="job" jdbcType="VARCHAR" property="job" />
|
||||
<result column="enable" jdbcType="BIT" property="enable" />
|
||||
<result column="resource_id" jdbcType="VARCHAR" property="resourceId" />
|
||||
<result column="user_id" jdbcType="VARCHAR" property="userId" />
|
||||
</resultMap>
|
||||
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.metersphere.base.domain.Schedule">
|
||||
<result column="custom_data" jdbcType="LONGVARCHAR" property="customData" />
|
||||
|
@ -72,7 +74,7 @@
|
|||
</where>
|
||||
</sql>
|
||||
<sql id="Base_Column_List">
|
||||
id, `key`, `type`, `value`, `group`, `enable`, resource_id
|
||||
id, `key`, `type`, `value`, `group`, job, `enable`, resource_id, user_id
|
||||
</sql>
|
||||
<sql id="Blob_Column_List">
|
||||
custom_data
|
||||
|
@ -127,10 +129,12 @@
|
|||
</delete>
|
||||
<insert id="insert" parameterType="io.metersphere.base.domain.Schedule">
|
||||
insert into schedule (id, `key`, `type`,
|
||||
`value`, `group`, `enable`, resource_id,
|
||||
`value`, `group`, job,
|
||||
`enable`, resource_id, user_id,
|
||||
custom_data)
|
||||
values (#{id,jdbcType=VARCHAR}, #{key,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR},
|
||||
#{value,jdbcType=VARCHAR}, #{group,jdbcType=VARCHAR}, #{enable,jdbcType=BIT}, #{resourceId,jdbcType=VARCHAR},
|
||||
#{value,jdbcType=VARCHAR}, #{group,jdbcType=VARCHAR}, #{job,jdbcType=VARCHAR},
|
||||
#{enable,jdbcType=BIT}, #{resourceId,jdbcType=VARCHAR}, #{userId,jdbcType=VARCHAR},
|
||||
#{customData,jdbcType=LONGVARCHAR})
|
||||
</insert>
|
||||
<insert id="insertSelective" parameterType="io.metersphere.base.domain.Schedule">
|
||||
|
@ -151,12 +155,18 @@
|
|||
<if test="group != null">
|
||||
`group`,
|
||||
</if>
|
||||
<if test="job != null">
|
||||
job,
|
||||
</if>
|
||||
<if test="enable != null">
|
||||
`enable`,
|
||||
</if>
|
||||
<if test="resourceId != null">
|
||||
resource_id,
|
||||
</if>
|
||||
<if test="userId != null">
|
||||
user_id,
|
||||
</if>
|
||||
<if test="customData != null">
|
||||
custom_data,
|
||||
</if>
|
||||
|
@ -177,12 +187,18 @@
|
|||
<if test="group != null">
|
||||
#{group,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="job != null">
|
||||
#{job,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="enable != null">
|
||||
#{enable,jdbcType=BIT},
|
||||
</if>
|
||||
<if test="resourceId != null">
|
||||
#{resourceId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="userId != null">
|
||||
#{userId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="customData != null">
|
||||
#{customData,jdbcType=LONGVARCHAR},
|
||||
</if>
|
||||
|
@ -212,12 +228,18 @@
|
|||
<if test="record.group != null">
|
||||
`group` = #{record.group,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="record.job != null">
|
||||
job = #{record.job,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="record.enable != null">
|
||||
`enable` = #{record.enable,jdbcType=BIT},
|
||||
</if>
|
||||
<if test="record.resourceId != null">
|
||||
resource_id = #{record.resourceId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="record.userId != null">
|
||||
user_id = #{record.userId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="record.customData != null">
|
||||
custom_data = #{record.customData,jdbcType=LONGVARCHAR},
|
||||
</if>
|
||||
|
@ -233,8 +255,10 @@
|
|||
`type` = #{record.type,jdbcType=VARCHAR},
|
||||
`value` = #{record.value,jdbcType=VARCHAR},
|
||||
`group` = #{record.group,jdbcType=VARCHAR},
|
||||
job = #{record.job,jdbcType=VARCHAR},
|
||||
`enable` = #{record.enable,jdbcType=BIT},
|
||||
resource_id = #{record.resourceId,jdbcType=VARCHAR},
|
||||
user_id = #{record.userId,jdbcType=VARCHAR},
|
||||
custom_data = #{record.customData,jdbcType=LONGVARCHAR}
|
||||
<if test="_parameter != null">
|
||||
<include refid="Update_By_Example_Where_Clause" />
|
||||
|
@ -247,8 +271,10 @@
|
|||
`type` = #{record.type,jdbcType=VARCHAR},
|
||||
`value` = #{record.value,jdbcType=VARCHAR},
|
||||
`group` = #{record.group,jdbcType=VARCHAR},
|
||||
job = #{record.job,jdbcType=VARCHAR},
|
||||
`enable` = #{record.enable,jdbcType=BIT},
|
||||
resource_id = #{record.resourceId,jdbcType=VARCHAR}
|
||||
resource_id = #{record.resourceId,jdbcType=VARCHAR},
|
||||
user_id = #{record.userId,jdbcType=VARCHAR}
|
||||
<if test="_parameter != null">
|
||||
<include refid="Update_By_Example_Where_Clause" />
|
||||
</if>
|
||||
|
@ -268,12 +294,18 @@
|
|||
<if test="group != null">
|
||||
`group` = #{group,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="job != null">
|
||||
job = #{job,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="enable != null">
|
||||
`enable` = #{enable,jdbcType=BIT},
|
||||
</if>
|
||||
<if test="resourceId != null">
|
||||
resource_id = #{resourceId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="userId != null">
|
||||
user_id = #{userId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="customData != null">
|
||||
custom_data = #{customData,jdbcType=LONGVARCHAR},
|
||||
</if>
|
||||
|
@ -286,8 +318,10 @@
|
|||
`type` = #{type,jdbcType=VARCHAR},
|
||||
`value` = #{value,jdbcType=VARCHAR},
|
||||
`group` = #{group,jdbcType=VARCHAR},
|
||||
job = #{job,jdbcType=VARCHAR},
|
||||
`enable` = #{enable,jdbcType=BIT},
|
||||
resource_id = #{resourceId,jdbcType=VARCHAR},
|
||||
user_id = #{userId,jdbcType=VARCHAR},
|
||||
custom_data = #{customData,jdbcType=LONGVARCHAR}
|
||||
where id = #{id,jdbcType=VARCHAR}
|
||||
</update>
|
||||
|
@ -297,8 +331,10 @@
|
|||
`type` = #{type,jdbcType=VARCHAR},
|
||||
`value` = #{value,jdbcType=VARCHAR},
|
||||
`group` = #{group,jdbcType=VARCHAR},
|
||||
job = #{job,jdbcType=VARCHAR},
|
||||
`enable` = #{enable,jdbcType=BIT},
|
||||
resource_id = #{resourceId,jdbcType=VARCHAR}
|
||||
resource_id = #{resourceId,jdbcType=VARCHAR},
|
||||
user_id = #{userId,jdbcType=VARCHAR}
|
||||
where id = #{id,jdbcType=VARCHAR}
|
||||
</update>
|
||||
</mapper>
|
|
@ -295,10 +295,11 @@ public class QuartzManager {
|
|||
addOrUpdateCronJob(jobKey, triggerKey, jobClass, cron, null);
|
||||
}
|
||||
|
||||
public static JobDataMap getDefaultJobDataMap(String resourceId, String expression) {
|
||||
public static JobDataMap getDefaultJobDataMap(String resourceId, String expression, String userId) {
|
||||
JobDataMap jobDataMap = new JobDataMap();
|
||||
jobDataMap.put("resourceId", resourceId);
|
||||
jobDataMap.put("expression", expression);
|
||||
jobDataMap.put("userId", userId);
|
||||
return jobDataMap;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ public class ApiTestJob extends MsScheduleJob {
|
|||
LogUtil.info("CronExpression: " + expression);
|
||||
SaveAPITestRequest request = new SaveAPITestRequest();
|
||||
request.setId(resourceId);
|
||||
request.setUserId(userId);
|
||||
apiTestService.run(request);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,8 @@ public abstract class MsScheduleJob implements Job{
|
|||
|
||||
protected String resourceId;
|
||||
|
||||
protected String userId;
|
||||
|
||||
protected String expression;
|
||||
|
||||
public void setResourceId(String resourceId) {
|
||||
|
@ -15,4 +17,8 @@ public abstract class MsScheduleJob implements Job{
|
|||
public void setExpression(String expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
package io.metersphere.listeners;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import io.metersphere.base.domain.Schedule;
|
||||
import io.metersphere.base.mapper.ScheduleMapper;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.job.QuartzManager;
|
||||
import io.metersphere.job.sechedule.ApiTestJob;
|
||||
import io.metersphere.service.ScheduleService;
|
||||
import org.quartz.JobKey;
|
||||
import org.quartz.SchedulerException;
|
||||
import org.quartz.TriggerKey;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 服务启动完成后打印日志
|
||||
*/
|
||||
@Component
|
||||
public class AppStartListener implements ApplicationListener<ApplicationReadyEvent> {
|
||||
|
||||
// private static boolean started = false;
|
||||
|
||||
@Resource
|
||||
private ScheduleService scheduleService;
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
|
||||
|
||||
System.out.println("================= 应用启动 =================");
|
||||
|
||||
try {
|
||||
Thread.sleep(5*60*1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
List<Schedule> Schedules = scheduleService.getEnableSchedule();
|
||||
|
||||
Schedules.forEach(schedule -> {
|
||||
try {
|
||||
if (schedule.getEnable()) {
|
||||
LogUtil.error("初始化任务:" + JSON.toJSONString(schedule));
|
||||
QuartzManager.addOrUpdateCronJob(new JobKey(schedule.getKey(), schedule.getGroup()),
|
||||
new TriggerKey(schedule.getKey(), schedule.getGroup()), Class.forName(schedule.getJob()), schedule.getValue(),
|
||||
QuartzManager.getDefaultJobDataMap(schedule.getResourceId(), schedule.getValue(), schedule.getUserId()));
|
||||
}
|
||||
Thread.sleep(1*60*1000);
|
||||
} catch (Exception e) {
|
||||
LogUtil.error("应用启动,初始化任务失败", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -48,4 +48,10 @@ public class ScheduleService {
|
|||
ScheduleExample example = new ScheduleExample();
|
||||
return scheduleMapper.selectByExample(example);
|
||||
}
|
||||
|
||||
public List<Schedule> getEnableSchedule() {
|
||||
ScheduleExample example = new ScheduleExample();
|
||||
example.createCriteria().andEnableEqualTo(true);
|
||||
return scheduleMapper.selectByExample(example);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
<crontab-result v-show="false" :ex="schedule.value" ref="crontabResult" @resultListChange="recentListChange"/>
|
||||
</div>
|
||||
<div>
|
||||
<span :class="{'disable-character': !schedule.enable}"> 下次执行时间:{{this.recentList.length > 0 ? this.recentList[0] : ''}} </span>
|
||||
<span :class="{'disable-character': !schedule.enable}"> 下次执行时间:{{this.recentList.length > 0 ? this.recentList[0] : '未设置'}} </span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
|
Loading…
Reference in New Issue