From 93e8e9c7af61903c9a7a3bc2d98f139be4cd8e38 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 3 May 2024 22:37:15 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91=E5=85=A8?= =?UTF-8?q?=E5=B1=80=EF=BC=9AJobController=20=E5=A2=9E=E5=8A=A0=20sync=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=AE=9E=E7=8E=B0=E5=B0=86=20infra?= =?UTF-8?q?=5Fjob=20=E5=90=8C=E6=AD=A5=E5=88=B0=20Quartz=20=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/job/JobController.http | 5 ++++ .../controller/admin/job/JobController.java | 8 ++++++ .../module/infra/service/job/JobService.java | 7 ++++++ .../infra/service/job/JobServiceImpl.java | 25 +++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/job/JobController.http diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/job/JobController.http b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/job/JobController.http new file mode 100644 index 000000000..62f8dd606 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/job/JobController.http @@ -0,0 +1,5 @@ +### 请求 /infra/job/sync 接口 => 成功 +POST {{baseUrl}}/infra/job/sync +Content-Type: application/json +tenant-id: {{adminTenentId}} +Authorization: Bearer {{token}} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/job/JobController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/job/JobController.java index 1881db53a..372b6391b 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/job/JobController.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/job/JobController.java @@ -90,6 +90,14 @@ public class JobController { return success(true); } + @PostMapping("/sync") + @Operation(summary = "同步定时任务") + @PreAuthorize("@ss.hasPermission('infra:job:create')") + public CommonResult syncJob() throws SchedulerException { + jobService.syncJob(); + return success(true); + } + @GetMapping("/get") @Operation(summary = "获得定时任务") @Parameter(name = "id", description = "编号", required = true, example = "1024") diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobService.java index 882c21112..971fd1b62 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobService.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobService.java @@ -45,6 +45,13 @@ public interface JobService { */ void triggerJob(Long id) throws SchedulerException; + /** + * 同步定时任务 + * + * 目的:自己存储的 Job 信息,强制同步到 Quartz 中 + */ + void syncJob() throws SchedulerException; + /** * 删除定时任务 * diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobServiceImpl.java index dd887e165..cfc52d29d 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobServiceImpl.java @@ -12,11 +12,15 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobDO; import cn.iocoder.yudao.module.infra.dal.mysql.job.JobMapper; import cn.iocoder.yudao.module.infra.enums.job.JobStatusEnum; import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; import org.quartz.SchedulerException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; +import java.util.List; +import java.util.Objects; + import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.containsAny; import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*; @@ -28,6 +32,7 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*; */ @Service @Validated +@Slf4j public class JobServiceImpl implements JobService { @Resource @@ -129,6 +134,26 @@ public class JobServiceImpl implements JobService { schedulerManager.triggerJob(job.getId(), job.getHandlerName(), job.getHandlerParam()); } + @Override + @Transactional(rollbackFor = Exception.class) + public void syncJob() throws SchedulerException { + // 1. 查询 Job 配置 + List jobList = jobMapper.selectList(); + + // 2. 遍历处理 + for (JobDO job : jobList) { + // 2.1 先删除,再创建 + schedulerManager.deleteJob(job.getHandlerName()); + schedulerManager.addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(), + job.getRetryCount(), job.getRetryInterval()); + // 2.2 如果 status 为暂停,则需要暂停 + if (Objects.equals(job.getStatus(), JobStatusEnum.STOP.getStatus())) { + schedulerManager.pauseJob(job.getHandlerName()); + } + log.info("[syncJob][id({}) handlerName({}) 同步完成]", job.getId(), job.getHandlerName()); + } + } + @Override @Transactional(rollbackFor = Exception.class) public void deleteJob(Long id) throws SchedulerException {