1. 添加AuditAdvice类,用以向审计接口持续发送用户操作数据

2. 添加任务结束状态Scheduled方法,用以周期性扫库将任务修改为已结束状态
This commit is contained in:
EnderByEndera
2024-01-17 09:44:29 +08:00
parent 3a770192b3
commit 7112e85a26
21 changed files with 172 additions and 10 deletions

View File

@@ -18,5 +18,9 @@ public interface CommandMapper {
Boolean startCommandsByTaskId(@Param("task_id") Long taskId);
Boolean setCommandValid(@Param("command_id") String commandId);
Boolean setCommandInvalid(@Param("command_id") String commandId);
List<TaskCommandInfo> queryCommandInfoByTaskId(@Param("task_id") Long taskId);
}

View File

@@ -50,7 +50,6 @@ public class CommandService {
};
sqlSessionWrapper.startBatchSession(CommandMapper.class, function, taskCommandInfos);
}
public List<TaskCommandInfo> queryCommandInfoByTaskId(Long taskId) {

View File

@@ -4,6 +4,7 @@ import com.realtime.protection.configuration.entity.task.Task;
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@@ -46,4 +47,7 @@ public interface TaskMapper {
List<Integer> queryDynamicRuleIdsFromTaskId(@Param("task_id") Long taskId);
List<Integer> queryStaticRuleIdsFromTaskId(@Param("task_id") Long taskId);
@Select("SELECT task_id FROM t_task WHERE task_end_time < NOW() AND task_status != #{task_status}")
List<Long> queryTasksByStatus(@Param("task_status") Integer taskStatus);
}

View File

@@ -3,13 +3,16 @@ package com.realtime.protection.server.task;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.realtime.protection.configuration.entity.task.Task;
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
import com.realtime.protection.configuration.utils.enums.StateEnum;
import com.realtime.protection.configuration.utils.status.AuditStatusValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Slf4j
@DS("mysql")
public class TaskService {
private final TaskMapper taskMapper;
@@ -119,4 +122,8 @@ public class TaskService {
taskMapper.newTaskUsingCommandInfo(taskCommandInfo);
return taskCommandInfo.getTaskId();
}
public List<Long> getFinishedTasks() {
return taskMapper.queryTasksByStatus(StateEnum.FINISHED.getStateNum());
}
}

View File

@@ -7,11 +7,17 @@ import com.realtime.protection.configuration.utils.status.State;
import com.realtime.protection.server.command.CommandService;
import com.realtime.protection.server.task.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
@Service
@EnableScheduling
@Slf4j
public class StateChangeService {
private final CommandService commandService;
@@ -73,4 +79,22 @@ public class StateChangeService {
// 我们需要保证只有任务创建函数才能将GENERATING状态转换为RUNNING状态
return !Objects.equals(originalState, StateEnum.GENERATING.getState());
}
@Scheduled(cron = "0 0/2 * * * ?")
@Transactional
@Async
protected void finishTasks() {
List<Long> finishedTaskIds = taskService.getFinishedTasks();
log.debug("成功扫描出所有需要变为结束状态的任务:" + finishedTaskIds);
for (Long taskId : finishedTaskIds) {
try {
changeState(StateEnum.FINISHED.getStateNum(), taskId, true);
} catch (Exception e) {
log.warn(String.format("任务%d从%s状态变为FINISHED状态遭遇异常%s",
taskId, taskService.queryTaskStatus(taskId), e.getMessage()));
}
}
}
}

View File

@@ -62,15 +62,19 @@ public class StateHandler {
return true;
}
// todo: 如果是实时任务或者研判后处置任务,那么就需要在任务启动之后,立刻向动态规则中指定的系统发送日志筛选请求。
// 筛选完成后,系统返回日志,需要由接收端点提取字段,并且合成一条静态规则,再按照任务开始时间、结束时间和任务类型进行指令创建
private Boolean handleJudgedTaskStart(CommandService commandService, TaskService taskService, Long taskId) {
// todo: 研判后处置任务的指令的is_valid字段一开始需要设置为false
return true;
}
private Boolean handleDynamicTaskStart(CommandService commandService, TaskService taskService, Long taskId) {
// todo: 实时任务的指令的is_valid字段一开始需要设置为true
return true;
}
private Boolean handleStaticTaskStart(CommandService commandService, TaskService taskService, Long taskId) throws DorisStartException {
private Boolean handleStaticTaskStart(CommandService commandService, TaskService taskService, Long taskId) {
// 如果未能获取staticTaskCommandInfos需要报错
List<TaskCommandInfo> staticTaskCommandInfos = taskService.getStaticCommandInfos(taskId);
if (staticTaskCommandInfos == null || staticTaskCommandInfos.isEmpty()) {

View File

@@ -13,6 +13,7 @@ public class FailedState extends StateHandler implements State {
return switch (StateEnum.getStateEnumByState(newState)) {
case RUNNING -> handleStart(taskService, commandService, taskId);
case STOP -> handleStop(commandService, taskId);
case FINISHED -> handleFinish(commandService, taskId);
default -> throw new IllegalStateException("Unexpected value: " + StateEnum.getStateEnumByState(newState));
};
}

View File

@@ -12,6 +12,7 @@ public class GeneratingState extends StateHandler implements State {
return switch (StateEnum.getStateEnumByState(newState)) {
case RUNNING -> true;
case FAILED -> handleFailed(commandService, taskId);
case FINISHED -> handleFinish(commandService, taskId);
default -> throw new IllegalStateException("Unexpected value: " + StateEnum.getStateEnumByState(newState));
};
}

View File

@@ -13,6 +13,7 @@ public class PendingState extends StateHandler implements State {
return switch (StateEnum.getStateEnumByState(newState)) {
case FAILED -> handleFailed(commandService, taskId);
case RUNNING -> handleStart(taskService, commandService, taskId);
case FINISHED -> handleFinish(commandService, taskId);
default -> throw new IllegalStateException("Unexpected value: " + StateEnum.getStateEnumByState(newState));
};
}

View File

@@ -14,6 +14,7 @@ public class StopState extends StateHandler implements State {
return switch (StateEnum.getStateEnumByState(newState)) {
case RUNNING -> handleStart(taskService, commandService, taskId);
case FAILED -> handleFailed(commandService, taskId);
case FINISHED -> handleFinish(commandService, taskId);
default -> throw new IllegalStateException("Unexpected value: " + StateEnum.getStateEnumByState(newState));
};
}