Commit c7e4b8bc by Ren Ping

fix:自动任务截止时间优化

1 parent 0eafd292
package com.dituhui.pea.dispatch.quartz.dispatch;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.dituhui.pea.dispatch.service.SchedulerService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
/**
* 自动派工任务
......@@ -28,20 +29,29 @@ public class AutoDispatchJob extends QuartzJobBean {
@Resource
private SchedulerService schedulerService;
@Autowired
private Scheduler scheduler;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
try {
JobKey jobKey = jobExecutionContext.getJobDetail().getKey();
CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(new TriggerKey(jobKey.getName(), jobKey.getName()));
String name = jobKey.getName();
String teamId = name.substring(TEAM_JOB_PREFIX.length());
long start = System.currentTimeMillis();
// 获取任务的下次执行时间
Date nextFireTime = cronTrigger.getFireTimeAfter(new Date());
log.info(">>> teamId:{},下次触发时间:{}, {}", teamId, nextFireTime.getTime(), DateUtil.format(nextFireTime, "yyyy-MM-dd HH:mm:ss"));
TimeInterval timeInterval = new TimeInterval();
log.info(">>> 自动派工(teamId:{}) 自动任务开始", teamId);
/*RedissonUtil.lockOperation(AutoDispatchJob.TEAM_JOB_PREFIX + teamId, 60, () -> {
schedulerService.dispatchRun2(teamId);
});*/
schedulerService.dispatchRun2(teamId);
long end = System.currentTimeMillis();
log.info(">>> 自动派工(teamId:{}) 自动任务结束,耗时:{}", teamId, end - start);
schedulerService.dispatchRun2(teamId, nextFireTime);
log.info(">>> 自动派工(teamId:{}) 自动任务结束,耗时:{}", teamId, timeInterval.interval());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
......
package com.dituhui.pea.dispatch.service;
import java.util.Date;
public interface SchedulerService {
/**
......@@ -9,5 +11,5 @@ public interface SchedulerService {
* @author RenPing
* @date 2023/11/02
*/
void dispatchRun2(String teamId);
void dispatchRun2(String teamId, Date nextFireTime);
}
......@@ -28,6 +28,7 @@ import java.io.File;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
......@@ -75,7 +76,7 @@ public class SchedulerServiceImpl implements SchedulerService {
}
@Override
public void dispatchRun2(String teamId) {
public void dispatchRun2(String teamId, Date nextFireTime) {
OrgTeamEntity orgTeamEntity = orgTeamDao.findByTeamId(teamId);
if (Objects.isNull(orgTeamEntity)) {
log.error(">>> teamId:{} 不存在", teamId);
......@@ -94,18 +95,14 @@ public class SchedulerServiceImpl implements SchedulerService {
cutOffTime = "18:00";
}
LocalDateTime now = LocalDateTime.now();
String today = DateUtil.format(now, "yyyy-MM-dd");
String nowTime = DateUtil.format(now, "HH:mm");
String nowTimePlus30 = DateUtil.format(now.plusMinutes(30), "HH:mm");
String nextTime = DateUtil.format(nextFireTime, "HH:mm");
boolean cutOff = false;
//当前时间大于等于截止时间,或者当前时间+30分钟大于截止时间,运行停止
if (nowTime.compareTo(cutOffTime) >= 0 || nowTimePlus30.compareTo(cutOffTime) > 0) {
if (nextTime.compareTo(cutOffTime) > 0) {
cutOff = true;
}
String groupId = orgTeamEntity.getGroupId();
log.info("dispatchRun group:{}, team:{} done", groupId, teamId);
......@@ -114,7 +111,8 @@ public class SchedulerServiceImpl implements SchedulerService {
Optional<DispatchBatch> optional = dispatchBatchRepository.findByTeamIdAndBatchDate(teamId, currDay);
if (optional.isPresent()
&& Objects.nonNull(optional.get().getCutoffedTime())
&& DateUtil.format(optional.get().getCutoffedTime(), "yyyy-MM-dd").equals(today)) {
&& DateUtil.format(optional.get().getCutoffedTime(), "yyyy-MM-dd")
.equals(DateUtil.format(new Date(), "yyyy-MM-dd"))) {
//自动任务截止
log.error(">>> teamId:{}, day:{} 自动任务已截止", teamId, currDay);
return;
......
......@@ -3,7 +3,7 @@ server:
dispatch:
cron:
expr: 0 50 8-23 * * ?
expr: 0 3 8-23 * * ?
next-day-limit: 2
scheduler:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!