Skip to content

Commit

Permalink
ADD: error alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohann0617 committed Dec 28, 2024
1 parent 8faf065 commit 050afbd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void createInstance(CreateInstanceParams params) {
.rootPassword(params.getRootPassword())
.build();
addTask(CommonUtils.CREATE_TASK_PREFIX + taskId, () ->
execCreate(sysUserDTO, instanceService, createTaskService),
execCreate(sysUserDTO, sysService,instanceService, createTaskService),
0, params.getInterval(), TimeUnit.SECONDS);
String beginCreateMsg = String.format(CommonUtils.BEGIN_CREATE_MESSAGE_TEMPLATE,
ociUser.getUsername(),
Expand Down Expand Up @@ -495,7 +495,7 @@ public static void stopTask(String taskId) {
}

public static void execCreate(
SysUserDTO sysUserDTO,
SysUserDTO sysUserDTO, ISysService sysService,
IInstanceService instanceService,
IOciCreateTaskService createTaskService) {

Expand All @@ -520,9 +520,12 @@ public static void execCreate(
TEMP_MAP.remove(CommonUtils.CREATE_COUNTS_PREFIX + sysUserDTO.getTaskId());
stopTask(CommonUtils.CREATE_TASK_PREFIX + sysUserDTO.getTaskId());
createTaskService.remove(new LambdaQueryWrapper<OciCreateTask>().eq(OciCreateTask::getId, sysUserDTO.getTaskId()));
log.error("【开机任务】用户:[{}] ,区域:[{}] ,系统架构:[{}] ,开机数量:[{}] 因异常而终止任务......",
log.error("【开机任务】用户:[{}] ,区域:[{}] ,系统架构:[{}] ,开机数量:[{}] 因超额而终止任务......",
sysUserDTO.getUsername(), sysUserDTO.getOciCfg().getRegion(),
sysUserDTO.getArchitecture(), sysUserDTO.getCreateNumbers());
sysService.sendMessage(String.format("【开机任务】用户:[%s] ,区域:[%s] ,系统架构:[%s] ,开机数量:[%s] 因超额而终止任务",
sysUserDTO.getUsername(), sysUserDTO.getOciCfg().getRegion(),
sysUserDTO.getArchitecture(), sysUserDTO.getCreateNumbers()));
}

if (sysUserDTO.getCreateNumbers() == successCounts || leftCreateNum == 0) {
Expand All @@ -547,6 +550,9 @@ public static void execCreate(
// TEMP_MAP.remove(CommonUtils.CREATE_COUNTS_PREFIX + sysUserDTO.getTaskId());
// stopTask(CommonUtils.CREATE_TASK_PREFIX + sysUserDTO.getTaskId());
// createTaskService.remove(new LambdaQueryWrapper<OciCreateTask>().eq(OciCreateTask::getId, sysUserDTO.getTaskId()));
sysService.sendMessage(String.format("【开机任务】用户:[%s] ,区域:[%s] ,系统架构:[%s] ,开机数量:[%s] 发生了异常",
sysUserDTO.getUsername(), sysUserDTO.getOciCfg().getRegion(),
sysUserDTO.getArchitecture(), sysUserDTO.getCreateNumbers()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public class SysServiceImpl implements ISysService {

@Override
public void sendMessage(String message) {
messageServiceFactory.getMessageService(MessageTypeEnum.MSG_TYPE_DING_DING).sendMessage(message);
messageServiceFactory.getMessageService(MessageTypeEnum.MSG_TYPE_TELEGRAM).sendMessage(message);
CompletableFuture.runAsync(() -> messageServiceFactory.getMessageService(MessageTypeEnum.MSG_TYPE_DING_DING).sendMessage(message));
CompletableFuture.runAsync(() -> messageServiceFactory.getMessageService(MessageTypeEnum.MSG_TYPE_TELEGRAM).sendMessage(message));
}

@Override
Expand All @@ -103,17 +103,20 @@ public String login(LoginParams params) {
if (getEnableMfa()) {
if (params.getMfaCode() == null) {
log.error("请求IP:{} 登录失败,如果不是本人操作,可能存在被攻击的风险", clientIp);
sendMessage(String.format("请求IP:%s 登录失败,如果不是本人操作,可能存在被攻击的风险", clientIp));
throw new OciException(-1, "验证码不能为空");
}
OciKv mfa = kvService.getOne(new LambdaQueryWrapper<OciKv>()
.eq(OciKv::getCode, SysCfgEnum.SYS_MFA_SECRET.getCode()));
if (!CommonUtils.verifyMfaCode(mfa.getValue(), params.getMfaCode())) {
log.error("请求IP:{} 登录失败,如果不是本人操作,可能存在被攻击的风险", clientIp);
sendMessage(String.format("请求IP:%s 登录失败,如果不是本人操作,可能存在被攻击的风险", clientIp));
throw new OciException(-1, "无效的验证码");
}
}
if (!params.getAccount().equals(account) || !params.getPassword().equals(password)) {
log.error("请求IP:{} 登录失败,如果不是本人操作,可能存在被攻击的风险", clientIp);
sendMessage(String.format("请求IP:%s 登录失败,如果不是本人操作,可能存在被攻击的风险", clientIp));
throw new OciException(-1, "账号或密码不正确");
}
Map<String, Object> payload = new HashMap<>(1);
Expand Down Expand Up @@ -425,7 +428,7 @@ private void cleanAndRestartTask() {
.build();
stopTask(CommonUtils.CREATE_TASK_PREFIX + task.getId());
addTask(CommonUtils.CREATE_TASK_PREFIX + task.getId(), () ->
execCreate(sysUserDTO, instanceService, createTaskService),
execCreate(sysUserDTO, this, instanceService, createTaskService),
0, task.getInterval(), TimeUnit.SECONDS);
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/yohann/ocihelper/task/OciTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class OciTask implements ApplicationRunner {
@Resource
private IOciKvService kvService;
@Resource
private ISysService sysService;
@Resource
private IOciCreateTaskService createTaskService;

@Value("${web.account}")
Expand Down Expand Up @@ -96,7 +98,7 @@ private void cleanAndRestartTask() {
.rootPassword(task.getRootPassword())
.build();
addTask(CommonUtils.CREATE_TASK_PREFIX + task.getId(), () ->
execCreate(sysUserDTO, instanceService, createTaskService),
execCreate(sysUserDTO, sysService, instanceService, createTaskService),
0, task.getInterval(), TimeUnit.SECONDS);
}
});
Expand Down

0 comments on commit 050afbd

Please sign in to comment.