|
@@ -20,7 +20,9 @@ import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.quartz.Scheduler;
|
|
import org.quartz.Scheduler;
|
|
|
|
|
+import org.quartz.SchedulerException;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
@@ -38,6 +40,7 @@ import java.util.List;
|
|
|
@RequestMapping("/sys-job")
|
|
@RequestMapping("/sys-job")
|
|
|
@Tag(description = "sys-job", name = "定时任务")
|
|
@Tag(description = "sys-job", name = "定时任务")
|
|
|
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
|
|
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
|
|
|
|
|
+@Slf4j
|
|
|
public class SysJobController {
|
|
public class SysJobController {
|
|
|
|
|
|
|
|
private final SysJobService sysJobService;
|
|
private final SysJobService sysJobService;
|
|
@@ -87,12 +90,15 @@ public class SysJobController {
|
|
|
@PreAuthorize("@pms.hasPermission('job_sys_job_add')")
|
|
@PreAuthorize("@pms.hasPermission('job_sys_job_add')")
|
|
|
@Operation(description = "新增定时任务")
|
|
@Operation(description = "新增定时任务")
|
|
|
public R save(@RequestBody SysJob sysJob) {
|
|
public R save(@RequestBody SysJob sysJob) {
|
|
|
|
|
+ long count = sysJobService.count(
|
|
|
|
|
+ Wrappers.query(SysJob.builder().jobName(sysJob.getJobName()).jobGroup(sysJob.getJobGroup()).build()));
|
|
|
|
|
+
|
|
|
|
|
+ if (count > 0) {
|
|
|
|
|
+ return R.failed("任务重复,请检查此组内是否已包含同名任务");
|
|
|
|
|
+ }
|
|
|
sysJob.setJobStatus(SkyQuartzEnum.JOB_STATUS_RELEASE.getType());
|
|
sysJob.setJobStatus(SkyQuartzEnum.JOB_STATUS_RELEASE.getType());
|
|
|
sysJob.setCreateBy(SecurityUtils.getUser().getUsername());
|
|
sysJob.setCreateBy(SecurityUtils.getUser().getUsername());
|
|
|
- sysJobService.save(sysJob);
|
|
|
|
|
- // 初始化任务
|
|
|
|
|
- taskUtil.addOrUpateJob(sysJob, scheduler);
|
|
|
|
|
- return R.ok();
|
|
|
|
|
|
|
+ return R.ok(sysJobService.save(sysJob));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -108,6 +114,7 @@ public class SysJobController {
|
|
|
sysJob.setUpdateBy(SecurityUtils.getUser().getUsername());
|
|
sysJob.setUpdateBy(SecurityUtils.getUser().getUsername());
|
|
|
SysJob querySysJob = this.sysJobService.getById(sysJob.getJobId());
|
|
SysJob querySysJob = this.sysJobService.getById(sysJob.getJobId());
|
|
|
if (SkyQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
|
|
if (SkyQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
|
|
|
|
|
+ // 如修改暂停的需更新调度器
|
|
|
this.taskUtil.addOrUpateJob(sysJob, scheduler);
|
|
this.taskUtil.addOrUpateJob(sysJob, scheduler);
|
|
|
sysJobService.updateById(sysJob);
|
|
sysJobService.updateById(sysJob);
|
|
|
}
|
|
}
|
|
@@ -214,17 +221,27 @@ public class SysJobController {
|
|
|
@PostMapping("/start-job/{id}")
|
|
@PostMapping("/start-job/{id}")
|
|
|
@PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
|
|
@PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
|
|
|
@Operation(description = "启动定时任务")
|
|
@Operation(description = "启动定时任务")
|
|
|
- public R startJob(@PathVariable("id") Long jobId) {
|
|
|
|
|
|
|
+ public R startJob(@PathVariable("id") Long jobId) throws SchedulerException {
|
|
|
SysJob querySysJob = this.sysJobService.getById(jobId);
|
|
SysJob querySysJob = this.sysJobService.getById(jobId);
|
|
|
- if (querySysJob != null && SkyQuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(querySysJob.getJobStatus())) {
|
|
|
|
|
|
|
+ if (querySysJob == null) {
|
|
|
|
|
+ return R.failed("无此定时任务,请确认");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果定时任务不存在,强制状态为1已发布
|
|
|
|
|
+ if (!scheduler.checkExists(TaskUtil.getJobKey(querySysJob))) {
|
|
|
|
|
+ querySysJob.setJobStatus(SkyQuartzEnum.JOB_STATUS_RELEASE.getType());
|
|
|
|
|
+ log.warn("定时任务不在quartz中,任务id:{},强制状态为已发布并加入调度器", jobId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (SkyQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
|
|
|
taskUtil.addOrUpateJob(querySysJob, scheduler);
|
|
taskUtil.addOrUpateJob(querySysJob, scheduler);
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
taskUtil.resumeJob(querySysJob, scheduler);
|
|
taskUtil.resumeJob(querySysJob, scheduler);
|
|
|
}
|
|
}
|
|
|
- // 更新定时任务状态条件,暂停状态3更新为运行状态2
|
|
|
|
|
|
|
+ // 更新定时任务状态为运行状态2
|
|
|
this.sysJobService
|
|
this.sysJobService
|
|
|
- .updateById(SysJob.builder().jobId(jobId).jobStatus(SkyQuartzEnum.JOB_STATUS_RUNNING.getType()).build());
|
|
|
|
|
|
|
+ .updateById(SysJob.builder().jobId(jobId).jobStatus(SkyQuartzEnum.JOB_STATUS_RUNNING.getType()).build());
|
|
|
return R.ok();
|
|
return R.ok();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -237,8 +254,16 @@ public class SysJobController {
|
|
|
@PostMapping("/run-job/{id}")
|
|
@PostMapping("/run-job/{id}")
|
|
|
@PreAuthorize("@pms.hasPermission('job_sys_job_run_job')")
|
|
@PreAuthorize("@pms.hasPermission('job_sys_job_run_job')")
|
|
|
@Operation(description = "立刻执行定时任务")
|
|
@Operation(description = "立刻执行定时任务")
|
|
|
- public R runJob(@PathVariable("id") Long jobId) {
|
|
|
|
|
|
|
+ public R runJob(@PathVariable("id") Long jobId) throws SchedulerException {
|
|
|
SysJob querySysJob = this.sysJobService.getById(jobId);
|
|
SysJob querySysJob = this.sysJobService.getById(jobId);
|
|
|
|
|
+
|
|
|
|
|
+ // 执行定时任务前判定任务是否在quartz中
|
|
|
|
|
+ if (!scheduler.checkExists(TaskUtil.getJobKey(querySysJob))) {
|
|
|
|
|
+ querySysJob.setJobStatus(SkyQuartzEnum.JOB_STATUS_NOT_RUNNING.getType());
|
|
|
|
|
+ log.warn("立刻执行定时任务-定时任务不在quartz中,任务id:{},强制状态为暂停并加入调度器", jobId);
|
|
|
|
|
+ taskUtil.addOrUpateJob(querySysJob, scheduler);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return TaskUtil.runOnce(scheduler, querySysJob) ? R.ok() : R.failed();
|
|
return TaskUtil.runOnce(scheduler, querySysJob) ? R.ok() : R.failed();
|
|
|
}
|
|
}
|
|
|
|
|
|