fix(任务中心): 优化任务中心执行中的任务检查,当无进行中的任务后停止检查

This commit is contained in:
fit2-zhao 2021-07-22 17:27:47 +08:00 committed by fit2-zhao
parent 8f6969bffe
commit ac6b92019c
6 changed files with 26 additions and 11 deletions

View File

@ -11,6 +11,6 @@ public interface ExtTaskMapper {
void deleteByResourceId(String id); void deleteByResourceId(String id);
List<TaskCenterDTO> getTasks (@Param("request") TaskCenterRequest request); List<TaskCenterDTO> getTasks (@Param("request") TaskCenterRequest request);
List<TaskCenterDTO> getRunningTasks (@Param("request") TaskCenterRequest request); int getRunningTasks (@Param("request") TaskCenterRequest request);
} }

View File

@ -52,8 +52,8 @@
</select> </select>
<select id="getRunningTasks" resultType="io.metersphere.task.dto.TaskCenterDTO" parameterType="java.lang.String"> <select id="getRunningTasks" resultType="java.lang.Integer" parameterType="java.lang.String">
SELECT tt.* FROM ( SELECT count(tt.id) FROM (
(select t.id,t.create_time as executionTime (select t.id,t.create_time as executionTime
from api_scenario_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.actuator = t2.id from api_scenario_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.actuator = t2.id
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.execute_type !='Debug' and t.execute_type !='Marge' and t.project_id= #{request.projectId} and t.status not in ("saved","completed","success","error") where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.execute_type !='Debug' and t.execute_type !='Marge' and t.project_id= #{request.projectId} and t.status not in ("saved","completed","success","error")

View File

@ -27,7 +27,7 @@ public class TaskController {
@PostMapping("/count/running") @PostMapping("/count/running")
@RequiresPermissions("PROJECT_API_SCENARIO:READ") @RequiresPermissions("PROJECT_API_SCENARIO:READ")
public int getRunningTasks(@RequestBody TaskCenterRequest request) { public int getRunningTasks(@RequestBody TaskCenterRequest request) {
return taskService.getRunningTasks(request).size(); return taskService.getRunningTasks(request);
} }
} }

View File

@ -22,9 +22,9 @@ public class TaskService {
return extTaskMapper.getTasks(request); return extTaskMapper.getTasks(request);
} }
public List<TaskCenterDTO> getRunningTasks(TaskCenterRequest request) { public int getRunningTasks(TaskCenterRequest request) {
if (StringUtils.isEmpty(request.getProjectId())) { if (StringUtils.isEmpty(request.getProjectId())) {
return new ArrayList<>(); return 0;
} }
return extTaskMapper.getRunningTasks(request); return extTaskMapper.getRunningTasks(request);
} }

View File

@ -31,7 +31,7 @@ public class TaskCenterWebSocket {
public void onOpen(@PathParam("projectId") String projectId, Session session) { public void onOpen(@PathParam("projectId") String projectId, Session session) {
Timer timer = new Timer(true); Timer timer = new Timer(true);
TaskCenterWebSocket.TaskCenter task = new TaskCenterWebSocket.TaskCenter(session, projectId); TaskCenterWebSocket.TaskCenter task = new TaskCenterWebSocket.TaskCenter(session, projectId);
timer.schedule(task, 0, 3 * 1000); timer.schedule(task, 0, 6 * 1000);
refreshTasks.putIfAbsent(session, timer); refreshTasks.putIfAbsent(session, timer);
} }
@ -92,11 +92,14 @@ public class TaskCenterWebSocket {
@Override @Override
public void run() { public void run() {
try { try {
int taskTotal = taskService.getRunningTasks(request).size(); int taskTotal = taskService.getRunningTasks(request);
if (!session.isOpen()) { if (!session.isOpen()) {
return; return;
} }
session.getBasicRemote().sendText(taskTotal + ""); session.getBasicRemote().sendText(taskTotal + "");
if (taskTotal == 0) {
session.close();
}
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
} }

View File

@ -10,7 +10,7 @@
<template v-slot:content> <template v-slot:content>
<span>{{ $t('commons.task_center') }}</span> <span>{{ $t('commons.task_center') }}</span>
</template> </template>
<div @click="showTaskCenter" v-if="runningTotal > 0"> <div @click="showTaskCenter" v-if="runningTotal > 0" >
<el-badge :value="runningTotal" class="item" type="primary"> <el-badge :value="runningTotal" class="item" type="primary">
<font-awesome-icon class="icon global focusing" :icon="['fas', 'tasks']" <font-awesome-icon class="icon global focusing" :icon="['fas', 'tasks']"
style="font-size: 18px"/> style="font-size: 18px"/>
@ -61,7 +61,7 @@
<div class="report-container"> <div class="report-container">
<div v-for="item in taskData" :key="item.id" style="margin-bottom: 5px"> <div v-for="item in taskData" :key="item.id" style="margin-bottom: 5px">
<el-card class="ms-card-task" @click.native="showReport(item,$event)"> <el-card class="ms-card-task" @click.native="showReport(item,$event)">
<span><el-link type="primary">{{getModeName(item.executionModule)}} </el-link>: {{ item.name }} </span><br/> <span><el-link type="primary">{{ getModeName(item.executionModule) }} </el-link>: {{ item.name }} </span><br/>
<span> <span>
执行器{{ item.actuator }} {{ item.executor }} 执行器{{ item.actuator }} {{ item.executor }}
{{ item.executionTime | timestampFormatDate }} {{ item.executionTime | timestampFormatDate }}
@ -217,7 +217,7 @@ export default {
} }
return 60; return 60;
}, },
getModeName(executionModule){ getModeName(executionModule) {
switch (executionModule) { switch (executionModule) {
case "SCENARIO": case "SCENARIO":
return this.$t('test_track.scenario_test_case'); return this.$t('test_track.scenario_test_case');
@ -287,11 +287,23 @@ export default {
getTaskRunning() { getTaskRunning() {
this.initWebSocket(); this.initWebSocket();
}, },
calculationRunningTotal() {
if (this.taskData) {
let total = 0;
this.taskData.forEach(item => {
if (this.getPercentage(item.executionStatus) !== 100) {
total++;
}
})
this.runningTotal = total;
}
},
init() { init() {
this.result.loading = true; this.result.loading = true;
this.condition.projectId = getCurrentProjectID(); this.condition.projectId = getCurrentProjectID();
this.result = this.$post('/task/center/list', this.condition, response => { this.result = this.$post('/task/center/list', this.condition, response => {
this.taskData = response.data; this.taskData = response.data;
this.calculationRunningTotal();
this.initEnd = true; this.initEnd = true;
}); });
} }