feat(sj_1.3.0-beta1):

1、新增定时任务告警配置
2、新增定时任务无节点告警配置
This commit is contained in:
wodeyangzipingpingwuqi 2024-12-07 17:28:07 +08:00
parent 10451db5f3
commit 11c57a9686
25 changed files with 167 additions and 136 deletions

View File

@ -78,8 +78,7 @@ public class JobExecutorFutureCallback implements FutureCallback<ExecuteResult>
initLogContext();
// 上报执行成功
SnailJobLog.REMOTE.info("任务执行成功 taskBatchId:[{}] [{}]", jobContext.getTaskBatchId(),
JsonUtil.toJsonString(result));
SnailJobLog.REMOTE.info("任务执行成功 taskBatchId:[{}] [{}]", jobContext.getTaskBatchId(), JsonUtil.toJsonString(result));
if (Objects.isNull(result)) {
result = ExecuteResult.success();
@ -197,7 +196,5 @@ public class JobExecutorFutureCallback implements FutureCallback<ExecuteResult>
} catch (Exception e1) {
SnailJobLog.LOCAL.error("Client failed to send component exception alert.", e1);
}
}
}

View File

@ -16,6 +16,7 @@ public enum JobNotifySceneEnum {
/********************************Job****************************************/
JOB_TASK_ERROR(1, "JOB任务执行失败", NodeTypeEnum.SERVER),
JOB_CLIENT_ERROR(2, "客户端执行失败", NodeTypeEnum.CLIENT),
JOB_NO_CLIENT_NODES_ERROR(3, "没有可执行的客户端节点", NodeTypeEnum.CLIENT),
/********************************Workflow****************************************/
WORKFLOW_TASK_ERROR(100, "Workflow任务执行失败", NodeTypeEnum.SERVER);

View File

@ -24,8 +24,9 @@ public enum RetryNotifySceneEnum {
RETRY_TASK_REACH_THRESHOLD(5, "任务重试次数超过阈值", NodeTypeEnum.SERVER),
RETRY_TASK_ENTER_DEAD_LETTER(6, "任务重试失败进入死信队列", NodeTypeEnum.SERVER);
RETRY_TASK_ENTER_DEAD_LETTER(6, "任务重试失败进入死信队列", NodeTypeEnum.SERVER),
RETRY_NO_CLIENT_NODES_ERROR(7, "没有可执行的客户端节点", NodeTypeEnum.CLIENT);
/**
* 通知场景

View File

@ -76,5 +76,6 @@ public class JobBatchResponseDO {
private String argsStr;
private String notifyIds;
}

View File

@ -142,4 +142,9 @@ public class Job extends CreateUpdateDt {
*/
private Integer deleted;
/**
* 通知告警场景配置id列表
*/
private String notifyIds;
}

View File

@ -34,6 +34,11 @@ public class NotifyConfig extends CreateUpdateDt {
private Integer notifyThreshold;
/**
* 通知告警场景名
*/
private String notifyName;
private Integer notifyScene;
private Integer rateLimiterStatus;

View File

@ -59,7 +59,8 @@
job.block_strategy,
job.trigger_type,
job.executor_info,
job.args_str
job.args_str,
job.notify_ids
FROM sj_job_task_batch batch
JOIN sj_job job ON batch.job_id = job.id
${ew.customSqlSegment}

View File

@ -1,4 +1,4 @@
package com.aizuda.snailjob.server.common.alarm;
package com.aizuda.snailjob.client.common.annotation;
import cn.hutool.core.collection.CollUtil;
import com.aizuda.snailjob.common.core.alarm.Alarm;
@ -69,15 +69,13 @@ public abstract class AbstractAlarm<E extends ApplicationEvent, A extends AlarmI
// 获取所有的组名称
Set<String> groupNames = new HashSet<>();
// 获取所有的场景名称
Set<String> businessIds = new HashSet<>();
Set<Long> notifyIds = new HashSet<>();
// 转换AlarmDTO 为了下面循环发送使用
Map<Triple<String, String, String>, List<A>> waitSendAlarmInfos = convertAlarmDTO(
alarmInfos, namespaceIds, groupNames, businessIds);
Map<Triple<String, String, Set<Long>>, List<A>> waitSendAlarmInfos = convertAlarmDTO(alarmInfos, namespaceIds, groupNames, notifyIds);
// 批量获取通知配置
Map<Triple<String, String, String>, List<NotifyConfigInfo>> notifyConfig = obtainNotifyConfig(namespaceIds,
groupNames, businessIds);
Map<Triple<String, String, Set<Long>>, List<NotifyConfigInfo>> notifyConfig = obtainNotifyConfig(namespaceIds, groupNames, notifyIds);
// 循环发送消息
waitSendAlarmInfos.forEach((key, list) -> {
@ -95,18 +93,17 @@ public abstract class AbstractAlarm<E extends ApplicationEvent, A extends AlarmI
}
protected Map<Triple<String, String, String>, List<NotifyConfigInfo>> obtainNotifyConfig(Set<String> namespaceIds,
Set<String> groupNames, Set<String> businessIds) {
protected Map<Triple<String, String, Set<Long>>, List<NotifyConfigInfo>> obtainNotifyConfig(Set<String> namespaceIds,
Set<String> groupNames, Set<Long> notifyIds) {
// 批量获取所需的通知配置
List<NotifyConfig> notifyConfigs = accessTemplate.getNotifyConfigAccess().list(
new LambdaQueryWrapper<NotifyConfig>()
.eq(NotifyConfig::getNotifyStatus, StatusEnum.YES.getStatus())
.in(NotifyConfig::getSystemTaskType, StreamUtils.toList(getSystemTaskType(), SyetemTaskTypeEnum::getType))
.eq(NotifyConfig::getNotifyScene, getNotifyScene())
.in(NotifyConfig::getNamespaceId, namespaceIds)
.in(NotifyConfig::getGroupName, groupNames)
.in(NotifyConfig::getBusinessId, businessIds)
.in(NotifyConfig::getId, notifyIds)
);
if (CollUtil.isEmpty(notifyConfigs)) {
return Maps.newHashMap();
@ -139,17 +136,15 @@ public abstract class AbstractAlarm<E extends ApplicationEvent, A extends AlarmI
});
configInfo.setRecipientInfos(recipients);
return ImmutableTriple.of(configInfo.getNamespaceId(),
configInfo.getGroupName(),
configInfo.getBusinessId());
return ImmutableTriple.of(configInfo.getNamespaceId(), configInfo.getGroupName(), notifyIds);
});
}
protected abstract List<SyetemTaskTypeEnum> getSystemTaskType();
protected abstract Map<Triple<String, String, String>, List<A>> convertAlarmDTO(List<A> alarmData,
Set<String> namespaceIds, Set<String> groupNames, Set<String> sceneNames);
protected abstract Map<Triple<String, String, Set<Long>>, List<A>> convertAlarmDTO(List<A> alarmData,
Set<String> namespaceIds, Set<String> groupNames, Set<Long> notifyIds);
protected abstract List<A> poll() throws InterruptedException;
@ -212,5 +207,3 @@ public abstract class AbstractAlarm<E extends ApplicationEvent, A extends AlarmI
protected abstract void doOnApplicationEvent(E event);
}

