This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
enderbyendera-realtime-prot…/src/main/java/com/realtime/protection/server/command/CommandService.java

119 lines
4.3 KiB
Java
Raw Normal View History

package com.realtime.protection.server.command;
import com.alibaba.excel.util.ListUtils;
2024-01-12 14:31:34 +08:00
import com.baomidou.dynamic.datasource.annotation.DS;
import com.realtime.protection.configuration.entity.task.Command;
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
import com.realtime.protection.configuration.exception.DorisStartException;
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
2024-01-12 14:31:34 +08:00
import com.realtime.protection.configuration.utils.enums.StateEnum;
import com.realtime.protection.server.task.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Function;
@Service
@Slf4j
public class CommandService {
private final CommandMapper commandMapper;
2024-01-12 14:31:34 +08:00
private final TaskService taskService;
private final SqlSessionWrapper sqlSessionWrapper;
2024-01-12 14:31:34 +08:00
private static final int BatchSize = 100;
private final Function<CommandMapper, Function<TaskCommandInfo, Void>> createCommandBatchFunction;
2024-01-12 14:31:34 +08:00
public CommandService(CommandMapper commandMapper, TaskService taskService, SqlSessionWrapper sqlSessionWrapper) {
this.commandMapper = commandMapper;
2024-01-12 14:31:34 +08:00
this.taskService = taskService;
this.sqlSessionWrapper = sqlSessionWrapper;
this.createCommandBatchFunction = mapper -> info -> {
if (info.getFrequency() == null) {
Command command = Command.generateCommand(info, info.getStartTime());
2024-01-12 14:31:34 +08:00
mapper.createCommand(command);
}
List<Command> commandBatch = ListUtils.newArrayListWithExpectedSize(BatchSize);
LocalDateTime validTime = info.getStartTime();
while (validTime.isBefore(info.getEndTime())) {
Command command = Command.generateCommand(info, validTime);
commandBatch.add(command);
validTime = validTime.plusMinutes(info.getFrequency());
if (commandBatch.size() < BatchSize) {
continue;
}
2024-01-12 14:31:34 +08:00
mapper.createCommands(commandBatch);
commandBatch.clear();
}
if (!commandBatch.isEmpty()) {
2024-01-12 14:31:34 +08:00
mapper.createCommands(commandBatch);
commandBatch.clear();
}
log.debug(String.format("create all the commands from task(%d), rule(%d)",
info.getTaskId(), info.getRuleId()));
return null;
};
}
@Async
2024-01-12 14:31:34 +08:00
@DS("doris")
public void createCommand(TaskCommandInfo commandInfo) throws DorisStartException {
try {
sqlSessionWrapper.startBatchSession(CommandMapper.class, createCommandBatchFunction, commandInfo);
} catch (Exception e) {
2024-01-12 14:31:34 +08:00
throw new DorisStartException(e, commandInfo.getTaskId());
}
}
@Async
2024-01-12 14:31:34 +08:00
@DS("doris")
public void createCommands(List<TaskCommandInfo> taskCommandInfos) throws DorisStartException {
Function<CommandMapper, Function<List<TaskCommandInfo>, Void>> function = mapper -> list -> {
if (list == null || list.isEmpty()) {
return null;
}
for (TaskCommandInfo info : list) {
createCommandBatchFunction.apply(mapper).apply(info);
}
2024-01-12 14:31:34 +08:00
taskService.changeTaskStatus(list.get(0).getTaskId(), StateEnum.RUNNING.getStateNum());
return null;
};
try {
sqlSessionWrapper.startBatchSession(CommandMapper.class, function, taskCommandInfos);
} catch (Exception e) {
2024-01-12 14:31:34 +08:00
TaskCommandInfo info = taskCommandInfos.get(0);
Long taskId = null;
if (info != null) {
taskId = info.getTaskId();
}
throw new DorisStartException(e, taskId);
}
}
2024-01-12 14:31:34 +08:00
@DS("doris")
public Boolean startCommandsByTaskId(Long taskId) {
return commandMapper.startCommandsByTaskId(taskId);
}
2024-01-12 14:31:34 +08:00
@DS("doris")
public Boolean stopCommandsByTaskId(Long taskId) {
return commandMapper.stopCommandsByTaskId(taskId);
}
2024-01-12 14:31:34 +08:00
@DS("doris")
public Boolean removeCommandsByTaskId(Long taskId) {
return commandMapper.removeCommandsByTaskId(taskId);
}
}