1. 同步最新的表结构
2. 记录执行日志
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
package cn.iocoder.dashboard.modules.infra.dal.dataobject.job;
|
||||
|
||||
import cn.iocoder.dashboard.common.pojo.CommonResult;
|
||||
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
|
||||
import cn.iocoder.dashboard.framework.quartz.core.handler.JobHandler;
|
||||
import cn.iocoder.dashboard.modules.infra.enums.job.InfJobLogStatusEnum;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@@ -18,6 +17,9 @@ import java.util.Date;
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class InfJobLogDO extends BaseDO {
|
||||
|
||||
/**
|
||||
@@ -46,7 +48,7 @@ public class InfJobLogDO extends BaseDO {
|
||||
/**
|
||||
* 开始执行时间
|
||||
*/
|
||||
private Date startTime;
|
||||
private Date beginTime;
|
||||
/**
|
||||
* 结束执行时间
|
||||
*/
|
||||
@@ -56,16 +58,16 @@ public class InfJobLogDO extends BaseDO {
|
||||
*/
|
||||
private Integer duration;
|
||||
/**
|
||||
* 结果码
|
||||
* 状态
|
||||
*
|
||||
* 目前使用的 {@link CommonResult#getCode()} 属性
|
||||
* 枚举 {@link InfJobLogStatusEnum}
|
||||
*/
|
||||
private Integer resultCode;
|
||||
private Integer status;
|
||||
/**
|
||||
* 结果
|
||||
* 结果数据
|
||||
*
|
||||
* 成功时,使用 {@link CommonResult#getData()} 数据
|
||||
* 失败时,使用 {@link CommonResult#getMsg()} 属性
|
||||
* 成功时,使用 {@link JobHandler#execute(String)} 的结果
|
||||
* 失败时,使用 {@link JobHandler#execute(String)} 的异常堆栈
|
||||
*/
|
||||
private String result;
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package cn.iocoder.dashboard.modules.infra.dal.mysql.job;
|
||||
|
||||
import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobLogDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 任务日志 Mapper
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Mapper
|
||||
public interface InfJobLogMapper extends BaseMapperX<InfJobLogDO> {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.dashboard.modules.infra.enums.job;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 任务日志的状态枚举
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum InfJobLogStatusEnum {
|
||||
|
||||
RUNNING(0), // 运行中
|
||||
SUCCESS(1), // 成功
|
||||
FAILURE(2); // 失败
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private final Integer status;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package cn.iocoder.dashboard.modules.infra.service.job;
|
||||
|
||||
import cn.iocoder.dashboard.framework.quartz.core.service.JobLogFrameworkService;
|
||||
|
||||
/**
|
||||
* Job 日志 Service 接口
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public interface InfJobLogService extends JobLogFrameworkService {
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package cn.iocoder.dashboard.modules.infra.service.job.impl;
|
||||
|
||||
import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobLogDO;
|
||||
import cn.iocoder.dashboard.modules.infra.dal.mysql.job.InfJobLogMapper;
|
||||
import cn.iocoder.dashboard.modules.infra.enums.job.InfJobLogStatusEnum;
|
||||
import cn.iocoder.dashboard.modules.infra.service.job.InfJobLogService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Job 日志 Service 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class InfJobLogServiceImpl implements InfJobLogService {
|
||||
|
||||
@Resource
|
||||
private InfJobLogMapper jobLogMapper;
|
||||
|
||||
@Override
|
||||
public Long createJobLog(Long jobId, Date beginTime, String jobHandlerName, String jobHandlerParam) {
|
||||
InfJobLogDO log = InfJobLogDO.builder().jobId(jobId).handlerName(jobHandlerName).handlerParam(jobHandlerParam)
|
||||
.beginTime(beginTime).status(InfJobLogStatusEnum.RUNNING.getStatus()).build();
|
||||
jobLogMapper.insert(log);
|
||||
return log.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async
|
||||
public void updateJobLogSuccessAsync(Long logId, Date endTime, Integer duration, String result) {
|
||||
updateJobLogResult(logId, endTime, duration, result, InfJobLogStatusEnum.SUCCESS);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async
|
||||
public void updateJobLogErrorAsync(Long logId, Date endTime, Integer duration, String result) {
|
||||
updateJobLogResult(logId, endTime, duration, result, InfJobLogStatusEnum.FAILURE);
|
||||
}
|
||||
|
||||
private void updateJobLogResult(Long logId, Date endTime, Integer duration, String result, InfJobLogStatusEnum status) {
|
||||
try {
|
||||
InfJobLogDO updateObj = InfJobLogDO.builder().id(logId).endTime(endTime).duration(duration)
|
||||
.status(status.getStatus()).result(result).build();
|
||||
jobLogMapper.updateById(updateObj);
|
||||
} catch (Exception ex) {
|
||||
log.error("[updateJobLogResult][logId({}) endTime({}) duration({}) result({}) status({})]",
|
||||
logId, endTime, duration, result, status);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package cn.iocoder.dashboard.modules.system.job.auth;
|
||||
|
||||
import cn.iocoder.dashboard.common.pojo.CommonResult;
|
||||
import cn.iocoder.dashboard.framework.quartz.core.handler.JobHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -15,7 +14,7 @@ import org.springframework.stereotype.Component;
|
||||
public class SysUserSessionTimeoutJob implements JobHandler {
|
||||
|
||||
@Override
|
||||
public CommonResult<String> execute(String param) throws Exception {
|
||||
public String execute(String param) throws Exception {
|
||||
// System.out.println("执行了一次任务");
|
||||
log.info("[execute][执行任务:{}]", param);
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user