fix: 缺少更新定时任务接口

This commit is contained in:
Bunny 2024-12-19 20:41:08 +08:00
parent f5d535a3fb
commit b2e2b80692
8 changed files with 78 additions and 15 deletions

View File

@ -58,6 +58,13 @@ public class GlobalExceptionHandler {
return Result.error(null, 500, "[" + primaryKeyErrorMatcher.group(1) + "]已存在");
}
// corn表达式错误
String cronExpression = "CronExpression '(.*?)' is invalid";
Matcher cronExpressionMatcher = Pattern.compile(cronExpression).matcher(message);
if (cronExpressionMatcher.find()) {
return Result.error(null, 500, "表达式 " + cronExpressionMatcher.group(1) + " 不合法");
}
log.error("GlobalExceptionHandler===>运行时异常信息:{}", message);
exception.printStackTrace();
return Result.error(null, 500, "服务器异常");

View File

@ -87,7 +87,7 @@ public class MinioUtil {
return minioFile;
} catch (Exception exception) {
throw new AuthCustomerException(ResultCodeEnum.UPDATE_ERROR);
throw new AuthCustomerException(ResultCodeEnum.UPLOAD_ERROR);
}
}
@ -135,7 +135,7 @@ public class MinioUtil {
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(filename).stream(inputStream, size, -1).build());
} catch (Exception exception) {
log.error("上传文件失败:{}", (Object) exception.getStackTrace());
throw new AuthCustomerException(ResultCodeEnum.UPDATE_ERROR);
throw new AuthCustomerException(ResultCodeEnum.UPLOAD_ERROR);
}
}

View File

@ -30,19 +30,19 @@ public class SchedulersUpdateDto {
@NotNull(message = "任务详情不能为空")
private String description;
@Schema(name = "jobClassName", title = "任务类名称")
@NotBlank(message = "corn表达式不能为空")
@NotNull(message = "corn表达式不能为空")
private String jobClassName;
// @Schema(name = "jobClassName", title = "任务类名称")
// @NotBlank(message = "corn表达式不能为空")
// @NotNull(message = "corn表达式不能为空")
// private String jobClassName;
@Schema(name = "cronExpression", title = "corn表达式")
@NotBlank(message = "corn表达式不能为空")
@NotNull(message = "corn表达式不能为空")
private String cronExpression;
@Schema(name = "jobMethodName", title = "执行方法")
@NotBlank(message = "执行方法不能为空")
@NotNull(message = "执行方法不能为空")
private String jobMethodName;
// @Schema(name = "jobMethodName", title = "执行方法")
// @NotBlank(message = "执行方法不能为空")
// @NotNull(message = "执行方法不能为空")
// private String jobMethodName;
}

View File

