修复调度任务手动出发执行相关bug

This commit is contained in:
aoshiguchen
2022-09-18 16:24:40 +08:00
parent eeda818041
commit 282631e7e7
10 changed files with 26 additions and 16 deletions
@@ -54,6 +54,7 @@ public class DefaultJobSource implements IJobSource {
.setDesc(handler.desc())
.setCron(handler.cron())
.setParam(handler.param())
.setEnable(true)
);
}
return jobInfoList;
@@ -31,7 +31,8 @@ public interface IJobCallback {
/**
* 执行日志
* @param jobInfo
* @param param
* @param throwable
*/
void executeLog(JobInfo jobInfo, Throwable throwable);
void executeLog(JobInfo jobInfo, String param, Throwable throwable);
}
@@ -112,7 +112,7 @@ public class JobExecutor implements ApplicationRunner, IJobExecutor {
@Override
public void add(JobInfo jobInfo) throws JobException {
if (null == jobInfo || StringUtil.isEmpty(jobInfo.getId()) || StringUtil.isEmpty(jobInfo.getName()) ||
StringUtil.isEmpty(jobInfo.getCron()) || runJobSet.contains(jobInfo.getName())) {
StringUtil.isEmpty(jobInfo.getCron())) {
return;
}
synchronized (jobInfo.getId()) {
@@ -128,8 +128,10 @@ public class JobExecutor implements ApplicationRunner, IJobExecutor {
JobDetail jobDetail = JobBuilder.newJob(JobBean.class).withIdentity(jobKey).build();
try {
scheduler.scheduleJob(jobDetail, cronTrigger);
scheduler.start();
if (jobInfo.isEnable()) {
scheduler.scheduleJob(jobDetail, cronTrigger);
scheduler.start();
}
} catch (Exception e) {
throw new RuntimeException(String.format("新增job[name=%s]异常", jobInfo.getName()));
}
@@ -190,10 +192,10 @@ public class JobExecutor implements ApplicationRunner, IJobExecutor {
try {
jobHandler.execute(param);
if (null != jobCallback) {
jobCallback.executeLog(jobInfo, null);
jobCallback.executeLog(jobInfo, param, null);
}
} catch (Throwable e) {
jobCallback.executeLog(jobInfo, e);
jobCallback.executeLog(jobInfo, param, e);
}
});
}
@@ -39,5 +39,6 @@ public class JobInfo {
private String desc;
private String cron;
private String param;
private boolean enable;
private Map<String, Object> extension;
}
@@ -34,7 +34,7 @@ import lombok.extern.slf4j.Slf4j;
public class JobCallback implements IJobCallback {
@Override
public void executeLog(JobInfo jobInfo, Throwable throwable) {
public void executeLog(JobInfo jobInfo, String param, Throwable throwable) {
if (null == throwable) {
log.info("job[name={}]执行完毕", jobInfo.getId(), jobInfo.getName());
} else {
@@ -16,6 +16,6 @@ import java.util.Date;
public interface DataCleanMapper extends SqlMapper {
@Delete("delete from `job_log` where create_time < ?")
void cleanJobLog(Date date);
void cleanJobLog(long date);
}
@@ -56,8 +56,8 @@ public interface JobInfoMapper extends SqlMapper {
void updateEnableStatus(@Param("id") Integer id, @Param("enable") Integer enable, @Param("updateTime") Date updateTime);
@ResultType(JobInfoDO.class)
@Select("select * from job_info where enable = 1")
List<JobInfoDO> findEnableList();
@Select("select * from job_info")
List<JobInfoDO> findList();
void update(JobInfoDO jobInfoDO);
}
@@ -11,6 +11,7 @@ import fun.asgc.neutrino.proxy.server.dal.DataCleanMapper;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
@@ -38,14 +39,16 @@ public class DataCleanJob implements IJobHandler {
public void execute(String s) throws Exception {
JobParams jobParams = getParams(s);
Date date = DateUtil.addDate(new Date(), Calendar.DATE, jobParams.getJobLogKeepDays());
Date date = DateUtil.addDate(new Date(), Calendar.DATE, -1 * jobParams.getJobLogKeepDays());
log.info("清理调度管理日志 date:{}", sdf.format(date));
dataCleanMapper.cleanJobLog(date);
dataCleanMapper.cleanJobLog(date.getTime());
}
public static JobParams getParams(String s) {
try {
return JSONObject.parseObject(s, JobParams.class);
if (StringUtils.isNotBlank(s)) {
return JSONObject.parseObject(s, JobParams.class);
}
} catch (Exception e) {
// ignore
}
@@ -74,6 +74,7 @@ public class JobInfoService implements IJobSource {
.setDesc(jobInfoDO.getDesc())
.setCron(jobInfoDO.getCron())
.setParam(jobInfoDO.getParam())
.setEnable(true)
);
} else {
BeanManager.getBean(JobExecutor.class).remove(String.valueOf(req.getId()));
@@ -89,7 +90,7 @@ public class JobInfoService implements IJobSource {
@Override
public List<JobInfo> sourceList() {
List<JobInfo> jobInfoList = Lists.newArrayList();
List<JobInfoDO> jobInfoDOList = jobInfoMapper.findEnableList();
List<JobInfoDO> jobInfoDOList = jobInfoMapper.findList();
if (CollectionUtil.isEmpty(jobInfoDOList)) {
return jobInfoList;
}
@@ -100,6 +101,7 @@ public class JobInfoService implements IJobSource {
.setDesc(item.getDesc())
.setCron(item.getCron())
.setParam(item.getParam())
.setEnable(EnableStatusEnum.ENABLE.getStatus().equals(item.getEnable()))
);
}
@@ -46,7 +46,7 @@ public class JobLogService implements IJobCallback {
private JobLogMapper jobLogMapper;
@Override
public void executeLog(JobInfo jobInfo, Throwable throwable) {
public void executeLog(JobInfo jobInfo, String param, Throwable throwable) {
Integer code = 0;
String msg = "";
if (null == throwable) {
@@ -60,7 +60,7 @@ public class JobLogService implements IJobCallback {
jobLogMapper.add(new JobLogDO()
.setJobId(Integer.valueOf(jobInfo.getId()))
.setHandler(jobInfo.getName())
.setParam(jobInfo.getParam())
.setParam(param)
.setCode(code)
.setMsg(msg)
.setAlarmStatus(0)