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..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 @@ -6,8 +6,11 @@ import com.consoleconnect.kraken.operator.core.repo.MgmtEventRepository; import com.consoleconnect.kraken.operator.core.repo.SystemInfoRepository; 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 +18,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 +27,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.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()); + 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); + } }); } } 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(