feat: 调度策略增加日志类型,允许日志不存储数据库提高性能

This commit is contained in:
qiufeng
2023-05-20 12:15:15 +08:00
parent f0962cabef
commit 458bab6d88
4 changed files with 25 additions and 8 deletions
@@ -55,6 +55,8 @@ public class JobLog extends BaseEntity<String> {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "结束时间")
private Date endTime;
@ApiModelProperty(value = "日志类型(0入库日志 1文件日志)")
private Integer logType;
@ApiModelProperty(value = "耗时(ms)")
private Long costTime;
@ApiModelProperty(value = "执行状态(0开始 1调度成功 2调度失败 3执行成功 4执行失败)")
@@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Date;
@@ -97,6 +98,10 @@ public class JobLogController {
if (!jobLog.getStatus().equals(JobStatus.执行成功.getValue()) && !jobLog.getStatus().equals(JobStatus.执行失败.getValue())) {
return Result.fail(false, "错误:传入状态不正确");
}
log.info(MessageFormat.format("任务:{0}-执行状态:{1}-任务ID:{2}-订阅ID:{3}", jobLog.getJobName(), jobLog.getStatus(), jobLog.getId(), jobLog.getSubscribeId()));
if (1 == jobLog.getLogType()) {
return Result.ok(true, "任务回调设置状态成功");
}
JobLog newLog = new JobLog().setId(jobLog.getId()).setStatus(jobLog.getStatus());
Date createTime = jobLog.getCreateTime();
if (createTime != null) {
@@ -52,6 +52,8 @@ public class Job extends BaseEntity<String> {
private String timeZone;
@ApiModelProperty(value = "备注信息")
private String remark;
@ApiModelProperty(value = "日志类型(0入库日志 1文件日志)")
private Integer logType;
@ApiModelProperty(value = "订阅列表")
@TableField(exist = false)
private List<JobSubscribe> subscribes;
@@ -2,12 +2,12 @@ package cn.com.mfish.scheduler.execute;
import cn.com.mfish.common.core.utils.SpringBeanFactory;
import cn.com.mfish.common.core.utils.Utils;
import cn.com.mfish.common.scheduler.api.entity.JobLog;
import cn.com.mfish.common.scheduler.config.enums.JobStatus;
import cn.com.mfish.common.scheduler.config.utils.InvokeUtils;
import cn.com.mfish.scheduler.common.JobUtils;
import cn.com.mfish.scheduler.entity.Job;
import cn.com.mfish.common.scheduler.api.entity.JobLog;
import cn.com.mfish.scheduler.entity.JobSubscribe;
import cn.com.mfish.common.scheduler.config.enums.JobStatus;
import cn.com.mfish.scheduler.enums.JobType;
import cn.com.mfish.scheduler.invoke.BaseInvoke;
import cn.com.mfish.scheduler.service.JobLogService;
@@ -64,7 +64,7 @@ public abstract class AbstractJobExecute extends QuartzJobBean {
execute(JobType.getJob(job.getJobType()), threadLocal.get(), list);
success = true;
} catch (Exception ex) {
log.error(MessageFormat.format("任务ID:{0}执行异常,任务ID:{1}:", job.getJobName(), job.getId()), ex);
log.error(MessageFormat.format("任务:{0}-执行状态:异常-任务ID:{1}-订阅ID:{2}", job.getJobName(), job.getId(), subscribeId), ex);
success = false;
error = ex.getMessage();
} finally {
@@ -88,8 +88,14 @@ public abstract class AbstractJobExecute extends QuartzJobBean {
.setClassName(job.getClassName())
.setMethodName(job.getMethodName())
.setParams(job.getParams())
.setStatus(JobStatus.开始.getValue());
.setStatus(JobStatus.开始.getValue())
.setLogType(job.getLogType());
jobLog.setCreateBy(OPERATOR).setCreateTime(new Date());
log.info(MessageFormat.format("任务:{0}-执行状态:{1}-任务ID:{2}-订阅ID:{3}", jobLog.getJobName(), jobLog.getStatus(), jobLog.getId(), jobLog.getSubscribeId()));
threadLocal.set(jobLog);
if (1 == jobLog.getLogType()) {
return;
}
JobSubscribeService jobSubscribeService = SpringBeanFactory.getBean(JobSubscribeService.class);
JobSubscribe jobSubscribe = jobSubscribeService.getById(subscribeId);
if (jobSubscribe != null) {
@@ -97,7 +103,6 @@ public abstract class AbstractJobExecute extends QuartzJobBean {
jobLog.setStartTime(jobSubscribe.getStartTime());
jobLog.setEndTime(jobSubscribe.getEndTime());
}
threadLocal.set(jobLog);
JobLogService jobLogService = SpringBeanFactory.getBean(JobLogService.class);
if (jobLogService.save(jobLog)) {
log.info(MessageFormat.format("任务:{0}调度成功,任务ID:{1}", job.getJobName(), job.getId()));
@@ -112,16 +117,19 @@ public abstract class AbstractJobExecute extends QuartzJobBean {
* @param success 任务是否成功
* @param remark 错误信息
*/
public static void executeCallBack(boolean success, String remark) {
private static void executeCallBack(boolean success, String remark) {
JobLog jobLog = threadLocal.get();
threadLocal.remove();
if (jobLog == null) {
log.error("错误:未获取到任务日志");
return;
}
JobLogService jobLogService = SpringBeanFactory.getBean(JobLogService.class);
JobStatus jobStatus = success ? JobStatus.调度成功 : JobStatus.调度失败;
log.info(MessageFormat.format("任务{0}执行{1},任务ID:{2}", jobLog.getJobName(), jobStatus, jobLog.getId()));
log.info(MessageFormat.format("任务:{0}-执行状态:{1}-任务ID:{2}-订阅ID:{3}", jobLog.getJobName(), jobStatus, jobLog.getId(), jobLog.getSubscribeId()));
if (1 == jobLog.getLogType()) {
return;
}
JobLogService jobLogService = SpringBeanFactory.getBean(JobLogService.class);
//数据库设置长度1000,截取999位
if (remark != null && remark.length() >= 1000) {
remark = remark.substring(0, 999);