潘志宝
9 天以前 11a7424fd4e119e5094764bf19cc359e2b3eb76d
提交 | 用户 | 时间
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.XxlJobLogReport;
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7
8 import java.util.Calendar;
9 import java.util.Date;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.concurrent.TimeUnit;
13
14 /**
15  * job log report helper
16  *
17  * @author xuxueli 2019-11-22
18  */
19 public class JobLogReportHelper {
20     private static Logger logger = LoggerFactory.getLogger(JobLogReportHelper.class);
21
22     private static JobLogReportHelper instance = new JobLogReportHelper();
23     public static JobLogReportHelper getInstance(){
24         return instance;
25     }
26
27
28     private Thread logrThread;
29     private volatile boolean toStop = false;
30     public void start(){
31         logrThread = new Thread(new Runnable() {
32
33             @Override
34             public void run() {
35
36                 // last clean log time
37                 long lastCleanLogTime = 0;
38
39
40                 while (!toStop) {
41
42                     // 1、log-report refresh: refresh log report in 3 days
43                     try {
44
45                         for (int i = 0; i < 3; i++) {
46
47                             // today
48                             Calendar itemDay = Calendar.getInstance();
49                             itemDay.add(Calendar.DAY_OF_MONTH, -i);
50                             itemDay.set(Calendar.HOUR_OF_DAY, 0);
51                             itemDay.set(Calendar.MINUTE, 0);
52                             itemDay.set(Calendar.SECOND, 0);
53                             itemDay.set(Calendar.MILLISECOND, 0);
54
55                             Date todayFrom = itemDay.getTime();
56
57                             itemDay.set(Calendar.HOUR_OF_DAY, 23);
58                             itemDay.set(Calendar.MINUTE, 59);
59                             itemDay.set(Calendar.SECOND, 59);
60                             itemDay.set(Calendar.MILLISECOND, 999);
61
62                             Date todayTo = itemDay.getTime();
63
64                             // refresh log-report every minute
65                             XxlJobLogReport xxlJobLogReport = new XxlJobLogReport();
66                             xxlJobLogReport.setTriggerDay(todayFrom);
67                             xxlJobLogReport.setRunningCount(0);
68                             xxlJobLogReport.setSucCount(0);
69                             xxlJobLogReport.setFailCount(0);
70
71                             Map<String, Object> triggerCountMap = XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().findLogReport(todayFrom, todayTo);
72                             if (triggerCountMap!=null && triggerCountMap.size()>0) {
73                                 int triggerDayCount = triggerCountMap.containsKey("triggerDayCount")?Integer.valueOf(String.valueOf(triggerCountMap.get("triggerDayCount"))):0;
74                                 int triggerDayCountRunning = triggerCountMap.containsKey("triggerDayCountRunning")?Integer.valueOf(String.valueOf(triggerCountMap.get("triggerDayCountRunning"))):0;
75                                 int triggerDayCountSuc = triggerCountMap.containsKey("triggerDayCountSuc")?Integer.valueOf(String.valueOf(triggerCountMap.get("triggerDayCountSuc"))):0;
76                                 int triggerDayCountFail = triggerDayCount - triggerDayCountRunning - triggerDayCountSuc;
77
78                                 xxlJobLogReport.setRunningCount(triggerDayCountRunning);
79                                 xxlJobLogReport.setSucCount(triggerDayCountSuc);
80                                 xxlJobLogReport.setFailCount(triggerDayCountFail);
81                             }
82
83                             // do refresh
84                             int ret = XxlJobAdminConfig.getAdminConfig().getXxlJobLogReportDao().update(xxlJobLogReport);
85                             if (ret < 1) {
86                                 XxlJobAdminConfig.getAdminConfig().getXxlJobLogReportDao().save(xxlJobLogReport);
87                             }
88                         }
89
90                     } catch (Exception e) {
91                         if (!toStop) {
92                             logger.error(">>>>>>>>>>> xxl-job, job log report thread error:{}", e);
93                         }
94                     }
95
96                     // 2、log-clean: switch open & once each day
97                     if (XxlJobAdminConfig.getAdminConfig().getLogretentiondays()>0
98                             && System.currentTimeMillis() - lastCleanLogTime > 24*60*60*1000) {
99
100                         // expire-time
101                         Calendar expiredDay = Calendar.getInstance();
102                         expiredDay.add(Calendar.DAY_OF_MONTH, -1 * XxlJobAdminConfig.getAdminConfig().getLogretentiondays());
103                         expiredDay.set(Calendar.HOUR_OF_DAY, 0);
104                         expiredDay.set(Calendar.MINUTE, 0);
105                         expiredDay.set(Calendar.SECOND, 0);
106                         expiredDay.set(Calendar.MILLISECOND, 0);
107                         Date clearBeforeTime = expiredDay.getTime();
108
109                         // clean expired log
110                         List<Long> logIds = null;
111                         do {
112                             logIds = XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().findClearLogIds(0, 0, clearBeforeTime, 0, 1000);
113                             if (logIds!=null && logIds.size()>0) {
114                                 XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().clearLog(logIds);
115                             }
116                         } while (logIds!=null && logIds.size()>0);
117
118                         // update clean time
119                         lastCleanLogTime = System.currentTimeMillis();
120                     }
121
122                     try {
123                         TimeUnit.MINUTES.sleep(1);
124                     } catch (Exception e) {
125                         if (!toStop) {
126                             logger.error(e.getMessage(), e);
127                         }
128                     }
129
130                 }
131
132                 logger.info(">>>>>>>>>>> xxl-job, job log report thread stop");
133
134             }
135         });
136         logrThread.setDaemon(true);
137         logrThread.setName("xxl-job, admin JobLogReportHelper");
138         logrThread.start();
139     }
140
141     public void toStop(){
142         toStop = true;
143         // interrupt and wait
144         logrThread.interrupt();
145         try {
146             logrThread.join();
147         } catch (InterruptedException e) {
148             logger.error(e.getMessage(), e);
149         }
150     }
151
152 }