From 4747a4b75dba8d87f7689e27e06892ff76f292e5 Mon Sep 17 00:00:00 2001 From: zhiyaoPCCW Date: Tue, 19 Nov 2024 15:20:42 +0800 Subject: [PATCH 1/2] improvement on sync kraken version --- .../service/PushKrakenVersionService.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java b/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java index 5924e8f4..4e3227e4 100644 --- a/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java +++ b/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java @@ -5,9 +5,13 @@ import com.consoleconnect.kraken.operator.core.enums.MgmtEventType; import com.consoleconnect.kraken.operator.core.repo.MgmtEventRepository; import com.consoleconnect.kraken.operator.core.repo.SystemInfoRepository; +import java.util.List; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; @Service @RequiredArgsConstructor @@ -15,6 +19,7 @@ public class PushKrakenVersionService { private static final String KEY = "CONTROL_PLANE"; private final SystemInfoRepository systemInfoRepository; private final MgmtEventRepository eventRepository; + private final MgmtEventRepository mgmtEventRepository; @Scheduled(cron = "${app.cron-job.sync-system-info-from-control-plane:-}") public void runIt() { @@ -23,12 +28,24 @@ public void runIt() { .findOneByKey(KEY) .ifPresent( systemInfoEntity -> { - MgmtEventEntity entity = new MgmtEventEntity(); - entity.setStatus(EventStatusType.WAIT_TO_SEND.name()); - entity.setPayload(systemInfoEntity); - entity.setResourceId(systemInfoEntity.getId().toString()); - entity.setEventType(MgmtEventType.CLIENT_SYSTEM_INFO.name()); - eventRepository.save(entity); + Page mgmtEventEntities = + mgmtEventRepository.findByEventTypeInAndStatus( + List.of(MgmtEventType.CLIENT_SYSTEM_INFO.name()), null, Pageable.ofSize(1)); + if (CollectionUtils.isEmpty(mgmtEventEntities.getContent())) { + MgmtEventEntity entity = new MgmtEventEntity(); + entity.setStatus(EventStatusType.WAIT_TO_SEND.name()); + entity.setPayload(systemInfoEntity); + entity.setResourceId(systemInfoEntity.getId().toString()); + entity.setEventType(MgmtEventType.CLIENT_SYSTEM_INFO.name()); + eventRepository.save(entity); + } else { + // if sync system info event exists, update state to wait_to_send to re-active the + // event + MgmtEventEntity eventEntity = mgmtEventEntities.getContent().get(0); + eventEntity.setStatus(EventStatusType.WAIT_TO_SEND.name()); + eventEntity.setPayload(systemInfoEntity); + eventRepository.save(eventEntity); + } }); } } From ee5cb1ff53eba185998580722267d48f1ade6ac3 Mon Sep 17 00:00:00 2001 From: zhiyaoPCCW Date: Tue, 19 Nov 2024 15:26:47 +0800 Subject: [PATCH 2/2] improvement on sync kraken version --- .../operator/sync/service/PushKrakenVersionService.java | 5 ++--- .../operator/sync/service/PushKrakenVersionServiceTest.java | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java b/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java index 4e3227e4..9e06658a 100644 --- a/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java +++ b/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java @@ -5,7 +5,6 @@ import com.consoleconnect.kraken.operator.core.enums.MgmtEventType; import com.consoleconnect.kraken.operator.core.repo.MgmtEventRepository; import com.consoleconnect.kraken.operator.core.repo.SystemInfoRepository; -import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -29,8 +28,8 @@ public void runIt() { .ifPresent( systemInfoEntity -> { Page mgmtEventEntities = - mgmtEventRepository.findByEventTypeInAndStatus( - List.of(MgmtEventType.CLIENT_SYSTEM_INFO.name()), null, Pageable.ofSize(1)); + mgmtEventRepository.search( + MgmtEventType.CLIENT_SYSTEM_INFO.name(), null, Pageable.ofSize(1)); if (CollectionUtils.isEmpty(mgmtEventEntities.getContent())) { MgmtEventEntity entity = new MgmtEventEntity(); entity.setStatus(EventStatusType.WAIT_TO_SEND.name()); diff --git a/kraken-java-sdk/kraken-java-sdk-sync/src/test/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionServiceTest.java b/kraken-java-sdk/kraken-java-sdk-sync/src/test/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionServiceTest.java index 622123a5..30f44f1c 100644 --- a/kraken-java-sdk/kraken-java-sdk-sync/src/test/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionServiceTest.java +++ b/kraken-java-sdk/kraken-java-sdk-sync/src/test/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionServiceTest.java @@ -42,6 +42,9 @@ void givenSystemInfoEvent_whenRun_thenPushSuccessfully() { systemInfoRepository.save(entity); // when pushKrakenVersionService.runIt(); + entity.setStageAppVersion("2.0.0"); + systemInfoRepository.save(entity); + pushKrakenVersionService.runIt(); // then Page eventEntities = mgmtEventRepository.findByEventTypeInAndStatus(