SysJobController.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2018-2025, hnqz All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the pig4cloud.com developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: hnqz
  16. */
  17. package net.yyc.quartz.controller;
  18. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  19. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  20. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  21. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  22. import lombok.AllArgsConstructor;
  23. import lombok.extern.slf4j.Slf4j;
  24. import net.yyc.common.core.util.R;
  25. import net.yyc.common.security.annotation.Inner;
  26. import net.yyc.common.security.util.SecurityUtils;
  27. import net.yyc.quartz.entity.SysJob;
  28. import net.yyc.quartz.entity.SysJobLog;
  29. import net.yyc.quartz.service.SysJobLogService;
  30. import net.yyc.quartz.service.SysJobService;
  31. import net.yyc.quartz.util.TaskUtil;
  32. import org.quartz.Scheduler;
  33. import org.springframework.security.access.prepost.PreAuthorize;
  34. import org.springframework.web.bind.annotation.*;
  35. import static net.yyc.quartz.constants.QuartzEnum.*;
  36. @Slf4j
  37. @RestController
  38. @AllArgsConstructor
  39. @RequestMapping("/sys-job")
  40. public class SysJobController {
  41. private final SysJobService sysJobService;
  42. private final SysJobLogService sysJobLogService;
  43. private final TaskUtil taskUtil;
  44. private final Scheduler scheduler;
  45. /**
  46. * 定时任务分页查询
  47. *
  48. * @param page 分页对象
  49. * @param sysJob 定时任务调度表
  50. */
  51. @GetMapping("/page")
  52. public R getSysJobPage(Page page, SysJob sysJob) {
  53. return R.ok(sysJobService.page(page, Wrappers.query(sysJob)));
  54. }
  55. /**
  56. * 通过id查询定时任务
  57. */
  58. @GetMapping("/{id}")
  59. public R getById(@PathVariable("id") Integer id) {
  60. return R.ok(sysJobService.getById(id));
  61. }
  62. /**
  63. * 新增定时任务
  64. */
  65. @PostMapping
  66. public R save(@RequestBody SysJob sysJob) {
  67. log.info("新增定时任务");
  68. sysJob.setJobStatus(JOB_STATUS_RELEASE.getType());
  69. sysJob.setCreateBy(SecurityUtils.getUser().getUsername());
  70. return R.ok(sysJobService.save(sysJob));
  71. }
  72. /**
  73. * 修改定时任务
  74. */
  75. @PutMapping
  76. public R updateById(@RequestBody SysJob sysJob) {
  77. sysJob.setUpdateBy(SecurityUtils.getUser().getUsername());
  78. SysJob querySysJob = this.sysJobService.getById(sysJob.getJobId());
  79. if (JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
  80. this.taskUtil.addOrUpdateJob(sysJob, scheduler);
  81. sysJobService.updateById(sysJob);
  82. } else if (JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
  83. sysJobService.updateById(sysJob);
  84. }
  85. return R.ok();
  86. }
  87. /**
  88. * 通过id删除定时任务
  89. */
  90. @DeleteMapping("/{id}")
  91. public R removeById(@PathVariable Integer id) {
  92. log.info("删除定时任务: {}", id);
  93. SysJob querySysJob = this.sysJobService.getById(id);
  94. if (JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
  95. this.taskUtil.removeJob(querySysJob, scheduler);
  96. this.sysJobService.removeById(id);
  97. } else if (JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
  98. this.sysJobService.removeById(id);
  99. }
  100. return R.ok();
  101. }
  102. /**
  103. * 暂停全部定时任务
  104. */
  105. @PostMapping("/shutdown-jobs")
  106. public R shutdownJobs() {
  107. log.info("暂停全部定时任务");
  108. taskUtil.pauseJobs(scheduler);
  109. int count = this.sysJobService
  110. .count(new LambdaQueryWrapper<SysJob>().eq(SysJob::getJobStatus, JOB_STATUS_RUNNING.getType()));
  111. if (count <= 0) {
  112. return R.ok("无正在运行定时任务");
  113. } else {
  114. // 更新定时任务状态条件,运行状态2更新为暂停状态2
  115. this.sysJobService.update(SysJob.builder().jobStatus(JOB_STATUS_NOT_RUNNING.getType()).build(),
  116. new UpdateWrapper<SysJob>().lambda().eq(SysJob::getJobStatus, JOB_STATUS_RUNNING.getType()));
  117. return R.ok("暂停成功");
  118. }
  119. }
  120. /**
  121. * 启动全部定时任务
  122. */
  123. @PostMapping("/start-jobs")
  124. public R startJobs() {
  125. log.info("启动全部定时任务");
  126. // 更新定时任务状态条件,暂停状态3更新为运行状态2
  127. this.sysJobService.update(SysJob.builder().jobStatus(JOB_STATUS_RUNNING.getType()).build(),
  128. new UpdateWrapper<SysJob>().lambda().eq(SysJob::getJobStatus, JOB_STATUS_NOT_RUNNING.getType()));
  129. taskUtil.startJobs(scheduler);
  130. return R.ok();
  131. }
  132. /**
  133. * 刷新全部定时任务
  134. */
  135. @Inner(value = false)
  136. @PostMapping("/refresh-jobs")
  137. public R<?> refreshJobs() {
  138. log.info("刷新全部定时任务");
  139. sysJobService.list().forEach((sysjob) -> {
  140. if (JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())
  141. || JOB_STATUS_DEL.getType().equals(sysjob.getJobStatus())) {
  142. taskUtil.removeJob(sysjob, scheduler);
  143. } else if (JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())
  144. || JOB_STATUS_NOT_RUNNING.getType().equals(sysjob.getJobStatus())) {
  145. taskUtil.addOrUpdateJob(sysjob, scheduler);
  146. } else {
  147. taskUtil.removeJob(sysjob, scheduler);
  148. }
  149. });
  150. return R.ok();
  151. }
  152. /**
  153. * 启动定时任务
  154. */
  155. @Inner(value = false)
  156. @PostMapping("/start-job/{id}")
  157. public R startJob(@PathVariable("id") Integer jobId) {
  158. log.info("启动定时任务: {}", jobId);
  159. SysJob querySysJob = this.sysJobService.getById(jobId);
  160. if (querySysJob != null && JOB_LOG_STATUS_FAIL.getType().equals(querySysJob.getJobStatus())) {
  161. taskUtil.addOrUpdateJob(querySysJob, scheduler);
  162. } else {
  163. taskUtil.resumeJob(querySysJob, scheduler);
  164. }
  165. // 更新定时任务状态条件,暂停状态3更新为运行状态2
  166. this.sysJobService.updateById(SysJob.builder().jobId(jobId).jobStatus(JOB_STATUS_RUNNING.getType()).build());
  167. return R.ok();
  168. }
  169. /**
  170. * 启动定时任务
  171. */
  172. @PostMapping("/run-job/{id}")
  173. @PreAuthorize("@pms.hasPermission('job_sys_job_run_job')")
  174. public R runJob(@PathVariable("id") Integer jobId) {
  175. log.info("立刻执行定时任务: {}", jobId);
  176. SysJob querySysJob = this.sysJobService.getById(jobId);
  177. return TaskUtil.runOnce(scheduler, querySysJob) ? R.ok() : R.failed();
  178. }
  179. /**
  180. * 暂停定时任务
  181. */
  182. @PostMapping("/shutdown-job/{id}")
  183. @PreAuthorize("@pms.hasPermission('job_sys_job_shutdown_job')")
  184. public R shutdownJob(@PathVariable("id") Integer id) {
  185. log.info("暂停定时任务: {}", id);
  186. SysJob querySysJob = this.sysJobService.getById(id);
  187. // 更新定时任务状态条件,运行状态2更新为暂停状态3
  188. this.sysJobService.updateById(
  189. SysJob.builder().jobId(querySysJob.getJobId()).jobStatus(JOB_STATUS_NOT_RUNNING.getType()).build());
  190. taskUtil.pauseJob(querySysJob, scheduler);
  191. return R.ok();
  192. }
  193. /**
  194. * 唯一标识查询定时执行日志
  195. */
  196. @GetMapping("/job-log")
  197. public R getJobLog(Page page, SysJobLog sysJobLog) {
  198. return R.ok(sysJobLogService.page(page, Wrappers.query(sysJobLog)));
  199. }
  200. /**
  201. * 检验任务名称和任务组联合是否唯一
  202. */
  203. @GetMapping("/is-valid-task-name")
  204. public R isValidTaskName(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup) {
  205. return this.sysJobService
  206. .count(Wrappers.query(SysJob.builder().jobName(jobName).jobGroup(jobGroup).build())) > 0 ? R.failed()
  207. : R.ok();
  208. }
  209. }