refactor(测试计划): 测试计划定时任务指定user

This commit is contained in:
CaptainB 2022-10-10 17:01:27 +08:00 committed by 刘瑞斌
parent 2344385ea1
commit 3e6b55cc17
4 changed files with 20 additions and 18 deletions

View File

@ -63,7 +63,6 @@ public class HttpHeaderUtils {
headers.add(SessionConstants.ACCESS_KEY, accessKey);
headers.add(SessionConstants.SIGNATURE, CodingUtil.aesDecrypt(accessKey + "|" + System.currentTimeMillis(), secretKey, accessKey));
headers.remove(HttpHeaders.COOKIE);
sessionUserThreadLocal.remove();
}
return headers;
@ -95,4 +94,8 @@ public class HttpHeaderUtils {
sessionUserThreadLocal.remove();
}
}
public static void clearUser() {
sessionUserThreadLocal.remove();
}
}

View File

@ -110,11 +110,6 @@ public class MicroService {
return JSON.parseArray(JSON.toJSONString(resultHolder.getData()), typeReference);
}
public MicroService runAsUser(UserDTO user) {
HttpHeaderUtils.runAsUser(user);
return this;
}
/**
* 批量GET
*

View File

@ -1,8 +1,8 @@
package io.metersphere.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fit2cloud.quartz.anno.QuartzScheduled;
import io.metersphere.commons.constants.MicroServiceName;
import io.metersphere.commons.utils.HttpHeaderUtils;
import io.metersphere.dto.ProjectDTO;
import io.metersphere.dto.UserDTO;
import org.springframework.stereotype.Service;
@ -17,11 +17,13 @@ public class SystemDemoService {
@Resource
private MicroService microService;
// @QuartzScheduled(cron = "0 0/1 * * * ?")
// @QuartzScheduled(cron = "0 0/1 * * * ?")
public void test1() {
UserDTO user = baseUserService.getUserDTO("admin");
List<ProjectDTO> result = microService.runAsUser(user).getForData(MicroServiceName.PROJECT_MANAGEMENT, "/project/get-owner-projects", new TypeReference<>() {
HttpHeaderUtils.runAsUser(user);
List<ProjectDTO> result = microService.getForData(MicroServiceName.PROJECT_MANAGEMENT, "/project/get-owner-projects", new TypeReference<>() {
});
HttpHeaderUtils.clearUser();
System.out.println(result);
}
}

View File

@ -3,9 +3,10 @@ package io.metersphere.plan.job;
import io.metersphere.commons.constants.ReportTriggerMode;
import io.metersphere.commons.constants.ScheduleGroup;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.HttpHeaderUtils;
import io.metersphere.plan.service.TestPlanService;
import io.metersphere.sechedule.MsScheduleJob;
import io.metersphere.service.BaseUserService;
import org.quartz.*;
/**
@ -20,8 +21,11 @@ public class TestPlanTestJob extends MsScheduleJob {
private TestPlanService testPlanService;
private BaseUserService baseUserService;
public TestPlanTestJob() {
this.testPlanService = CommonBeanFactory.getBean(TestPlanService.class);
this.baseUserService = CommonBeanFactory.getBean(BaseUserService.class);
}
/**
@ -43,6 +47,7 @@ public class TestPlanTestJob extends MsScheduleJob {
businessExecute(context);
}
@Override
protected void businessExecute(JobExecutionContext context) {
@ -52,14 +57,11 @@ public class TestPlanTestJob extends MsScheduleJob {
String runResourceId = this.resourceId;
String runProjectId = this.projectID;
String runUserId = this.userId;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
LogUtil.info("Start test_plan_scehdule. test_plan_id:" + runResourceId);
testPlanService.run(runResourceId, runProjectId, runUserId, ReportTriggerMode.SCHEDULE.name(),null,config);
}
});
thread.start();
// 定时任务指定调用微服务的user
HttpHeaderUtils.runAsUser(baseUserService.getUserDTO(runUserId));
testPlanService.run(runResourceId, runProjectId, runUserId, ReportTriggerMode.SCHEDULE.name(), null, config);
HttpHeaderUtils.clearUser();
}
public static JobKey getJobKey(String testId) {