View File

@ -1,14 +1,14 @@
package com.aizuda.snailjob.server.common.alarm;
import com.aizuda.snailjob.client.common.annotation.AbstractAlarm;
import com.aizuda.snailjob.common.core.util.JsonUtil;
import com.aizuda.snailjob.common.core.util.StreamUtils;
import com.aizuda.snailjob.server.common.dto.JobAlarmInfo;
import com.aizuda.snailjob.server.common.triple.ImmutableTriple;
import com.aizuda.snailjob.server.common.triple.Triple;
import org.springframework.context.ApplicationEvent;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* @author xiaowoniu
@ -18,15 +18,17 @@ import java.util.Set;
public abstract class AbstractJobAlarm<E extends ApplicationEvent> extends AbstractAlarm<E, JobAlarmInfo> {
@Override
protected Map<Triple<String, String, String>, List<JobAlarmInfo>> convertAlarmDTO(List<JobAlarmInfo> alarmInfos, Set<String> namespaceIds, Set<String> groupNames, Set<String> jobIds) {
protected Map<Triple<String, String, Set<Long>>, List<JobAlarmInfo>> convertAlarmDTO(List<JobAlarmInfo> alarmInfos, Set<String> namespaceIds, Set<String> groupNames, Set<Long> notifyIds) {
return StreamUtils.groupByKey(alarmInfos, alarmInfo -> {
String namespaceId = alarmInfo.getNamespaceId();
String groupName = alarmInfo.getGroupName();
String jobId = String.valueOf(alarmInfo.getJobId());
HashSet<Long> notifyIdsSet = Objects.isNull(alarmInfo.getNotifyIds()) ? new HashSet<>() : new HashSet<>(JsonUtil.parseList(alarmInfo.getNotifyIds(), Long.class));
namespaceIds.add(namespaceId);
groupNames.add(groupName);
jobIds.add(jobId);
return ImmutableTriple.of(namespaceId, groupName, jobId);
notifyIds.addAll(notifyIdsSet);
return ImmutableTriple.of(namespaceId, groupName, notifyIdsSet);
});
}
}

View File

@ -1,14 +1,14 @@
package com.aizuda.snailjob.server.common.alarm;
import com.aizuda.snailjob.client.common.annotation.AbstractAlarm;
import com.aizuda.snailjob.common.core.util.JsonUtil;
import com.aizuda.snailjob.common.core.util.StreamUtils;
import com.aizuda.snailjob.server.common.dto.RetryAlarmInfo;
import com.aizuda.snailjob.server.common.triple.ImmutableTriple;
import com.aizuda.snailjob.server.common.triple.Triple;
import org.springframework.context.ApplicationEvent;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* @author xiaowoniu
@ -17,24 +17,21 @@ import java.util.Set;
*/
public abstract class AbstractRetryAlarm<E extends ApplicationEvent> extends AbstractAlarm<E, RetryAlarmInfo> {
@Override
protected Map<Triple<String, String, String>, List<RetryAlarmInfo>> convertAlarmDTO(
List<RetryAlarmInfo> alarmDataList,
protected Map<Triple<String, String, Set<Long>>, List<RetryAlarmInfo>> convertAlarmDTO(
List<RetryAlarmInfo> retryAlarmInfoList,
Set<String> namespaceIds,
Set<String> groupNames,
Set<String> sceneNames) {
Set<Long> notifyIds) {
return StreamUtils.groupByKey(alarmDataList, alarmData -> {
String namespaceId = alarmData.getNamespaceId();
String groupName = alarmData.getGroupName();
String sceneName = alarmData.getSceneName();
return StreamUtils.groupByKey(retryAlarmInfoList, retryAlarmInfo -> {
String namespaceId = retryAlarmInfo.getNamespaceId();
String groupName = retryAlarmInfo.getGroupName();
HashSet<Long> notifyIdsSet = Objects.isNull(retryAlarmInfo.getNotifyIds()) ? new HashSet<>() : new HashSet<>(JsonUtil.parseList(retryAlarmInfo.getNotifyIds(), Long.class));
namespaceIds.add(namespaceId);
groupNames.add(groupName);
sceneNames.add(sceneName);
return ImmutableTriple.of(namespaceId, groupName, sceneName);
notifyIds.addAll(notifyIdsSet);
return ImmutableTriple.of(namespaceId, groupName, notifyIdsSet);
});
}
}

View File

@ -1,13 +1,13 @@
package com.aizuda.snailjob.server.common.alarm;
import com.aizuda.snailjob.client.common.annotation.AbstractAlarm;
import com.aizuda.snailjob.common.core.util.JsonUtil;
import com.aizuda.snailjob.server.common.dto.WorkflowAlarmInfo;
import com.aizuda.snailjob.server.common.triple.ImmutableTriple;
import com.aizuda.snailjob.server.common.triple.Triple;
import org.springframework.context.ApplicationEvent;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -18,16 +18,17 @@ import java.util.stream.Collectors;
public abstract class AbstractWorkflowAlarm<E extends ApplicationEvent> extends AbstractAlarm<E, WorkflowAlarmInfo> {
@Override
protected Map<Triple<String, String, String>, List<WorkflowAlarmInfo>> convertAlarmDTO(List<WorkflowAlarmInfo> alarmInfos, Set<String> namespaceIds, Set<String> groupNames, Set<String> jobIds) {
protected Map<Triple<String, String, Set<Long>>, List<WorkflowAlarmInfo>> convertAlarmDTO(List<WorkflowAlarmInfo> alarmInfos, Set<String> namespaceIds, Set<String> groupNames, Set<Long> notifyIds) {
return alarmInfos.stream().collect(Collectors.groupingBy(workflowAlarmInfo -> {
String namespaceId = workflowAlarmInfo.getNamespaceId();
String groupName = workflowAlarmInfo.getGroupName();
HashSet<Long> notifyIdsSet = Objects.isNull(workflowAlarmInfo.getNotifyIds()) ? new HashSet<>() : new HashSet<>(JsonUtil.parseList(workflowAlarmInfo.getNotifyIds(), Long.class));
return alarmInfos.stream().collect(Collectors.groupingBy(i -> {
String namespaceId = i.getNamespaceId();
String groupName = i.getGroupName();
String jobId = String.valueOf(i.getWorkflowId());
namespaceIds.add(namespaceId);
groupNames.add(groupName);
jobIds.add(jobId);
return ImmutableTriple.of(namespaceId, groupName, jobId);
notifyIds.addAll(notifyIdsSet);
return ImmutableTriple.of(namespaceId, groupName, notifyIdsSet);
}));
}
}

View File

@ -1,5 +1,7 @@
package com.aizuda.snailjob.server.common.convert;
import cn.hutool.core.util.StrUtil;
import com.aizuda.snailjob.common.core.util.JsonUtil;
import com.aizuda.snailjob.server.common.util.DateUtils;
import com.aizuda.snailjob.server.common.vo.JobResponseVO;
import com.aizuda.snailjob.template.datasource.persistence.po.Job;
@ -9,8 +11,10 @@ import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
/**
* @author opensnail
@ -28,7 +32,8 @@ public interface JobResponseVOConverter {
List<JobResponseVO> convertList(List<Job> jobs);
@Mappings({
@Mapping(target = "nextTriggerAt", expression = "java(JobResponseVOConverter.toLocalDateTime(job.getNextTriggerAt()))")
@Mapping(target = "nextTriggerAt", expression = "java(JobResponseVOConverter.toLocalDateTime(job.getNextTriggerAt()))"),
@Mapping(target = "notifyIds", expression = "java(JobResponseVOConverter.toJobNotifyIds(job.getNotifyIds()))")
})
JobResponseVO convert(Job job);
@ -39,4 +44,12 @@ public interface JobResponseVOConverter {
return DateUtils.toLocalDateTime(nextTriggerAt);
}
static Set<Long> toJobNotifyIds(String notifyIds) {
if (StrUtil.isBlank(notifyIds)) {
return new HashSet<>();
}
return new HashSet<>(JsonUtil.parseList(notifyIds, Long.class));
}
}

View File

@ -37,4 +37,9 @@ public class JobAlarmInfo extends AlarmInfo {
*/
private Integer operationReason;
/**
* 通知告警场景
*/
private String notifyIds;
}

View File

@ -33,4 +33,7 @@ public class RetryAlarmInfo extends AlarmInfo {
private Integer retryCount;
private LocalDateTime createDt;
private String notifyIds;
}

View File

@ -29,4 +29,9 @@ public class WorkflowAlarmInfo extends AlarmInfo {
*/
private Integer operationReason;
/**
* 通知告警场景
*/
private String notifyIds;
}

View File

@ -3,6 +3,7 @@ package com.aizuda.snailjob.server.common.vo;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.Set;
/**
* @author opensnail
@ -129,4 +130,9 @@ public class JobResponseVO {
*/
private Integer deleted;
/**
* 通知告警场景
*/
private Set<Long> notifyIds;
}

View File

@ -116,8 +116,7 @@ public class JobExecutorActor extends AbstractActor {
if (Objects.isNull(job)) {
taskStatus = JobTaskBatchStatusEnum.CANCEL.getStatus();
operationReason = JobOperationReasonEnum.JOB_CLOSED.getReason();
} else if (CollUtil.isEmpty(CacheRegisterTable.getServerNodeSet(job.getGroupName(),
job.getNamespaceId()))) {
} else if (CollUtil.isEmpty(CacheRegisterTable.getServerNodeSet(job.getGroupName(), job.getNamespaceId()))) {
taskStatus = JobTaskBatchStatusEnum.CANCEL.getStatus();
operationReason = JobOperationReasonEnum.NOT_CLIENT.getReason();

View File

@ -2,6 +2,7 @@ package com.aizuda.snailjob.server.job.task.support.generator.batch;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.aizuda.snailjob.common.core.context.SnailSpringContext;
import com.aizuda.snailjob.common.core.enums.JobOperationReasonEnum;
import com.aizuda.snailjob.common.core.enums.JobTaskBatchStatusEnum;
import com.aizuda.snailjob.server.common.cache.CacheRegisterTable;
@ -12,6 +13,7 @@ import com.aizuda.snailjob.server.job.task.dto.JobTimerTaskDTO;
import com.aizuda.snailjob.server.job.task.dto.TaskExecuteDTO;
import com.aizuda.snailjob.server.job.task.dto.WorkflowNodeTaskExecuteDTO;
import com.aizuda.snailjob.server.job.task.support.JobTaskConverter;
import com.aizuda.snailjob.server.job.task.support.alarm.event.JobTaskFailAlarmEvent;
import com.aizuda.snailjob.server.job.task.support.handler.JobTaskBatchHandler;
import com.aizuda.snailjob.server.job.task.support.handler.WorkflowBatchHandler;
import com.aizuda.snailjob.server.job.task.support.timer.JobTimerTask;
@ -64,7 +66,6 @@ public class JobTaskBatchGenerator {
CollUtil.isEmpty(CacheRegisterTable.getServerNodeSet(context.getGroupName(), context.getNamespaceId()))) {
jobTaskBatch.setTaskBatchStatus(JobTaskBatchStatusEnum.CANCEL.getStatus());
jobTaskBatch.setOperationReason(JobOperationReasonEnum.NOT_CLIENT.getReason());
} else {
// 生成一个新的任务
jobTaskBatch.setTaskBatchStatus(Optional.ofNullable(context.getTaskBatchStatus()).orElse(JobTaskBatchStatusEnum.WAITING.getStatus()));
@ -79,7 +80,11 @@ public class JobTaskBatchGenerator {
.eq(JobTaskBatch::getWorkflowTaskBatchId, context.getWorkflowTaskBatchId())
.eq(JobTaskBatch::getWorkflowNodeId, context.getWorkflowNodeId())
);
}
// 无执行的节点-告警通知
if (JobTaskBatchStatusEnum.CANCEL.getStatus() == jobTaskBatch.getTaskBatchStatus()) {
SnailSpringContext.getContext().publishEvent(new JobTaskFailAlarmEvent(jobTaskBatch.getId()));
}
// 非待处理状态无需进入时间轮中

View File

@ -6,6 +6,7 @@ import com.aizuda.snailjob.server.web.model.request.NotifyConfigQueryVO;
import com.aizuda.snailjob.server.web.model.request.NotifyConfigRequestVO;
import com.aizuda.snailjob.server.web.model.response.NotifyConfigResponseVO;
import com.aizuda.snailjob.server.web.service.NotifyConfigService;
import com.aizuda.snailjob.template.datasource.persistence.po.NotifyConfig;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
@ -32,6 +33,12 @@ public class NotifyConfigController {
return notifyConfigService.getNotifyConfigList(queryVO);
}
@LoginRequired
@GetMapping("/all/{systemTaskType}")
public List<NotifyConfig> getNotifyConfigBySystemTaskTypeList(@PathVariable("systemTaskType") Integer systemTaskType) {
return notifyConfigService.getNotifyConfigBySystemTaskTypeList(systemTaskType);
}
@LoginRequired
@GetMapping("{id}")
public NotifyConfigResponseVO getNotifyConfigDetail(@PathVariable("id") Long id) {

View File

@ -9,6 +9,8 @@ import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import java.util.Set;
/**
* @author opensnail
* @date 2023-10-11 22:37:55
@ -123,4 +125,9 @@ public class JobRequestVO {
*/
private String description;
/**
* 通知告警场景配置id列表
*/
private Set<Long> notifyIds;
}

View File

@ -25,7 +25,8 @@ public class NotifyConfigRequestVO {
/**
* 业务id (scene_name或job_id或workflow_id)
*/
@NotBlank(message = "业务id不能为空")
//@NotBlank(message = "业务id不能为空")
@Deprecated
private String businessId;
/**
@ -37,6 +38,9 @@ public class NotifyConfigRequestVO {
@NotNull(message = "通知状态不能为空")
private Integer notifyStatus;
@NotNull(message = "通知告警场景名不能为空")
private String notifyName;
@NotEmpty(message = "通知人列表")
private Set<Long> recipientIds;

View File

@ -4,6 +4,7 @@ import com.aizuda.snailjob.server.web.model.base.PageResult;
import com.aizuda.snailjob.server.web.model.request.NotifyConfigQueryVO;
import com.aizuda.snailjob.server.web.model.request.NotifyConfigRequestVO;
import com.aizuda.snailjob.server.web.model.response.NotifyConfigResponseVO;
import com.aizuda.snailjob.template.datasource.persistence.po.NotifyConfig;
import java.util.List;
import java.util.Set;
@ -16,6 +17,8 @@ public interface NotifyConfigService {
PageResult<List<NotifyConfigResponseVO>> getNotifyConfigList(NotifyConfigQueryVO queryVO);
List<NotifyConfig> getNotifyConfigBySystemTaskTypeList(Integer systemTaskType);
Boolean saveNotify(NotifyConfigRequestVO requestVO);
Boolean updateNotify(NotifyConfigRequestVO requestVO);

View File

@ -1,11 +1,18 @@
package com.aizuda.snailjob.server.web.service.convert;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.aizuda.snailjob.common.core.util.JsonUtil;
import com.aizuda.snailjob.server.web.model.request.JobRequestVO;
import com.aizuda.snailjob.template.datasource.persistence.po.Job;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author: opensnail
@ -17,8 +24,31 @@ public interface JobConverter {
JobConverter INSTANCE = Mappers.getMapper(JobConverter.class);
Job convert(JobRequestVO jobRequestVO);
List<JobRequestVO> convertList(List<Job> jobs);
@Mappings({
@Mapping(target = "notifyIds", expression = "java(JobConverter.toNotifyIds(job.getNotifyIds()))")
})
JobRequestVO convert(Job job);
@Mappings({
@Mapping(target = "notifyIds", expression = "java(JobConverter.toNotifyIdsStr(jobRequestVO.getNotifyIds()))")
})
Job convert(JobRequestVO jobRequestVO);
static Set<Long> toNotifyIds(String notifyIds) {
if (StrUtil.isBlank(notifyIds)) {
return new HashSet<>();
}
return new HashSet<>(JsonUtil.parseList(notifyIds, Long.class));
}
static String toNotifyIdsStr(Set<Long> notifyIds) {
if (CollUtil.isEmpty(notifyIds)) {
return null;
}
return JsonUtil.toJsonString(notifyIds);
}
}

View File

@ -152,6 +152,7 @@ public class JobServiceImpl implements JobService {
// 判断常驻任务
Job updateJob = JobConverter.INSTANCE.convert(jobRequestVO);
updateJob.setNotifyIds(JsonUtil.toJsonString(jobRequestVO.getNotifyIds()));
updateJob.setResident(isResident(jobRequestVO));
updateJob.setNamespaceId(job.getNamespaceId());
@ -270,8 +271,7 @@ public class JobServiceImpl implements JobService {
new LambdaQueryWrapper<Job>()
.eq(Job::getNamespaceId, namespaceId)
.eq(StrUtil.isNotBlank(exportJobVO.getGroupName()), Job::getGroupName, exportJobVO.getGroupName())
.likeRight(StrUtil.isNotBlank(exportJobVO.getJobName()), Job::getJobName,
StrUtil.trim(exportJobVO.getJobName()))
.likeRight(StrUtil.isNotBlank(exportJobVO.getJobName()), Job::getJobName, StrUtil.trim(exportJobVO.getJobName()))
.eq(Objects.nonNull(exportJobVO.getJobStatus()), Job::getJobStatus, exportJobVO.getJobStatus())
.in(CollUtil.isNotEmpty(exportJobVO.getJobIds()), Job::getId, exportJobVO.getJobIds())
.eq(Job::getDeleted, StatusEnum.NO.getStatus())

View File

@ -4,8 +4,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.aizuda.snailjob.common.core.util.JsonUtil;
import com.aizuda.snailjob.common.core.util.StreamUtils;
import com.aizuda.snailjob.server.common.enums.SyetemTaskTypeEnum;
import com.aizuda.snailjob.server.common.exception.SnailJobServerException;
import com.aizuda.snailjob.server.web.model.base.PageResult;
import com.aizuda.snailjob.server.web.model.request.NotifyConfigQueryVO;
@ -19,25 +17,17 @@ import com.aizuda.snailjob.server.web.service.handler.SyncConfigHandler;
import com.aizuda.snailjob.server.web.util.UserSessionUtils;
import com.aizuda.snailjob.template.datasource.access.AccessTemplate;
import com.aizuda.snailjob.template.datasource.access.ConfigAccess;
import com.aizuda.snailjob.template.datasource.persistence.mapper.JobMapper;
import com.aizuda.snailjob.template.datasource.persistence.mapper.NotifyRecipientMapper;
import com.aizuda.snailjob.template.datasource.persistence.mapper.WorkflowMapper;
import com.aizuda.snailjob.template.datasource.persistence.po.Job;
import com.aizuda.snailjob.template.datasource.persistence.po.NotifyConfig;
import com.aizuda.snailjob.template.datasource.persistence.po.NotifyRecipient;
import com.aizuda.snailjob.template.datasource.persistence.po.Workflow;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.List;
import java.util.Set;
/**
* @author: opensnail
@ -48,9 +38,6 @@ import java.util.stream.Collectors;
public class NotifyConfigServiceImpl implements NotifyConfigService {
private final AccessTemplate accessTemplate;
private final NotifyRecipientMapper notifyRecipientMapper;
private final JobMapper jobMapper;
private final WorkflowMapper workflowMapper;
@Override
public PageResult<List<NotifyConfigResponseVO>> getNotifyConfigList(NotifyConfigQueryVO queryVO) {
@ -74,66 +61,19 @@ public class NotifyConfigServiceImpl implements NotifyConfigService {
List<NotifyConfigResponseVO> notifyConfigResponseVOS = NotifyConfigResponseVOConverter.INSTANCE.convertList(
notifyConfigs);
Map<Long, String> recipientNameMap = getRecipientNameMap(notifyConfigResponseVOS);
Map<Long, String> jobNameMap = getJobNameMap(notifyConfigResponseVOS);
Map<Long, String> workflowNameMap = getWorkflowNameMap(notifyConfigResponseVOS);
for (final NotifyConfigResponseVO notifyConfigResponseVO : notifyConfigResponseVOS) {
notifyConfigResponseVO.setRecipientNames(StreamUtils.toSet(notifyConfigResponseVO.getRecipientIds(),
recipientId -> recipientNameMap.getOrDefault(recipientId, StrUtil.EMPTY)));
if (Objects.equals(notifyConfigResponseVO.getSystemTaskType(), SyetemTaskTypeEnum.RETRY.getType()) ||
Objects.equals(notifyConfigResponseVO.getSystemTaskType(), SyetemTaskTypeEnum.CALLBACK.getType())) {
notifyConfigResponseVO.setBusinessName(notifyConfigResponseVO.getBusinessId());
} else if (Objects.equals(notifyConfigResponseVO.getSystemTaskType(), SyetemTaskTypeEnum.JOB.getType())) {
notifyConfigResponseVO.setBusinessName(
jobNameMap.get(Long.parseLong(notifyConfigResponseVO.getBusinessId())));
} else if (Objects.equals(notifyConfigResponseVO.getSystemTaskType(),
SyetemTaskTypeEnum.WORKFLOW.getType())) {
notifyConfigResponseVO.setBusinessName(
workflowNameMap.get(Long.parseLong(notifyConfigResponseVO.getBusinessId())));
}
}
return new PageResult<>(pageDTO, notifyConfigResponseVOS);
}
private Map<Long, String> getWorkflowNameMap(final List<NotifyConfigResponseVO> notifyConfigResponseVOS) {
Set<Long> workflowIds = notifyConfigResponseVOS.stream().filter(responseVO ->
responseVO.getSystemTaskType().equals(SyetemTaskTypeEnum.WORKFLOW.getType()))
.map(responseVO -> Long.parseLong(responseVO.getBusinessId()))
.collect(Collectors.toSet());
if (CollUtil.isNotEmpty(workflowIds)) {
List<Workflow> workflows = workflowMapper.selectBatchIds(workflowIds);
return StreamUtils.toMap(workflows, Workflow::getId, Workflow::getWorkflowName);
}
return new HashMap<>();
}
private Map<Long, String> getJobNameMap(final List<NotifyConfigResponseVO> notifyConfigResponseVOS) {
Set<Long> jobIds = notifyConfigResponseVOS.stream().filter(responseVO ->
responseVO.getSystemTaskType().equals(SyetemTaskTypeEnum.JOB.getType()))
.map(responseVO -> Long.parseLong(responseVO.getBusinessId()))
.collect(Collectors.toSet());
if (CollUtil.isNotEmpty(jobIds)) {
List<Job> jobs = jobMapper.selectBatchIds(jobIds);
return StreamUtils.toMap(jobs, Job::getId, Job::getJobName);
}
return new HashMap<>();
}
@NotNull
private Map<Long, String> getRecipientNameMap(final List<NotifyConfigResponseVO> notifyConfigResponseVOS) {
Set<Long> recipientIds = StreamUtils.toSetByFlatMap(notifyConfigResponseVOS,
NotifyConfigResponseVO::getRecipientIds, Collection::stream);
if (CollUtil.isEmpty(recipientIds)) {
return Maps.newHashMap();
}
List<NotifyRecipient> notifyRecipients = notifyRecipientMapper.selectBatchIds(recipientIds);
return StreamUtils.toMap(notifyRecipients, NotifyRecipient::getId, NotifyRecipient::getRecipientName);
@Override
public List<NotifyConfig> getNotifyConfigBySystemTaskTypeList(Integer systemTaskType) {
String namespaceId = UserSessionUtils.currentUserSession().getNamespaceId();
List<NotifyConfig> notifyConfigList = accessTemplate.getNotifyConfigAccess().list(new LambdaQueryWrapper<NotifyConfig>()
.select(NotifyConfig::getId, NotifyConfig::getNotifyName)
.eq(NotifyConfig::getNamespaceId, namespaceId)
.eq(NotifyConfig::getSystemTaskType, systemTaskType)
.orderByDesc(NotifyConfig::getId)
);
return notifyConfigList;
}
@Override