Оптимизация очистки старых пайплайнов
This commit is contained in:
@@ -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.PipelineStatus;
|
||||||
import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline;
|
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.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -23,8 +21,6 @@ public interface PipelineRepository {
|
|||||||
|
|
||||||
List<Pipeline> findAllById(Set<Long> pipelineIds);
|
List<Pipeline> findAllById(Set<Long> pipelineIds);
|
||||||
|
|
||||||
void deleteAllByIds(Set<Long> pipelineIds);
|
void deleteByCreatedBefore(LocalDateTime date);
|
||||||
|
|
||||||
Page<Pipeline> filter(Filter filter, Pageable pagination);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,5 @@ public interface CleanService {
|
|||||||
*/
|
*/
|
||||||
void cleanOldMergedRequests();
|
void cleanOldMergedRequests();
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Удаляет старые пайплайны</p>
|
|
||||||
* <p>По умолчанию удаляет все пайплайны старше суток</p>
|
|
||||||
*/
|
|
||||||
void cleanOldPipelines();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.ExistContainer;
|
||||||
import dev.struchkov.bot.gitlab.context.domain.PipelineStatus;
|
import dev.struchkov.bot.gitlab.context.domain.PipelineStatus;
|
||||||
import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline;
|
import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline;
|
||||||
import dev.struchkov.bot.gitlab.context.domain.filter.PipelineFilter;
|
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.data.domain.Pageable;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -28,9 +25,8 @@ public interface PipelineService {
|
|||||||
|
|
||||||
List<Pipeline> getAllByStatuses(@NonNull Set<PipelineStatus> statuses);
|
List<Pipeline> getAllByStatuses(@NonNull Set<PipelineStatus> statuses);
|
||||||
|
|
||||||
Page<Pipeline> getAll(@NonNull PipelineFilter filter, @NonNull Pageable pagination);
|
|
||||||
|
|
||||||
ExistContainer<Pipeline, Long> existsById(@NonNull Set<Long> pipelineIds);
|
ExistContainer<Pipeline, Long> existsById(@NonNull Set<Long> pipelineIds);
|
||||||
|
|
||||||
void deleteAllById(Set<Long> pipelineIds);
|
void cleanOld();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,15 @@
|
|||||||
package dev.struchkov.bot.gitlab.core.service.impl;
|
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.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.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.CleanService;
|
||||||
import dev.struchkov.bot.gitlab.context.service.MergeRequestsService;
|
import dev.struchkov.bot.gitlab.context.service.MergeRequestsService;
|
||||||
import dev.struchkov.bot.gitlab.context.service.PipelineService;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -36,7 +32,6 @@ public class CleanServiceImpl implements CleanService {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
private final MergeRequestsService mergeRequestsService;
|
private final MergeRequestsService mergeRequestsService;
|
||||||
private final PipelineService pipelineService;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cleanOldMergedRequests() {
|
public void cleanOldMergedRequests() {
|
||||||
@@ -56,29 +51,4 @@ public class CleanServiceImpl implements CleanService {
|
|||||||
log.debug("Конец очистки старых MR");
|
log.debug("Конец очистки старых MR");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void cleanOldPipelines() {
|
|
||||||
log.debug("Старт очистки старых пайплайнов");
|
|
||||||
int page = 0;
|
|
||||||
final PipelineFilter filter = cleanPipelineFilter();
|
|
||||||
Page<Pipeline> sheet = pipelineService.getAll(filter, PageRequest.of(page, COUNT));
|
|
||||||
|
|
||||||
while (sheet.hasContent()) {
|
|
||||||
final Set<Long> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.PipelineStatus;
|
||||||
import dev.struchkov.bot.gitlab.context.domain.entity.Person;
|
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.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.domain.notify.pipeline.PipelineNotify;
|
||||||
import dev.struchkov.bot.gitlab.context.repository.PipelineRepository;
|
import dev.struchkov.bot.gitlab.context.repository.PipelineRepository;
|
||||||
import dev.struchkov.bot.gitlab.context.service.NotifyService;
|
import dev.struchkov.bot.gitlab.context.service.NotifyService;
|
||||||
import dev.struchkov.bot.gitlab.context.service.PipelineService;
|
import dev.struchkov.bot.gitlab.context.service.PipelineService;
|
||||||
import dev.struchkov.bot.gitlab.core.service.impl.filter.PipelineFilterService;
|
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.data.domain.Page;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.domain.Pageable;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -34,6 +32,7 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull;
|
|||||||
*
|
*
|
||||||
* @author upagge 17.01.2021
|
* @author upagge 17.01.2021
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class PipelineServiceImpl implements PipelineService {
|
public class PipelineServiceImpl implements PipelineService {
|
||||||
@@ -43,7 +42,6 @@ public class PipelineServiceImpl implements PipelineService {
|
|||||||
|
|
||||||
private final NotifyService notifyService;
|
private final NotifyService notifyService;
|
||||||
private final PipelineRepository repository;
|
private final PipelineRepository repository;
|
||||||
private final PipelineFilterService pipelineFilterService;
|
|
||||||
|
|
||||||
private final PersonInformation personInformation;
|
private final PersonInformation personInformation;
|
||||||
|
|
||||||
@@ -111,10 +109,6 @@ public class PipelineServiceImpl implements PipelineService {
|
|||||||
return repository.findAllByStatuses(statuses);
|
return repository.findAllByStatuses(statuses);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Page<Pipeline> getAll(@NonNull PipelineFilter filter, @NonNull Pageable pagination) {
|
|
||||||
return pipelineFilterService.getAll(filter, pagination);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ExistContainer<Pipeline, Long> existsById(@NonNull Set<Long> pipelineIds) {
|
public ExistContainer<Pipeline, Long> existsById(@NonNull Set<Long> pipelineIds) {
|
||||||
@@ -131,8 +125,10 @@ public class PipelineServiceImpl implements PipelineService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteAllById(Set<Long> pipelineIds) {
|
public void cleanOld() {
|
||||||
repository.deleteAllByIds(pipelineIds);
|
log.debug("Старт очистки старых пайплайнов");
|
||||||
|
repository.deleteByCreatedBefore(LocalDateTime.now().minusDays(1L));
|
||||||
|
log.debug("Конец очистки старых пайплайнов");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<Pipeline> getAll(PipelineFilter filter, Pageable pagination) {
|
|
||||||
return pipelineRepository.filter(createFilter(filter), pagination);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Filter createFilter(@NonNull PipelineFilter pipelineFilter) {
|
|
||||||
return CriteriaFilter.<Pipeline>create()
|
|
||||||
.and(convertAnd(pipelineFilter));
|
|
||||||
}
|
|
||||||
|
|
||||||
private FilterQuery convertAnd(PipelineFilter pipelineFilter) {
|
|
||||||
return CriteriaQuery.<Pipeline>create()
|
|
||||||
.lessThan(PipelineFields.created, pipelineFilter.getLessThanCreatedDate());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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.domain.entity.Pipeline;
|
||||||
import dev.struchkov.bot.gitlab.context.repository.PipelineRepository;
|
import dev.struchkov.bot.gitlab.context.repository.PipelineRepository;
|
||||||
import dev.struchkov.bot.gitlab.data.jpa.PipelineJpaRepository;
|
import dev.struchkov.bot.gitlab.data.jpa.PipelineJpaRepository;
|
||||||
import dev.struchkov.haiti.filter.Filter;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
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.stereotype.Repository;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -45,13 +43,9 @@ public class PipelineRepositoryImpl implements PipelineRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteAllByIds(Set<Long> pipelineIds) {
|
@Transactional
|
||||||
jpaRepository.deleteAllById(pipelineIds);
|
public void deleteByCreatedBefore(LocalDateTime date) {
|
||||||
}
|
jpaRepository.deleteAllByCreatedBefore(date);
|
||||||
|
|
||||||
@Override
|
|
||||||
public Page<Pipeline> filter(Filter filter, Pageable pagination) {
|
|
||||||
return jpaRepository.findAll(filter.<Specification<Pipeline>>build(), pagination);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import dev.struchkov.bot.gitlab.context.domain.PipelineStatus;
|
|||||||
import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline;
|
import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline;
|
||||||
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
|
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@@ -14,4 +15,6 @@ public interface PipelineJpaRepository extends JpaRepositoryImplementation<Pipel
|
|||||||
|
|
||||||
List<Pipeline> findAllByStatusIn(Set<PipelineStatus> statuses);
|
List<Pipeline> findAllByStatusIn(Set<PipelineStatus> statuses);
|
||||||
|
|
||||||
|
void deleteAllByCreatedBefore(LocalDateTime date);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.struchkov.bot.gitlab.scheduler;
|
package dev.struchkov.bot.gitlab.scheduler;
|
||||||
|
|
||||||
import dev.struchkov.bot.gitlab.context.service.CleanService;
|
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.DiscussionParser;
|
||||||
import dev.struchkov.bot.gitlab.core.service.parser.MergeRequestParser;
|
import dev.struchkov.bot.gitlab.core.service.parser.MergeRequestParser;
|
||||||
import dev.struchkov.bot.gitlab.core.service.parser.PipelineParser;
|
import dev.struchkov.bot.gitlab.core.service.parser.PipelineParser;
|
||||||
@@ -22,6 +23,8 @@ public class SchedulerService {
|
|||||||
private final CleanService cleanService;
|
private final CleanService cleanService;
|
||||||
private final DiscussionParser discussionParser;
|
private final DiscussionParser discussionParser;
|
||||||
|
|
||||||
|
private final PipelineService pipelineService;
|
||||||
|
|
||||||
@Scheduled(cron = "0 */1 * * * *")
|
@Scheduled(cron = "0 */1 * * * *")
|
||||||
public void newMergeRequest() {
|
public void newMergeRequest() {
|
||||||
log.debug("Запуск процесса обновления данных");
|
log.debug("Запуск процесса обновления данных");
|
||||||
@@ -31,7 +34,7 @@ public class SchedulerService {
|
|||||||
pipelineParser.scanNewPipeline();
|
pipelineParser.scanNewPipeline();
|
||||||
discussionParser.scanOldDiscussions();
|
discussionParser.scanOldDiscussions();
|
||||||
discussionParser.scanNewDiscussion();
|
discussionParser.scanNewDiscussion();
|
||||||
cleanService.cleanOldPipelines();
|
pipelineService.cleanOld();
|
||||||
cleanService.cleanOldMergedRequests();
|
cleanService.cleanOldMergedRequests();
|
||||||
log.debug("Конец процесса обновления данных");
|
log.debug("Конец процесса обновления данных");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user