parent
e52a5cd73b
commit
ba849694f1
@ -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()); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue