malei05 5 днів тому
батько
коміт
a3d8e1ff36
1 змінених файлів з 198 додано та 199 видалено
  1. 198 199
      src/main/java/net/yyc/quartz/controller/SysJobController.java

+ 198 - 199
src/main/java/net/yyc/quartz/controller/SysJobController.java

@@ -21,6 +21,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import net.yyc.common.core.util.R;
 import net.yyc.common.security.annotation.Inner;
 import net.yyc.common.security.util.SecurityUtils;
@@ -29,8 +31,6 @@ import net.yyc.quartz.entity.SysJobLog;
 import net.yyc.quartz.service.SysJobLogService;
 import net.yyc.quartz.service.SysJobService;
 import net.yyc.quartz.util.TaskUtil;
-import lombok.AllArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
 import org.quartz.Scheduler;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
@@ -49,218 +49,217 @@ import static net.yyc.quartz.constants.QuartzEnum.*;
 @RequestMapping("/sys-job")
 public class SysJobController {
 
-	private final SysJobService sysJobService;
+    private final SysJobService sysJobService;
 
-	private final SysJobLogService sysJobLogService;
+    private final SysJobLogService sysJobLogService;
 
-	private final TaskUtil taskUtil;
+    private final TaskUtil taskUtil;
 
-	private final Scheduler scheduler;
+    private final Scheduler scheduler;
 
-	/**
-	 * 定时任务分页查询
-	 * @param page 分页对象
-	 * @param sysJob 定时任务调度表
-	 * @return
-	 */
-	@GetMapping("/page")
-	public R getSysJobPage(Page page, SysJob sysJob) {
-		return R.ok(sysJobService.page(page, Wrappers.query(sysJob)));
-	}
+    /**
+     * 定时任务分页查询
+     *
+     * @param page   分页对象
+     * @param sysJob 定时任务调度表
+     */
+    @GetMapping("/page")
+    public R getSysJobPage(Page page, SysJob sysJob) {
+        return R.ok(sysJobService.page(page, Wrappers.query(sysJob)));
+    }
 
-	/**
-	 * 通过id查询定时任务
-	 * @param id id
-	 * @return R
-	 */
-	@GetMapping("/{id}")
-	public R getById(@PathVariable("id") Integer id) {
-		return R.ok(sysJobService.getById(id));
-	}
+    /**
+     * 通过id查询定时任务
+     *
+     * @param id id
+     * @return R
+     */
+    @GetMapping("/{id}")
+    public R getById(@PathVariable("id") Integer id) {
+        return R.ok(sysJobService.getById(id));
+    }
 
-	/**
-	 * 新增定时任务
-	 * @param sysJob 定时任务调度表
-	 * @return R
-	 */
-	@PostMapping
-	@PreAuthorize("@pms.hasPermission('job_sys_job_add')")
-	public R save(@RequestBody SysJob sysJob) {
-		log.info("新增定时任务");
-		sysJob.setJobStatus(JOB_STATUS_RELEASE.getType());
-		sysJob.setCreateBy(SecurityUtils.getUser().getUsername());
-		return R.ok(sysJobService.save(sysJob));
-	}
+    /**
+     * 新增定时任务
+     *
+     * @param sysJob 定时任务调度表
+     * @return R
+     */
+    @PostMapping
+    public R save(@RequestBody SysJob sysJob) {
+        log.info("新增定时任务");
+        sysJob.setJobStatus(JOB_STATUS_RELEASE.getType());
+        sysJob.setCreateBy(SecurityUtils.getUser().getUsername());
+        return R.ok(sysJobService.save(sysJob));
+    }
 
-	/**
-	 * 修改定时任务
-	 * @param sysJob 定时任务调度表
-	 * @return R
-	 */
-	@PutMapping
-	@PreAuthorize("@pms.hasPermission('job_sys_job_edit')")
-	public R updateById(@RequestBody SysJob sysJob) {
-		log.info("修改定时任务");
-		sysJob.setUpdateBy(SecurityUtils.getUser().getUsername());
-		SysJob querySysJob = this.sysJobService.getById(sysJob.getJobId());
-		if (JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
-			this.taskUtil.addOrUpdateJob(sysJob, scheduler);
-			sysJobService.updateById(sysJob);
-		}
-		else if (JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
-			sysJobService.updateById(sysJob);
-		}
-		return R.ok();
-	}
+    /**
+     * 修改定时任务
+     *
+     * @param sysJob 定时任务调度表
+     * @return R
+     */
+    @PutMapping
+    public R updateById(@RequestBody SysJob sysJob) {
+        sysJob.setUpdateBy(SecurityUtils.getUser().getUsername());
+        SysJob querySysJob = this.sysJobService.getById(sysJob.getJobId());
+        if (JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
+            this.taskUtil.addOrUpdateJob(sysJob, scheduler);
+            sysJobService.updateById(sysJob);
+        } else if (JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
+            sysJobService.updateById(sysJob);
+        }
+        return R.ok();
+    }
 
-	/**
-	 * 通过id删除定时任务
-	 * @param id id
-	 * @return R
-	 */
-	@DeleteMapping("/{id}")
-	@PreAuthorize("@pms.hasPermission('job_sys_job_del')")
-	public R removeById(@PathVariable Integer id) {
-		log.info("删除定时任务: {}", id);
-		SysJob querySysJob = this.sysJobService.getById(id);
-		if (JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
-			this.taskUtil.removeJob(querySysJob, scheduler);
-			this.sysJobService.removeById(id);
-		}
-		else if (JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
-			this.sysJobService.removeById(id);
-		}
-		return R.ok();
-	}
+    /**
+     * 通过id删除定时任务
+     *
+     * @param id id
+     * @return R
+     */
+    @DeleteMapping("/{id}")
+    public R removeById(@PathVariable Integer id) {
+        log.info("删除定时任务: {}", id);
+        SysJob querySysJob = this.sysJobService.getById(id);
+        if (JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
+            this.taskUtil.removeJob(querySysJob, scheduler);
+            this.sysJobService.removeById(id);
+        } else if (JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
+            this.sysJobService.removeById(id);
+        }
+        return R.ok();
+    }
 
-	/**
-	 * 暂停全部定时任务
-	 * @return
-	 */
-	@PostMapping("/shutdown-jobs")
-	@PreAuthorize("@pms.hasPermission('job_sys_job_shutdown_job')")
-	public R shutdownJobs() {
-		log.info("暂停全部定时任务");
-		taskUtil.pauseJobs(scheduler);
-		int count = this.sysJobService
-				.count(new LambdaQueryWrapper<SysJob>().eq(SysJob::getJobStatus, JOB_STATUS_RUNNING.getType()));
-		if (count <= 0) {
-			return R.ok("无正在运行定时任务");
-		}
-		else {
-			// 更新定时任务状态条件,运行状态2更新为暂停状态2
-			this.sysJobService.update(SysJob.builder().jobStatus(JOB_STATUS_NOT_RUNNING.getType()).build(),
-					new UpdateWrapper<SysJob>().lambda().eq(SysJob::getJobStatus, JOB_STATUS_RUNNING.getType()));
-			return R.ok("暂停成功");
-		}
-	}
+    /**
+     * 暂停全部定时任务
+     *
+     * @return
+     */
+    @PostMapping("/shutdown-jobs")
+    public R shutdownJobs() {
+        log.info("暂停全部定时任务");
+        taskUtil.pauseJobs(scheduler);
+        int count = this.sysJobService
+                .count(new LambdaQueryWrapper<SysJob>().eq(SysJob::getJobStatus, JOB_STATUS_RUNNING.getType()));
+        if (count <= 0) {
+            return R.ok("无正在运行定时任务");
+        } else {
+            // 更新定时任务状态条件,运行状态2更新为暂停状态2
+            this.sysJobService.update(SysJob.builder().jobStatus(JOB_STATUS_NOT_RUNNING.getType()).build(),
+                    new UpdateWrapper<SysJob>().lambda().eq(SysJob::getJobStatus, JOB_STATUS_RUNNING.getType()));
+            return R.ok("暂停成功");
+        }
+    }
 
-	/**
-	 * 启动全部定时任务
-	 * @return
-	 */
-	@PostMapping("/start-jobs")
-	@PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
-	public R startJobs() {
-		log.info("启动全部定时任务");
-		// 更新定时任务状态条件,暂停状态3更新为运行状态2
-		this.sysJobService.update(SysJob.builder().jobStatus(JOB_STATUS_RUNNING.getType()).build(),
-				new UpdateWrapper<SysJob>().lambda().eq(SysJob::getJobStatus, JOB_STATUS_NOT_RUNNING.getType()));
-		taskUtil.startJobs(scheduler);
-		return R.ok();
-	}
+    /**
+     * 启动全部定时任务
+     *
+     * @return
+     */
+    @PostMapping("/start-jobs")
+    public R startJobs() {
+        log.info("启动全部定时任务");
+        // 更新定时任务状态条件,暂停状态3更新为运行状态2
+        this.sysJobService.update(SysJob.builder().jobStatus(JOB_STATUS_RUNNING.getType()).build(),
+                new UpdateWrapper<SysJob>().lambda().eq(SysJob::getJobStatus, JOB_STATUS_NOT_RUNNING.getType()));
+        taskUtil.startJobs(scheduler);
+        return R.ok();
+    }
 
-	/**
-	 * 刷新全部定时任务
-	 */
-	@Inner()
-	@PostMapping("/refresh-jobs")
-	public R<?> refreshJobs() {
-		log.info("刷新全部定时任务");
-		sysJobService.list().forEach((sysjob) -> {
-			if (JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())
-					|| JOB_STATUS_DEL.getType().equals(sysjob.getJobStatus())) {
-				taskUtil.removeJob(sysjob, scheduler);
-			}
-			else if (JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())
-					|| JOB_STATUS_NOT_RUNNING.getType().equals(sysjob.getJobStatus())) {
-				taskUtil.addOrUpdateJob(sysjob, scheduler);
-			}
-			else {
-				taskUtil.removeJob(sysjob, scheduler);
-			}
-		});
-		return R.ok();
-	}
+    /**
+     * 刷新全部定时任务
+     */
+    @Inner()
+    @PostMapping("/refresh-jobs")
+    public R<?> refreshJobs() {
+        log.info("刷新全部定时任务");
+        sysJobService.list().forEach((sysjob) -> {
+            if (JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())
+                    || JOB_STATUS_DEL.getType().equals(sysjob.getJobStatus())) {
+                taskUtil.removeJob(sysjob, scheduler);
+            } else if (JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())
+                    || JOB_STATUS_NOT_RUNNING.getType().equals(sysjob.getJobStatus())) {
+                taskUtil.addOrUpdateJob(sysjob, scheduler);
+            } else {
+                taskUtil.removeJob(sysjob, scheduler);
+            }
+        });
+        return R.ok();
+    }
 
-	/**
-	 * 启动定时任务
-	 * @param jobId
-	 * @return
-	 */
-	@PostMapping("/start-job/{id}")
-	@PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
-	public R startJob(@PathVariable("id") Integer jobId) {
-		log.info("启动定时任务: {}", jobId);
-		SysJob querySysJob = this.sysJobService.getById(jobId);
-		if (querySysJob != null && JOB_LOG_STATUS_FAIL.getType().equals(querySysJob.getJobStatus())) {
-			taskUtil.addOrUpdateJob(querySysJob, scheduler);
-		}
-		else {
-			taskUtil.resumeJob(querySysJob, scheduler);
-		}
-		// 更新定时任务状态条件,暂停状态3更新为运行状态2
-		this.sysJobService.updateById(SysJob.builder().jobId(jobId).jobStatus(JOB_STATUS_RUNNING.getType()).build());
-		return R.ok();
-	}
+    /**
+     * 启动定时任务
+     *
+     * @param jobId
+     * @return
+     */
+    @PostMapping("/start-job/{id}")
+    @PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
+    public R startJob(@PathVariable("id") Integer jobId) {
+        log.info("启动定时任务: {}", jobId);
+        SysJob querySysJob = this.sysJobService.getById(jobId);
+        if (querySysJob != null && JOB_LOG_STATUS_FAIL.getType().equals(querySysJob.getJobStatus())) {
+            taskUtil.addOrUpdateJob(querySysJob, scheduler);
+        } else {
+            taskUtil.resumeJob(querySysJob, scheduler);
+        }
+        // 更新定时任务状态条件,暂停状态3更新为运行状态2
+        this.sysJobService.updateById(SysJob.builder().jobId(jobId).jobStatus(JOB_STATUS_RUNNING.getType()).build());
+        return R.ok();
+    }
 
-	/**
-	 * 启动定时任务
-	 * @param jobId
-	 * @return
-	 */
-	@PostMapping("/run-job/{id}")
-	@PreAuthorize("@pms.hasPermission('job_sys_job_run_job')")
-	public R runJob(@PathVariable("id") Integer jobId) {
-		log.info("立刻执行定时任务: {}", jobId);
-		SysJob querySysJob = this.sysJobService.getById(jobId);
-		return TaskUtil.runOnce(scheduler, querySysJob) ? R.ok() : R.failed();
-	}
+    /**
+     * 启动定时任务
+     *
+     * @param jobId
+     * @return
+     */
+    @PostMapping("/run-job/{id}")
+    @PreAuthorize("@pms.hasPermission('job_sys_job_run_job')")
+    public R runJob(@PathVariable("id") Integer jobId) {
+        log.info("立刻执行定时任务: {}", jobId);
+        SysJob querySysJob = this.sysJobService.getById(jobId);
+        return TaskUtil.runOnce(scheduler, querySysJob) ? R.ok() : R.failed();
+    }
 
-	/**
-	 * 暂停定时任务
-	 * @return
-	 */
-	@PostMapping("/shutdown-job/{id}")
-	@PreAuthorize("@pms.hasPermission('job_sys_job_shutdown_job')")
-	public R shutdownJob(@PathVariable("id") Integer id) {
-		log.info("暂停定时任务: {}", id);
-		SysJob querySysJob = this.sysJobService.getById(id);
-		// 更新定时任务状态条件,运行状态2更新为暂停状态3
-		this.sysJobService.updateById(
-				SysJob.builder().jobId(querySysJob.getJobId()).jobStatus(JOB_STATUS_NOT_RUNNING.getType()).build());
-		taskUtil.pauseJob(querySysJob, scheduler);
-		return R.ok();
-	}
+    /**
+     * 暂停定时任务
+     *
+     * @return
+     */
+    @PostMapping("/shutdown-job/{id}")
+    @PreAuthorize("@pms.hasPermission('job_sys_job_shutdown_job')")
+    public R shutdownJob(@PathVariable("id") Integer id) {
+        log.info("暂停定时任务: {}", id);
+        SysJob querySysJob = this.sysJobService.getById(id);
+        // 更新定时任务状态条件,运行状态2更新为暂停状态3
+        this.sysJobService.updateById(
+                SysJob.builder().jobId(querySysJob.getJobId()).jobStatus(JOB_STATUS_NOT_RUNNING.getType()).build());
+        taskUtil.pauseJob(querySysJob, scheduler);
+        return R.ok();
+    }
 
-	/**
-	 * 唯一标识查询定时执行日志
-	 * @return
-	 */
-	@GetMapping("/job-log")
-	public R getJobLog(Page page, SysJobLog sysJobLog) {
-		return R.ok(sysJobLogService.page(page, Wrappers.query(sysJobLog)));
-	}
+    /**
+     * 唯一标识查询定时执行日志
+     *
+     * @return
+     */
+    @GetMapping("/job-log")
+    public R getJobLog(Page page, SysJobLog sysJobLog) {
+        return R.ok(sysJobLogService.page(page, Wrappers.query(sysJobLog)));
+    }
 
-	/**
-	 * 检验任务名称和任务组联合是否唯一
-	 * @return
-	 */
-	@GetMapping("/is-valid-task-name")
-	public R isValidTaskName(@RequestParam("params")String jobName, @RequestParam("params")String jobGroup) {
-		return this.sysJobService
-				.count(Wrappers.query(SysJob.builder().jobName(jobName).jobGroup(jobGroup).build())) > 0 ? R.failed()
-						: R.ok();
-	}
+    /**
+     * 检验任务名称和任务组联合是否唯一
+     *
+     * @return
+     */
+    @GetMapping("/is-valid-task-name")
+    public R isValidTaskName(@RequestParam("params") String jobName, @RequestParam("params") String jobGroup) {
+        return this.sysJobService
+                .count(Wrappers.query(SysJob.builder().jobName(jobName).jobGroup(jobGroup).build())) > 0 ? R.failed()
+                : R.ok();
+    }
 
 }