From b51b3260c7d9e51922ad41d32b22effe6927a90d Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Fri, 21 Feb 2025 10:24:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=AB=E8=A1=A8=E6=A3=80=E6=B5=8B=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E4=BB=BB=E5=8A=A1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ycwl/basic/watchdog/TaskWatchDog.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/com/ycwl/basic/watchdog/TaskWatchDog.java diff --git a/src/main/java/com/ycwl/basic/watchdog/TaskWatchDog.java b/src/main/java/com/ycwl/basic/watchdog/TaskWatchDog.java new file mode 100644 index 0000000..c2ef2f4 --- /dev/null +++ b/src/main/java/com/ycwl/basic/watchdog/TaskWatchDog.java @@ -0,0 +1,47 @@ +package com.ycwl.basic.watchdog; + +import com.ycwl.basic.mapper.TaskMapper; +import com.ycwl.basic.model.pc.task.entity.TaskEntity; +import com.ycwl.basic.notify.NotifyFactory; +import com.ycwl.basic.notify.entity.NotifyContent; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@Profile("prod") +public class TaskWatchDog { + + @Autowired + private TaskMapper taskMapper; + + @Scheduled(fixedDelay = 1000 * 60L) + public void scanTaskStatus() { + List allNotRunningTaskList = taskMapper.selectAllNotRunning(); + String title = "任务堆积警告!"; + StringBuilder content = new StringBuilder(); + if (allNotRunningTaskList.size() > 10) { + content.append("当前任务队列中存在超过10个未运行任务,请及时处理!"); + } + List allRunningTaskList = taskMapper.selectAllRunning(); + for (TaskEntity taskEntity : allRunningTaskList) { + if (taskEntity.getStartTime() == null) { + continue; + } + // startTime已经过去3分钟了 + if (System.currentTimeMillis() - taskEntity.getStartTime().getTime() > 1000 * 60 * 3) { + content.append("当前【").append(taskEntity.getWorkerId()).append("】渲染机的【").append(taskEntity.getId()).append("】任务已超过3分钟未完成!"); + } + } + if (StringUtils.isNotBlank(content)) { + NotifyFactory.via().sendTo( + new NotifyContent(title, content.toString()), + "default_user" + ); + } + } +}