应用启动时启动任务调度

This commit is contained in:
chenjianxing 2020-06-24 13:06:57 +08:00
parent 2afce7f966
commit c0faacae36
12 changed files with 271 additions and 10 deletions

View File

@ -19,5 +19,7 @@ public class SaveAPITestRequest {
private List<Scenario> scenarioDefinition; private List<Scenario> scenarioDefinition;
private String userId;
private Schedule schedule; private Schedule schedule;
} }

View File

@ -108,7 +108,7 @@ public class APIReportService {
report.setCreateTime(System.currentTimeMillis()); report.setCreateTime(System.currentTimeMillis());
report.setUpdateTime(System.currentTimeMillis()); report.setUpdateTime(System.currentTimeMillis());
report.setStatus(APITestStatus.Running.name()); report.setStatus(APITestStatus.Running.name());
report.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); report.setUserId(test.getUserId());
apiTestReportMapper.insert(report); apiTestReportMapper.insert(report);
return report.getId(); return report.getId();

View File

@ -141,7 +141,11 @@ public class APITestService {
byte[] bytes = fileService.loadFileAsBytes(file.getFileId()); byte[] bytes = fileService.loadFileAsBytes(file.getFileId());
InputStream is = new ByteArrayInputStream(bytes); 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); changeStatus(request.getId(), APITestStatus.Running);
jMeterService.run(request.getId(), is); jMeterService.run(request.getId(), is);
@ -239,10 +243,12 @@ public class APITestService {
Schedule schedule = new Schedule(); Schedule schedule = new Schedule();
schedule.setResourceId(request.getResourceId()); schedule.setResourceId(request.getResourceId());
schedule.setEnable(request.getEnable()); schedule.setEnable(request.getEnable());
schedule.setJob(ApiTestJob.class.getName());
schedule.setValue(request.getValue().trim()); schedule.setValue(request.getValue().trim());
schedule.setGroup(ScheduleGroup.API_TEST.name()); schedule.setGroup(ScheduleGroup.API_TEST.name());
schedule.setKey(request.getResourceId()); schedule.setKey(request.getResourceId());
schedule.setType(ScheduleType.CRON.name()); schedule.setType(ScheduleType.CRON.name());
schedule.setUserId(SessionUtils.getUser().getId());
return schedule; return schedule;
} }
@ -252,7 +258,7 @@ public class APITestService {
if (enable != null && enable && StringUtils.isNotBlank(cronExpression)) { if (enable != null && enable && StringUtils.isNotBlank(cronExpression)) {
try { try {
QuartzManager.addOrUpdateCronJob(ApiTestJob.getJobKey(request.getResourceId()), 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) { } catch (SchedulerException e) {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
MSException.throwException("定时任务开启异常"); MSException.throwException("定时任务开启异常");

View File

@ -15,10 +15,14 @@ public class Schedule implements Serializable {
private String group; private String group;
private String job;
private Boolean enable; private Boolean enable;
private String resourceId; private String resourceId;
private String userId;
private String customData; private String customData;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -454,6 +454,76 @@ public class ScheduleExample {
return (Criteria) this; 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() { public Criteria andEnableIsNull() {
addCriterion("`enable` is null"); addCriterion("`enable` is null");
return (Criteria) this; return (Criteria) this;
@ -583,6 +653,76 @@ public class ScheduleExample {
addCriterion("resource_id not between", value1, value2, "resourceId"); addCriterion("resource_id not between", value1, value2, "resourceId");
return (Criteria) this; 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 { public static class Criteria extends GeneratedCriteria {

View File

@ -7,8 +7,10 @@
<result column="type" jdbcType="VARCHAR" property="type" /> <result column="type" jdbcType="VARCHAR" property="type" />
<result column="value" jdbcType="VARCHAR" property="value" /> <result column="value" jdbcType="VARCHAR" property="value" />
<result column="group" jdbcType="VARCHAR" property="group" /> <result column="group" jdbcType="VARCHAR" property="group" />
<result column="job" jdbcType="VARCHAR" property="job" />
<result column="enable" jdbcType="BIT" property="enable" /> <result column="enable" jdbcType="BIT" property="enable" />
<result column="resource_id" jdbcType="VARCHAR" property="resourceId" /> <result column="resource_id" jdbcType="VARCHAR" property="resourceId" />
<result column="user_id" jdbcType="VARCHAR" property="userId" />
</resultMap> </resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.metersphere.base.domain.Schedule"> <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.metersphere.base.domain.Schedule">
<result column="custom_data" jdbcType="LONGVARCHAR" property="customData" /> <result column="custom_data" jdbcType="LONGVARCHAR" property="customData" />
@ -72,7 +74,7 @@
</where> </where>
</sql> </sql>
<sql id="Base_Column_List"> <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>
<sql id="Blob_Column_List"> <sql id="Blob_Column_List">
custom_data custom_data
@ -127,10 +129,12 @@
</delete> </delete>
<insert id="insert" parameterType="io.metersphere.base.domain.Schedule"> <insert id="insert" parameterType="io.metersphere.base.domain.Schedule">
insert into schedule (id, `key`, `type`, insert into schedule (id, `key`, `type`,
`value`, `group`, `enable`, resource_id, `value`, `group`, job,
`enable`, resource_id, user_id,
custom_data) custom_data)
values (#{id,jdbcType=VARCHAR}, #{key,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, 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}) #{customData,jdbcType=LONGVARCHAR})
</insert> </insert>
<insert id="insertSelective" parameterType="io.metersphere.base.domain.Schedule"> <insert id="insertSelective" parameterType="io.metersphere.base.domain.Schedule">
@ -151,12 +155,18 @@
<if test="group != null"> <if test="group != null">
`group`, `group`,
</if> </if>
<if test="job != null">
job,
</if>
<if test="enable != null"> <if test="enable != null">
`enable`, `enable`,
</if> </if>
<if test="resourceId != null"> <if test="resourceId != null">
resource_id, resource_id,
</if> </if>
<if test="userId != null">
user_id,
</if>
<if test="customData != null"> <if test="customData != null">
custom_data, custom_data,
</if> </if>
@ -177,12 +187,18 @@
<if test="group != null"> <if test="group != null">
#{group,jdbcType=VARCHAR}, #{group,jdbcType=VARCHAR},
</if> </if>
<if test="job != null">
#{job,jdbcType=VARCHAR},
</if>
<if test="enable != null"> <if test="enable != null">
#{enable,jdbcType=BIT}, #{enable,jdbcType=BIT},
</if> </if>
<if test="resourceId != null"> <if test="resourceId != null">
#{resourceId,jdbcType=VARCHAR}, #{resourceId,jdbcType=VARCHAR},
</if> </if>
<if test="userId != null">
#{userId,jdbcType=VARCHAR},
</if>
<if test="customData != null"> <if test="customData != null">
#{customData,jdbcType=LONGVARCHAR}, #{customData,jdbcType=LONGVARCHAR},
</if> </if>
@ -212,12 +228,18 @@
<if test="record.group != null"> <if test="record.group != null">
`group` = #{record.group,jdbcType=VARCHAR}, `group` = #{record.group,jdbcType=VARCHAR},
</if> </if>
<if test="record.job != null">
job = #{record.job,jdbcType=VARCHAR},
</if>
<if test="record.enable != null"> <if test="record.enable != null">
`enable` = #{record.enable,jdbcType=BIT}, `enable` = #{record.enable,jdbcType=BIT},
</if> </if>
<if test="record.resourceId != null"> <if test="record.resourceId != null">
resource_id = #{record.resourceId,jdbcType=VARCHAR}, resource_id = #{record.resourceId,jdbcType=VARCHAR},
</if> </if>
<if test="record.userId != null">
user_id = #{record.userId,jdbcType=VARCHAR},
</if>
<if test="record.customData != null"> <if test="record.customData != null">
custom_data = #{record.customData,jdbcType=LONGVARCHAR}, custom_data = #{record.customData,jdbcType=LONGVARCHAR},
</if> </if>
@ -233,8 +255,10 @@
`type` = #{record.type,jdbcType=VARCHAR}, `type` = #{record.type,jdbcType=VARCHAR},
`value` = #{record.value,jdbcType=VARCHAR}, `value` = #{record.value,jdbcType=VARCHAR},
`group` = #{record.group,jdbcType=VARCHAR}, `group` = #{record.group,jdbcType=VARCHAR},
job = #{record.job,jdbcType=VARCHAR},
`enable` = #{record.enable,jdbcType=BIT}, `enable` = #{record.enable,jdbcType=BIT},
resource_id = #{record.resourceId,jdbcType=VARCHAR}, resource_id = #{record.resourceId,jdbcType=VARCHAR},
user_id = #{record.userId,jdbcType=VARCHAR},
custom_data = #{record.customData,jdbcType=LONGVARCHAR} custom_data = #{record.customData,jdbcType=LONGVARCHAR}
<if test="_parameter != null"> <if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" /> <include refid="Update_By_Example_Where_Clause" />
@ -247,8 +271,10 @@
`type` = #{record.type,jdbcType=VARCHAR}, `type` = #{record.type,jdbcType=VARCHAR},
`value` = #{record.value,jdbcType=VARCHAR}, `value` = #{record.value,jdbcType=VARCHAR},
`group` = #{record.group,jdbcType=VARCHAR}, `group` = #{record.group,jdbcType=VARCHAR},
job = #{record.job,jdbcType=VARCHAR},
`enable` = #{record.enable,jdbcType=BIT}, `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"> <if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" /> <include refid="Update_By_Example_Where_Clause" />
</if> </if>
@ -268,12 +294,18 @@
<if test="group != null"> <if test="group != null">
`group` = #{group,jdbcType=VARCHAR}, `group` = #{group,jdbcType=VARCHAR},
</if> </if>
<if test="job != null">
job = #{job,jdbcType=VARCHAR},
</if>
<if test="enable != null"> <if test="enable != null">
`enable` = #{enable,jdbcType=BIT}, `enable` = #{enable,jdbcType=BIT},
</if> </if>
<if test="resourceId != null"> <if test="resourceId != null">
resource_id = #{resourceId,jdbcType=VARCHAR}, resource_id = #{resourceId,jdbcType=VARCHAR},
</if> </if>
<if test="userId != null">
user_id = #{userId,jdbcType=VARCHAR},
</if>
<if test="customData != null"> <if test="customData != null">
custom_data = #{customData,jdbcType=LONGVARCHAR}, custom_data = #{customData,jdbcType=LONGVARCHAR},
</if> </if>
@ -286,8 +318,10 @@
`type` = #{type,jdbcType=VARCHAR}, `type` = #{type,jdbcType=VARCHAR},
`value` = #{value,jdbcType=VARCHAR}, `value` = #{value,jdbcType=VARCHAR},
`group` = #{group,jdbcType=VARCHAR}, `group` = #{group,jdbcType=VARCHAR},
job = #{job,jdbcType=VARCHAR},
`enable` = #{enable,jdbcType=BIT}, `enable` = #{enable,jdbcType=BIT},
resource_id = #{resourceId,jdbcType=VARCHAR}, resource_id = #{resourceId,jdbcType=VARCHAR},
user_id = #{userId,jdbcType=VARCHAR},
custom_data = #{customData,jdbcType=LONGVARCHAR} custom_data = #{customData,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR}
</update> </update>
@ -297,8 +331,10 @@
`type` = #{type,jdbcType=VARCHAR}, `type` = #{type,jdbcType=VARCHAR},
`value` = #{value,jdbcType=VARCHAR}, `value` = #{value,jdbcType=VARCHAR},
`group` = #{group,jdbcType=VARCHAR}, `group` = #{group,jdbcType=VARCHAR},
job = #{job,jdbcType=VARCHAR},
`enable` = #{enable,jdbcType=BIT}, `enable` = #{enable,jdbcType=BIT},
resource_id = #{resourceId,jdbcType=VARCHAR} resource_id = #{resourceId,jdbcType=VARCHAR},
user_id = #{userId,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR}
</update> </update>
</mapper> </mapper>

View File

@ -295,10 +295,11 @@ public class QuartzManager {
addOrUpdateCronJob(jobKey, triggerKey, jobClass, cron, null); 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 jobDataMap = new JobDataMap();
jobDataMap.put("resourceId", resourceId); jobDataMap.put("resourceId", resourceId);
jobDataMap.put("expression", expression); jobDataMap.put("expression", expression);
jobDataMap.put("userId", userId);
return jobDataMap; return jobDataMap;
} }
} }

View File

@ -26,6 +26,7 @@ public class ApiTestJob extends MsScheduleJob {
LogUtil.info("CronExpression: " + expression); LogUtil.info("CronExpression: " + expression);
SaveAPITestRequest request = new SaveAPITestRequest(); SaveAPITestRequest request = new SaveAPITestRequest();
request.setId(resourceId); request.setId(resourceId);
request.setUserId(userId);
apiTestService.run(request); apiTestService.run(request);
} }

View File

@ -6,6 +6,8 @@ public abstract class MsScheduleJob implements Job{
protected String resourceId; protected String resourceId;
protected String userId;
protected String expression; protected String expression;
public void setResourceId(String resourceId) { public void setResourceId(String resourceId) {
@ -15,4 +17,8 @@ public abstract class MsScheduleJob implements Job{
public void setExpression(String expression) { public void setExpression(String expression) {
this.expression = expression; this.expression = expression;
} }
public void setUserId(String userId) {
this.userId = userId;
}
} }

View File

@ -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();
}
});
}
}

View File

@ -48,4 +48,10 @@ public class ScheduleService {
ScheduleExample example = new ScheduleExample(); ScheduleExample example = new ScheduleExample();
return scheduleMapper.selectByExample(example); return scheduleMapper.selectByExample(example);
} }
public List<Schedule> getEnableSchedule() {
ScheduleExample example = new ScheduleExample();
example.createCriteria().andEnableEqualTo(true);
return scheduleMapper.selectByExample(example);
}
} }

View File

@ -10,7 +10,7 @@
<crontab-result v-show="false" :ex="schedule.value" ref="crontabResult" @resultListChange="recentListChange"/> <crontab-result v-show="false" :ex="schedule.value" ref="crontabResult" @resultListChange="recentListChange"/>
</div> </div>
<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>
</div> </div>
</template> </template>