fix: jenkins 执行场景日志输出卡住

This commit is contained in:
Captain.B 2021-09-08 17:55:14 +08:00 committed by 刘瑞斌
parent 7ed0d1c572
commit 33454ea828
3 changed files with 27 additions and 11 deletions

View File

@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.service.*;
import io.metersphere.base.domain.ApiDefinitionExecResult;
import io.metersphere.base.domain.ApiScenarioReport;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.ApiTestReport;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.utils.CommonBeanFactory;
@ -76,6 +77,7 @@ public class MsKafkaListener {
TestResult testResult = this.formatResult(execResult);
ApiTestReport report = null;
String reportUrl = null;
String projectId = null;
// 这部分后续优化只留 DEFINITION SCENARIO 两部分
if (StringUtils.equals(testResult.getRunMode(), ApiRunMode.DEFINITION.name())) {
// 调试操作不需要存储结果
@ -96,7 +98,9 @@ public class MsKafkaListener {
report.setStatus(result.getStatus());
report.setId(result.getId());
report.setTriggerMode(ApiRunMode.API.name());
report.setName(apiDefinitionService.getApiCaseInfo(testResult.getTestId()).getName());
ApiTestCaseWithBLOBs apiCaseInfo = apiDefinitionService.getApiCaseInfo(testResult.getTestId());
report.setName(apiCaseInfo.getName());
projectId = apiCaseInfo.getProjectId();
}
} else if (StringUtils.equalsAny(testResult.getRunMode(), ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name())) {
apiDefinitionService.addResult(testResult);
@ -128,7 +132,7 @@ public class MsKafkaListener {
assert systemParameterService != null;
BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo();
reportUrl = baseSystemConfigDTO.getUrl() + "/#/api/automation/report";
projectId = scenarioReport.getProjectId();
testResult.setTestId(scenarioReport.getScenarioId());
} else {
apiTestService.changeStatus(testResult.getTestId(), APITestStatus.Completed);
@ -151,12 +155,12 @@ public class MsKafkaListener {
}
if (report != null) {
if (StringUtils.equals(ReportTriggerMode.API.name(), report.getTriggerMode()) || StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), report.getTriggerMode())) {
sendTask(report, reportUrl, testResult);
sendTask(report, reportUrl, testResult, projectId);
}
}
}
private static void sendTask(ApiTestReport report, String reportUrl, TestResult testResult) {
private static void sendTask(ApiTestReport report, String reportUrl, TestResult testResult, String projectId) {
if (report == null) {
return;
}
@ -200,6 +204,7 @@ public class MsKafkaListener {
paramMap.put("type", "api");
paramMap.put("url", baseSystemConfigDTO.getUrl());
paramMap.put("status", report.getStatus());
paramMap.put("projectId", projectId);
NoticeModel noticeModel = NoticeModel.builder()
.successContext(successContext)
.successMailTemplate("ApiSuccessfulNotification")

View File

@ -51,11 +51,12 @@ public class NoticeSendService {
public void send(String taskType, NoticeModel noticeModel) {
String loadReportId = (String) noticeModel.getParamMap().get("id");
String projectId = (String) noticeModel.getParamMap().get("projectId");
try {
List<MessageDetail> messageDetails;
switch (taskType) {
case NoticeConstants.Mode.API:
messageDetails = noticeService.searchMessageByTypeBySend(NoticeConstants.TaskType.JENKINS_TASK, loadReportId);
messageDetails = noticeService.searchMessageByTypeBySend(NoticeConstants.TaskType.JENKINS_TASK, loadReportId, projectId);
break;
case NoticeConstants.Mode.SCHEDULE:
messageDetails = noticeService.searchMessageByTestId(noticeModel.getTestId());

View File

@ -3,9 +3,9 @@ package io.metersphere.notice.service;
import com.alibaba.fastjson.JSON;
import io.metersphere.base.domain.MessageTask;
import io.metersphere.base.domain.MessageTaskExample;
import io.metersphere.base.mapper.LoadTestReportMapper;
import io.metersphere.base.mapper.MessageTaskMapper;
import io.metersphere.base.mapper.UserMapper;
import io.metersphere.base.domain.Project;
import io.metersphere.base.domain.Workspace;
import io.metersphere.base.mapper.*;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.LogUtil;
@ -35,6 +35,10 @@ public class NoticeService {
private LoadTestReportMapper loadTestReportMapper;
@Resource
private UserMapper userMapper;
@Resource
private ProjectMapper projectMapper;
@Resource
private WorkspaceMapper workspaceMapper;
public void saveMessageTask(MessageDetail messageDetail) {
MessageTaskExample example = new MessageTaskExample();
@ -146,12 +150,18 @@ public class NoticeService {
}
}
public List<MessageDetail> searchMessageByTypeBySend(String type, String id) {
public List<MessageDetail> searchMessageByTypeBySend(String type, String id, String projectId) {
try {
String orgId = "";
if (null == SessionUtils.getUser()) {
String userId = loadTestReportMapper.selectByPrimaryKey(id).getUserId();
orgId = userMapper.selectByPrimaryKey(userId).getLastOrganizationId();
if (StringUtils.isNotBlank(projectId)) {
Project project = projectMapper.selectByPrimaryKey(projectId);
Workspace workspace = workspaceMapper.selectByPrimaryKey(project.getWorkspaceId());
orgId = workspace.getOrganizationId();
} else {
String userId = loadTestReportMapper.selectByPrimaryKey(id).getUserId();
orgId = userMapper.selectByPrimaryKey(userId).getLastOrganizationId();
}
} else {
SessionUser user = SessionUtils.getUser();
orgId = user.getLastOrganizationId();