fix(sj_1.1.1): 任务批次删除逻辑错误,参数验证异常

This commit is contained in:
dhb52 2024-07-17 21:39:28 +08:00
parent e6cac1a812
commit e60136c933
3 changed files with 36 additions and 24 deletions

View File

@ -20,6 +20,7 @@ import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -133,6 +134,23 @@ public class RestExceptionHandler {
return null; return null;
} }
/**
* Contrller 参数检验错误
*
* @param e 异常对象
* @return HttpResult
*/
@ExceptionHandler(HandlerMethodValidationException.class)
public Result onHandlerMethodValidationException(HandlerMethodValidationException e) {
Object[] detailMessageArguments = e.getDetailMessageArguments();
if (detailMessageArguments != null && detailMessageArguments.length > 0) {
return new Result<String>(0, detailMessageArguments[0].toString());
}
return new Result<>("参数校验失败");
}
/** /**
* 400错误 * 400错误
*/ */

View File

@ -5,7 +5,6 @@ import com.aizuda.snailjob.server.web.model.base.PageResult;
import com.aizuda.snailjob.server.web.model.request.JobBatchQueryVO; import com.aizuda.snailjob.server.web.model.request.JobBatchQueryVO;
import com.aizuda.snailjob.server.web.model.response.JobBatchResponseVO; import com.aizuda.snailjob.server.web.model.response.JobBatchResponseVO;
import com.aizuda.snailjob.server.web.service.JobBatchService; import com.aizuda.snailjob.server.web.service.JobBatchService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size; import jakarta.validation.constraints.Size;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -52,10 +51,10 @@ public class JobBatchController {
@DeleteMapping("/ids") @DeleteMapping("/ids")
@LoginRequired @LoginRequired
public Boolean deleteJobBatchByIds(@RequestBody @Valid public Boolean deleteJobBatchByIds(@RequestBody
@NotEmpty(message = "ids不能为空") @NotEmpty(message = "ids不能为空")
@Size(max = 100, message = "最多删除5") @Size(max = 100, message = "最多删除 {max} ")
Set<Long> ids) { Long[] ids) {
return jobBatchService.deleteJobBatchByIds(ids); return jobBatchService.deleteJobBatchByIds(Set.of(ids));
} }
} }

View File

@ -155,9 +155,9 @@ public class JobHandler {
} }
WorkflowTaskBatch workflowTaskBatch = workflowTaskBatchMapper.selectOne( WorkflowTaskBatch workflowTaskBatch = workflowTaskBatchMapper.selectOne(
new LambdaQueryWrapper<WorkflowTaskBatch>() new LambdaQueryWrapper<WorkflowTaskBatch>()
.select(WorkflowTaskBatch::getWfContext) .select(WorkflowTaskBatch::getWfContext)
.eq(WorkflowTaskBatch::getId, workflowTaskBatchId) .eq(WorkflowTaskBatch::getId, workflowTaskBatchId)
); );
if (Objects.isNull(workflowTaskBatch)) { if (Objects.isNull(workflowTaskBatch)) {
@ -170,29 +170,24 @@ public class JobHandler {
/** /**
* 批次删除定时任务批次 * 批次删除定时任务批次
* *
* @param ids 任务批次id * @param ids 任务批次id
* @param namespaceId 命名空间 * @param namespaceId 命名空间
*/ */
@Transactional @Transactional
public void deleteJobTaskBatchByIds(Set<Long> ids, String namespaceId) { public void deleteJobTaskBatchByIds(Set<Long> ids, String namespaceId) {
// 1. 删除任务批次 job_task_batch
Assert.isTrue(jobTaskBatchMapper.deleteByIds(ids) > 0,
() -> new SnailJobServerException("删除任务批次失败"));
Assert.isTrue(ids.size() == jobTaskBatchMapper.delete( // 2. 删除任务实例 job_task
new LambdaQueryWrapper<JobTaskBatch>() jobTaskMapper.delete(new LambdaQueryWrapper<JobTask>()
.in(JobTaskBatch::getId, ids) .eq(JobTask::getNamespaceId, namespaceId)
), () -> new SnailJobServerException("删除任务批次失败")); .in(JobTask::getTaskBatchId, ids));
// 3. 删除调度日志 job_log_message
Assert.isTrue(ids.size() == jobTaskMapper.delete(
new LambdaQueryWrapper<JobTask>()
.eq(JobTask::getNamespaceId, namespaceId)
.in(JobTask::getId, ids)
), () -> new SnailJobServerException("删除任务批次失败"));
// 删除日志信息
jobLogMessageMapper.delete(new LambdaQueryWrapper<JobLogMessage>() jobLogMessageMapper.delete(new LambdaQueryWrapper<JobLogMessage>()
.eq(JobLogMessage::getNamespaceId, namespaceId) .eq(JobLogMessage::getNamespaceId, namespaceId)
.in(JobLogMessage::getTaskId, ids) .in(JobLogMessage::getTaskBatchId, ids)
); );
} }
} }