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.util.SecurityUtils;
  24. import net.yyc.quartz.entity.SysJob;
  25. import net.yyc.quartz.entity.SysJobLog;
  26. import net.yyc.quartz.service.SysJobLogService;
  27. import net.yyc.quartz.service.SysJobService;
  28. import net.yyc.quartz.util.TaskUtil;
  29. import lombok.AllArgsConstructor;
  30. import lombok.extern.slf4j.Slf4j;
  31. import org.quartz.Scheduler;
  32. import org.springframework.security.access.prepost.PreAuthorize;
  33. import org.springframework.web.bind.annotation.*;
  34. import static net.yyc.quartz.constants.QuartzEnum.*;
  35. /**
  36. * @author frwcloud
  37. * @date 2019-01-27 10:04:42
  38. * <p>
  39. * 定时任务管理
  40. */
  41. @Slf4j
  42. @RestController
  43. @AllArgsConstructor
  44. @RequestMapping("/sys-job")
  45. public class SysJobController {
  46. private final SysJobService sysJobService;
  47. private final SysJobLogService sysJobLogService;
  48. private final TaskUtil taskUtil;
  49. private final Scheduler scheduler;
  50. /**
  51. * 定时任务分页查询
  52. * @param page 分页对象
  53. * @param sysJob 定时任务调度表
  54. * @return
  55. */
  56. @GetMapping("/page")
  57. public R getSysJobPage(Page page, SysJob sysJob) {
  58. return R.ok(sysJobService.page(page, Wrappers.query(sysJob)));
  59. }
  60. /**
  61. * 通过id查询定时任务
  62. * @param id id
  63. * @return R
  64. */
  65. @GetMapping("/{id}")
  66. public R getById(@PathVariable("id") Integer id) {
  67. return R.ok(sysJobService.getById(id));
  68. }
  69. /**
  70. * 新增定时任务
  71. * @param sysJob 定时任务调度表
  72. * @return R
  73. */
  74. @PostMapping
  75. @PreAuthorize("@pms.hasPermission('job_sys_job_add')")
  76. public R save(@RequestBody SysJob sysJob) {
  77. log.info("新增定时任务");
  78. sysJob.setJobStatus(JOB_STATUS_RELEASE.getType());
  79. sysJob.setCreateBy(SecurityUtils.getUser().getUsername());
  80. return R.ok(sysJobService.save(sysJob));
  81. }
  82. /**
  83. * 修改定时任务
  84. * @param sysJob 定时任务调度表
  85. * @return R
  86. */
  87. @PutMapping
  88. @PreAuthorize("@pms.hasPermission('job_sys_job_edit')")
  89. public R updateById(@RequestBody SysJob sysJob) {
  90. log.info("修改定时任务");
  91. sysJob.setUpdateBy(SecurityUtils.getUser().getUsername());
  92. SysJob querySysJob = this.sysJobService.getById(sysJob.getJobId());
  93. if (JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
  94. this.taskUtil.addOrUpateJob(sysJob, scheduler);
  95. sysJobService.updateById(sysJob);
  96. }
  97. else if (JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
  98. sysJobService.updateById(sysJob);
  99. }
  100. return R.ok();
  101. }
  102. /**
  103. * 通过id删除定时任务
  104. * @param id id
  105. * @return R
  106. */
  107. @DeleteMapping("/{id}")
  108. @PreAuthorize("@pms.hasPermission('job_sys_job_del')")
  109. public R removeById(@PathVariable Integer id) {
  110. log.info("删除定时任务: {}", id);
  111. SysJob querySysJob = this.sysJobService.getById(id);
  112. if (JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
  113. this.taskUtil.removeJob(querySysJob, scheduler);
  114. this.sysJobService.removeById(id);
  115. }
  116. else if (JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
  117. this.sysJobService.removeById(id);
  118. }
  119. return R.ok();
  120. }
  121. /**
  122. * 暂停全部定时任务
  123. * @return
  124. */
  125. @PostMapping("/shutdown-jobs")
  126. @PreAuthorize("@pms.hasPermission('job_sys_job_shutdown_job')")
  127. public R shutdownJobs() {
  128. log.info("暂停全部定时任务");
  129. taskUtil.pauseJobs(scheduler);
  130. int count = this.sysJobService
  131. .count(new LambdaQueryWrapper<SysJob>().eq(SysJob::getJobStatus, JOB_STATUS_RUNNING.getType()));
  132. if (count <= 0) {
  133. return R.ok("无正在运行定时任务");
  134. }
  135. else {
  136. // 更新定时任务状态条件,运行状态2更新为暂停状态2
  137. this.sysJobService.update(SysJob.builder().jobStatus(JOB_STATUS_NOT_RUNNING.getType()).build(),
  138. new UpdateWrapper<SysJob>().lambda().eq(SysJob::getJobStatus, JOB_STATUS_RUNNING.getType()));
  139. return R.ok("暂停成功");
  140. }
  141. }
  142. /**
  143. * 启动全部定时任务
  144. * @return
  145. */
  146. @PostMapping("/start-jobs")
  147. @PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
  148. public R startJobs() {
  149. log.info("启动全部定时任务");
  150. // 更新定时任务状态条件,暂停状态3更新为运行状态2
  151. this.sysJobService.update(SysJob.builder().jobStatus(JOB_STATUS_RUNNING.getType()).build(),
  152. new UpdateWrapper<SysJob>().lambda().eq(SysJob::getJobStatus, JOB_STATUS_NOT_RUNNING.getType()));
  153. taskUtil.startJobs(scheduler);
  154. return R.ok();
  155. }
  156. /**
  157. * 刷新全部定时任务
  158. * @return
  159. */
  160. @PostMapping("/refresh-jobs")
  161. @PreAuthorize("@pms.hasPermission('job_sys_job_refresh_job')")
  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.addOrUpateJob(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.addOrUpateJob(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 String jobName, @RequestParam 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. }