/* * Copyright (c) 2018-2025, hnqz All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the pig4cloud.com developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: hnqz */ package net.yyc.quartz.controller; 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 net.yyc.common.core.util.R; import net.yyc.common.security.util.SecurityUtils; import net.yyc.quartz.entity.SysJob; 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.*; import static net.yyc.quartz.constants.QuartzEnum.*; /** * @author frwcloud * @date 2019-01-27 10:04:42 *
* 定时任务管理
*/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/sys-job")
public class SysJobController {
private final SysJobService sysJobService;
private final SysJobLogService sysJobLogService;
private final TaskUtil taskUtil;
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)));
}
/**
* 通过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
*/
@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.addOrUpateJob(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();
}
/**
* 暂停全部定时任务
* @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