diff --git a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/mapper/ScheduleRulesMapper.java b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/mapper/ScheduleRulesMapper.java index a2df65b0..5baf8948 100644 --- a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/mapper/ScheduleRulesMapper.java +++ b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/mapper/ScheduleRulesMapper.java @@ -16,6 +16,12 @@ public interface ScheduleRulesMapper { + /** + * 将 status 为 1 或 5 的记录更新为 3 + * @return 更新的记录数 + */ + int updateStatusFrom1And5To3(); + public List selectList(); public List selectListOne(); diff --git a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/ScheduleShutdownHook.java b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/ScheduleShutdownHook.java new file mode 100644 index 00000000..a150e71c --- /dev/null +++ b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/ScheduleShutdownHook.java @@ -0,0 +1,57 @@ +package com.alops.system.service.impl; + +import com.alops.system.mapper.ScheduleRulesMapper; +import com.alops.system.service.IAlertSenderService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +@Component +public class ScheduleShutdownHook { + private static final Logger log = LoggerFactory.getLogger(ScheduleShutdownHook.class); + + @Autowired + private ScheduleRulesMapper scheduleRulesMapper; + + @Autowired + private IAlertSenderService iAlertSenderService; + + private static ScheduleShutdownHook instance; + + @PostConstruct + public void init() { + instance = this; + registerShutdownHook(); + } + + private void registerShutdownHook() { + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + log.info("检测到应用关闭,开始更新任务状态..."); + updateInterruptedStatus(); + })); + } + + private void updateInterruptedStatus() { + try { + // 将所有状态为1(执行中)、5(等待中)的任务更新为3(执行失败) + int affectedRows = scheduleRulesMapper.updateStatusFrom1And5To3(); + log.info("成功更新 {} 个任务状态为中断", affectedRows); + + // 发送站内信 + if (affectedRows > 0) { + iAlertSenderService.sendInternalMessage( + "系统中断", + "采集任务被外部中断,已更新" + affectedRows + "个任务状态", + "2" + ); + } + + } catch (Exception e) { + log.error("中断处理失败: {}", e.getMessage(), e); + // 备用方案:直接输出到控制台 + System.err.println("中断处理失败: " + e.getMessage()); + } + } +} diff --git a/ALOps_sys_backend/alops-system/src/main/resources/mapper/system/ScheduleRulesMapper.xml b/ALOps_sys_backend/alops-system/src/main/resources/mapper/system/ScheduleRulesMapper.xml index 4533e85c..7598fa92 100644 --- a/ALOps_sys_backend/alops-system/src/main/resources/mapper/system/ScheduleRulesMapper.xml +++ b/ALOps_sys_backend/alops-system/src/main/resources/mapper/system/ScheduleRulesMapper.xml @@ -42,6 +42,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" select id, rule_name, rule_type, frequency, gather_time, start_time, end_time, status,created_at ,created_by from schedule_rules + + UPDATE schedule_rules + SET status = 3 + WHERE status IN (1, 5) + + +