worker common 模块
This commit is contained in:
parent
ee9ac3b7b4
commit
0924444849
@ -0,0 +1,44 @@
|
||||
package cd.casic.devops.common.worker.task;
|
||||
|
||||
import cd.casic.ci.process.api.engine.common.Timeout;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public final class TaskExecutorCache {
|
||||
|
||||
private static final TaskExecutorCache INSTANCE = new TaskExecutorCache();
|
||||
|
||||
// Caffeine 缓存
|
||||
private final Cache<String, ExecutorService> taskExecutorCache = Caffeine.newBuilder()
|
||||
.maximumSize(50)
|
||||
.expireAfterWrite(Timeout.MAX_JOB_RUN_DAYS, TimeUnit.DAYS)
|
||||
.build();
|
||||
|
||||
// 私有构造函数
|
||||
private TaskExecutorCache() {}
|
||||
|
||||
// 获取单例实例
|
||||
public static TaskExecutorCache getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public void invalidate(String taskId) {
|
||||
taskExecutorCache.invalidate(taskId);
|
||||
}
|
||||
|
||||
public void put(String taskId, ExecutorService executor) {
|
||||
taskExecutorCache.put(taskId, executor);
|
||||
}
|
||||
|
||||
public ExecutorService getIfPresent(String taskId) {
|
||||
return taskExecutorCache.getIfPresent(taskId);
|
||||
}
|
||||
|
||||
public Map<String, ExecutorService> getAllPresent(Set<String> taskIds) {
|
||||
return taskExecutorCache.getAllPresent(taskIds);
|
||||
}
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
package cd.casic.devops.common.worker.task;
|
||||
|
||||
import cd.casic.ci.common.pipeline.pojo.element.agent.LinuxScriptElement;
|
||||
import cd.casic.ci.common.pipeline.pojo.element.agent.WindowsScriptElement;
|
||||
import cd.casic.ci.common.pipeline.pojo.element.market.MarketBuildAtomElement;
|
||||
import cd.casic.ci.common.pipeline.pojo.element.market.MarketBuildLessAtomElement;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class TaskFactory {
|
||||
// 单例实例
|
||||
private static final TaskFactory INSTANCE = new TaskFactory();
|
||||
|
||||
// 任务类型映射表
|
||||
private final Map<String, Class<? extends ITask>> taskMap = new ConcurrentHashMap<>();
|
||||
|
||||
// 私有构造方法
|
||||
private TaskFactory() {}
|
||||
|
||||
// 获取单例实例
|
||||
public static TaskFactory getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public void init() {
|
||||
// 注册基础任务类型
|
||||
register(LinuxScriptElement.classType, LinuxScriptTask.class);
|
||||
register(WindowsScriptElement.classType, WindowsScriptTask.class);
|
||||
register(MarketBuildAtomElement.classType, MarketAtomTask.class);
|
||||
register(MarketBuildLessAtomElement.classType, MarketAtomTask.class);
|
||||
|
||||
// 使用反射扫描任务类
|
||||
Reflections reflections = new Reflections("com.tencent.devops.plugin.worker.task");
|
||||
Set<Class<? extends ITask>> taskClasses = reflections.getSubTypesOf(ITask.class);
|
||||
|
||||
Map<String, Integer> candidatePriorityMap = new HashMap<>();
|
||||
Map<String, Class<? extends ITask>> candidateMap = new HashMap<>();
|
||||
|
||||
if (taskClasses != null) {
|
||||
for (Class<? extends ITask> taskClazz : taskClasses) {
|
||||
if (!Modifier.isAbstract(taskClazz.getModifiers())) {
|
||||
TaskClassType taskClassType = taskClazz.getAnnotation(TaskClassType.class);
|
||||
if (taskClassType != null) {
|
||||
for (String classType : taskClassType.classTypes()) {
|
||||
boolean find = false;
|
||||
Integer priority = candidatePriorityMap.get(classType);
|
||||
|
||||
if (taskClassType.priority() > (priority != null ? priority : 0)) {
|
||||
priority = taskClassType.priority();
|
||||
find = true;
|
||||
}
|
||||
|
||||
if (priority == null || find) {
|
||||
candidatePriorityMap.put(classType, priority != null ? priority : 0);
|
||||
candidateMap.put(classType, taskClazz);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 注册扫描到的任务类
|
||||
for (Map.Entry<String, Class<? extends ITask>> entry : candidateMap.entrySet()) {
|
||||
register(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 注册任务类型
|
||||
private void register(String classType, Class<? extends ITask> taskClass) {
|
||||
taskMap.put(classType, taskClass);
|
||||
}
|
||||
|
||||
// 创建任务实例
|
||||
public ITask create(String type) {
|
||||
Class<? extends ITask> clazz = taskMap.get(type);
|
||||
if (clazz == null) {
|
||||
return new EmptyTask(type);
|
||||
}
|
||||
|
||||
try {
|
||||
// 尝试获取无参构造器
|
||||
java.lang.reflect.Constructor<? extends ITask> ctor = clazz.getDeclaredConstructor();
|
||||
if (ctor != null) {
|
||||
return ctor.newInstance();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 构造失败时返回EmptyTask
|
||||
}
|
||||
return new EmptyTask(type);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user