From 11b42d7e09d7c9c318155aa6495ec704628ec761 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 7 Feb 2023 23:29:13 +0400 Subject: [PATCH] #81 -- Start indexing on failed tasks --- .../extractor/GitExtractorApplication.java | 17 +++++++++++ .../branch/repository/BranchRepository.java | 3 ++ .../branch/service/BranchService.java | 16 +++++++++++ .../gitrepository/service/IndexService.java | 28 +++++++++++++++++++ .../resources/templates/listBranches.html | 2 +- 5 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java b/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java index f9a272d..1cf7928 100644 --- a/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java +++ b/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java @@ -2,12 +2,29 @@ package ru.ulstu.extractor; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; +import ru.ulstu.extractor.gitrepository.service.IndexService; @SpringBootApplication @EnableScheduling +@EnableAsync public class GitExtractorApplication { + private final IndexService indexService; + + public GitExtractorApplication(IndexService indexService) { + this.indexService = indexService; + } + public static void main(String[] args) { SpringApplication.run(GitExtractorApplication.class, args); } + + @EventListener(ApplicationReadyEvent.class) + public void doSomethingAfterStartup() { + indexService.indexFailedBranchesOnStart(); + } + } diff --git a/src/main/java/ru/ulstu/extractor/branch/repository/BranchRepository.java b/src/main/java/ru/ulstu/extractor/branch/repository/BranchRepository.java index af14f53..5a8b1f6 100644 --- a/src/main/java/ru/ulstu/extractor/branch/repository/BranchRepository.java +++ b/src/main/java/ru/ulstu/extractor/branch/repository/BranchRepository.java @@ -5,6 +5,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import ru.ulstu.extractor.branch.model.Branch; +import ru.ulstu.extractor.branch.model.IndexingStatus; import ru.ulstu.extractor.gitrepository.model.GitRepository; import java.util.List; @@ -18,4 +19,6 @@ public interface BranchRepository extends JpaRepository { List findByGitRepositoryId(Integer repositoryId); Page findByGitRepository(GitRepository gitRepository, Pageable pageable); + + List findAllByIndexingStatus(IndexingStatus indexingStatus); } diff --git a/src/main/java/ru/ulstu/extractor/branch/service/BranchService.java b/src/main/java/ru/ulstu/extractor/branch/service/BranchService.java index ce37a85..0a11945 100644 --- a/src/main/java/ru/ulstu/extractor/branch/service/BranchService.java +++ b/src/main/java/ru/ulstu/extractor/branch/service/BranchService.java @@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.extractor.branch.model.Branch; +import ru.ulstu.extractor.branch.model.IndexingStatus; import ru.ulstu.extractor.branch.repository.BranchRepository; import ru.ulstu.extractor.commit.model.Commit; import ru.ulstu.extractor.commit.service.CommitService; @@ -32,6 +33,11 @@ public class BranchService { this.commitService = commitService; } + @Transactional + public Branch save(Branch branch) { + return branchRepository.save(branch); + } + @Transactional public Branch save(Branch branch, List commits) { LOG.debug("Start save {} branch with {} commits ", branch.getName(), commits.size()); @@ -63,4 +69,14 @@ public class BranchService { public List findAll() { return branchRepository.findAll(); } + + public List findAllByIndexingStatus(IndexingStatus indexingStatus) { + return branchRepository.findAllByIndexingStatus(indexingStatus); + } + + @Transactional + public Branch updateStatus(Branch branch, IndexingStatus indexingStatus) { + branch.setIndexingStatus(indexingStatus); + return branchRepository.save(branch); + } } diff --git a/src/main/java/ru/ulstu/extractor/gitrepository/service/IndexService.java b/src/main/java/ru/ulstu/extractor/gitrepository/service/IndexService.java index b510338..a442286 100644 --- a/src/main/java/ru/ulstu/extractor/gitrepository/service/IndexService.java +++ b/src/main/java/ru/ulstu/extractor/gitrepository/service/IndexService.java @@ -9,8 +9,11 @@ import com.sun.istack.NotNull; import org.eclipse.jgit.api.errors.GitAPIException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import ru.ulstu.extractor.branch.model.Branch; +import ru.ulstu.extractor.branch.model.IndexingStatus; import ru.ulstu.extractor.branch.service.BranchService; import ru.ulstu.extractor.commit.model.Commit; import ru.ulstu.extractor.gitrepository.model.GitRepository; @@ -55,6 +58,7 @@ public class IndexService { branch = new Branch(gitRepository, branchName); } branchService.save(branch, Collections.emptyList()); + branch = branchService.updateStatus(branch, IndexingStatus.INDEXING); int commitsFrom = 0; int commitsTo = COMMITS_PAGE_SIZE; List commits = gitRepositoryService.getCommits(repositoryUrl, branchName, commitsFrom, commitsTo, true); @@ -69,6 +73,30 @@ public class IndexService { Integer repositoryId = gitRepository.getId(); final Branch branchForSave = branch; timeSeriesCreators.forEach(tsCreator -> tsCreator.addTimeSeries(repositoryId, branchForSave)); + branchService.updateStatus(branch, IndexingStatus.FINISHED); LOG.debug("Complete indexing {} branch", branchName); } + + @Transactional + @Async + public void indexFailedBranchesOnStart() { + LOG.info("Старт проверки незавершенных задач для индексирований..."); + + List failedBranches = branchService.findAllByIndexingStatus(IndexingStatus.INDEXING); + if (failedBranches.size() > 0) { + LOG.info("Найдено {} незавершенных задач для индексирования", failedBranches.size()); + failedBranches.forEach(failedBranch -> { + try { + index(failedBranch.getId()); + LOG.info("Завершено индексирование ветки {}", failedBranch.getName()); + branchService.updateStatus(failedBranch, IndexingStatus.FINISHED); + } catch (Exception ex) { + LOG.warn(ex.getMessage()); + } + }); + LOG.info("Завершено индексирование незавершенных задач"); + } else { + LOG.info("Не найдено незавершенных веток для индексирования"); + } + } } diff --git a/src/main/resources/templates/listBranches.html b/src/main/resources/templates/listBranches.html index 2ecdaae..a9cb074 100644 --- a/src/main/resources/templates/listBranches.html +++ b/src/main/resources/templates/listBranches.html @@ -30,7 +30,7 @@ Индексируется... -