From c0faacae360177acc8d83db9ff635d8e4f7369c1 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Wed, 24 Jun 2020 13:06:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E4=BB=BB=E5=8A=A1=E8=B0=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/SaveAPITestRequest.java | 2 + .../api/service/APIReportService.java | 2 +- .../api/service/APITestService.java | 10 +- .../io/metersphere/base/domain/Schedule.java | 4 + .../base/domain/ScheduleExample.java | 140 ++++++++++++++++++ .../base/mapper/ScheduleMapper.xml | 46 +++++- .../io/metersphere/job/QuartzManager.java | 3 +- .../metersphere/job/sechedule/ApiTestJob.java | 1 + .../job/sechedule/MsScheduleJob.java | 6 + .../listeners/AppStartListener.java | 59 ++++++++ .../metersphere/service/ScheduleService.java | 6 + .../common/components/MsScheduleConfig.vue | 2 +- 12 files changed, 271 insertions(+), 10 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/listeners/AppStartListener.java diff --git a/backend/src/main/java/io/metersphere/api/dto/SaveAPITestRequest.java b/backend/src/main/java/io/metersphere/api/dto/SaveAPITestRequest.java index 96d5687c65..8531011633 100644 --- a/backend/src/main/java/io/metersphere/api/dto/SaveAPITestRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/SaveAPITestRequest.java @@ -19,5 +19,7 @@ public class SaveAPITestRequest { private List scenarioDefinition; + private String userId; + private Schedule schedule; } diff --git a/backend/src/main/java/io/metersphere/api/service/APIReportService.java b/backend/src/main/java/io/metersphere/api/service/APIReportService.java index 5b2c114b45..42de9fbc56 100644 --- a/backend/src/main/java/io/metersphere/api/service/APIReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/APIReportService.java @@ -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(); diff --git a/backend/src/main/java/io/metersphere/api/service/APITestService.java b/backend/src/main/java/io/metersphere/api/service/APITestService.java index 2f5064c44a..c21fb15332 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -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("定时任务开启异常"); diff --git a/backend/src/main/java/io/metersphere/base/domain/Schedule.java b/backend/src/main/java/io/metersphere/base/domain/Schedule.java index 07145fd200..1c6f2976e8 100644 --- a/backend/src/main/java/io/metersphere/base/domain/Schedule.java +++ b/backend/src/main/java/io/metersphere/base/domain/Schedule.java @@ -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; diff --git a/backend/src/main/java/io/metersphere/base/domain/ScheduleExample.java b/backend/src/main/java/io/metersphere/base/domain/ScheduleExample.java index dac884f07d..da9a199055 100644 --- a/backend/src/main/java/io/metersphere/base/domain/ScheduleExample.java +++ b/backend/src/main/java/io/metersphere/base/domain/ScheduleExample.java @@ -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 values) { + addCriterion("job in", values, "job"); + return (Criteria) this; + } + + public Criteria andJobNotIn(List 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 values) { + addCriterion("user_id in", values, "userId"); + return (Criteria) this; + } + + public Criteria andUserIdNotIn(List 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 { diff --git a/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.xml index 3de84fefea..4ce95a2a3f 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.xml @@ -7,8 +7,10 @@ + + @@ -72,7 +74,7 @@ - id, `key`, `type`, `value`, `group`, `enable`, resource_id + id, `key`, `type`, `value`, `group`, job, `enable`, resource_id, user_id custom_data @@ -127,10 +129,12 @@ 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}) @@ -151,12 +155,18 @@ `group`, + + job, + `enable`, resource_id, + + user_id, + custom_data, @@ -177,12 +187,18 @@ #{group,jdbcType=VARCHAR}, + + #{job,jdbcType=VARCHAR}, + #{enable,jdbcType=BIT}, #{resourceId,jdbcType=VARCHAR}, + + #{userId,jdbcType=VARCHAR}, + #{customData,jdbcType=LONGVARCHAR}, @@ -212,12 +228,18 @@ `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}, @@ -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} @@ -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} @@ -268,12 +294,18 @@ `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}, @@ -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} @@ -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} \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/job/QuartzManager.java b/backend/src/main/java/io/metersphere/job/QuartzManager.java index c519ce8a2b..a3d9ff42ab 100644 --- a/backend/src/main/java/io/metersphere/job/QuartzManager.java +++ b/backend/src/main/java/io/metersphere/job/QuartzManager.java @@ -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; } } diff --git a/backend/src/main/java/io/metersphere/job/sechedule/ApiTestJob.java b/backend/src/main/java/io/metersphere/job/sechedule/ApiTestJob.java index b4081c3dff..d502fec273 100644 --- a/backend/src/main/java/io/metersphere/job/sechedule/ApiTestJob.java +++ b/backend/src/main/java/io/metersphere/job/sechedule/ApiTestJob.java @@ -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); } diff --git a/backend/src/main/java/io/metersphere/job/sechedule/MsScheduleJob.java b/backend/src/main/java/io/metersphere/job/sechedule/MsScheduleJob.java index 2ebccc1cf3..3e947d2bdd 100644 --- a/backend/src/main/java/io/metersphere/job/sechedule/MsScheduleJob.java +++ b/backend/src/main/java/io/metersphere/job/sechedule/MsScheduleJob.java @@ -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; + } } diff --git a/backend/src/main/java/io/metersphere/listeners/AppStartListener.java b/backend/src/main/java/io/metersphere/listeners/AppStartListener.java new file mode 100644 index 0000000000..728ee1b1cd --- /dev/null +++ b/backend/src/main/java/io/metersphere/listeners/AppStartListener.java @@ -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 { + +// 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 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(); + } + }); + } +} diff --git a/backend/src/main/java/io/metersphere/service/ScheduleService.java b/backend/src/main/java/io/metersphere/service/ScheduleService.java index e4a76bd609..163070f928 100644 --- a/backend/src/main/java/io/metersphere/service/ScheduleService.java +++ b/backend/src/main/java/io/metersphere/service/ScheduleService.java @@ -48,4 +48,10 @@ public class ScheduleService { ScheduleExample example = new ScheduleExample(); return scheduleMapper.selectByExample(example); } + + public List getEnableSchedule() { + ScheduleExample example = new ScheduleExample(); + example.createCriteria().andEnableEqualTo(true); + return scheduleMapper.selectByExample(example); + } } diff --git a/frontend/src/business/components/common/components/MsScheduleConfig.vue b/frontend/src/business/components/common/components/MsScheduleConfig.vue index e1990ac02d..56900fb8d9 100644 --- a/frontend/src/business/components/common/components/MsScheduleConfig.vue +++ b/frontend/src/business/components/common/components/MsScheduleConfig.vue @@ -10,7 +10,7 @@
- 下次执行时间:{{this.recentList.length > 0 ? this.recentList[0] : ''}} + 下次执行时间:{{this.recentList.length > 0 ? this.recentList[0] : '未设置'}}