1. application.yml修改为application-dev.yml和application-prod.yml
2. 添加更多Exception拦截器 3. 编写状态模式处理task状态的更改 4. 添加StateChangeService,用以处理所有任务状态转换相关的内容 5. 添加StateEnum, ProtocolEnum,TaskTypeEnum用以处理任务和协议相关的所有状态和类型
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package com.realtime.protection.server.task.status;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
||||
import com.realtime.protection.configuration.exception.DorisStartException;
|
||||
import com.realtime.protection.configuration.utils.enums.StateEnum;
|
||||
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.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class StateChangeService {
|
||||
private final CommandService commandService;
|
||||
private final TaskService taskService;
|
||||
|
||||
public StateChangeService(CommandService commandService, TaskService taskService) {
|
||||
this.commandService = commandService;
|
||||
this.taskService = taskService;
|
||||
}
|
||||
|
||||
@DSTransactional
|
||||
public Boolean changeState(Integer stateNum, Long taskId) throws DorisStartException {
|
||||
Integer originalStateNum = taskService.queryTaskStatus(taskId);
|
||||
if (originalStateNum == null) {
|
||||
throw new IllegalArgumentException("cannot find status of task " + taskId + ", maybe task doesn't exist?");
|
||||
}
|
||||
|
||||
State originalState = StateEnum.getStateByNum(originalStateNum);
|
||||
|
||||
State newState = StateEnum.getStateByNum(stateNum);
|
||||
|
||||
if (!originalState.handle(newState, commandService, taskService, taskId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
log.debug(String.format("successfully let task(%d) change state from %s to %s",
|
||||
taskId,
|
||||
originalState.getClass().getSimpleName(),
|
||||
newState.getClass().getSimpleName()));
|
||||
|
||||
// 这里一定是handle成功的状态,我们再进行task status的修改,如果handle失败,要么返回false,要么抛出异常,不会进入此处
|
||||
return taskService.changeTaskStatus(taskId, stateNum);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.realtime.protection.server.task.status;
|
||||
|
||||
import com.realtime.protection.configuration.entity.task.Task;
|
||||
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
|
||||
import com.realtime.protection.configuration.exception.DorisStartException;
|
||||
import com.realtime.protection.configuration.utils.enums.TaskTypeEnum;
|
||||
import com.realtime.protection.configuration.utils.status.AuditStatus;
|
||||
import com.realtime.protection.server.command.CommandService;
|
||||
import com.realtime.protection.server.task.TaskService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class StateHandler {
|
||||
|
||||
protected Boolean handleStart(TaskService taskService, CommandService commandService, Long taskId) throws DorisStartException {
|
||||
Task task = taskService.queryTask(taskId);
|
||||
|
||||
if (task == null) {
|
||||
throw new IllegalArgumentException("invalid task id");
|
||||
}
|
||||
|
||||
Integer taskAuditStatus = task.getTaskAuditStatus();
|
||||
|
||||
if (taskAuditStatus == null) {
|
||||
throw new IllegalArgumentException("invalid task id, because task_audit_status is null");
|
||||
}
|
||||
|
||||
// 如果审核状态不为已通过审核,则无效
|
||||
if (taskAuditStatus != AuditStatus.AUDITED.getAuditStatus()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return switch (TaskTypeEnum.getTaskTypeByNum(task.getTaskType())) {
|
||||
case STATIC -> handleStaticTaskStart(commandService, taskService, taskId);
|
||||
case DYNAMIC -> handleDynamicTaskStart(commandService, taskService, taskId);
|
||||
case JUDGED -> handleJudgedTaskStart(commandService, taskService, taskId);
|
||||
};
|
||||
}
|
||||
|
||||
protected Boolean handleResume(CommandService commandService, Long taskId) {
|
||||
commandService.startCommandsByTaskId(taskId);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected Boolean handlePause(CommandService commandService, Long taskId) {
|
||||
commandService.stopCommandsByTaskId(taskId);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected Boolean handleStop(CommandService commandService, Long taskId) {
|
||||
commandService.removeCommandsByTaskId(taskId);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected Boolean handleFinish(CommandService commandService, Long taskId) {
|
||||
commandService.removeCommandsByTaskId(taskId);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected Boolean handleFailed(CommandService commandService, Long taskId) {
|
||||
commandService.removeCommandsByTaskId(taskId);
|
||||
return true;
|
||||
}
|
||||
|
||||
private Boolean handleJudgedTaskStart(CommandService commandService, TaskService taskService, Long taskId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
private Boolean handleDynamicTaskStart(CommandService commandService, TaskService taskService, Long taskId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
private Boolean handleStaticTaskStart(CommandService commandService, TaskService taskService, Long taskId) throws DorisStartException {
|
||||
// 如果未能获取staticTaskCommandInfos,需要报错
|
||||
List<TaskCommandInfo> staticTaskCommandInfos = taskService.getStaticCommandInfos(taskId);
|
||||
if (staticTaskCommandInfos == null || staticTaskCommandInfos.isEmpty()) {
|
||||
throw new IllegalArgumentException("static rules are empty, need to choose at least one static rule");
|
||||
}
|
||||
|
||||
try {
|
||||
commandService.createCommands(staticTaskCommandInfos);
|
||||
} catch (DorisStartException e) {
|
||||
e.taskId = taskId;
|
||||
throw e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.realtime.protection.server.task.status.states;
|
||||
|
||||
import com.realtime.protection.configuration.exception.DorisStartException;
|
||||
import com.realtime.protection.configuration.utils.enums.StateEnum;
|
||||
import com.realtime.protection.configuration.utils.status.State;
|
||||
import com.realtime.protection.server.command.CommandService;
|
||||
import com.realtime.protection.server.task.TaskService;
|
||||
import com.realtime.protection.server.task.status.StateHandler;
|
||||
|
||||
public class FailedState extends StateHandler implements State {
|
||||
@Override
|
||||
public Boolean handle(State newState, CommandService commandService, TaskService taskService, Long taskId) throws DorisStartException {
|
||||
return switch (StateEnum.getStateEnumByState(newState)) {
|
||||
case RUNNING -> handleStart(taskService, commandService, taskId);
|
||||
case STOP -> handleStop(commandService, taskId);
|
||||
case FAILED -> true;
|
||||
default -> throw new IllegalStateException("Unexpected value: " + StateEnum.getStateEnumByState(newState));
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.realtime.protection.server.task.status.states;
|
||||
|
||||
import com.realtime.protection.configuration.utils.status.State;
|
||||
import com.realtime.protection.server.command.CommandService;
|
||||
import com.realtime.protection.server.task.TaskService;
|
||||
|
||||
public class FinishedState implements State {
|
||||
@Override
|
||||
public Boolean handle(State newState, CommandService commandService, TaskService taskService, Long taskId) {
|
||||
return newState instanceof FinishedState;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.realtime.protection.server.task.status.states;
|
||||
|
||||
import com.realtime.protection.configuration.utils.enums.StateEnum;
|
||||
import com.realtime.protection.configuration.utils.status.State;
|
||||
import com.realtime.protection.server.command.CommandService;
|
||||
import com.realtime.protection.server.task.TaskService;
|
||||
import com.realtime.protection.server.task.status.StateHandler;
|
||||
|
||||
public class PauseState extends StateHandler implements State {
|
||||
@Override
|
||||
public Boolean handle(State newState, CommandService commandService, TaskService taskService, Long taskId) {
|
||||
return switch (StateEnum.getStateEnumByState(newState)) {
|
||||
case RUNNING -> handleResume(commandService, taskId);
|
||||
case STOP -> handleStop(commandService, taskId);
|
||||
case FINISHED -> handleFinish(commandService, taskId);
|
||||
case FAILED -> handleFailed(commandService, taskId);
|
||||
default -> throw new IllegalStateException("Unexpected value: " + StateEnum.getStateEnumByState(newState));
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.realtime.protection.server.task.status.states;
|
||||
|
||||
import com.realtime.protection.configuration.exception.DorisStartException;
|
||||
import com.realtime.protection.configuration.utils.enums.StateEnum;
|
||||
import com.realtime.protection.configuration.utils.status.State;
|
||||
import com.realtime.protection.server.command.CommandService;
|
||||
import com.realtime.protection.server.task.TaskService;
|
||||
import com.realtime.protection.server.task.status.StateHandler;
|
||||
|
||||
public class PendingState extends StateHandler implements State {
|
||||
@Override
|
||||
public Boolean handle(State newState, CommandService commandService, TaskService taskService, Long taskId) throws DorisStartException {
|
||||
return switch (StateEnum.getStateEnumByState(newState)) {
|
||||
case RUNNING -> handleStart(taskService, commandService, taskId);
|
||||
case FAILED -> handleFailed(commandService, taskId);
|
||||
default -> throw new IllegalStateException("Unexpected value: " + StateEnum.getStateEnumByState(newState));
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.realtime.protection.server.task.status.states;
|
||||
|
||||
import com.realtime.protection.configuration.utils.enums.StateEnum;
|
||||
import com.realtime.protection.configuration.utils.status.State;
|
||||
import com.realtime.protection.server.command.CommandService;
|
||||
import com.realtime.protection.server.task.TaskService;
|
||||
import com.realtime.protection.server.task.status.StateHandler;
|
||||
|
||||
public class RunningState extends StateHandler implements State {
|
||||
@Override
|
||||
public Boolean handle(State newState, CommandService commandService, TaskService taskService, Long taskId) {
|
||||
return switch(StateEnum.getStateEnumByState(newState)) {
|
||||
case RUNNING -> true;
|
||||
case PAUSED -> handlePause(commandService, taskId);
|
||||
case STOP -> handleStop(commandService, taskId);
|
||||
case FINISHED -> handleFinish(commandService, taskId);
|
||||
case FAILED -> handleFailed(commandService, taskId);
|
||||
default -> throw new IllegalStateException("Unexpected value: " + StateEnum.getStateEnumByState(newState));
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.realtime.protection.server.task.status.states;
|
||||
|
||||
import com.realtime.protection.configuration.exception.DorisStartException;
|
||||
import com.realtime.protection.configuration.utils.enums.StateEnum;
|
||||
import com.realtime.protection.configuration.utils.status.State;
|
||||
import com.realtime.protection.server.command.CommandService;
|
||||
import com.realtime.protection.server.task.TaskService;
|
||||
import com.realtime.protection.server.task.status.StateHandler;
|
||||
|
||||
public class StopState extends StateHandler implements State {
|
||||
|
||||
@Override
|
||||
public Boolean handle(State newState, CommandService commandService, TaskService taskService, Long taskId) throws DorisStartException {
|
||||
return switch (StateEnum.getStateEnumByState(newState)) {
|
||||
case RUNNING -> handleStart(taskService, commandService, taskId);
|
||||
case FAILED -> handleFailed(commandService, taskId);
|
||||
default -> throw new IllegalStateException("Unexpected value: " + StateEnum.getStateEnumByState(newState));
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user