Commit 93e8e9c7af61903c9a7a3bc2d98f139be4cd8e38
1 parent
ffac294f
【优化】全局:JobController 增加 sync 接口,实现将 infra_job 同步到 Quartz 中
Showing
4 changed files
with
45 additions
and
0 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/job/JobController.http
0 → 100644
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 { |
| 90 | 90 | return success(true); |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | + @PostMapping("/sync") | |
| 94 | + @Operation(summary = "同步定时任务") | |
| 95 | + @PreAuthorize("@ss.hasPermission('infra:job:create')") | |
| 96 | + public CommonResult<Boolean> syncJob() throws SchedulerException { | |
| 97 | + jobService.syncJob(); | |
| 98 | + return success(true); | |
| 99 | + } | |
| 100 | + | |
| 93 | 101 | @GetMapping("/get") |
| 94 | 102 | @Operation(summary = "获得定时任务") |
| 95 | 103 | @Parameter(name = "id", description = "编号", required = true, example = "1024") | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobService.java
| ... | ... | @@ -46,6 +46,13 @@ public interface JobService { |
| 46 | 46 | void triggerJob(Long id) throws SchedulerException; |
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | + * 同步定时任务 | |
| 50 | + * | |
| 51 | + * 目的:自己存储的 Job 信息,强制同步到 Quartz 中 | |
| 52 | + */ | |
| 53 | + void syncJob() throws SchedulerException; | |
| 54 | + | |
| 55 | + /** | |
| 49 | 56 | * 删除定时任务 |
| 50 | 57 | * |
| 51 | 58 | * @param id 编号 | ... | ... |
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; |
| 12 | 12 | import cn.iocoder.yudao.module.infra.dal.mysql.job.JobMapper; |
| 13 | 13 | import cn.iocoder.yudao.module.infra.enums.job.JobStatusEnum; |
| 14 | 14 | import jakarta.annotation.Resource; |
| 15 | +import lombok.extern.slf4j.Slf4j; | |
| 15 | 16 | import org.quartz.SchedulerException; |
| 16 | 17 | import org.springframework.stereotype.Service; |
| 17 | 18 | import org.springframework.transaction.annotation.Transactional; |
| 18 | 19 | import org.springframework.validation.annotation.Validated; |
| 19 | 20 | |
| 21 | +import java.util.List; | |
| 22 | +import java.util.Objects; | |
| 23 | + | |
| 20 | 24 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 21 | 25 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.containsAny; |
| 22 | 26 | import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*; |
| ... | ... | @@ -28,6 +32,7 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*; |
| 28 | 32 | */ |
| 29 | 33 | @Service |
| 30 | 34 | @Validated |
| 35 | +@Slf4j | |
| 31 | 36 | public class JobServiceImpl implements JobService { |
| 32 | 37 | |
| 33 | 38 | @Resource |
| ... | ... | @@ -131,6 +136,26 @@ public class JobServiceImpl implements JobService { |
| 131 | 136 | |
| 132 | 137 | @Override |
| 133 | 138 | @Transactional(rollbackFor = Exception.class) |
| 139 | + public void syncJob() throws SchedulerException { | |
| 140 | + // 1. 查询 Job 配置 | |
| 141 | + List<JobDO> jobList = jobMapper.selectList(); | |
| 142 | + | |
| 143 | + // 2. 遍历处理 | |
| 144 | + for (JobDO job : jobList) { | |
| 145 | + // 2.1 先删除,再创建 | |
| 146 | + schedulerManager.deleteJob(job.getHandlerName()); | |
| 147 | + schedulerManager.addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(), | |
| 148 | + job.getRetryCount(), job.getRetryInterval()); | |
| 149 | + // 2.2 如果 status 为暂停,则需要暂停 | |
| 150 | + if (Objects.equals(job.getStatus(), JobStatusEnum.STOP.getStatus())) { | |
| 151 | + schedulerManager.pauseJob(job.getHandlerName()); | |
| 152 | + } | |
| 153 | + log.info("[syncJob][id({}) handlerName({}) 同步完成]", job.getId(), job.getHandlerName()); | |
| 154 | + } | |
| 155 | + } | |
| 156 | + | |
| 157 | + @Override | |
| 158 | + @Transactional(rollbackFor = Exception.class) | |
| 134 | 159 | public void deleteJob(Long id) throws SchedulerException { |
| 135 | 160 | // 校验存在 |
| 136 | 161 | JobDO job = validateJobExists(id); | ... | ... |