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; + }); } }