diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/PipelineRepository.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/PipelineRepository.java index b0ef6ef..2b05e1a 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/PipelineRepository.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/PipelineRepository.java @@ -2,10 +2,8 @@ package dev.struchkov.bot.gitlab.context.repository; import dev.struchkov.bot.gitlab.context.domain.PipelineStatus; import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline; -import dev.struchkov.haiti.filter.Filter; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import java.util.Set; @@ -23,8 +21,6 @@ public interface PipelineRepository { List findAllById(Set pipelineIds); - void deleteAllByIds(Set pipelineIds); - - Page filter(Filter filter, Pageable pagination); + void deleteByCreatedBefore(LocalDateTime date); } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/CleanService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/CleanService.java index 98d386c..59bf0ae 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/CleanService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/CleanService.java @@ -14,10 +14,5 @@ public interface CleanService { */ void cleanOldMergedRequests(); - /** - *

Удаляет старые пайплайны

- *

По умолчанию удаляет все пайплайны старше суток

- */ - void cleanOldPipelines(); } 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 4707c31..b5bbe39 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 @@ -3,10 +3,7 @@ package dev.struchkov.bot.gitlab.context.service; import dev.struchkov.bot.gitlab.context.domain.ExistContainer; import dev.struchkov.bot.gitlab.context.domain.PipelineStatus; import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline; -import dev.struchkov.bot.gitlab.context.domain.filter.PipelineFilter; import lombok.NonNull; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import java.util.List; import java.util.Set; @@ -28,9 +25,8 @@ public interface PipelineService { List getAllByStatuses(@NonNull Set statuses); - Page getAll(@NonNull PipelineFilter filter, @NonNull Pageable pagination); - ExistContainer existsById(@NonNull Set pipelineIds); - void deleteAllById(Set pipelineIds); + void cleanOld(); + } diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/CleanServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/CleanServiceImpl.java index 2e0ccb6..1d0cb56 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/CleanServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/CleanServiceImpl.java @@ -1,19 +1,15 @@ package dev.struchkov.bot.gitlab.core.service.impl; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; -import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline; import dev.struchkov.bot.gitlab.context.domain.filter.MergeRequestFilter; -import dev.struchkov.bot.gitlab.context.domain.filter.PipelineFilter; import dev.struchkov.bot.gitlab.context.service.CleanService; import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; -import dev.struchkov.bot.gitlab.context.service.PipelineService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; -import java.time.LocalDateTime; import java.util.Set; import java.util.stream.Collectors; @@ -36,7 +32,6 @@ public class CleanServiceImpl implements CleanService { .build(); private final MergeRequestsService mergeRequestsService; - private final PipelineService pipelineService; @Override public void cleanOldMergedRequests() { @@ -56,29 +51,4 @@ public class CleanServiceImpl implements CleanService { log.debug("Конец очистки старых MR"); } - @Override - public void cleanOldPipelines() { - log.debug("Старт очистки старых пайплайнов"); - int page = 0; - final PipelineFilter filter = cleanPipelineFilter(); - Page sheet = pipelineService.getAll(filter, PageRequest.of(page, COUNT)); - - while (sheet.hasContent()) { - final Set ids = sheet.getContent().stream() - .map(Pipeline::getId) - .collect(Collectors.toUnmodifiableSet()); - - pipelineService.deleteAllById(ids); - - sheet = pipelineService.getAll(filter, PageRequest.of(page, COUNT)); - } - log.debug("Конец очистки старых пайплайнов"); - } - - private PipelineFilter cleanPipelineFilter() { - final PipelineFilter pipelineFilter = new PipelineFilter(); - pipelineFilter.setLessThanCreatedDate(LocalDateTime.now().minusDays(1L)); - return pipelineFilter; - } - } 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 6e692db..ab3e872 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 @@ -5,19 +5,17 @@ import dev.struchkov.bot.gitlab.context.domain.PersonInformation; import dev.struchkov.bot.gitlab.context.domain.PipelineStatus; import dev.struchkov.bot.gitlab.context.domain.entity.Person; import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline; -import dev.struchkov.bot.gitlab.context.domain.filter.PipelineFilter; import dev.struchkov.bot.gitlab.context.domain.notify.pipeline.PipelineNotify; import dev.struchkov.bot.gitlab.context.repository.PipelineRepository; import dev.struchkov.bot.gitlab.context.service.NotifyService; import dev.struchkov.bot.gitlab.context.service.PipelineService; -import dev.struchkov.bot.gitlab.core.service.impl.filter.PipelineFilterService; import lombok.NonNull; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -34,6 +32,7 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull; * * @author upagge 17.01.2021 */ +@Slf4j @Service @RequiredArgsConstructor public class PipelineServiceImpl implements PipelineService { @@ -43,7 +42,6 @@ public class PipelineServiceImpl implements PipelineService { private final NotifyService notifyService; private final PipelineRepository repository; - private final PipelineFilterService pipelineFilterService; private final PersonInformation personInformation; @@ -111,10 +109,6 @@ public class PipelineServiceImpl implements PipelineService { return repository.findAllByStatuses(statuses); } - @Override - public Page getAll(@NonNull PipelineFilter filter, @NonNull Pageable pagination) { - return pipelineFilterService.getAll(filter, pagination); - } @Override public ExistContainer existsById(@NonNull Set pipelineIds) { @@ -131,8 +125,10 @@ public class PipelineServiceImpl implements PipelineService { } @Override - public void deleteAllById(Set pipelineIds) { - repository.deleteAllByIds(pipelineIds); + public void cleanOld() { + log.debug("Старт очистки старых пайплайнов"); + repository.deleteByCreatedBefore(LocalDateTime.now().minusDays(1L)); + log.debug("Конец очистки старых пайплайнов"); } } diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/filter/PipelineFilterService.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/filter/PipelineFilterService.java deleted file mode 100644 index a18afc2..0000000 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/filter/PipelineFilterService.java +++ /dev/null @@ -1,43 +0,0 @@ -package dev.struchkov.bot.gitlab.core.service.impl.filter; - -import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline; -import dev.struchkov.bot.gitlab.context.domain.entity.PipelineFields; -import dev.struchkov.bot.gitlab.context.domain.filter.PipelineFilter; -import dev.struchkov.bot.gitlab.context.repository.PipelineRepository; -import dev.struchkov.haiti.filter.Filter; -import dev.struchkov.haiti.filter.FilterQuery; -import dev.struchkov.haiti.filter.criteria.CriteriaFilter; -import dev.struchkov.haiti.filter.criteria.CriteriaQuery; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; - -/** - * Сервис фильтрации пайплайнов. - * - * @author upagge 08.02.2021 - */ -@Service -@RequiredArgsConstructor -public class PipelineFilterService { - - private final PipelineRepository pipelineRepository; - - public Page getAll(PipelineFilter filter, Pageable pagination) { - return pipelineRepository.filter(createFilter(filter), pagination); - } - - private Filter createFilter(@NonNull PipelineFilter pipelineFilter) { - return CriteriaFilter.create() - .and(convertAnd(pipelineFilter)); - } - - private FilterQuery convertAnd(PipelineFilter pipelineFilter) { - return CriteriaQuery.create() - .lessThan(PipelineFields.created, pipelineFilter.getLessThanCreatedDate()); - } - - -} diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/PipelineRepositoryImpl.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/PipelineRepositoryImpl.java index 40fc713..224972d 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/PipelineRepositoryImpl.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/PipelineRepositoryImpl.java @@ -4,13 +4,11 @@ import dev.struchkov.bot.gitlab.context.domain.PipelineStatus; import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline; import dev.struchkov.bot.gitlab.context.repository.PipelineRepository; import dev.struchkov.bot.gitlab.data.jpa.PipelineJpaRepository; -import dev.struchkov.haiti.filter.Filter; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import java.util.Set; @@ -45,13 +43,9 @@ public class PipelineRepositoryImpl implements PipelineRepository { } @Override - public void deleteAllByIds(Set pipelineIds) { - jpaRepository.deleteAllById(pipelineIds); - } - - @Override - public Page filter(Filter filter, Pageable pagination) { - return jpaRepository.findAll(filter.>build(), pagination); + @Transactional + public void deleteByCreatedBefore(LocalDateTime date) { + jpaRepository.deleteAllByCreatedBefore(date); } } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/PipelineJpaRepository.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/PipelineJpaRepository.java index ee196ad..f6d6991 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/PipelineJpaRepository.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/PipelineJpaRepository.java @@ -4,6 +4,7 @@ import dev.struchkov.bot.gitlab.context.domain.PipelineStatus; import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline; import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation; +import java.time.LocalDateTime; import java.util.List; import java.util.Set; @@ -14,4 +15,6 @@ public interface PipelineJpaRepository extends JpaRepositoryImplementation findAllByStatusIn(Set statuses); + void deleteAllByCreatedBefore(LocalDateTime date); + } diff --git a/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java b/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java index c69fb81..63ab34f 100644 --- a/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java +++ b/gitlab-app/src/main/java/dev/struchkov/bot/gitlab/scheduler/SchedulerService.java @@ -1,6 +1,7 @@ package dev.struchkov.bot.gitlab.scheduler; import dev.struchkov.bot.gitlab.context.service.CleanService; +import dev.struchkov.bot.gitlab.context.service.PipelineService; import dev.struchkov.bot.gitlab.core.service.parser.DiscussionParser; import dev.struchkov.bot.gitlab.core.service.parser.MergeRequestParser; import dev.struchkov.bot.gitlab.core.service.parser.PipelineParser; @@ -22,6 +23,8 @@ public class SchedulerService { private final CleanService cleanService; private final DiscussionParser discussionParser; + private final PipelineService pipelineService; + @Scheduled(cron = "0 */1 * * * *") public void newMergeRequest() { log.debug("Запуск процесса обновления данных"); @@ -31,7 +34,7 @@ public class SchedulerService { pipelineParser.scanNewPipeline(); discussionParser.scanOldDiscussions(); discussionParser.scanNewDiscussion(); - cleanService.cleanOldPipelines(); + pipelineService.cleanOld(); cleanService.cleanOldMergedRequests(); log.debug("Конец процесса обновления данных"); }