SysJobController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package com.kxs.daemon.quartz.controller;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.kxs.common.core.util.R;
  8. import com.kxs.common.log.annotation.SysLog;
  9. import com.kxs.common.security.util.SecurityUtils;
  10. import com.kxs.daemon.quartz.constants.SkyQuartzEnum;
  11. import com.kxs.daemon.quartz.entity.SysJob;
  12. import com.kxs.daemon.quartz.entity.SysJobLog;
  13. import com.kxs.daemon.quartz.service.SysJobLogService;
  14. import com.kxs.daemon.quartz.service.SysJobService;
  15. import com.kxs.daemon.quartz.util.TaskUtil;
  16. import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
  17. import io.swagger.v3.oas.annotations.Operation;
  18. import io.swagger.v3.oas.annotations.security.SecurityRequirement;
  19. import io.swagger.v3.oas.annotations.tags.Tag;
  20. import lombok.AllArgsConstructor;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.quartz.Scheduler;
  23. import org.quartz.SchedulerException;
  24. import org.springframework.http.HttpHeaders;
  25. import org.springframework.security.access.prepost.PreAuthorize;
  26. import org.springframework.web.bind.annotation.*;
  27. import java.util.List;
  28. /**
  29. * @author LXQ
  30. * @date 2021-01-27 10:04:42
  31. * <p>
  32. * 定时任务管理
  33. */
  34. @RestController
  35. @AllArgsConstructor
  36. @RequestMapping("/sysJob")
  37. @Tag(description = "sys-job", name = "定时任务")
  38. @SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
  39. @Slf4j
  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. * @param page 分页对象
  48. * @param sysJob 定时任务调度表
  49. * @return
  50. */
  51. @GetMapping("/page")
  52. @Operation(description = "分页定时业务查询")
  53. public R getSysJobPage(Page page, SysJob sysJob) {
  54. LambdaQueryWrapper<SysJob> wrapper = Wrappers.<SysJob>lambdaQuery()
  55. .like(StrUtil.isNotBlank(sysJob.getJobName()), SysJob::getJobName, sysJob.getJobName())
  56. .like(StrUtil.isNotBlank(sysJob.getJobGroup()), SysJob::getJobGroup, sysJob.getJobGroup())
  57. .eq(StrUtil.isNotBlank(sysJob.getJobStatus()), SysJob::getJobStatus, sysJob.getJobGroup())
  58. .eq(StrUtil.isNotBlank(sysJob.getJobExecuteStatus()), SysJob::getJobExecuteStatus,
  59. sysJob.getJobExecuteStatus());
  60. return R.ok(sysJobService.page(page, wrapper));
  61. }
  62. /**
  63. * 通过id查询定时任务
  64. * @param id id
  65. * @return R
  66. */
  67. @GetMapping("/{id}")
  68. @Operation(description = "唯一标识查询定时任务")
  69. public R getById(@PathVariable("id") Long id) {
  70. return R.ok(sysJobService.getById(id));
  71. }
  72. /**
  73. * 新增定时任务
  74. * @param sysJob 定时任务调度表
  75. * @return R
  76. */
  77. @SysLog("新增定时任务")
  78. @PostMapping("add")
  79. @PreAuthorize("@pms.hasPermission('job_sys_job_add')")
  80. @Operation(description = "新增定时任务")
  81. public R save(@RequestBody SysJob sysJob) {
  82. long count = sysJobService.count(
  83. Wrappers.query(SysJob.builder().jobName(sysJob.getJobName()).jobGroup(sysJob.getJobGroup()).build()));
  84. if (count > 0) {
  85. return R.failed("任务重复,请检查此组内是否已包含同名任务");
  86. }
  87. sysJob.setJobStatus(SkyQuartzEnum.JOB_STATUS_RELEASE.getType());
  88. sysJob.setCreateBy(SecurityUtils.getUser().getUsername());
  89. return R.ok(sysJobService.save(sysJob));
  90. }
  91. /**
  92. * 修改定时任务
  93. * @param sysJob 定时任务调度表
  94. * @return R
  95. */
  96. @SysLog("修改定时任务")
  97. @PutMapping("update")
  98. @PreAuthorize("@pms.hasPermission('job_sys_job_edit')")
  99. @Operation(description = "修改定时任务")
  100. public R updateById(@RequestBody SysJob sysJob) {
  101. sysJob.setUpdateBy(SecurityUtils.getUser().getUsername());
  102. SysJob querySysJob = this.sysJobService.getById(sysJob.getJobId());
  103. if (SkyQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
  104. // 如修改暂停的需更新调度器
  105. this.taskUtil.addOrUpateJob(sysJob, scheduler);
  106. sysJobService.updateById(sysJob);
  107. }
  108. else if (SkyQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
  109. sysJobService.updateById(sysJob);
  110. }
  111. return R.ok();
  112. }
  113. /**
  114. * 通过id删除定时任务
  115. * @param id id
  116. * @return R
  117. */
  118. @SysLog("删除定时任务")
  119. @DeleteMapping("remove/{id}")
  120. @PreAuthorize("@pms.hasPermission('job_sys_job_del')")
  121. @Operation(description = "唯一标识查询定时任务,暂停任务才能删除")
  122. public R removeById(@PathVariable Long id) {
  123. SysJob querySysJob = this.sysJobService.getById(id);
  124. if (SkyQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
  125. this.taskUtil.removeJob(querySysJob, scheduler);
  126. this.sysJobService.removeById(id);
  127. }
  128. else if (SkyQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
  129. this.sysJobService.removeById(id);
  130. }
  131. return R.ok();
  132. }
  133. /**
  134. * 暂停全部定时任务
  135. * @return
  136. */
  137. @SysLog("暂停全部定时任务")
  138. @PostMapping("/shutdownJobs")
  139. @PreAuthorize("@pms.hasPermission('job_sys_job_shutdown_job')")
  140. @Operation(description = "暂停全部定时任务")
  141. public R shutdownJobs() {
  142. taskUtil.pauseJobs(scheduler);
  143. long count = this.sysJobService.count(
  144. new LambdaQueryWrapper<SysJob>().eq(SysJob::getJobStatus, SkyQuartzEnum.JOB_STATUS_RUNNING.getType()));
  145. if (count <= 0) {
  146. return R.ok("无正在运行定时任务");
  147. }
  148. else {
  149. // 更新定时任务状态条件,运行状态2更新为暂停状态2
  150. this.sysJobService.update(
  151. SysJob.builder().jobStatus(SkyQuartzEnum.JOB_STATUS_NOT_RUNNING.getType()).build(),
  152. new UpdateWrapper<SysJob>().lambda()
  153. .eq(SysJob::getJobStatus, SkyQuartzEnum.JOB_STATUS_RUNNING.getType()));
  154. return R.ok("暂停成功");
  155. }
  156. }
  157. /**
  158. * 启动全部定时任务
  159. * @return
  160. */
  161. @SysLog("启动全部定时任务")
  162. @PostMapping("/startJobs")
  163. @PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
  164. @Operation(description = "启动全部定时任务")
  165. public R startJobs() {
  166. // 更新定时任务状态条件,暂停状态3更新为运行状态2
  167. this.sysJobService.update(SysJob.builder().jobStatus(SkyQuartzEnum.JOB_STATUS_RUNNING.getType()).build(),
  168. new UpdateWrapper<SysJob>().lambda()
  169. .eq(SysJob::getJobStatus, SkyQuartzEnum.JOB_STATUS_NOT_RUNNING.getType()));
  170. taskUtil.startJobs(scheduler);
  171. return R.ok();
  172. }
  173. /**
  174. * 刷新全部定时任务
  175. * @return
  176. */
  177. @SysLog("刷新全部定时任务")
  178. @PostMapping("/refreshJobs")
  179. @PreAuthorize("@pms.hasPermission('job_sys_job_refresh_job')")
  180. @Operation(description = "刷新全部定时任务")
  181. public R refreshJobs() {
  182. sysJobService.list().forEach((sysjob) -> {
  183. if (SkyQuartzEnum.JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())
  184. || SkyQuartzEnum.JOB_STATUS_DEL.getType().equals(sysjob.getJobStatus())) {
  185. taskUtil.removeJob(sysjob, scheduler);
  186. }
  187. else if (SkyQuartzEnum.JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())
  188. || SkyQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(sysjob.getJobStatus())) {
  189. taskUtil.addOrUpateJob(sysjob, scheduler);
  190. }
  191. else {
  192. taskUtil.removeJob(sysjob, scheduler);
  193. }
  194. });
  195. return R.ok();
  196. }
  197. /**
  198. * 启动定时任务
  199. * @param jobId
  200. * @return
  201. */
  202. @SysLog("启动定时任务")
  203. @PostMapping("/startJob/{id}")
  204. @PreAuthorize("@pms.hasPermission('job_sys_job_start_job')")
  205. @Operation(description = "启动定时任务")
  206. public R startJob(@PathVariable("id") Long jobId) throws SchedulerException {
  207. SysJob querySysJob = this.sysJobService.getById(jobId);
  208. if (querySysJob == null) {
  209. return R.failed("无此定时任务,请确认");
  210. }
  211. // 如果定时任务不存在,强制状态为1已发布
  212. if (!scheduler.checkExists(TaskUtil.getJobKey(querySysJob))) {
  213. querySysJob.setJobStatus(SkyQuartzEnum.JOB_STATUS_RELEASE.getType());
  214. log.warn("定时任务不在quartz中,任务id:{},强制状态为已发布并加入调度器", jobId);
  215. }
  216. if (SkyQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
  217. taskUtil.addOrUpateJob(querySysJob, scheduler);
  218. }
  219. else {
  220. taskUtil.resumeJob(querySysJob, scheduler);
  221. }
  222. // 更新定时任务状态为运行状态2
  223. this.sysJobService
  224. .updateById(SysJob.builder().jobId(jobId).jobStatus(SkyQuartzEnum.JOB_STATUS_RUNNING.getType()).build());
  225. return R.ok();
  226. }
  227. /**
  228. * 启动定时任务
  229. * @param jobId
  230. * @return
  231. */
  232. @SysLog("立刻执行定时任务")
  233. @PostMapping("/runJob/{id}")
  234. @PreAuthorize("@pms.hasPermission('job_sys_job_run_job')")
  235. @Operation(description = "立刻执行定时任务")
  236. public R runJob(@PathVariable("id") Long jobId) throws SchedulerException {
  237. SysJob querySysJob = this.sysJobService.getById(jobId);
  238. // 执行定时任务前判定任务是否在quartz中
  239. if (!scheduler.checkExists(TaskUtil.getJobKey(querySysJob))) {
  240. querySysJob.setJobStatus(SkyQuartzEnum.JOB_STATUS_NOT_RUNNING.getType());
  241. log.warn("立刻执行定时任务-定时任务不在quartz中,任务id:{},强制状态为暂停并加入调度器", jobId);
  242. taskUtil.addOrUpateJob(querySysJob, scheduler);
  243. }
  244. return TaskUtil.runOnce(scheduler, querySysJob) ? R.ok() : R.failed();
  245. }
  246. /**
  247. * 暂停定时任务
  248. * @return
  249. */
  250. @SysLog("暂停定时任务")
  251. @PostMapping("/shutdownJob/{id}")
  252. @PreAuthorize("@pms.hasPermission('job_sys_job_shutdown_job')")
  253. @Operation(description = "暂停定时任务")
  254. public R shutdownJob(@PathVariable("id") Long id) {
  255. SysJob querySysJob = this.sysJobService.getById(id);
  256. // 更新定时任务状态条件,运行状态2更新为暂停状态3
  257. this.sysJobService.updateById(SysJob.builder()
  258. .jobId(querySysJob.getJobId())
  259. .jobStatus(SkyQuartzEnum.JOB_STATUS_NOT_RUNNING.getType())
  260. .build());
  261. taskUtil.pauseJob(querySysJob, scheduler);
  262. return R.ok();
  263. }
  264. /**
  265. * 唯一标识查询定时执行日志
  266. * @return
  267. */
  268. @GetMapping("/jobLog")
  269. @Operation(description = "唯一标识查询定时执行日志")
  270. public R getJobLog(Page page, SysJobLog sysJobLog) {
  271. return R.ok(sysJobLogService.page(page, Wrappers.query(sysJobLog)));
  272. }
  273. /**
  274. * 检验任务名称和任务组联合是否唯一
  275. * @return
  276. */
  277. @GetMapping("/isValidTaskName")
  278. @Operation(description = "检验任务名称和任务组联合是否唯一")
  279. public R isValidTaskName(@RequestParam String jobName, @RequestParam String jobGroup) {
  280. return this.sysJobService
  281. .count(Wrappers.query(SysJob.builder().jobName(jobName).jobGroup(jobGroup).build())) > 0
  282. ? R.failed("任务重复,请检查此组内是否已包含同名任务") : R.ok();
  283. }
  284. /**
  285. * 导出任务
  286. * @param sysJob
  287. * @return
  288. */
  289. @ResponseExcel
  290. @GetMapping("/export")
  291. @Operation(description = "导出任务")
  292. public List<SysJob> export(SysJob sysJob) {
  293. return sysJobService.list(Wrappers.query(sysJob));
  294. }
  295. }