【优化】全局:JobController 增加 sync 接口,实现将 infra_job 同步到 Quartz 中
This commit is contained in:
parent
ffac294fbd
commit
93e8e9c7af
|
@ -0,0 +1,5 @@
|
|||
### 请求 /infra/job/sync 接口 => 成功
|
||||
POST {{baseUrl}}/infra/job/sync
|
||||
Content-Type: application/json
|
||||
tenant-id: {{adminTenentId}}
|
||||
Authorization: Bearer {{token}}
|
|
@ -90,6 +90,14 @@ public class JobController {
|
|||
return success(true);
|
||||
}
|
||||
|
||||
@PostMapping("/sync")
|
||||
@Operation(summary = "同步定时任务")
|
||||
@PreAuthorize("@ss.hasPermission('infra:job:create')")
|
||||
public CommonResult<Boolean> syncJob() throws SchedulerException {
|
||||
jobService.syncJob();
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获得定时任务")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
|
|
|
@ -45,6 +45,13 @@ public interface JobService {
|
|||
*/
|
||||
void triggerJob(Long id) throws SchedulerException;
|
||||
|
||||
/**
|
||||
* 同步定时任务
|
||||
*
|
||||
* 目的:自己存储的 Job 信息,强制同步到 Quartz 中
|
||||
*/
|
||||
void syncJob() throws SchedulerException;
|
||||
|
||||
/**
|
||||
* 删除定时任务
|
||||
*
|
||||
|
|
|
@ -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<JobDO> 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 {
|
||||
|
|
Loading…
Reference in New Issue