fix(消息设置):性能测试通知

This commit is contained in:
wenyann 2020-10-26 17:12:50 +08:00
parent 76455e54a5
commit 6b5361f8b2
4 changed files with 175 additions and 7 deletions

View File

@ -112,6 +112,25 @@ public class MailService {
LogUtil.error(e); LogUtil.error(e);
} }
} }
public void sendLoadJenkinsNotification(String context, MessageDetail messageDetail) throws MessagingException {
JavaMailSenderImpl javaMailSender = getMailSender();
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(javaMailSender.getUsername());
helper.setSubject("MeterSphere平台" + Translator.get("task_notification"));
helper.setText(context);
List<UserDetail> list = userService.queryTypeByIds(messageDetail.getUserIds());
List<String> EmailList = new ArrayList<>();
list.forEach(u -> {
EmailList.add(u.getEmail());
});
helper.setTo(EmailList.toArray(new String[0]));
try {
javaMailSender.send(mimeMessage);
} catch (MailException e) {
LogUtil.error(e);
}
}
private void sendHtmlTimeTasks(List<NoticeDetail> noticeList, String status, Map<String, String> context, String template) throws MessagingException { private void sendHtmlTimeTasks(List<NoticeDetail> noticeList, String status, Map<String, String> context, String template) throws MessagingException {
JavaMailSenderImpl javaMailSender = getMailSender(); JavaMailSenderImpl javaMailSender = getMailSender();
@ -168,7 +187,7 @@ public class MailService {
} }
} }
/*新建评审*/ //新建评审
public void sendReviewerNotice(MessageDetail messageDetail, List<String> userIds, SaveTestCaseReviewRequest reviewRequest, String eventType) { public void sendReviewerNotice(MessageDetail messageDetail, List<String> userIds, SaveTestCaseReviewRequest reviewRequest, String eventType) {
Map<String, String> context = getReviewContext(reviewRequest); Map<String, String> context = getReviewContext(reviewRequest);
try { try {

View File

@ -0,0 +1,148 @@
package io.metersphere.performance.notice;
import io.metersphere.base.domain.LoadTest;
import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.base.mapper.LoadTestMapper;
import io.metersphere.commons.constants.NoticeConstants;
import io.metersphere.commons.constants.PerformanceTestStatus;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.notice.domain.MessageDetail;
import io.metersphere.notice.domain.MessageSettingDetail;
import io.metersphere.notice.domain.NoticeDetail;
import io.metersphere.notice.service.DingTaskService;
import io.metersphere.notice.service.MailService;
import io.metersphere.notice.service.NoticeService;
import io.metersphere.notice.service.WxChatTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Component
public class PerformanceNoticeTask {
@Resource
private NoticeService noticeService;
@Resource
private DingTaskService dingTaskService;
@Resource
private WxChatTaskService wxChatTaskService;
@Resource
private MailService mailService;
@Resource
private LoadTestMapper loadTestMapper;
private final ExecutorService executorService = Executors.newFixedThreadPool(20);
private boolean isRunning = true;
@PreDestroy
public void preDestroy() {
isRunning = false;
}
public void registerNoticeTask(String triggerMode,LoadTestWithBLOBs loadTest) {
executorService.submit(() -> {
while (isRunning) {
LoadTestWithBLOBs result = loadTestMapper.selectByPrimaryKey(loadTest.getId());
if (StringUtils.equals(result.getStatus(), PerformanceTestStatus.Completed.name())) {
isRunning = false;
sendSuccessNotice(triggerMode,loadTest);
return;
}
if (StringUtils.equals(result.getStatus(), PerformanceTestStatus.Error.name())) {
isRunning = false;
sendFailNotice(triggerMode,loadTest);
return;
}
try {
Thread.sleep(1000 * 60);// 每分钟检查 loadtest 的状态
} catch (InterruptedException e) {
LogUtil.error(e);
}
}
});
}
public void sendSuccessNotice(String triggerMode,LoadTestWithBLOBs loadTest) {
if (StringUtils.equals(NoticeConstants.API, "API")||StringUtils.equals(NoticeConstants.SCHEDULE,"SCHEDULE")) {
List<String> userIds = new ArrayList<>();
MessageSettingDetail messageSettingDetail = noticeService.searchMessage();
List<MessageDetail> taskList = messageSettingDetail.getJenkinsTask();
if(StringUtils.equals(triggerMode,NoticeConstants.SCHEDULE)){
List<NoticeDetail> noticeList = null;
noticeList = noticeService.queryNotice(loadTest.getId());
mailService.sendPerformanceNotification(noticeList, PerformanceTestStatus.Completed.name(), loadTest, loadTest.getId()); }else{
return;
}
if(StringUtils.equals(triggerMode,NoticeConstants.SCHEDULE)){
String contextSuccess="";
contextSuccess = "jenkins任务通知:" + loadTest.getName() + "执行成功";
String finalContextSuccess = contextSuccess;
taskList.forEach(r -> {
switch (r.getType()) {
case NoticeConstants.NAIL_ROBOT:
if (StringUtils.equals(NoticeConstants.EXECUTE_SUCCESSFUL, r.getEvent())
&& StringUtils.equals(loadTest.getStatus(), PerformanceTestStatus.Completed.name())) {
dingTaskService.sendNailRobot(r, userIds, finalContextSuccess, NoticeConstants.EXECUTE_SUCCESSFUL);
}
break;
case NoticeConstants.WECHAT_ROBOT:
if (StringUtils.equals(NoticeConstants.EXECUTE_SUCCESSFUL, r.getEvent())
&& StringUtils.equals(loadTest.getStatus(), PerformanceTestStatus.Completed.name())) {
wxChatTaskService.sendWechatRobot(r, userIds, finalContextSuccess, NoticeConstants.EXECUTE_SUCCESSFUL);
}
break;
case NoticeConstants.EMAIL:
if (StringUtils.equals(NoticeConstants.EXECUTE_SUCCESSFUL, r.getEvent())
&& StringUtils.equals(loadTest.getStatus(), PerformanceTestStatus.Completed.name())) {
try {
mailService.sendLoadJenkinsNotification(finalContextSuccess, r);
} catch (MessagingException messagingException) {
messagingException.printStackTrace();
}
}
break;
}
});
}
}
}
public void sendFailNotice(String triggerMode,LoadTestWithBLOBs loadTest) {
if (StringUtils.equals(NoticeConstants.API, "API")) {
List<String> userIds = new ArrayList<>();
MessageSettingDetail messageSettingDetail = noticeService.searchMessage();
List<MessageDetail> taskList = messageSettingDetail.getJenkinsTask();
String contextFailed = "jenkins任务通知:" + loadTest.getName() + "执行失败";
taskList.forEach(r -> {
switch (r.getType()) {
case NoticeConstants.NAIL_ROBOT:
if (StringUtils.equals(NoticeConstants.EXECUTE_FAILED, r.getEvent())) {
dingTaskService.sendNailRobot(r, userIds, contextFailed, NoticeConstants.EXECUTE_FAILED);
}
break;
case NoticeConstants.WECHAT_ROBOT:
if (StringUtils.equals(NoticeConstants.EXECUTE_FAILED, r.getEvent())) {
wxChatTaskService.sendWechatRobot(r, userIds, contextFailed, NoticeConstants.EXECUTE_FAILED);
}
break;
case NoticeConstants.EMAIL:
if (StringUtils.equals(NoticeConstants.EXECUTE_FAILED, r.getEvent())) {
try {
mailService.sendLoadJenkinsNotification(contextFailed, r);
} catch (MessagingException messagingException) {
messagingException.printStackTrace();
}
}
break;
}
});
}
}
}

View File

@ -24,6 +24,7 @@ import io.metersphere.notice.service.MailService;
import io.metersphere.notice.service.NoticeService; import io.metersphere.notice.service.NoticeService;
import io.metersphere.performance.engine.Engine; import io.metersphere.performance.engine.Engine;
import io.metersphere.performance.engine.EngineFactory; import io.metersphere.performance.engine.EngineFactory;
import io.metersphere.performance.notice.PerformanceNoticeTask;
import io.metersphere.service.FileService; import io.metersphere.service.FileService;
import io.metersphere.service.QuotaService; import io.metersphere.service.QuotaService;
import io.metersphere.service.ScheduleService; import io.metersphere.service.ScheduleService;
@ -68,25 +69,21 @@ public class PerformanceTestService {
@Resource @Resource
private ExtLoadTestReportDetailMapper extLoadTestReportDetailMapper; private ExtLoadTestReportDetailMapper extLoadTestReportDetailMapper;
@Resource @Resource
private LoadTestReportLogMapper loadTestReportLogMapper;
@Resource
private LoadTestReportResultMapper loadTestReportResultMapper; private LoadTestReportResultMapper loadTestReportResultMapper;
@Resource @Resource
private TestResourceService testResourceService;
@Resource
private ReportService reportService; private ReportService reportService;
@Resource @Resource
private KafkaProperties kafkaProperties; private KafkaProperties kafkaProperties;
@Resource @Resource
private ScheduleService scheduleService; private ScheduleService scheduleService;
@Resource @Resource
private TestCaseMapper testCaseMapper;
@Resource
private TestCaseService testCaseService; private TestCaseService testCaseService;
@Resource @Resource
private NoticeService noticeService; private NoticeService noticeService;
@Resource @Resource
private MailService mailService; private MailService mailService;
@Resource
private PerformanceNoticeTask performanceNoticeTask;
public List<LoadTestDTO> list(QueryTestPlanRequest request) { public List<LoadTestDTO> list(QueryTestPlanRequest request) {
request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders())); request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders()));
@ -247,6 +244,9 @@ public class PerformanceTestService {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
} }
} }
if(request.getTriggerMode().equals("API")){
performanceNoticeTask.registerNoticeTask(request.getTriggerMode(),loadTest);
}
return engine.getReportId(); return engine.getReportId();
} }

View File

@ -66,6 +66,7 @@
prop="type" prop="type"
:label="$t('schedule.receiving_mode')" :label="$t('schedule.receiving_mode')"
> >
</el-table-column> </el-table-column>
<el-table-column <el-table-column
:label="$t('test_resource_pool.enable_disable')" :label="$t('test_resource_pool.enable_disable')"