Commit c7e4b8bc by Ren Ping

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

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