SysJobController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 net.yyc.common.core.util.R;
  23. import net.yyc.common.security.annotation.Inner;
  24. import net.yyc.common.security.util.SecurityUtils;
  25. import net.yyc.quartz.entity.SysJob;
  26. import net.yyc.quartz.entity.SysJobLog;
  27. import net.yyc.quartz.service.SysJobLogService;
  28. import net.yyc.quartz.service.SysJobService;
  29. import net.yyc.quartz.util.TaskUtil;
  30. import lombok.AllArgsConstructor;
  31. import lombok.extern.slf4j.Slf4j;
  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. /**
  37. * @author frwcloud
  38. * @date 2019-01-27 10:04:42
  39. * <p>
  40. * 定时任务管理
  41. */
  42. @Slf4j
  43. @RestController
  44. @AllArgsConstructor
  45. @RequestMapping("/sys-job")
  46. public class SysJobController {
  47. private final SysJobService sysJobService;
  48. private final SysJobLogService sysJobLogService;
  49. private final TaskUtil taskUtil;
  50. private final Scheduler scheduler;
  51. /**
  52. * 定时任务分页查询
  53. * @param page 分页对象
  54. * @param sysJob 定时任务调度表
  55. * @return
  56. */
  57. @GetMapping("/page")
  58. public R getSysJobPage(Page page, SysJob sysJob) {
  59. return R.ok(sysJobService.page(page, Wrappers.query(sysJob)));
  60. }
  61. /**
  62. * 通过id查询定时任务
  63. * @param id id
  64. * @return R
  65. */
  66. @GetMapping("/{id}")
  67. public R getById(@PathVariable("id") Integer id) {
  68. return R.ok(sysJobService.getById(id));
  69. }
  70. /**
  71. * 新增定时任务
  72. * @param sysJob 定时任务调度表
  73. * @return R
  74. */
  75. @PostMapping
  76. @PreAuthorize("@pms.hasPermission('job_sys_job_add')")
  77. public R save(@RequestBody SysJob sysJob) {
  78. log.info("新增定时任务");
  79. sysJob.setJobStatus(JOB_STATUS_RELEASE.getType());
  80. sysJob.setCreateBy(SecurityUtils.getUser().getUsername());
  81. return R.ok(sysJobService.save(sysJob));
  82. }
  83. /**
  84. * 修改定时任务
  85. * @param sysJob 定时任务调度表
  86. * @return R
  87. */
  88. @PutMapping
  89. @PreAuthorize("@pms.hasPermission('job_sys_job_edit')")
  90. public R updateById(@RequestBody SysJob sysJob) {
  91. log.info("修改定时任务");
  92. sysJob.setUpdateBy(SecurityUtils.getUser().getUsername());
  93. SysJob querySysJob = this.sysJobService.getById(sysJob.getJobId());
  94. if (JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
  95. this.taskUtil.addOrUpdateJob(sysJob, scheduler);
  96. sysJobService.updateById(sysJob);
  97. }
  98. else if (JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
  99. sysJobService.updateById(sysJob);
  100. }
  101. return R.ok();
  102. }
  103. /**
  104. * 通过id删除定时任务
  105. * @param id id
  106. * @return R
  107. */
  108. @DeleteMapping("/{id}")
  109. @PreAuthorize("@pms.hasPermission('job_sys_job_del')")
  110. public R removeById(@PathVariable Integer id) {
  111. log.info("删除定时任务: {}", id);
  112. SysJob querySysJob = this.sysJobService.getById(id);
  113. if (JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
  114. this.taskUtil.removeJob(querySysJob, scheduler);
  115. this.sysJobService.removeById(id);
  116. }
  117. else if (JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
  118. this.sysJobService.removeById(id);
  119. }
  120. return R.ok();
  121. }
  122. /**
  123. * 暂停全部定时任务
  124. * @return
  125. */
  126. @PostMapping("/shutdown-jobs")
  127. @PreAuthorize("@pms.hasPermission('job_sys_job_shutdown_job')")
  128. public R shutdownJobs() {
  129. log.info("暂停全部定时任务");
  130. taskUtil.pauseJobs(scheduler);
  131. int count = this.sysJobService
  132. .count(new LambdaQueryWrapper<SysJob>().eq(SysJob::getJobStatus, JOB_STATUS_RUNNING.getType()));
  133. if (count <= 0) {
  134. return R.ok("无正在运行定时任务");
  135. }
  136. else {
  137. // 更新定时任务状态条件,运行状态2更新为暂停状态2
  138. this.sysJobService.update(SysJob.builder().jobStatus(JOB_STATUS_NOT_RUNNING.getType()).build(),
  139. new UpdateWrapper<SysJob>().lambda().eq(SysJob::getJobStatus, JOB_STATUS_RUNNING.getType()));
  140. return R.ok("暂停成功");
  141. }
  142. }
  143. /**
  144. * 启动全部定时任务
  145. * @return
  146. */
  147. @PostMapping("/start-jobs")
  148. @PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
  149. public R startJobs() {
  150. log.info("启动全部定时任务");
  151. // 更新定时任务状态条件,暂停状态3更新为运行状态2
  152. this.sysJobService.update(SysJob.builder().jobStatus(JOB_STATUS_RUNNING.getType()).build(),
  153. new UpdateWrapper<SysJob>().lambda().eq(SysJob::getJobStatus, JOB_STATUS_NOT_RUNNING.getType()));
  154. taskUtil.startJobs(scheduler);
  155. return R.ok();
  156. }
  157. /**
  158. * 刷新全部定时任务
  159. */
  160. @Inner()
  161. @PostMapping("/refresh-jobs")
  162. public R<?> refreshJobs() {
  163. log.info("刷新全部定时任务");
  164. sysJobService.list().forEach((sysjob) -> {
  165. if (JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())
  166. || JOB_STATUS_DEL.getType().equals(sysjob.getJobStatus())) {
  167. taskUtil.removeJob(sysjob, scheduler);
  168. }
  169. else if (JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())
  170. || JOB_STATUS_NOT_RUNNING.getType().equals(sysjob.getJobStatus())) {
  171. taskUtil.addOrUpdateJob(sysjob, scheduler);
  172. }
  173. else {
  174. taskUtil.removeJob(sysjob, scheduler);
  175. }
  176. });
  177. return R.ok();
  178. }
  179. /**
  180. * 启动定时任务
  181. * @param jobId
  182. * @return
  183. */
  184. @PostMapping("/start-job/{id}")
  185. @PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
  186. public R startJob(@PathVariable("id") Integer jobId) {
  187. log.info("启动定时任务: {}", jobId);
  188. SysJob querySysJob = this.sysJobService.getById(jobId);
  189. if (querySysJob != null && JOB_LOG_STATUS_FAIL.getType().equals(querySysJob.getJobStatus())) {
  190. taskUtil.addOrUpdateJob(querySysJob, scheduler);
  191. }
  192. else {
  193. taskUtil.resumeJob(querySysJob, scheduler);
  194. }
  195. // 更新定时任务状态条件,暂停状态3更新为运行状态2
  196. this.sysJobService.updateById(SysJob.builder().jobId(jobId).jobStatus(JOB_STATUS_RUNNING.getType()).build());
  197. return R.ok();
  198. }
  199. /**
  200. * 启动定时任务
  201. * @param jobId
  202. * @return
  203. */
  204. @PostMapping("/run-job/{id}")
  205. @PreAuthorize("@pms.hasPermission('job_sys_job_run_job')")
  206. public R runJob(@PathVariable("id") Integer jobId) {
  207. log.info("立刻执行定时任务: {}", jobId);
  208. SysJob querySysJob = this.sysJobService.getById(jobId);
  209. return TaskUtil.runOnce(scheduler, querySysJob) ? R.ok() : R.failed();
  210. }
  211. /**
  212. * 暂停定时任务
  213. * @return
  214. */
  215. @PostMapping("/shutdown-job/{id}")
  216. @PreAuthorize("@pms.hasPermission('job_sys_job_shutdown_job')")
  217. public R shutdownJob(@PathVariable("id") Integer id) {
  218. log.info("暂停定时任务: {}", id);
  219. SysJob querySysJob = this.sysJobService.getById(id);
  220. // 更新定时任务状态条件,运行状态2更新为暂停状态3
  221. this.sysJobService.updateById(
  222. SysJob.builder().jobId(querySysJob.getJobId()).jobStatus(JOB_STATUS_NOT_RUNNING.getType()).build());
  223. taskUtil.pauseJob(querySysJob, scheduler);
  224. return R.ok();
  225. }
  226. /**
  227. * 唯一标识查询定时执行日志
  228. * @return
  229. */
  230. @GetMapping("/job-log")
  231. public R getJobLog(Page page, SysJobLog sysJobLog) {
  232. return R.ok(sysJobLogService.page(page, Wrappers.query(sysJobLog)));
  233. }
  234. /**
  235. * 检验任务名称和任务组联合是否唯一
  236. * @return
  237. */
  238. @GetMapping("/is-valid-task-name")
  239. public R isValidTaskName(@RequestParam("params")String jobName, @RequestParam("params")String jobGroup) {
  240. return this.sysJobService
  241. .count(Wrappers.query(SysJob.builder().jobName(jobName).jobGroup(jobGroup).build())) > 0 ? R.failed()
  242. : R.ok();
  243. }
  244. }