Merge branch 'master' of http://1.14.125.6:3000/mianbin/ops-pro
This commit is contained in:
commit
2031ce9233
@ -0,0 +1,24 @@
|
|||||||
|
package cd.casic.ci.commons.rpc.annototion;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName Exporter
|
||||||
|
* @Author hopeli
|
||||||
|
* @Date 2025/4/28 17:11
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@Target({ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface Exporter {
|
||||||
|
String name() default "";
|
||||||
|
|
||||||
|
String version() default "0.0.0";
|
||||||
|
}
|
@ -1,128 +0,0 @@
|
|||||||
package cd.casic.module.process.dispatch.trigger;
|
|
||||||
|
|
||||||
|
|
||||||
import cd.casic.module.process.support.trigger.entity.Trigger;
|
|
||||||
import cd.casic.module.process.support.trigger.entity.TriggerQuery;
|
|
||||||
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 流水线触发器服务接口
|
|
||||||
*/
|
|
||||||
public interface TriggerService {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建配置及任务
|
|
||||||
*
|
|
||||||
* @param trigger 配置
|
|
||||||
* @return 配置id
|
|
||||||
*/
|
|
||||||
String createTrigger(Trigger trigger);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询所有配置
|
|
||||||
*
|
|
||||||
* @param triggerQuery 流水线id
|
|
||||||
* @return 配置列表
|
|
||||||
*/
|
|
||||||
List<Object> findAllTrigger(TriggerQuery triggerQuery);
|
|
||||||
|
|
||||||
|
|
||||||
void cloneTrigger(String pipelineId, String clonePipelineId);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除流水线所有定时任务
|
|
||||||
*
|
|
||||||
* @param pipelineId 流水线id
|
|
||||||
*/
|
|
||||||
void deleteAllTrigger(String pipelineId);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 更新单个定时任务
|
|
||||||
*
|
|
||||||
* @param triggerId 定时任务id
|
|
||||||
*/
|
|
||||||
void updateTrigger(String triggerId);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 更新配置信息
|
|
||||||
*
|
|
||||||
* @param trigger 配置
|
|
||||||
*/
|
|
||||||
void updateTrigger(Trigger trigger);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据流水线id查询触发器配置
|
|
||||||
*
|
|
||||||
* @param triggerQuery 流水线id
|
|
||||||
* @return 配置
|
|
||||||
*/
|
|
||||||
List<Trigger> findTriggerList(TriggerQuery triggerQuery);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除单个定时任务
|
|
||||||
*
|
|
||||||
* @param triggerId 定时任务id
|
|
||||||
*/
|
|
||||||
void deleteTrigger(String triggerId);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询所有配置
|
|
||||||
*
|
|
||||||
* @return 配置列表
|
|
||||||
*/
|
|
||||||
List<Trigger> findAllTrigger();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据ID查询配置
|
|
||||||
*
|
|
||||||
* @param triggerId 配置ID
|
|
||||||
* @return 配置
|
|
||||||
*/
|
|
||||||
Trigger findOneTriggerById(String triggerId);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据ID列表查询配置
|
|
||||||
*
|
|
||||||
* @param idList 配置ID列表
|
|
||||||
* @return 配置列表
|
|
||||||
*/
|
|
||||||
List<Trigger> findAllTriggerConfigList(List<String> idList);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,105 +0,0 @@
|
|||||||
package cd.casic.module.process.dispatch.trigger;
|
|
||||||
|
|
||||||
|
|
||||||
import cd.casic.module.process.support.trigger.entity.TriggerTime;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 流水线触发器时间服务接口
|
|
||||||
*/
|
|
||||||
public interface TriggerTimeService {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建所有关联时间信息
|
|
||||||
*
|
|
||||||
* @param triggerTime 信息
|
|
||||||
*/
|
|
||||||
String createTriggerTime(TriggerTime triggerTime, String pipelineId);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据配置id查询消息类型
|
|
||||||
*
|
|
||||||
* @param triggerId 配置id
|
|
||||||
* @return 消息
|
|
||||||
*/
|
|
||||||
TriggerTime findTriggerTime(String triggerId);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据配置查询所有任务
|
|
||||||
*
|
|
||||||
* @param triggerId 配置id
|
|
||||||
* @return 任务集合
|
|
||||||
*/
|
|
||||||
List<TriggerTime> findAllTriggerTime(String triggerId);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询cron时间列表
|
|
||||||
*
|
|
||||||
* @param cron cron表达式
|
|
||||||
* @return 时间列表
|
|
||||||
*/
|
|
||||||
List<TriggerTime> fondCronTimeList(String cron);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据配置获取所有时间
|
|
||||||
*
|
|
||||||
* @param configId 配置id
|
|
||||||
* @return 时间集合
|
|
||||||
*/
|
|
||||||
List<Integer> findAllDataConfig(String configId);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除当前配置下的的所有任务
|
|
||||||
*
|
|
||||||
* @param triggerId 配置id
|
|
||||||
*/
|
|
||||||
void deleteAllTime(String triggerId, String pipelineId);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 周期任务更新执行时间
|
|
||||||
*
|
|
||||||
* @param timeId 任务id
|
|
||||||
*/
|
|
||||||
Boolean deleteCronTime(String pipelineId, String timeId);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除
|
|
||||||
*
|
|
||||||
* @param timeId timeId
|
|
||||||
*/
|
|
||||||
void deleteTime(String timeId);
|
|
||||||
|
|
||||||
|
|
||||||
void updateTime(TriggerTime triggerTime);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询单个信息
|
|
||||||
*
|
|
||||||
* @param timeId pipelineTimeId
|
|
||||||
* @return time信息
|
|
||||||
*/
|
|
||||||
TriggerTime findOneTime(String timeId);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询所有信息
|
|
||||||
*
|
|
||||||
* @return time信息集合
|
|
||||||
*/
|
|
||||||
List<TriggerTime> findAllTime();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据ID列表查询时间
|
|
||||||
*
|
|
||||||
* @param idList 时间ID列表
|
|
||||||
* @return 时间列表
|
|
||||||
*/
|
|
||||||
List<TriggerTime> findAllTimeList(List<String> idList);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package cd.casic.module.process.dispatch.trigger.dao;
|
|
||||||
|
|
||||||
import cd.casic.framework.mybatis.core.mapper.BaseMapperX;
|
|
||||||
import cd.casic.module.process.dispatch.trigger.dataobject.TriggerDo;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author by mianbin
|
|
||||||
* @Classname TriggerDao
|
|
||||||
* @Description TODO
|
|
||||||
* @Date 2025/4/23 20:03
|
|
||||||
*/
|
|
||||||
@Mapper
|
|
||||||
public interface TriggerDao extends BaseMapperX<TriggerDo> {
|
|
||||||
}
|
|
@ -9,6 +9,7 @@ import jakarta.validation.constraints.NotNull;
|
|||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -18,6 +19,8 @@ import java.util.List;
|
|||||||
* @Date 2025/4/28 10:43
|
* @Date 2025/4/28 10:43
|
||||||
* @Version 1.0
|
* @Version 1.0
|
||||||
*/
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/postprocess")
|
||||||
public class PostprocessController {
|
public class PostprocessController {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
package cd.casic.module.process.support.postprocess.service;
|
package cd.casic.module.process.support.postprocess.service;
|
||||||
|
|
||||||
import cd.casic.ci.commons.bean.engine.postprocess.Postprocess;
|
import cd.casic.ci.commons.bean.engine.postprocess.Postprocess;
|
||||||
|
import cd.casic.ci.commons.join.annototion.JoinProvider;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流水线后置处理服务接口
|
* 流水线后置处理服务接口
|
||||||
*/
|
*/
|
||||||
|
@JoinProvider(model = Postprocess.class)
|
||||||
public interface PostprocessService {
|
public interface PostprocessService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,6 +13,7 @@ import cd.casic.module.process.support.util.util.PipelineFinal;
|
|||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -27,6 +28,8 @@ import java.util.Objects;
|
|||||||
* @Date 2025/4/28 15:39
|
* @Date 2025/4/28 15:39
|
||||||
* @Version 1.0
|
* @Version 1.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@Service
|
||||||
public class PostprocessExecServiceImpl implements PostprocessExecService {
|
public class PostprocessExecServiceImpl implements PostprocessExecService {
|
||||||
@Resource
|
@Resource
|
||||||
PostprocessService postprocessService;
|
PostprocessService postprocessService;
|
||||||
|
@ -7,6 +7,7 @@ import cd.casic.module.process.support.postprocess.service.PostprocessInstanceSe
|
|||||||
import cd.casic.module.process.toolkit.beans.BeanMapper;
|
import cd.casic.module.process.toolkit.beans.BeanMapper;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -17,6 +18,7 @@ import java.util.List;
|
|||||||
* @Date 2025/4/28 15:42
|
* @Date 2025/4/28 15:42
|
||||||
* @Version 1.0
|
* @Version 1.0
|
||||||
*/
|
*/
|
||||||
|
@Service
|
||||||
public class PostprocessInstanceServiceImpl implements PostprocessInstanceService {
|
public class PostprocessInstanceServiceImpl implements PostprocessInstanceService {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
|
@ -10,6 +10,7 @@ import cd.casic.module.process.support.postprocess.entity.PostprocessDo;
|
|||||||
import cd.casic.module.process.support.postprocess.service.PostprocessService;
|
import cd.casic.module.process.support.postprocess.service.PostprocessService;
|
||||||
import cd.casic.module.process.toolkit.beans.BeanMapper;
|
import cd.casic.module.process.toolkit.beans.BeanMapper;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -24,6 +25,7 @@ import java.util.Objects;
|
|||||||
* @Date 2025/4/28 16:26
|
* @Date 2025/4/28 16:26
|
||||||
* @Version 1.0
|
* @Version 1.0
|
||||||
*/
|
*/
|
||||||
|
@Service
|
||||||
public class PostprocessServiceImpl implements PostprocessService {
|
public class PostprocessServiceImpl implements PostprocessService {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package cd.casic.module.process.support.trigger.dao;
|
||||||
|
|
||||||
|
import cd.casic.framework.mybatis.core.mapper.BaseMapperX;
|
||||||
|
import cd.casic.module.process.support.trigger.dateObject.Trigger;
|
||||||
|
import cd.casic.module.process.support.trigger.dateObject.TriggerQuery;
|
||||||
|
import cd.casic.module.process.support.trigger.entity.TriggerDo;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author by mianbin
|
||||||
|
* @Classname TriggerDao
|
||||||
|
* @Description TODO
|
||||||
|
* @Date 2025/4/23 20:03
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface TriggerDao extends BaseMapperX<TriggerDo> {
|
||||||
|
String createTrigger(TriggerDo triggerEntity);
|
||||||
|
|
||||||
|
void updateTrigger(TriggerDo triggerEntity);
|
||||||
|
|
||||||
|
List<TriggerDo> findTriggerList(TriggerQuery triggerQuery);
|
||||||
|
|
||||||
|
TriggerDo findOneTrigger(String triggerId);
|
||||||
|
|
||||||
|
void deleteTrigger(String triggerId);
|
||||||
|
|
||||||
|
List<Trigger> findAllTrigger();
|
||||||
|
|
||||||
|
List<Trigger> findAllTriggerList(List<String> idList);
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package cd.casic.module.process.dispatch.trigger.dao;
|
package cd.casic.module.process.support.trigger.dao;
|
||||||
|
|
||||||
import cd.casic.framework.mybatis.core.mapper.BaseMapperX;
|
import cd.casic.framework.mybatis.core.mapper.BaseMapperX;
|
||||||
import cd.casic.module.process.dispatch.trigger.dataobject.TriggerTimeDo;
|
import cd.casic.module.process.support.trigger.entity.TriggerTimeDo;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -12,4 +12,5 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
*/
|
*/
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface TriggerTimeDao extends BaseMapperX<TriggerTimeDo> {
|
public interface TriggerTimeDao extends BaseMapperX<TriggerTimeDo> {
|
||||||
|
String createTime(TriggerTimeDo triggerTimeEntity);
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package cd.casic.module.process.support.trigger.entity;
|
package cd.casic.module.process.support.trigger.dateObject;
|
||||||
|
|
||||||
|
|
||||||
import cd.casic.ci.commons.bean.process.definition.Pipeline;
|
import cd.casic.ci.commons.bean.process.definition.Pipeline;
|
||||||
@ -43,4 +43,7 @@ public class Trigger {
|
|||||||
// 状态 1--未执行 2--已执行,后面枚举
|
// 状态 1--未执行 2--已执行,后面枚举
|
||||||
private String state;
|
private String state;
|
||||||
|
|
||||||
|
@Schema(description = "任务类型")
|
||||||
|
private int taskType;
|
||||||
|
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package cd.casic.module.process.support.trigger.entity;
|
package cd.casic.module.process.support.trigger.dateObject;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
@ -1,4 +1,4 @@
|
|||||||
package cd.casic.module.process.support.trigger.entity;
|
package cd.casic.module.process.support.trigger.dateObject;
|
||||||
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
@ -1,4 +1,4 @@
|
|||||||
package cd.casic.module.process.dispatch.trigger.dataobject;
|
package cd.casic.module.process.support.trigger.entity;
|
||||||
|
|
||||||
import cd.casic.framework.mybatis.core.dataobject.BaseDO;
|
import cd.casic.framework.mybatis.core.dataobject.BaseDO;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
@ -1,4 +1,4 @@
|
|||||||
package cd.casic.module.process.dispatch.trigger.dataobject;
|
package cd.casic.module.process.support.trigger.entity;
|
||||||
|
|
||||||
import cd.casic.framework.mybatis.core.dataobject.BaseDO;
|
import cd.casic.framework.mybatis.core.dataobject.BaseDO;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
@ -0,0 +1,289 @@
|
|||||||
|
package cd.casic.module.process.support.trigger.quartz;
|
||||||
|
|
||||||
|
import cd.casic.ci.commons.bean.utils.PipelineUtil;
|
||||||
|
import cd.casic.module.process.support.trigger.service.CronUtils;
|
||||||
|
import org.quartz.CronScheduleBuilder;
|
||||||
|
import org.quartz.CronTrigger;
|
||||||
|
import org.quartz.JobBuilder;
|
||||||
|
import org.quartz.JobDataMap;
|
||||||
|
import org.quartz.JobDetail;
|
||||||
|
import org.quartz.JobExecutionContext;
|
||||||
|
import org.quartz.JobKey;
|
||||||
|
import org.quartz.Scheduler;
|
||||||
|
import org.quartz.SchedulerException;
|
||||||
|
import org.quartz.SchedulerFactory;
|
||||||
|
import org.quartz.Trigger;
|
||||||
|
import org.quartz.TriggerBuilder;
|
||||||
|
import org.quartz.TriggerKey;
|
||||||
|
import org.quartz.impl.StdSchedulerFactory;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.boot.autoconfigure.batch.BatchProperties.Job;
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName job
|
||||||
|
* @Author hopeli
|
||||||
|
* @Date 2025/4/29 09:36
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Scope("singleton")
|
||||||
|
public class job {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(Job.class);
|
||||||
|
|
||||||
|
private static final SchedulerFactory schedulerFactory = new StdSchedulerFactory();
|
||||||
|
|
||||||
|
private Scheduler scheduler = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param jobClass 执行不同的任务
|
||||||
|
* @param cron 时间设置,参考quartz说明文档
|
||||||
|
*/
|
||||||
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
|
public void addJob(String group, String pipelineId, Class jobClass, String cron,String triggerId) throws SchedulerException {
|
||||||
|
Map<String, String> map = CronUtils.cronWeek(cron);
|
||||||
|
String weekTime = map.get("weekTime");
|
||||||
|
Date date = PipelineUtil.StringChengeDate(weekTime);
|
||||||
|
if (date.getTime() <= new Date().getTime()){
|
||||||
|
logger.warn("定时任务时间已过,跳过添加:{},执行流水线id:{},执行时间:{},cron:{}",group,pipelineId, weekTime,cron);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.warn("添加定时任务,定时任务组:{},执行流水线id:{},执行时间:{},cron:{}",group,pipelineId, weekTime,cron);
|
||||||
|
|
||||||
|
// 任务名,任务组,任务执行类
|
||||||
|
Scheduler scheduler = schedulerFactory.getScheduler();
|
||||||
|
|
||||||
|
boolean isNewTrigger = false;
|
||||||
|
|
||||||
|
JobKey jobKey = JobKey.jobKey(group);
|
||||||
|
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
|
||||||
|
|
||||||
|
if (Objects.isNull(jobDetail)){
|
||||||
|
JobBuilder jobBuilder = JobBuilder.newJob(jobClass);
|
||||||
|
//添加pipelineId执行信息
|
||||||
|
JobDataMap jobDataMap = new JobDataMap();
|
||||||
|
jobDataMap.put("group",group);
|
||||||
|
jobDataMap.put("triggerId",triggerId);
|
||||||
|
jobDataMap.put("pipelineId",pipelineId);
|
||||||
|
jobDataMap.put("cron",cron);
|
||||||
|
jobDataMap.put("weekTime",map.get("weekTime"));
|
||||||
|
jobBuilder.setJobData(jobDataMap);
|
||||||
|
|
||||||
|
jobDetail = jobBuilder.withIdentity(group).build();
|
||||||
|
|
||||||
|
isNewTrigger = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
String triggerName = pipelineId + "_" + cron + "_" + triggerId;
|
||||||
|
|
||||||
|
// 添加触发器
|
||||||
|
addTrigger(scheduler,jobDetail,group,triggerName,cron,isNewTrigger);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加触发器
|
||||||
|
* @param scheduler 定时器
|
||||||
|
* @param jobDetail JobDetail
|
||||||
|
* @param state 是否为新建trigger
|
||||||
|
* @param triggerName 流水线ID
|
||||||
|
* @param cron 时间
|
||||||
|
* @throws SchedulerException 添加失败
|
||||||
|
*/
|
||||||
|
private void addTrigger( Scheduler scheduler,JobDetail jobDetail,String group,String triggerName,String cron,Boolean state) throws SchedulerException {
|
||||||
|
|
||||||
|
//触发器名
|
||||||
|
// String triggerName = pipelineId + "_" + cron;
|
||||||
|
|
||||||
|
// 触发器
|
||||||
|
TriggerBuilder<org.quartz.CronTrigger> triggerBuilder = TriggerBuilder.newTrigger()
|
||||||
|
.withIdentity(triggerName, group) // 触发器名,触发器组
|
||||||
|
.withSchedule(CronScheduleBuilder.cronSchedule(cron));// 触发器时间设定
|
||||||
|
|
||||||
|
// Job存在则指定job
|
||||||
|
if (!state){
|
||||||
|
triggerBuilder.forJob(jobDetail);
|
||||||
|
}
|
||||||
|
|
||||||
|
CronTrigger trigger = (CronTrigger) triggerBuilder.build();
|
||||||
|
|
||||||
|
// 调度容器设置JobDetail和Trigger
|
||||||
|
if (state){
|
||||||
|
scheduler.scheduleJob(jobDetail, (Trigger) trigger);
|
||||||
|
}else {
|
||||||
|
scheduler.scheduleJob((Trigger) trigger);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 启动
|
||||||
|
if (!scheduler.isShutdown()) {
|
||||||
|
scheduler.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean findTask(String triggerName ,String triggerGroupName){
|
||||||
|
try {
|
||||||
|
Scheduler scheduler = schedulerFactory.getScheduler();
|
||||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
|
||||||
|
JobKey jobKey = JobKey.jobKey(" ", " ");
|
||||||
|
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
|
||||||
|
if (trigger == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (SchedulerException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 修改一个任务的触发时间
|
||||||
|
* @param jobName
|
||||||
|
* @param jobGroupName
|
||||||
|
* @param triggerName 触发器名
|
||||||
|
* @param triggerGroupName 触发器组名
|
||||||
|
* @param cron 时间设置,参考quartz说明文档
|
||||||
|
*/
|
||||||
|
public void modifyJobTime(String jobName,String jobGroupName, String triggerName, String triggerGroupName, String cron) {
|
||||||
|
try {
|
||||||
|
Scheduler scheduler = schedulerFactory.getScheduler();
|
||||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
|
||||||
|
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
|
||||||
|
if (trigger == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String oldTime = trigger.getCronExpression();
|
||||||
|
if (!oldTime.equalsIgnoreCase(cron)) {
|
||||||
|
logger.warn("任务:"+jobName+"被修改");
|
||||||
|
/** 方式一 :调用 rescheduleJob 开始 */
|
||||||
|
/* // 触发器
|
||||||
|
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
|
||||||
|
// 触发器名,触发器组
|
||||||
|
triggerBuilder.withIdentity(triggerName, triggerGroupName);
|
||||||
|
triggerBuilder.startNow();
|
||||||
|
// 触发器时间设定
|
||||||
|
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
|
||||||
|
// 创建Trigger对象
|
||||||
|
trigger = (CronTrigger) triggerBuilder.build();
|
||||||
|
// 方式一 :修改一个任务的触发时间
|
||||||
|
scheduler.rescheduleJob(triggerKey, trigger);*/
|
||||||
|
/** 方式一 :调用 rescheduleJob 结束 */
|
||||||
|
|
||||||
|
/** 方式二:先删除,然后在创建一个新的Job */
|
||||||
|
JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(jobName, jobGroupName));
|
||||||
|
Class<? extends org.quartz.Job> jobClass = jobDetail.getJobClass();
|
||||||
|
removeJob(jobName, jobGroupName, triggerName, triggerGroupName);
|
||||||
|
// addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass,cron);
|
||||||
|
/** 方式二 :先删除,然后在创建一个新的Job */
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void removeJob(String group,String triggerName){
|
||||||
|
// String triggerName = pipelineId + "_" + cron;
|
||||||
|
try {
|
||||||
|
Scheduler scheduler = schedulerFactory.getScheduler();
|
||||||
|
// JobKey jobKey = JobKey.jobKey(group);
|
||||||
|
|
||||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,group);
|
||||||
|
scheduler.pauseTrigger(triggerKey);// 停止触发器
|
||||||
|
scheduler.unscheduleJob(triggerKey);// 移除触发器
|
||||||
|
|
||||||
|
logger.warn("移除触发器,定时任务组:{},triggerName:{}",group , triggerName );
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 移除一个任务
|
||||||
|
*
|
||||||
|
* @param jobName
|
||||||
|
* @param jobGroupName
|
||||||
|
* @param triggerName
|
||||||
|
* @param triggerGroupName
|
||||||
|
*/
|
||||||
|
public void removeJob(String jobName, String jobGroupName,String triggerName, String triggerGroupName) {
|
||||||
|
try {
|
||||||
|
Scheduler scheduler = schedulerFactory.getScheduler();
|
||||||
|
|
||||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
|
||||||
|
|
||||||
|
scheduler.pauseTrigger(triggerKey);// 停止触发器
|
||||||
|
scheduler.unscheduleJob(triggerKey);// 移除触发器
|
||||||
|
scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));// 删除任务
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description:启动所有定时任务
|
||||||
|
*/
|
||||||
|
public void startJobs() {
|
||||||
|
try {
|
||||||
|
Scheduler scheduler = schedulerFactory.getScheduler();
|
||||||
|
scheduler.start();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description:关闭所有定时任务
|
||||||
|
*/
|
||||||
|
public void shutdownJobs() {
|
||||||
|
try {
|
||||||
|
Scheduler scheduler = schedulerFactory.getScheduler();
|
||||||
|
if (!scheduler.isShutdown()) {
|
||||||
|
scheduler.shutdown();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前正在执行的任务
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean getCurrentJobs(String name){
|
||||||
|
try {
|
||||||
|
Scheduler scheduler = schedulerFactory.getScheduler();
|
||||||
|
List<JobExecutionContext> jobContexts = scheduler.getCurrentlyExecutingJobs();
|
||||||
|
for (JobExecutionContext context : jobContexts) {
|
||||||
|
if (name.equals(context.getTrigger().getJobKey().getName())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Scheduler getScheduler() {
|
||||||
|
return scheduler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScheduler(Scheduler scheduler) {
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package cd.casic.module.process.support.trigger.service;
|
package cd.casic.module.process.support.trigger.service;
|
||||||
|
|
||||||
import cd.casic.module.process.support.trigger.entity.Trigger;
|
import cd.casic.module.process.support.trigger.dateObject.Trigger;
|
||||||
import cd.casic.module.process.support.trigger.entity.TriggerQuery;
|
import cd.casic.module.process.support.trigger.dateObject.TriggerQuery;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ package cd.casic.module.process.support.trigger.service;
|
|||||||
import cd.casic.ci.commons.join.annototion.FindList;
|
import cd.casic.ci.commons.join.annototion.FindList;
|
||||||
import cd.casic.ci.commons.join.annototion.FindOne;
|
import cd.casic.ci.commons.join.annototion.FindOne;
|
||||||
import cd.casic.ci.commons.join.annototion.JoinProvider;
|
import cd.casic.ci.commons.join.annototion.JoinProvider;
|
||||||
import cd.casic.module.process.support.trigger.entity.TriggerTime;
|
import cd.casic.module.process.support.trigger.dateObject.TriggerTime;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -0,0 +1,262 @@
|
|||||||
|
package cd.casic.module.process.support.trigger.service.impl;
|
||||||
|
|
||||||
|
import cd.casic.ci.commons.bean.process.definition.Pipeline;
|
||||||
|
import cd.casic.ci.commons.bean.utils.PipelineUtil;
|
||||||
|
import cd.casic.ci.commons.rpc.annototion.Exporter;
|
||||||
|
import cd.casic.module.process.support.trigger.dao.TriggerDao;
|
||||||
|
import cd.casic.module.process.support.trigger.dateObject.Trigger;
|
||||||
|
import cd.casic.module.process.support.trigger.dateObject.TriggerQuery;
|
||||||
|
import cd.casic.module.process.support.trigger.dateObject.TriggerTime;
|
||||||
|
import cd.casic.module.process.support.trigger.entity.TriggerDo;
|
||||||
|
import cd.casic.module.process.support.trigger.service.CronUtils;
|
||||||
|
import cd.casic.module.process.support.trigger.service.TriggerService;
|
||||||
|
import cd.casic.module.process.support.trigger.service.TriggerTimeService;
|
||||||
|
import cd.casic.module.process.toolkit.beans.BeanMapper;
|
||||||
|
import cd.casic.module.process.toolkit.join.JoinTemplate;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName TriggerServiceImpl
|
||||||
|
* @Author hopeli
|
||||||
|
* @Date 2025/4/28 16:41
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Exporter
|
||||||
|
public class TriggerServiceImpl implements TriggerService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
TriggerDao triggerDao;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
JoinTemplate joinTemplate;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
TriggerTimeService timeServer;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建配置及任务
|
||||||
|
* @param trigger 配置
|
||||||
|
* @return 配置id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String createTrigger(Trigger trigger) {
|
||||||
|
trigger.setCreateTime(PipelineUtil.date(1));
|
||||||
|
String triggerId = createTriggerConfig(trigger);
|
||||||
|
int taskType = trigger.getTaskType();
|
||||||
|
String pipelineId = trigger.getPipeline().getId();
|
||||||
|
if (taskType == 81){
|
||||||
|
String object = JSON.toJSONString(trigger.getValues());
|
||||||
|
TriggerTime triggerTime = JSON.parseObject(object, TriggerTime.class);
|
||||||
|
List<Integer> timeList = triggerTime.getTimeList();
|
||||||
|
for (Integer integer : timeList) {
|
||||||
|
triggerTime.setDayTime(integer);
|
||||||
|
triggerTime.setTriggerId(triggerId);
|
||||||
|
timeServer.createTriggerTime(triggerTime,pipelineId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return triggerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Object> findAllTrigger(TriggerQuery triggerQuery){
|
||||||
|
List<Trigger> triggerList = findTriggerList(triggerQuery);
|
||||||
|
if (triggerList.isEmpty()){
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
List<TriggerTime> triggerTimeList = new ArrayList<>();
|
||||||
|
for (Trigger trigger : triggerList) {
|
||||||
|
String triggerId = trigger.getTriggerId();
|
||||||
|
TriggerTime triggerTime = timeServer.findTriggerTime(triggerId);
|
||||||
|
if (triggerTime == null){
|
||||||
|
deleteTrigger(trigger.getTriggerId());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int taskType = trigger.getTaskType();
|
||||||
|
triggerTime.setType(taskType);
|
||||||
|
triggerTime.setState(trigger.getState());
|
||||||
|
triggerTimeList.add(triggerTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
triggerTimeList.sort(Comparator.comparing(TriggerTime::getWeekTime));
|
||||||
|
|
||||||
|
return new ArrayList<>(triggerTimeList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cloneTrigger(String pipelineId,String clonePipelineId){
|
||||||
|
TriggerQuery triggerQuery = new TriggerQuery(null,0,null,null);
|
||||||
|
triggerQuery.setPipelineId(pipelineId);
|
||||||
|
List<Trigger> allTrigger = findTriggerList(triggerQuery);
|
||||||
|
if (allTrigger.isEmpty()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (Trigger trigger : allTrigger) {
|
||||||
|
String triggerId = trigger.getTriggerId();
|
||||||
|
trigger.setPipeline(new Pipeline(clonePipelineId));
|
||||||
|
TriggerDo triggerEntity = BeanMapper.map(trigger, TriggerDo.class);
|
||||||
|
String triggerEntityId = triggerDao.createTrigger(triggerEntity);
|
||||||
|
|
||||||
|
TriggerTime triggerTime = timeServer.findTriggerTime(triggerId);
|
||||||
|
List<Integer> timeList = triggerTime.getTimeList();
|
||||||
|
for (Integer integer : timeList) {
|
||||||
|
triggerTime.setDayTime(integer);
|
||||||
|
triggerTime.setTriggerId(triggerEntityId);
|
||||||
|
timeServer.createTriggerTime(triggerTime,clonePipelineId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除流水线所有定时任务
|
||||||
|
* @param pipelineId 流水线id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void deleteAllTrigger(String pipelineId){
|
||||||
|
TriggerQuery triggerQuery = new TriggerQuery(null,0,null,null);
|
||||||
|
triggerQuery.setPipelineId(pipelineId);
|
||||||
|
List<Trigger> allTriggerConfig = findTriggerList(triggerQuery);
|
||||||
|
if ( allTriggerConfig.isEmpty()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (Trigger trigger : allTriggerConfig) {
|
||||||
|
String triggerId = trigger.getTriggerId();
|
||||||
|
deleteTrigger(triggerId);
|
||||||
|
timeServer.deleteAllTime(triggerId,pipelineId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新定时任务
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void updateTrigger(String triggerId){
|
||||||
|
|
||||||
|
// List<TriggerTime> triggerTimeList = timeServer.fondCronTimeList(cron);
|
||||||
|
// if (triggerTimeList.isEmpty()){
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// List<TriggerTime> timeList = triggerTimeList.stream()
|
||||||
|
// .filter(triggerTime -> {
|
||||||
|
// String triggerId = triggerTime.getTriggerId();
|
||||||
|
// Trigger trigger = findOneTriggerById(triggerId);
|
||||||
|
// String id = trigger.getPipeline().getId();
|
||||||
|
// return id.equals(pipelineId);
|
||||||
|
// }).toList();
|
||||||
|
//
|
||||||
|
// if (timeList.isEmpty()){
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// String triggerId = timeList.get(0).getTriggerId();
|
||||||
|
|
||||||
|
boolean b = true;
|
||||||
|
List<TriggerTime> allTriggerTime = timeServer.findAllTriggerTime(triggerId);
|
||||||
|
for (TriggerTime time : allTriggerTime) {
|
||||||
|
String weekTime = CronUtils.weekTime(time.getCron());
|
||||||
|
Date date = PipelineUtil.StringChengeDate(weekTime);
|
||||||
|
|
||||||
|
if (date.getTime() < new Date().getTime()){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
b = false;
|
||||||
|
}
|
||||||
|
if (b){
|
||||||
|
Trigger trigger = findOneTriggerById(triggerId);
|
||||||
|
System.out.println("更新状态:"+trigger.getState() + " 更新ID:" + trigger.getTriggerId());
|
||||||
|
TriggerDo triggerEntity = BeanMapper.map(trigger.setState("2"), TriggerDo.class);
|
||||||
|
triggerDao.updateTrigger(triggerEntity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateTrigger(Trigger trigger){
|
||||||
|
String triggerId = trigger.getTriggerId();
|
||||||
|
if (triggerId == null){
|
||||||
|
triggerId = createTrigger(trigger);
|
||||||
|
}
|
||||||
|
trigger.setTriggerId(triggerId);
|
||||||
|
|
||||||
|
int taskType = trigger.getTaskType();
|
||||||
|
Pipeline pipeline = trigger.getPipeline();
|
||||||
|
String pipelineId = pipeline.getId();
|
||||||
|
String object = JSON.toJSONString(trigger.getValues());
|
||||||
|
if (taskType == 81){
|
||||||
|
TriggerTime triggerTime = JSON.parseObject(object, TriggerTime.class);
|
||||||
|
if (Objects.isNull(triggerTime)){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
triggerTime.setTriggerId(triggerId);
|
||||||
|
timeServer.deleteAllTime(triggerId,pipelineId);
|
||||||
|
|
||||||
|
List<Integer> timeList = triggerTime.getTimeList();
|
||||||
|
for (Integer integer : timeList) {
|
||||||
|
triggerTime.setDayTime(integer);
|
||||||
|
timeServer.createTriggerTime(triggerTime,pipelineId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据流水线id查询触发器配置
|
||||||
|
* @param triggerQuery 条件
|
||||||
|
* @return 配置
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<Trigger> findTriggerList(TriggerQuery triggerQuery) {
|
||||||
|
List<TriggerDo> triggerEntityList = triggerDao.findTriggerList(triggerQuery);
|
||||||
|
|
||||||
|
if ( triggerEntityList.isEmpty()){
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return BeanMapper.mapList(triggerEntityList, Trigger.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
//创建
|
||||||
|
public String createTriggerConfig(Trigger trigger){
|
||||||
|
TriggerDo configEntity = BeanMapper.map(trigger, TriggerDo.class);
|
||||||
|
return triggerDao.createTrigger(configEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Trigger findOneTriggerById(String triggerId) {
|
||||||
|
TriggerDo triggerConfigEntity = triggerDao.findOneTrigger(triggerId);
|
||||||
|
Trigger trigger = BeanMapper.map(triggerConfigEntity, Trigger.class);
|
||||||
|
joinTemplate.joinQuery(trigger);
|
||||||
|
return trigger;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteTrigger(String triggerId) {
|
||||||
|
Trigger trigger = findOneTriggerById(triggerId);
|
||||||
|
String pipelineId = trigger.getPipeline().getId();
|
||||||
|
timeServer.deleteAllTime(triggerId,pipelineId);
|
||||||
|
triggerDao.deleteTrigger(triggerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<Trigger> findAllTrigger() {
|
||||||
|
return BeanMapper.mapList(triggerDao.findAllTrigger(), Trigger.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Trigger> findAllTriggerConfigList(List<String> idList) {
|
||||||
|
return BeanMapper.mapList(triggerDao.findAllTriggerList(idList), Trigger.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,223 @@
|
|||||||
|
package cd.casic.module.process.support.trigger.service.impl;
|
||||||
|
|
||||||
|
import cd.casic.ci.commons.bean.exception.ApplicationException;
|
||||||
|
import cd.casic.ci.commons.rpc.annototion.Exporter;
|
||||||
|
import cd.casic.module.process.support.trigger.dao.TriggerTimeDao;
|
||||||
|
import cd.casic.module.process.support.trigger.dateObject.TriggerTime;
|
||||||
|
import cd.casic.module.process.support.trigger.entity.TriggerTimeDo;
|
||||||
|
import cd.casic.module.process.support.trigger.service.CronUtils;
|
||||||
|
import cd.casic.module.process.support.trigger.service.TriggerTimeService;
|
||||||
|
import cd.casic.module.process.toolkit.beans.BeanMapper;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.boot.autoconfigure.batch.BatchProperties.Job;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName TriggerTimeServiceImpl
|
||||||
|
* @Author hopeli
|
||||||
|
* @Date 2025/4/28 17:24
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Exporter
|
||||||
|
public class TriggerTimeServiceImpl implements TriggerTimeService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
TriggerTimeDao triggerTimeDao;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
Job manager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String createTriggerTime(TriggerTime triggerTime, String pipelineId){
|
||||||
|
List<Integer> timeList = triggerTime.getTimeList();
|
||||||
|
if (timeList == null || timeList.isEmpty()){
|
||||||
|
throw new ApplicationException(50001,"无法获取到执行时间");
|
||||||
|
}
|
||||||
|
String time = triggerTime.getTime();
|
||||||
|
triggerTime.setDate(triggerTime.getDayTime());
|
||||||
|
String cron = CronUtils.weekCron(time, triggerTime.getDayTime());
|
||||||
|
triggerTime.setCron(cron);
|
||||||
|
|
||||||
|
String triggerId = triggerTime.getTriggerId();
|
||||||
|
|
||||||
|
TriggerTimeDo triggerTimeEntity = BeanMapper.map(triggerTime, TriggerTimeDo.class);
|
||||||
|
String timeId = triggerTimeDao.createTime(triggerTimeEntity);
|
||||||
|
|
||||||
|
try {
|
||||||
|
manager.addJob(DEFAULT,pipelineId, RunJob.class,cron,triggerId);
|
||||||
|
} catch (SchedulerException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new ApplicationException(50001,"当前时间已经添加过,无需重复添加。");
|
||||||
|
}
|
||||||
|
return timeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TriggerTime findTriggerTime(String configId){
|
||||||
|
List<TriggerTime> allTriggerTime = findAllTriggerTime(configId);
|
||||||
|
if (allTriggerTime == null || allTriggerTime.isEmpty()){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//获取时间
|
||||||
|
List<Integer> allDataConfig = findAllDataConfig(configId);
|
||||||
|
|
||||||
|
StringBuilder execTime = new StringBuilder();
|
||||||
|
for (Integer integer : allDataConfig) {
|
||||||
|
TriggerTime triggerTime = findOneConfig(configId, integer);
|
||||||
|
triggerTime.setTimeList(allDataConfig);
|
||||||
|
Map<String, String> map = CronUtils.cronWeek(triggerTime.getCron());
|
||||||
|
execTime.append(map.get("cron")).append(" | ");
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer integer = allDataConfig.get(0);
|
||||||
|
TriggerTime triggerTime = findOneConfig(configId, integer);
|
||||||
|
triggerTime.setTimeList(allDataConfig);
|
||||||
|
//获取具体时间
|
||||||
|
Map<String, String> map = CronUtils.cronWeek(triggerTime.getCron());
|
||||||
|
|
||||||
|
triggerTime.setExecTime(execTime + map.get("time"));
|
||||||
|
triggerTime.setTime(map.get("time"));
|
||||||
|
triggerTime.setWeekTime(map.get("weekTime"));
|
||||||
|
|
||||||
|
return triggerTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<TriggerTime> findAllTriggerTime(String configId){
|
||||||
|
List<TriggerTime> allTriggerTime = findAllTime();
|
||||||
|
if (allTriggerTime == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
List<TriggerTime> list = new ArrayList<>();
|
||||||
|
for (TriggerTime triggerTime : allTriggerTime) {
|
||||||
|
if (triggerTime.getTriggerId().equals(configId)){
|
||||||
|
list.add(triggerTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TriggerTime findOneConfig(String configId, int day){
|
||||||
|
List<TriggerTime> allTriggerTime = findAllTime();
|
||||||
|
if (allTriggerTime == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (TriggerTime triggerTime : allTriggerTime) {
|
||||||
|
String timeConfigId = triggerTime.getTriggerId();
|
||||||
|
int date = triggerTime.getDate();
|
||||||
|
if (timeConfigId.equals(configId) && date == day){
|
||||||
|
return triggerTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<TriggerTime> fondCronTimeList(String cron){
|
||||||
|
List<TriggerTime> allTriggerTime = findAllTime();
|
||||||
|
if (allTriggerTime == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<TriggerTime> triggerTimeList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (TriggerTime triggerTime : allTriggerTime) {
|
||||||
|
if (!triggerTime.getCron().equals(cron)){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
triggerTimeList.add(triggerTime);
|
||||||
|
}
|
||||||
|
return triggerTimeList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Integer> findAllDataConfig(String configId){
|
||||||
|
List<TriggerTime> allTriggerTime = findAllTriggerTime(configId);
|
||||||
|
if (allTriggerTime == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
List<Integer> list = new ArrayList<>();
|
||||||
|
for (TriggerTime triggerTime : allTriggerTime) {
|
||||||
|
String s = CronUtils.weekTime(triggerTime.getCron());
|
||||||
|
triggerTime.setTime(s);
|
||||||
|
}
|
||||||
|
allTriggerTime.sort(Comparator.comparing(TriggerTime::getTime));
|
||||||
|
|
||||||
|
for (TriggerTime triggerTime : allTriggerTime) {
|
||||||
|
list.add(triggerTime.getDate());
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteAllTime(String triggerId,String pipelineId){
|
||||||
|
List<TriggerTime> triggerTimeConfig = findAllTriggerTime(triggerId);
|
||||||
|
if (triggerTimeConfig == null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (TriggerTime triggerTime : triggerTimeConfig) {
|
||||||
|
String cron = triggerTime.getCron();
|
||||||
|
String triggerName = pipelineId + "_" + cron + "_" + triggerId;
|
||||||
|
manager.removeJob(DEFAULT,triggerName);
|
||||||
|
deleteTime(triggerTime.getTimeId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean deleteCronTime(String pipelineId,String timeId){
|
||||||
|
TriggerTime oneTriggerTime = findOneTime(timeId);
|
||||||
|
if (oneTriggerTime.getTaskType() == 1){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
String cron = oneTriggerTime.getCron();
|
||||||
|
String triggerId = oneTriggerTime.getTriggerId();
|
||||||
|
String[] s = cron.split(" ");
|
||||||
|
String time = s[2] + ":" + s[1];
|
||||||
|
int date = oneTriggerTime.getDate();
|
||||||
|
String weekCron = CronUtils.weekCron(time, date);
|
||||||
|
oneTriggerTime.setCron(weekCron);
|
||||||
|
try {
|
||||||
|
manager.addJob(DEFAULT,pipelineId, RunJob.class,weekCron,triggerId);
|
||||||
|
} catch (SchedulerException e) {
|
||||||
|
throw new ApplicationException(50001,"当前时间已经添加过,无需重复添加。");
|
||||||
|
}
|
||||||
|
updateTime(oneTriggerTime);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteTime(String timeId) {
|
||||||
|
triggerTimeDao.deleteTime(timeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateTime(TriggerTime triggerTime) {
|
||||||
|
TriggerTimeEntity triggerTimeEntity = BeanMapper.map(triggerTime, TriggerTimeEntity.class);
|
||||||
|
triggerTimeDao.updateTime(triggerTimeEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TriggerTime findOneTime(String timeId) {
|
||||||
|
TriggerTimeEntity timeEntity = triggerTimeDao.findOneTime(timeId);
|
||||||
|
return BeanMapper.map(timeEntity, TriggerTime.class);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<TriggerTime> findAllTime() {
|
||||||
|
return BeanMapper.mapList(triggerTimeDao.findAllTime(), TriggerTime.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<TriggerTime> findAllTimeList(List<String> idList) {
|
||||||
|
return BeanMapper.mapList(triggerTimeDao.findAllTimeList(idList), TriggerTime.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -28,4 +28,14 @@
|
|||||||
2. 研发工具,提升研发效率与质量。 例如:接口文档等等
|
2. 研发工具,提升研发效率与质量。 例如:接口文档等等
|
||||||
</description>
|
</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.quartz-scheduler</groupId>
|
||||||
|
<artifactId>quartz</artifactId>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user