From 0980b05925f154dc93ec228e3e58a061b6d4fa60 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Tue, 20 Dec 2022 20:21:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D0=BE?= =?UTF-8?q?=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D1=83=20=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D1=80=D1=8B=D1=85=20=D0=BF=D0=B0=D0=B9=D0=BF=D0=BB=D0=B0?= =?UTF-8?q?=D0=B9=D0=BD=D0=BE=D0=B2=20=D1=81=20=D0=B8=D1=81=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=D0=BC=20?= =?UTF-8?q?ForkJoinPool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/service/PipelineService.java | 3 +- .../service/impl/PipelineServiceImpl.java | 7 +++ .../core/service/parser/PipelineParser.java | 54 +++++++++---------- .../parser/forktask/GetPipelineTask.java | 17 +++--- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/PipelineService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/PipelineService.java index a45e7fc..4707c31 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/PipelineService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/PipelineService.java @@ -24,6 +24,8 @@ public interface PipelineService { Pipeline update(@NonNull Pipeline pipeline); + List updateAll(@NonNull List pipelines); + List getAllByStatuses(@NonNull Set statuses); Page getAll(@NonNull PipelineFilter filter, @NonNull Pageable pagination); @@ -31,5 +33,4 @@ public interface PipelineService { ExistContainer existsById(@NonNull Set pipelineIds); void deleteAllById(Set pipelineIds); - } diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java index 11015a4..6e692db 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/PipelineServiceImpl.java @@ -92,6 +92,13 @@ public class PipelineServiceImpl implements PipelineService { return oldPipeline; } + @Override + public List updateAll(@NonNull List pipelines) { + return pipelines.stream() + .map(this::update) + .collect(Collectors.toList()); + } + private boolean isNeedNotifyNewPipeline(@NonNull Pipeline pipeline) { final Person personPipelineCreator = pipeline.getPerson(); return notificationStatus.contains(pipeline.getStatus()) // Пайплайн имеет статус необходимый для уведомления diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/PipelineParser.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/PipelineParser.java index f5a0cd9..5f6d1d8 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/PipelineParser.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/PipelineParser.java @@ -9,16 +9,13 @@ import dev.struchkov.bot.gitlab.core.config.properties.GitlabProperty; import dev.struchkov.bot.gitlab.core.config.properties.PersonProperty; import dev.struchkov.bot.gitlab.core.service.parser.forktask.GetPipelineShortTask; import dev.struchkov.bot.gitlab.core.service.parser.forktask.GetPipelineTask; -import dev.struchkov.bot.gitlab.core.utils.StringUtils; import dev.struchkov.bot.gitlab.sdk.domain.PipelineJson; import dev.struchkov.bot.gitlab.sdk.domain.PipelineShortJson; -import dev.struchkov.haiti.utils.network.HttpParse; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.core.convert.ConversionService; import org.springframework.stereotype.Service; -import java.text.MessageFormat; import java.time.LocalDateTime; import java.util.List; import java.util.Map; @@ -38,7 +35,6 @@ import static dev.struchkov.haiti.utils.Checker.checkFalse; import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; import static dev.struchkov.haiti.utils.concurrent.ForkJoinUtils.pullTaskResult; import static dev.struchkov.haiti.utils.concurrent.ForkJoinUtils.pullTaskResults; -import static dev.struchkov.haiti.utils.network.HttpParse.ACCEPT; /** * Парсер пайплайнов. @@ -91,12 +87,9 @@ public class PipelineParser { if (checkFalse(existContainer.isAllFound())) { final Set idsNotFound = existContainer.getIdNoFound(); - final List pipelineJsons = getNewPipelineJson(pipelineProjectMap, idsNotFound); + final List newPipelines = getNewPipelines(pipelineProjectMap, idsNotFound); - if (checkNotEmpty(pipelineJsons)) { - final List newPipelines = pipelineJsons.stream() - .map(json -> conversionService.convert(json, Pipeline.class)) - .collect(Collectors.toList()); + if (checkNotEmpty(newPipelines)) { pipelineService.createAll(newPipelines); } } @@ -106,8 +99,8 @@ public class PipelineParser { log.debug("Конец обработки новых пайплайнов"); } - private List getNewPipelineJson(Map pipelineProjectMap, Set idsNotFound) { - final List> tasks = idsNotFound.stream() + private List getNewPipelines(Map pipelineProjectMap, Set idsNotFound) { + final List>> tasks = idsNotFound.stream() .map(pipelineId -> new GetPipelineTask( gitlabProperty.getUrlPipeline(), pipelineProjectMap.get(pipelineId), @@ -117,8 +110,10 @@ public class PipelineParser { .map(forkJoinPool::submit) .collect(Collectors.toList()); - final List pipelineJsons = pullTaskResult(tasks); - return pipelineJsons; + return pullTaskResult(tasks).stream() + .flatMap(Optional::stream) + .map(json -> conversionService.convert(json, Pipeline.class)) + .collect(Collectors.toList()); } private List getPipelineShortJsons(Set projectIds) { @@ -144,20 +139,25 @@ public class PipelineParser { log.debug("Старт обработки старых пайплайнов"); final List pipelines = pipelineService.getAllByStatuses(oldStatus); - for (Pipeline pipeline : pipelines) { - final Optional optNewPipeline = HttpParse.request( - MessageFormat.format(gitlabProperty.getUrlPipeline(), pipeline.getProjectId(), pipeline.getId()) - ) - .header(ACCEPT) - .header(StringUtils.H_PRIVATE_TOKEN, personProperty.getToken()) - .execute(PipelineJson.class) - .map(json -> conversionService.convert(json, Pipeline.class)); - if (optNewPipeline.isPresent()) { - final Pipeline newPipeline = optNewPipeline.get(); - pipelineService.update(newPipeline); - } else { - log.error("Ошибка обновления пайплайна. ProjectId:{}, PipelineId:{}", pipeline.getProjectId(), pipeline.getId()); - } + final List>> tasks = pipelines.stream() + .map( + pipeline -> new GetPipelineTask( + gitlabProperty.getUrlPipeline(), + pipeline.getProjectId(), + pipeline.getId(), + personProperty.getToken() + ) + ) + .map(forkJoinPool::submit) + .collect(Collectors.toList()); + + final List newPipelines = pullTaskResult(tasks).stream() + .flatMap(Optional::stream) + .map(json -> conversionService.convert(json, Pipeline.class)) + .collect(Collectors.toList()); + + if (checkNotEmpty(newPipelines)) { + pipelineService.updateAll(pipelines); } log.debug("Конец обработки старых пайплайнов"); diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java index 5ed72a5..c7a1241 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/forktask/GetPipelineTask.java @@ -8,14 +8,14 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import java.text.MessageFormat; +import java.util.Optional; import java.util.concurrent.RecursiveTask; -import static dev.struchkov.haiti.context.exception.ConvertException.convertException; import static dev.struchkov.haiti.utils.network.HttpParse.ACCEPT; @Slf4j @RequiredArgsConstructor -public class GetPipelineTask extends RecursiveTask { +public class GetPipelineTask extends RecursiveTask> { private final String urlPipeline; private final long projectId; @@ -24,17 +24,16 @@ public class GetPipelineTask extends RecursiveTask { @Override @SneakyThrows - protected PipelineJson compute() { + protected Optional compute() { Thread.sleep(100); - final PipelineJson pipelineJson = HttpParse.request( - MessageFormat.format(urlPipeline, projectId, pipelineId) - ) + return HttpParse.request(MessageFormat.format(urlPipeline, projectId, pipelineId)) .header(ACCEPT) .header(StringUtils.H_PRIVATE_TOKEN, gitlabToken) .execute(PipelineJson.class) - .orElseThrow(convertException("Ошибка обновления Pipelines")); - pipelineJson.setProjectId(projectId); - return pipelineJson; + .map(json -> { + json.setProjectId(projectId); + return json; + }); } }