|
|
@@ -0,0 +1,170 @@
|
|
|
+package cn.tr.module.quartz.job.controller;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.tr.core.exception.ServiceException;
|
|
|
+import cn.tr.core.exception.TRExcCode;
|
|
|
+import cn.tr.module.quartz.constant.Constants;
|
|
|
+import cn.tr.module.quartz.exception.TaskException;
|
|
|
+import cn.tr.module.quartz.job.dto.SysJobSmallDTO;
|
|
|
+import cn.tr.module.quartz.utils.CronUtils;
|
|
|
+import cn.tr.module.quartz.utils.ScheduleUtils;
|
|
|
+import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
|
|
+import cn.dev33.satoken.annotation.SaCheckPermission;
|
|
|
+import cn.tr.core.validation.Insert;
|
|
|
+import cn.tr.core.validation.Update;
|
|
|
+import cn.tr.core.pojo.CommonResult;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.quartz.SchedulerException;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import cn.tr.module.quartz.job.dto.SysJobDTO;
|
|
|
+import cn.tr.module.quartz.job.service.ISysJobService;
|
|
|
+import cn.tr.module.quartz.job.dto.SysJobQueryDTO;
|
|
|
+import java.util.*;
|
|
|
+import cn.tr.plugin.mybatis.base.BaseController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import cn.tr.plugin.operatelog.annotation.OperateLog;
|
|
|
+import cn.tr.core.pojo.TableDataInfo;
|
|
|
+/**
|
|
|
+ * 定时任务调度表控制器
|
|
|
+ *
|
|
|
+ * @author lf
|
|
|
+ * @date 2023/05/05 10:55
|
|
|
+ */
|
|
|
+@Api(tags = "定时任务调度表")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/quartz/job")
|
|
|
+@AllArgsConstructor
|
|
|
+public class SysJobController extends BaseController{
|
|
|
+
|
|
|
+ private final ISysJobService sysJobService;
|
|
|
+
|
|
|
+ @ApiOperationSupport(author = "lf",order = 1)
|
|
|
+ @ApiOperation(value="根据条件查询定时任务调度表",notes = "权限: 无")
|
|
|
+ @PostMapping("/query/page")
|
|
|
+ public TableDataInfo<SysJobDTO> selectList(@RequestBody SysJobQueryDTO query) {
|
|
|
+ startPage();
|
|
|
+ return getDataTable(sysJobService.selectSysJobList(query));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperationSupport(author = "lf",order = 2)
|
|
|
+ @ApiOperation(value = "根据id查询定时任务调度表",notes = "权限: quartz:job:query")
|
|
|
+ @GetMapping("/detail/{id}")
|
|
|
+ @SaCheckPermission("quartz:job:query")
|
|
|
+ public CommonResult<SysJobDTO> findById(@PathVariable("id") String id){
|
|
|
+ return CommonResult.success(sysJobService.selectSysJobById(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperationSupport(author = "lf",order = 3)
|
|
|
+ @ApiOperation(value="添加定时任务调度表",notes = "权限: quartz:job:add")
|
|
|
+ @PostMapping("/add")
|
|
|
+ @OperateLog
|
|
|
+ @SaCheckPermission("quartz:job:add")
|
|
|
+ public CommonResult<Boolean> add(@RequestBody@Validated(Insert.class) SysJobDTO source) throws SchedulerException, TaskException {
|
|
|
+ validateSource(source);
|
|
|
+ return CommonResult.success(sysJobService.insertSysJob(source));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperationSupport(author = "lf",order = 4)
|
|
|
+ @ApiOperation(value="通过主键id编辑定时任务调度表",notes = "权限: quartz:job:edit")
|
|
|
+ @PostMapping("/edit")
|
|
|
+ @OperateLog
|
|
|
+ @SaCheckPermission("quartz:job:edit")
|
|
|
+ public CommonResult<Boolean> edit(@RequestBody@Validated(Update.class) SysJobDTO source) throws SchedulerException, TaskException {
|
|
|
+ validateSource(source);
|
|
|
+ return CommonResult.success(sysJobService.updateSysJobById(source));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperationSupport(author = "lf",order = 5)
|
|
|
+ @ApiOperation(value="删除定时任务调度表",notes = "权限: quartz:job:remove")
|
|
|
+ @PostMapping("/removeByIds")
|
|
|
+ @OperateLog
|
|
|
+ @SaCheckPermission("quartz:job:remove")
|
|
|
+ public CommonResult<Integer> delete(@RequestBody Collection<String> ids) throws SchedulerException {
|
|
|
+ return CommonResult.success(sysJobService.removeSysJobByIds(ids));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 任务调度立即执行一次
|
|
|
+ */
|
|
|
+ @ApiOperationSupport(author = "lf",order = 6)
|
|
|
+ @OperateLog
|
|
|
+ @ApiOperation(value="立即执行一次",notes = "权限: quartz:job:edit")
|
|
|
+ @PostMapping("/run/{jobId}")
|
|
|
+ @SaCheckPermission("quartz:job:edit")
|
|
|
+ public CommonResult<Boolean> run(@PathVariable("jobId") String jobId) throws SchedulerException {
|
|
|
+ return CommonResult.success(sysJobService.run(jobId));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验cron表达式是否有效
|
|
|
+ */
|
|
|
+ @ApiOperationSupport(author = "lf",order = 7)
|
|
|
+ @ApiOperation(value="校验cron表达式是否有效",notes = "权限: 无")
|
|
|
+ @PostMapping("/checkCronExpressionIsValid")
|
|
|
+ public CommonResult<Boolean> checkCronExpressionIsValid(@RequestBody String cronExpression) {
|
|
|
+ return CommonResult.success(sysJobService.checkCronExpressionIsValid(cronExpression));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询cron表达式近5次的执行时间
|
|
|
+ */
|
|
|
+ @ApiOperationSupport(author = "lf",order = 8)
|
|
|
+ @PostMapping("/queryCronExpression")
|
|
|
+ @ApiOperation(value="查询cron表达式近5次的执行时间",notes = "权限: 无")
|
|
|
+ public CommonResult<List<String>> queryCronExpression(@RequestBody String cronExpression)
|
|
|
+ {
|
|
|
+ if (sysJobService.checkCronExpressionIsValid(cronExpression)) {
|
|
|
+ List<String> dateList = CronUtils.getRecentTriggerTime(cronExpression);
|
|
|
+ return CommonResult.success(dateList);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return CommonResult.error(TRExcCode.SYSTEM_ERROR_B0001,"表达式无效");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 任务调度状态修改
|
|
|
+ */
|
|
|
+ @ApiOperationSupport(author = "lf",order = 6)
|
|
|
+ @OperateLog
|
|
|
+ @ApiOperation(value="任务调度状态修改",notes = "权限: quartz:job:edit")
|
|
|
+ @PostMapping("/changeStatus")
|
|
|
+ @SaCheckPermission("quartz:job:edit")
|
|
|
+ public CommonResult<Boolean> changeStatus(@RequestBody@Validated SysJobSmallDTO source) throws SchedulerException {
|
|
|
+ return CommonResult.success(sysJobService.changeStatus(source.getId(),source.getStatus()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validateSource(SysJobDTO source){
|
|
|
+ if (!CronUtils.isValid(source.getCronExpression()))
|
|
|
+ {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"新增任务'" + source.getJobName() + "'失败,Cron表达式不正确");
|
|
|
+ }
|
|
|
+ else if (StrUtil.containsIgnoreCase(source.getInvokeTarget(), Constants.LOOKUP_RMI))
|
|
|
+ {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"新增任务'" + source.getJobName() + "'失败,目标字符串不允许'rmi'调用");
|
|
|
+ }
|
|
|
+ else if (StrUtil.containsAnyIgnoreCase(source.getInvokeTarget(), new String[] { Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS }))
|
|
|
+ {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"新增任务'" + source.getJobName() + "'失败,目标字符串不允许'ldap(s)'调用");
|
|
|
+ }
|
|
|
+ else if (StrUtil.containsAnyIgnoreCase(source.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
|
|
|
+ {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"新增任务'" + source.getJobName() + "'失败,目标字符串不允许'http(s)'调用");
|
|
|
+ }
|
|
|
+ else if (StrUtil.containsAnyIgnoreCase(source.getInvokeTarget(), Constants.JOB_ERROR_STR))
|
|
|
+ {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"新增任务'" + source.getJobName() + "'失败,目标字符串存在违规");
|
|
|
+ }
|
|
|
+ else if (!ScheduleUtils.whiteList(source.getInvokeTarget()))
|
|
|
+ {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"新增任务'" + source.getJobName() + "'失败,目标字符串不在白名单内");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|