@ -58,6 +58,7 @@ public enum ResultCodeEnum {
THE_SAME_USER_HAS_LOGGED_IN(209, "相同用户已登录"),
// 提示错误
UPDATE_ERROR(216, "修改失败"),
URL_ENCODE_ERROR(216, "URL编码失败"),
ILLEGAL_CALLBACK_REQUEST_ERROR(217, "非法回调请求"),
FETCH_USERINFO_ERROR(219, "获取用户信息失败"),
@ -75,7 +76,7 @@ public enum ResultCodeEnum {
// 系统错误 500
UNKNOWN_EXCEPTION(500, "服务异常"),
SERVICE_ERROR(500, "服务异常"),
UPDATE_ERROR(500, "上传文件失败"),
UPLOAD_ERROR(500, "上传失败"),
FAIL(500, "失败"),
;

View File

@ -3,6 +3,7 @@ package cn.bunny.services.controller;
import cn.bunny.dao.dto.quartz.SchedulersOperationDto;
import cn.bunny.dao.dto.quartz.schedule.SchedulersAddDto;
import cn.bunny.dao.dto.quartz.schedule.SchedulersDto;
import cn.bunny.dao.dto.quartz.schedule.SchedulersUpdateDto;
import cn.bunny.dao.entity.quartz.Schedulers;
import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.pojo.result.Result;
@ -64,6 +65,14 @@ public class SchedulersController {
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS));
}
@Operation(summary = "更新任务", description = "更新任务")
@PutMapping("updateSchedulers")
public Result<String> updateSchedulers(@Valid @RequestBody SchedulersUpdateDto dto) {
schedulersService.updateSchedulers(dto);
return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
}
@Operation(summary = "暂停任务", description = "暂停任务")
@PutMapping("/pauseSchedulers")
public Result<String> pause(@RequestBody SchedulersOperationDto dto) {

View File

@ -3,6 +3,7 @@ package cn.bunny.services.service;
import cn.bunny.dao.dto.quartz.SchedulersOperationDto;
import cn.bunny.dao.dto.quartz.schedule.SchedulersAddDto;
import cn.bunny.dao.dto.quartz.schedule.SchedulersDto;
import cn.bunny.dao.dto.quartz.schedule.SchedulersUpdateDto;
import cn.bunny.dao.entity.quartz.Schedulers;
import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.vo.quartz.SchedulersVo;
@ -64,4 +65,11 @@ public interface SchedulersService extends IService<Schedulers> {
* @return 所有调度任务内容
*/
List<Map<String, String>> getAllScheduleJobList();
/**
* 更新任务
*
* @param dto 更新任务表单
*/
void updateSchedulers(SchedulersUpdateDto dto);
}

View File

@ -130,6 +130,15 @@ public class EmailTemplateServiceImpl extends ServiceImpl<EmailTemplateMapper, E
updateList.add(emailTemplate);
updateBatchById(updateList);
// 默认邮件
List<EmailTemplate> emailTemplates = list(Wrappers.<EmailTemplate>lambdaQuery().eq(EmailTemplate::getType, type));
List<EmailTemplate> isEmailTemplateListEmpty = emailTemplates.stream().filter(template -> template.getIsDefault().equals(true)).toList();
if (isEmailTemplateListEmpty.isEmpty()) {
EmailTemplate template = emailTemplates.get(0);
template.setIsDefault(Boolean.TRUE);
updateById(template);
}
}
/**

View File

@ -4,8 +4,10 @@ import cn.bunny.common.service.exception.AuthCustomerException;
import cn.bunny.dao.dto.quartz.SchedulersOperationDto;
import cn.bunny.dao.dto.quartz.schedule.SchedulersAddDto;
import cn.bunny.dao.dto.quartz.schedule.SchedulersDto;
import cn.bunny.dao.dto.quartz.schedule.SchedulersUpdateDto;
import cn.bunny.dao.entity.quartz.Schedulers;
import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.pojo.result.ResultCodeEnum;
import cn.bunny.dao.vo.quartz.SchedulersVo;
import cn.bunny.services.aop.AnnotationScanner;
import cn.bunny.services.aop.annotation.QuartzSchedulers;
@ -20,10 +22,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* <p>
@ -92,6 +91,36 @@ public class SchedulersServiceImpl extends ServiceImpl<SchedulersMapper, Schedul
}).toList();
}
/**
* 更新任务
*
* @param dto 更新任务表单
*/
@Override
public void updateSchedulers(SchedulersUpdateDto dto) {
String jobName = dto.getJobName();
String jobGroup = dto.getJobGroup();
String cronExpression = dto.getCronExpression();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(jobName, jobGroup)
.withDescription(dto.getDescription())
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
.build();
try {
TriggerKey key = new TriggerKey(jobName, jobGroup);
Trigger oldTrigger = scheduler.getTrigger(key);
Date date = scheduler.rescheduleJob(oldTrigger.getKey(), trigger);
if (date == null) {
throw new AuthCustomerException(ResultCodeEnum.UPDATE_ERROR);
}
} catch (SchedulerException e) {
throw new AuthCustomerException(ResultCodeEnum.UPDATE_ERROR);
}
}
/**
* 添加Schedulers视图
*