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

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

View File

@ -17,3 +17,7 @@ ALTER TABLE `user`
ADD platform_info LONGTEXT NULL COMMENT ' 其他平台对接信息'; ADD platform_info LONGTEXT NULL COMMENT ' 其他平台对接信息';
ALTER TABLE issues ADD platform_status varchar(50) 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');