潘志宝
2024-11-21 d338b50afd6504a9676f0a26b3ecbcc844483e7c
提交 | 用户 | 时间
e7c126 1 package com.xxl.job.admin.core.thread;
H 2
3 import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
4 import com.xxl.job.admin.core.model.XxlJobInfo;
5 import com.xxl.job.admin.core.model.XxlJobLog;
6 import com.xxl.job.admin.core.trigger.TriggerTypeEnum;
7 import com.xxl.job.admin.core.util.I18nUtil;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import java.util.List;
12 import java.util.concurrent.TimeUnit;
13
14 /**
15  * job monitor instance
16  *
17  * @author xuxueli 2015-9-1 18:05:56
18  */
19 public class JobFailMonitorHelper {
20     private static Logger logger = LoggerFactory.getLogger(JobFailMonitorHelper.class);
21     
22     private static JobFailMonitorHelper instance = new JobFailMonitorHelper();
23     public static JobFailMonitorHelper getInstance(){
24         return instance;
25     }
26
27     // ---------------------- monitor ----------------------
28
29     private Thread monitorThread;
30     private volatile boolean toStop = false;
31     public void start(){
32         monitorThread = new Thread(new Runnable() {
33
34             @Override
35             public void run() {
36
37                 // monitor
38                 while (!toStop) {
39                     try {
40
41                         List<Long> failLogIds = XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().findFailJobLogIds(1000);
42                         if (failLogIds!=null && !failLogIds.isEmpty()) {
43                             for (long failLogId: failLogIds) {
44
45                                 // lock log
46                                 int lockRet = XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().updateAlarmStatus(failLogId, 0, -1);
47                                 if (lockRet < 1) {
48                                     continue;
49                                 }
50                                 XxlJobLog log = XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().load(failLogId);
51                                 XxlJobInfo info = XxlJobAdminConfig.getAdminConfig().getXxlJobInfoDao().loadById(log.getJobId());
52
53                                 // 1、fail retry monitor
54                                 if (log.getExecutorFailRetryCount() > 0) {
55                                     JobTriggerPoolHelper.trigger(log.getJobId(), TriggerTypeEnum.RETRY, (log.getExecutorFailRetryCount()-1), log.getExecutorShardingParam(), log.getExecutorParam(), null);
56                                     String retryMsg = "<br><br><span style=\"color:#F39C12;\" > >>>>>>>>>>>"+ I18nUtil.getString("jobconf_trigger_type_retry") +"<<<<<<<<<<< </span><br>";
57                                     log.setTriggerMsg(log.getTriggerMsg() + retryMsg);
58                                     XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().updateTriggerInfo(log);
59                                 }
60
61                                 // 2、fail alarm monitor
62                                 int newAlarmStatus = 0;        // 告警状态:0-默认、-1=锁定状态、1-无需告警、2-告警成功、3-告警失败
63                                 if (info != null) {
64                                     boolean alarmResult = XxlJobAdminConfig.getAdminConfig().getJobAlarmer().alarm(info, log);
65                                     newAlarmStatus = alarmResult?2:3;
66                                 } else {
67                                     newAlarmStatus = 1;
68                                 }
69
70                                 XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().updateAlarmStatus(failLogId, -1, newAlarmStatus);
71                             }
72                         }
73
74                     } catch (Exception e) {
75                         if (!toStop) {
76                             logger.error(">>>>>>>>>>> xxl-job, job fail monitor thread error:{}", e);
77                         }
78                     }
79
80                     try {
81                         TimeUnit.SECONDS.sleep(10);
82                     } catch (Exception e) {
83                         if (!toStop) {
84                             logger.error(e.getMessage(), e);
85                         }
86                     }
87
88                 }
89
90                 logger.info(">>>>>>>>>>> xxl-job, job fail monitor thread stop");
91
92             }
93         });
94         monitorThread.setDaemon(true);
95         monitorThread.setName("xxl-job, admin JobFailMonitorHelper");
96         monitorThread.start();
97     }
98
99     public void toStop(){
100         toStop = true;
101         // interrupt and wait
102         monitorThread.interrupt();
103         try {
104             monitorThread.join();
105         } catch (InterruptedException e) {
106             logger.error(e.getMessage(), e);
107         }
108     }
109
110 }