From 3c2bd9ce53171db94539345b53ae59839d97412b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com>
Date: Thu, 28 Mar 2024 14:15:41 +0800
Subject: [PATCH] =?UTF-8?q?fix:=201.=E6=95=B0=E6=8D=AE=E5=BA=93=E4=BF=AE?=
=?UTF-8?q?=E6=94=B9sys=5Fjob=20=E4=BB=BB=E5=8A=A1=E5=90=8D+=E7=BB=84?=
=?UTF-8?q?=E5=90=8D=E4=B8=BA=E5=94=AF=E4=B8=80key=202.=E6=96=B0=E5=A2=9E?=
=?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=87=8D=E5=A4=8D=E4=BB=BB=E5=8A=A1=E5=90=8D?=
=?UTF-8?q?+=E7=BB=84=E5=90=8D=E5=88=A4=E6=96=AD=203.=E6=96=B0=E5=A2=9E?=
=?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E5=92=8C?=
=?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E6=97=B6?=
=?UTF-8?q?=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6=E5=9C=A8=E8=B0=83=E5=BA=A6?=
=?UTF-8?q?=E5=99=A8=E4=B8=AD,=E8=A7=A3=E5=86=B3=E6=B7=BB=E5=8A=A0?=
=?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=90=8E=E9=87=8D=E5=90=AF=E5=AF=BC=E8=87=B4?=
=?UTF-8?q?quartz=E4=BB=BB=E5=8A=A1=E4=B8=A2=E5=A4=B1=E6=97=A0=E6=B3=95?=
=?UTF-8?q?=E5=90=AF=E5=8A=A8=E7=9A=84bug?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
db/pig_job.sql | 3 +-
.../quartz/controller/SysJobController.java | 144 +++++++++++-------
2 files changed, 91 insertions(+), 56 deletions(-)
diff --git a/db/pig_job.sql b/db/pig_job.sql
index dabbde1a..0fde0582 100644
--- a/db/pig_job.sql
+++ b/db/pig_job.sql
@@ -31,7 +31,8 @@ CREATE TABLE `sys_job` (
`previous_time` timestamp NULL DEFAULT NULL COMMENT '上次执行时间',
`next_time` timestamp NULL DEFAULT NULL COMMENT '下次执行时间',
`remark` varchar(500) CHARACTER SET utf8mb4 DEFAULT '' COMMENT '备注信息',
- PRIMARY KEY (`job_id`,`job_name`,`job_group`) USING BTREE
+ PRIMARY KEY (`job_id`) USING BTREE,
+ UNIQUE KEY `job_name_group_idx` (`job_name`,`job_group`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='定时任务调度表';
-- ----------------------------
diff --git a/pig-visual/pig-quartz/src/main/java/com/pig4cloud/pig/daemon/quartz/controller/SysJobController.java b/pig-visual/pig-quartz/src/main/java/com/pig4cloud/pig/daemon/quartz/controller/SysJobController.java
index dd0dfb64..f78e1e1f 100644
--- a/pig-visual/pig-quartz/src/main/java/com/pig4cloud/pig/daemon/quartz/controller/SysJobController.java
+++ b/pig-visual/pig-quartz/src/main/java/com/pig4cloud/pig/daemon/quartz/controller/SysJobController.java
@@ -36,7 +36,9 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
+import org.quartz.SchedulerException;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@@ -44,8 +46,8 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
- * @author frwcloud
- * @date 2019-01-27 10:04:42
+ * @author guoliang
+ * @date 2024-3-26 11:19:18
*
* 定时任务管理
*/
@@ -54,6 +56,7 @@ import java.util.List;
@RequestMapping("/sys-job")
@Tag(description = "sys-job", name = "定时任务")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
+@Slf4j
public class SysJobController {
private final SysJobService sysJobService;
@@ -66,24 +69,26 @@ public class SysJobController {
/**
* 定时任务分页查询
- * @param page 分页对象
+ *
+ * @param page 分页对象
* @param sysJob 定时任务调度表
- * @return
+ * @return R
*/
@GetMapping("/page")
@Operation(description = "分页定时业务查询")
public R getSysJobPage(Page page, SysJob sysJob) {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery()
- .like(StrUtil.isNotBlank(sysJob.getJobName()), SysJob::getJobName, sysJob.getJobName())
- .like(StrUtil.isNotBlank(sysJob.getJobGroup()), SysJob::getJobGroup, sysJob.getJobGroup())
- .eq(StrUtil.isNotBlank(sysJob.getJobStatus()), SysJob::getJobStatus, sysJob.getJobGroup())
- .eq(StrUtil.isNotBlank(sysJob.getJobExecuteStatus()), SysJob::getJobExecuteStatus,
- sysJob.getJobExecuteStatus());
+ .like(StrUtil.isNotBlank(sysJob.getJobName()), SysJob::getJobName, sysJob.getJobName())
+ .like(StrUtil.isNotBlank(sysJob.getJobGroup()), SysJob::getJobGroup, sysJob.getJobGroup())
+ .eq(StrUtil.isNotBlank(sysJob.getJobStatus()), SysJob::getJobStatus, sysJob.getJobGroup())
+ .eq(StrUtil.isNotBlank(sysJob.getJobExecuteStatus()), SysJob::getJobExecuteStatus,
+ sysJob.getJobExecuteStatus());
return R.ok(sysJobService.page(page, wrapper));
}
/**
* 通过id查询定时任务
+ *
* @param id id
* @return R
*/
@@ -94,7 +99,8 @@ public class SysJobController {
}
/**
- * 新增定时任务
+ * 新增定时任务,默认新增状态为1已发布
+ *
* @param sysJob 定时任务调度表
* @return R
*/
@@ -103,16 +109,25 @@ public class SysJobController {
@PreAuthorize("@pms.hasPermission('job_sys_job_add')")
@Operation(description = "新增定时任务")
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(PigQuartzEnum.JOB_STATUS_RELEASE.getType());
sysJob.setCreateBy(SecurityUtils.getUser().getUsername());
- sysJobService.save(sysJob);
- // 初始化任务
- taskUtil.addOrUpateJob(sysJob, scheduler);
- return R.ok();
+ return R.ok(sysJobService.save(sysJob));
}
/**
* 修改定时任务
+ *
* @param sysJob 定时任务调度表
* @return R
*/
@@ -124,10 +139,10 @@ public class SysJobController {
sysJob.setUpdateBy(SecurityUtils.getUser().getUsername());
SysJob querySysJob = this.sysJobService.getById(sysJob.getJobId());
if (PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
+ //如修改暂停的需更新调度器
this.taskUtil.addOrUpateJob(sysJob, scheduler);
sysJobService.updateById(sysJob);
- }
- else if (PigQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
+ } else if (PigQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
sysJobService.updateById(sysJob);
}
return R.ok();
@@ -135,6 +150,7 @@ public class SysJobController {
/**
* 通过id删除定时任务
+ *
* @param id id
* @return R
*/
@@ -147,8 +163,7 @@ public class SysJobController {
if (PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
this.taskUtil.removeJob(querySysJob, scheduler);
this.sysJobService.removeById(id);
- }
- else if (PigQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
+ } else if (PigQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
this.sysJobService.removeById(id);
}
return R.ok();
@@ -156,7 +171,8 @@ public class SysJobController {
/**
* 暂停全部定时任务
- * @return
+ *
+ * @return R
*/
@SysLog("暂停全部定时任务")
@PostMapping("/shutdown-jobs")
@@ -168,53 +184,50 @@ public class SysJobController {
new LambdaQueryWrapper().eq(SysJob::getJobStatus, PigQuartzEnum.JOB_STATUS_RUNNING.getType()));
if (count <= 0) {
return R.ok("无正在运行定时任务");
- }
- else {
- // 更新定时任务状态条件,运行状态2更新为暂停状态2
+ } else {
+ // 更新定时任务状态条件,运行状态2更新为暂停状态3
this.sysJobService.update(
SysJob.builder().jobStatus(PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType()).build(),
- new UpdateWrapper().lambda()
- .eq(SysJob::getJobStatus, PigQuartzEnum.JOB_STATUS_RUNNING.getType()));
+ new UpdateWrapper().lambda().eq(SysJob::getJobStatus,
+ PigQuartzEnum.JOB_STATUS_RUNNING.getType()));
return R.ok("暂停成功");
}
}
/**
* 启动全部定时任务
+ *
* @return
*/
- @SysLog("启动全部定时任务")
+ @SysLog("启动全部暂停的定时任务")
@PostMapping("/start-jobs")
@PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
- @Operation(description = "启动全部定时任务")
+ @Operation(description = "启动全部暂停的定时任务")
public R startJobs() {
// 更新定时任务状态条件,暂停状态3更新为运行状态2
this.sysJobService.update(SysJob.builder().jobStatus(PigQuartzEnum.JOB_STATUS_RUNNING.getType()).build(),
- new UpdateWrapper().lambda()
- .eq(SysJob::getJobStatus, PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType()));
+ new UpdateWrapper().lambda().eq(SysJob::getJobStatus,
+ PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType()));
taskUtil.startJobs(scheduler);
return R.ok();
}
/**
* 刷新全部定时任务
- * @return
+ * 暂停和运行的添加到调度器其他状态从调度器移除
+ *
+ * @return R
*/
@SysLog("刷新全部定时任务")
@PostMapping("/refresh-jobs")
@PreAuthorize("@pms.hasPermission('job_sys_job_refresh_job')")
@Operation(description = "刷新全部定时任务")
public R refreshJobs() {
- sysJobService.list().forEach((sysjob) -> {
- if (PigQuartzEnum.JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())
- || PigQuartzEnum.JOB_STATUS_DEL.getType().equals(sysjob.getJobStatus())) {
- taskUtil.removeJob(sysjob, scheduler);
- }
- else if (PigQuartzEnum.JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())
+ sysJobService.list().forEach(sysjob -> {
+ if (PigQuartzEnum.JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())
|| PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(sysjob.getJobStatus())) {
taskUtil.addOrUpateJob(sysjob, scheduler);
- }
- else {
+ } else {
taskUtil.removeJob(sysjob, scheduler);
}
});
@@ -223,43 +236,63 @@ public class SysJobController {
/**
* 启动定时任务
- * @param jobId
- * @return
+ *
+ * @param jobId 任务id
+ * @return R
*/
@SysLog("启动定时任务")
@PostMapping("/start-job/{id}")
@PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
@Operation(description = "启动定时任务")
- public R startJob(@PathVariable("id") Long jobId) {
+ public R startJob(@PathVariable("id") Long jobId) throws SchedulerException {
SysJob querySysJob = this.sysJobService.getById(jobId);
- if (querySysJob != null && PigQuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(querySysJob.getJobStatus())) {
- taskUtil.addOrUpateJob(querySysJob, scheduler);
+ if (querySysJob == null) {
+ return R.failed("无此定时任务,请确认");
}
- else {
+
+ //如果定时任务不存在,强制状态为1已发布
+ if (!scheduler.checkExists(TaskUtil.getJobKey(querySysJob))) {
+ querySysJob.setJobStatus(PigQuartzEnum.JOB_STATUS_RELEASE.getType());
+ log.warn("定时任务不在quartz中,任务id:{},强制状态为已发布并加入调度器", jobId);
+ }
+
+ if (PigQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
+ taskUtil.addOrUpateJob(querySysJob, scheduler);
+ } else {
taskUtil.resumeJob(querySysJob, scheduler);
}
- // 更新定时任务状态条件,暂停状态3更新为运行状态2
- this.sysJobService
- .updateById(SysJob.builder().jobId(jobId).jobStatus(PigQuartzEnum.JOB_STATUS_RUNNING.getType()).build());
+ // 更新定时任务状态为运行状态2
+ this.sysJobService.updateById(
+ SysJob.builder().jobId(jobId).jobStatus(PigQuartzEnum.JOB_STATUS_RUNNING.getType()).build());
return R.ok();
}
/**
* 启动定时任务
- * @param jobId
- * @return
+ *
+ * @param jobId 任务id
+ * @return R
*/
@SysLog("立刻执行定时任务")
@PostMapping("/run-job/{id}")
@PreAuthorize("@pms.hasPermission('job_sys_job_run_job')")
@Operation(description = "立刻执行定时任务")
- public R runJob(@PathVariable("id") Long jobId) {
+ public R runJob(@PathVariable("id") Long jobId) throws SchedulerException {
SysJob querySysJob = this.sysJobService.getById(jobId);
+
+ //执行定时任务前判定任务是否在quartz中
+ if (!scheduler.checkExists(TaskUtil.getJobKey(querySysJob))) {
+ querySysJob.setJobStatus(PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType());
+ log.warn("立刻执行定时任务-定时任务不在quartz中,任务id:{},强制状态为暂停并加入调度器", jobId);
+ taskUtil.addOrUpateJob(querySysJob, scheduler);
+ }
+
return TaskUtil.runOnce(scheduler, querySysJob) ? R.ok() : R.failed();
}
/**
* 暂停定时任务
+ *
* @return
*/
@SysLog("暂停定时任务")
@@ -269,16 +302,15 @@ public class SysJobController {
public R shutdownJob(@PathVariable("id") Long id) {
SysJob querySysJob = this.sysJobService.getById(id);
// 更新定时任务状态条件,运行状态2更新为暂停状态3
- this.sysJobService.updateById(SysJob.builder()
- .jobId(querySysJob.getJobId())
- .jobStatus(PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType())
- .build());
+ this.sysJobService.updateById(SysJob.builder().jobId(querySysJob.getJobId())
+ .jobStatus(PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType()).build());
taskUtil.pauseJob(querySysJob, scheduler);
return R.ok();
}
/**
* 唯一标识查询定时执行日志
+ *
* @return
*/
@GetMapping("/job-log")
@@ -289,18 +321,20 @@ public class SysJobController {
/**
* 检验任务名称和任务组联合是否唯一
+ *
* @return
*/
@GetMapping("/is-valid-task-name")
@Operation(description = "检验任务名称和任务组联合是否唯一")
public R isValidTaskName(@RequestParam String jobName, @RequestParam String jobGroup) {
return this.sysJobService
- .count(Wrappers.query(SysJob.builder().jobName(jobName).jobGroup(jobGroup).build())) > 0
- ? R.failed("任务重复,请检查此组内是否已包含同名任务") : R.ok();
+ .count(Wrappers.query(SysJob.builder().jobName(jobName).jobGroup(jobGroup).build())) > 0
+ ? R.failed("任务重复,请检查此组内是否已包含同名任务") : R.ok();
}
/**
* 导出任务
+ *
* @param sysJob
* @return
*/