大声道撒旦

This commit is contained in:
dengqichen 2025-01-09 15:26:05 +08:00
parent 0f74f2e646
commit 285d7e5189
2 changed files with 65 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.core.task.AsyncTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
@ -42,4 +43,51 @@ public class ThreadPoolConfig {
executor.initialize(); executor.initialize();
return executor; 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;
// }
} }

View File

@ -19,13 +19,13 @@ import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.ArrayList;
/** /**
* Git仓库项目服务实现 * Git仓库项目服务实现
@ -83,6 +83,7 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
// 4. 用于跟踪已处理的项目ID // 4. 用于跟踪已处理的项目ID
Set<Long> processedProjectIds = new HashSet<>(); Set<Long> processedProjectIds = new HashSet<>();
int totalCount = 0; int totalCount = 0;
List<RepositoryProject> projectsToSave = new ArrayList<>();
// 5. 遍历每个组同步项目 // 5. 遍历每个组同步项目
for (RepositoryGroup group : groups) { for (RepositoryGroup group : groups) {
@ -126,10 +127,24 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
project.setPathWithNamespace(projectResponse.getPathWithNamespace()); project.setPathWithNamespace(projectResponse.getPathWithNamespace());
project.setCreatedAt(projectResponse.getCreatedAt()); project.setCreatedAt(projectResponse.getCreatedAt());
repositoryProjectRepository.save(project); projectsToSave.add(project);
existingProjects.remove(projectResponse.getId()); existingProjects.remove(projectResponse.getId());
// 每100个项目批量保存一次避免内存占用过大
if (projectsToSave.size() >= 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. 删除不存在的项目 // 6. 删除不存在的项目
if (!existingProjects.isEmpty()) { if (!existingProjects.isEmpty()) {