前言
本文对JobLogReportHelper 内容进行介绍。
一、JobLogReportHelper 作用:
JobLogReportHelper是xxl-job-admin中的一个辅助类,主要用于处理和上报任务执行日志的相关操作。其作用包括:
-
收集任务执行日志:JobLogReportHelper负责收集任务执行的相关日志信息,包括任务执行详细日志、执行结果、执行时间等。
-
存储日志信息:将收集到的任务执行日志信息进行存储,可以存储到数据库或日志文件中,便于后续查阅和分析。
-
上报日志信息:JobLogReportHelper负责将任务执行日志信息上报给xxl-job-admin,用于任务执行情况的监控和统计。
-
分析和报告:通过JobLogReportHelper,管理员可以对任务执行日志信息进行分析和报告,了解任务执行的情况和效果。
总的来说,JobLogReportHelper在xxl-job-admin中扮演着任务执行日志信息收集、上报和分析的重要角色,帮助管理员监控任务执行情况,分析任务执行效果,并及时发现和处理问题,提高系统的稳定性和可靠性。
二、JobLogReportHelper 源码内容:
2.1 start() 初始化:
// 日志对象创建
private static Logger logger = LoggerFactory.getLogger(JobLogReportHelper.class);
// JobLogReportHelper 对象实例化
private static JobLogReportHelper instance = new JobLogReportHelper();
public static JobLogReportHelper getInstance(){
return instance;
}
private Thread logrThread;
private volatile boolean toStop = false;
public void start(){
logrThread = new Thread(new Runnable() {
@Override
public void run() {
// last clean log time
long lastCleanLogTime = 0;
while (!toStop) {
// 1、log-report refresh: refresh log report in 3 days
try {
// 统计最近3天的任务执行情况
for (int i = 0; i < 3; i++) {
// today
Calendar itemDay = Calendar.getInstance();
itemDay.add(Calendar.DAY_OF_MONTH, -i);
itemDay.set(Calendar.HOUR_OF_DAY, 0);
itemDay.set(Calendar.MINUTE, 0);
itemDay.set(Calendar.SECOND, 0);
itemDay.set(Calendar.MILLISECOND, 0);
Date todayFrom = itemDay.getTime();
itemDay.set(Calendar.HOUR_OF_DAY, 23);
itemDay.set(Calendar.MINUTE, 59);
itemDay.set(Calendar.SECOND, 59);
itemDay.set(Calendar.MILLISECOND, 999);
Date todayTo = itemDay.getTime();
// refresh log-report every minute
XxlJobLogReport xxlJobLogReport = new XxlJobLogReport();
xxlJobLogReport.setTriggerDay(todayFrom);
xxlJobLogReport.setRunningCount(0);
xxlJobLogReport.setSucCount(0);
xxlJobLogReport.setFailCount(0);
// 获取某一天的任务执行器请求
/**
* SELECT
COUNT(handle_code) triggerDayCount,
SUM(CASE WHEN (trigger_code in (0, 200) and handle_code = 0) then 1 else 0 end) as triggerDayCountRunning,
SUM(CASE WHEN handle_code = 200 then 1 else 0 end) as triggerDayCountSuc
FROM xxl_job_log
WHERE trigger_time BETWEEN #{from} and #{to}
**/
Map<String, Object> triggerCountMap = XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().findLogReport(todayFrom, todayTo);
if (triggerCountMap!=null && triggerCountMap.size()>0) {
int triggerDayCount = triggerCountMap.containsKey("triggerDayCount")?Integer.valueOf(String.valueOf(triggerCountMap.get("triggerDayCount"))):0;
// triggerDayCountRunning 任务触发成功的数量
int triggerDayCountRunning = triggerCountMap.containsKey("triggerDayCountRunning")?Integer.valueOf(String.valueOf(triggerCountMap.get("triggerDayCountRunning"))):0;
// triggerDayCountSuc 任务执行成功的数量
int triggerDayCountSuc = triggerCountMap.containsKey("triggerDayCountSuc")?Integer.valueOf(String.valueOf(triggerCountMap.get("triggerDayCountSuc"))):0;
// triggerDayCountFail 任务执行失败的数量
int triggerDayCountFail = triggerDayCount - triggerDayCountRunning - triggerDayCountSuc;
xxlJobLogReport.setRunningCount(triggerDayCountRunning);
xxlJobLogReport.setSucCount(triggerDayCountSuc);
xxlJobLogReport.setFailCount(triggerDayCountFail);
}
// do refresh 更新某天的任务执行状况
int ret = XxlJobAdminConfig.getAdminConfig().getXxlJobLogReportDao().update(xxlJobLogReport);
if (ret < 1) {
// 如果这天还没有报告则插入一条数据
XxlJobAdminConfig.getAdminConfig().getXxlJobLogReportDao().save(xxlJobLogReport);
}
}
} catch (Exception e) {
if (!toStop) {
logger.error(">>>>>>>>>>> xxl-job, job log report thread error:{}", e);
}
}
// 2、log-clean: switch open & once each day
// 日志清除,如果保留日志的天数配置的是大于7 填的( xxl.job.logretentiondays 默认值30)
// 并且今天还没有去清理过,则进行日志清理
if (XxlJobAdminConfig.getAdminConfig().getLogretentiondays()>0
&& System.currentTimeMillis() - lastCleanLogTime > 24*60*60*1000) {
// expire-time
Calendar expiredDay = Calendar.getInstance();
expiredDay.add(Calendar.DAY_OF_MONTH, -1 * XxlJobAdminConfig.getAdminConfig().getLogretentiondays());
expiredDay.set(Calendar.HOUR_OF_DAY, 0);
expiredDay.set(Calendar.MINUTE, 0);
expiredDay.set(Calendar.SECOND, 0);
expiredDay.set(Calendar.MILLISECOND, 0);
Date clearBeforeTime = expiredDay.getTime();
// clean expired log
List<Long> logIds = null;
do {
// 获取xxl.job.logretentiondays 配置的时间之前的日志id
logIds = XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().findClearLogIds(0, 0, clearBeforeTime, 0, 1000);
if (logIds!=null && logIds.size()>0) {
// 删除日志记录
/**
* delete from xxl_job_log
WHERE id in
<foreach collection="logIds" item="item" open="(" close=")" separator="," >
#{item}
</foreach>
**/
XxlJobAdminConfig.getAdminConfig().getXxlJobLogDao().clearLog(logIds);
}
} while (logIds!=null && logIds.size()>0);
// update clean time 更新最新的清除时间标记
lastCleanLogTime = System.currentTimeMillis();
}
try {
TimeUnit.MINUTES.sleep(1);
} catch (Exception e) {
if (!toStop) {
logger.error(e.getMessage(), e);
}
}
}
logger.info(">>>>>>>>>>> xxl-job, job log report thread stop");
}
});
// 设置线程的守护线程,线程名称,开启线程
logrThread.setDaemon(true);
logrThread.setName("xxl-job, admin JobLogReportHelper");
logrThread.start();
}
保留的日志天数获取:
public int getLogretentiondays() {
// 如果配置的日志保留天数小于7 则不进行日志清除
if (logretentiondays < 7) {
return -1; // Limit greater than or equal to 7, otherwise close
}
return logretentiondays;
}
2.2 toStop() 释放资源:
当服务端项目停止时触发toStop() 方法进行资源的释放.
public void toStop(){
// 停止标识重置,使其跳出 logrThread 线程的while 循环
toStop = true;
// interrupt and wait
logrThread.interrupt();
try {
// 等待logrThread 任务执行完毕
logrThread.join();
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
}
总结
本文对JobLogReportHelper 工作内容进行介绍。