refactor: 添加同步缺陷定时任务

This commit is contained in:
chenjianxing 2021-07-01 22:27:40 +08:00 committed by fit2-zhao
parent c8c2432868
commit 94976e191d
5 changed files with 46 additions and 42 deletions

View File

@ -2098,29 +2098,20 @@ public class ApiAutomationService {
}
public void checkApiScenarioUseUrl() {
try {
final String key = "init.scenario.url";
String value = systemParameterService.getValue(key);
if (StringUtils.isBlank(value)) {
List<String> noUrlScenarioIdList = extApiScenarioMapper.selectIdsByUseUrlIsNull();
for (String id : noUrlScenarioIdList) {
ApiScenarioWithBLOBs scenario = apiScenarioMapper.selectByPrimaryKey(id);
if (scenario.getUseUrl() == null) {
List<ApiMethodUrlDTO> useUrl = this.parseUrl(scenario);
if (useUrl != null) {
ApiScenarioWithBLOBs updateModel = new ApiScenarioWithBLOBs();
updateModel.setId(scenario.getId());
updateModel.setUseUrl(JSONArray.toJSONString(useUrl));
apiScenarioMapper.updateByPrimaryKeySelective(updateModel);
updateModel = null;
}
}
scenario = null;
List<String> noUrlScenarioIdList = extApiScenarioMapper.selectIdsByUseUrlIsNull();
for (String id : noUrlScenarioIdList) {
ApiScenarioWithBLOBs scenario = apiScenarioMapper.selectByPrimaryKey(id);
if (scenario.getUseUrl() == null) {
List<ApiMethodUrlDTO> useUrl = this.parseUrl(scenario);
if (useUrl != null) {
ApiScenarioWithBLOBs updateModel = new ApiScenarioWithBLOBs();
updateModel.setId(scenario.getId());
updateModel.setUseUrl(JSONArray.toJSONString(useUrl));
apiScenarioMapper.updateByPrimaryKeySelective(updateModel);
updateModel = null;
}
systemParameterService.saveInitParam(key);
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
scenario = null;
}
}

View File

@ -0,0 +1,5 @@
package io.metersphere.commons.utils;
public interface RunInterface {
void run();
}

View File

@ -5,9 +5,12 @@ import io.metersphere.api.jmeter.NewDriverManager;
import io.metersphere.api.service.ApiAutomationService;
import io.metersphere.base.domain.JarConfig;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.RunInterface;
import io.metersphere.service.JarConfigService;
import io.metersphere.service.ScheduleService;
import io.metersphere.service.SystemParameterService;
import io.metersphere.track.service.IssuesService;
import org.apache.commons.lang3.StringUtils;
import org.python.core.Options;
import org.python.util.PythonInterpreter;
import org.springframework.beans.factory.annotation.Value;
@ -30,6 +33,8 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
@Resource
private ApiAutomationService apiAutomationService;
@Resource
private SystemParameterService systemParameterService;
@Resource
private IssuesService issuesService;
@Value("${jmeter.home}")
private String jmeterHome;
@ -45,9 +50,8 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
initPythonEnv();
checkApiScenarioUseUrl();
issuesService.syncThirdPartyIssues();
initOperate(apiAutomationService::checkApiScenarioUseUrl, "init.scenario.url");
initOperate(issuesService::syncThirdPartyIssues, "init.issue");
try {
Thread.sleep(1 * 60 * 1000);
@ -58,8 +62,17 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
scheduleService.startEnableSchedules();
}
private void checkApiScenarioUseUrl() {
apiAutomationService.checkApiScenarioUseUrl();
private void initOperate(RunInterface initFuc, final String key) {
try {
String value = systemParameterService.getValue(key);
if (StringUtils.isBlank(value)) {
initFuc.run();
systemParameterService.saveInitParam(key);
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
}
/**

View File

@ -364,23 +364,14 @@ public class IssuesService {
}
public void syncThirdPartyIssues() {
try {
final String key = "init.issue";
String value = systemParameterService.getValue(key);
if (StringUtils.isBlank(value)) {
List<String> projectIds = projectService.getProjectIds();
projectIds.forEach(id -> {
try {
syncThirdPartyIssues(id);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
});
systemParameterService.saveInitParam(key);
List<String> projectIds = projectService.getProjectIds();
projectIds.forEach(id -> {
try {
syncThirdPartyIssues(id);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
});
}
public void syncThirdPartyIssues(String projectId) {

View File

@ -17,3 +17,7 @@ ALTER TABLE `user`
ADD platform_info LONGTEXT NULL COMMENT ' 其他平台对接信息';
ALTER TABLE issues ADD platform_status varchar(50) NULL COMMENT '第三方平台状态';
-- 定时同步缺陷
INSERT INTO metersphere.schedule (id,`key`,`type`,value,`group`,job,enable,resource_id,user_id,workspace_id,create_time,update_time,project_id,name)
VALUES ('7a23d4db-9909-438d-9e36-58e432c8c4ae','ISSUE_SYNC','CRON','0 0 3 * * ? ','ISSUE_SYNC','io.metersphere.job.sechedule.IssueSyncJob',1,'system','admin','system',unix_timestamp() * 1000,unix_timestamp() * 1000,'system','ISSUE_SYNC');