Skip to content

Commit

Permalink
refactor: fix-thread-executor (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicheng-ning authored Apr 20, 2023
1 parent a76d3e1 commit e807603
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/main/java/cn/nzcer/odapi/cron/SyncDataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,16 @@ public Set<String> parseRepo(String url, int count) throws IOException {
// 每月 3 日凌晨 1 点启动定时任务更新仓库的所有指标数据
@Scheduled(cron = "0 0 1 3 * ?")
public void insertAllRepoMetrics() throws IOException, BrokenBarrierException, InterruptedException {
// RepoMetrics 线程池
ThreadPoolExecutor executor = new ThreadPoolExecutor(20,
100,
10,
TimeUnit.MICROSECONDS,
new LinkedBlockingQueue<Runnable>());
log.info("Repo Metrics 定时任务启动:" + new Date());
log.info("清空 repo_metric 表");
repoMetricService.truncateRepoMetric();
Set<String> allRepo = getAllRepo();
log.info("获取到的 repo 数量为: " + allRepo.size());
List<String> tokens = getTokens();
List<String> errorRepos = new ArrayList<>();
CountDownLatch countDownLatch = new CountDownLatch(allRepo.size());
Expand Down Expand Up @@ -171,15 +176,10 @@ public void insertAllRepoMetrics() throws IOException, BrokenBarrierException, I
writer.write(str + System.lineSeparator());
}
writer.close();
executor.shutdown();
}


// 自定义线程池
private static ThreadPoolExecutor executor = new ThreadPoolExecutor(20,
100,
10,
TimeUnit.MICROSECONDS,
new LinkedBlockingQueue<Runnable>());

/**
* 插入所有仓库的 star 和 fork 数据
Expand All @@ -188,11 +188,16 @@ public void insertAllRepoMetrics() throws IOException, BrokenBarrierException, I
*/
@Scheduled(cron = "0 0 5 * * ?")
public void insertAllRepoStarAndFork() throws InterruptedException, BrokenBarrierException {
// RepoStarAndFork 线程池
ThreadPoolExecutor executor = new ThreadPoolExecutor(20,
100,
10,
TimeUnit.MICROSECONDS,
new LinkedBlockingQueue<Runnable>());
log.info("Repo Statistic 定时任务启动:" + new Date());
log.info("清空 repo_statistic 表");
repoStatisticService.truncateRepoStatistic();
List<Map<String, String>> repoInfo = repoMetricService.getRepoInfo();
log.info("仓库的数量: " + String.valueOf(repoInfo.size()));
List<String> tokens = getTokens();
CountDownLatch countDownLatch = new CountDownLatch(repoInfo.size());
int totalRepo = 0;
Expand All @@ -214,6 +219,7 @@ public void insertAllRepoStarAndFork() throws InterruptedException, BrokenBarrie
log.info("仓库的数量: " + String.valueOf(repoInfo.size()));
log.info("已执行完的仓库数量:" + executor.getCompletedTaskCount());
log.info("所有子线程执行完毕");
executor.shutdown();
}

/**
Expand Down

0 comments on commit e807603

Please sign in to comment.