From 285d7e5189cc2468205f3bbd6a88dd6b2162b294 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Thu, 9 Jan 2025 15:26:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=A3=B0=E9=81=93=E6=92=92=E6=97=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../deploy/config/ThreadPoolConfig.java | 48 +++++++++++++++++++ .../impl/RepositoryProjectServiceImpl.java | 19 +++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/config/ThreadPoolConfig.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/config/ThreadPoolConfig.java index 94038f37..42eadc51 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/config/ThreadPoolConfig.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/config/ThreadPoolConfig.java @@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.core.task.AsyncTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; @@ -42,4 +43,51 @@ public class ThreadPoolConfig { executor.initialize(); return executor; } + +// @Bean("repositorySyncExecutor") +// public ThreadPoolTaskExecutor repositorySyncExecutor() { +// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); +// +// // 核心线程数:CPU核心数 * 2 +// executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2); +// +// // 最大线程数:CPU核心数 * 4 +// executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 4); +// +// // 队列容量:5000 +// executor.setQueueCapacity(5000); +// +// // 线程名前缀 +// executor.setThreadNamePrefix("repository-sync-"); +// +// // 线程空闲时间:60秒 +// executor.setKeepAliveSeconds(60); +// +// // 拒绝策略:由调用线程处理 +// executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); +// +// // 等待所有任务完成再关闭线程池 +// executor.setWaitForTasksToCompleteOnShutdown(true); +// +// // 等待时间(秒) +// executor.setAwaitTerminationSeconds(60); +// +// executor.initialize(); +// return executor; +// } +// +// @Bean("applicationTaskExecutor") +// public AsyncTaskExecutor applicationTaskExecutor() { +// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); +// executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2); +// executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 4); +// executor.setQueueCapacity(500); +// executor.setThreadNamePrefix("application-task-"); +// executor.setKeepAliveSeconds(60); +// executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); +// executor.setWaitForTasksToCompleteOnShutdown(true); +// executor.setAwaitTerminationSeconds(60); +// executor.initialize(); +// return executor; +// } } \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryProjectServiceImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryProjectServiceImpl.java index 778ee8a4..4984cd9c 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryProjectServiceImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryProjectServiceImpl.java @@ -19,13 +19,13 @@ import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; import java.util.HashSet; import java.util.Set; +import java.util.ArrayList; /** * Git仓库项目服务实现 @@ -83,6 +83,7 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl processedProjectIds = new HashSet<>(); int totalCount = 0; + List projectsToSave = new ArrayList<>(); // 5. 遍历每个组,同步项目 for (RepositoryGroup group : groups) { @@ -126,11 +127,25 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl= 100) { + log.info("Batch saving {} projects", projectsToSave.size()); + repositoryProjectRepository.saveAll(projectsToSave); + projectsToSave.clear(); + } } } + // 保存剩余的项目 + if (!projectsToSave.isEmpty()) { + log.info("Batch saving remaining {} projects", projectsToSave.size()); + repositoryProjectRepository.saveAll(projectsToSave); + projectsToSave.clear(); + } + // 6. 删除不存在的项目 if (!existingProjects.isEmpty()) { List toDeleteIds = existingProjects.values().stream()