-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from cardano-foundation/custom-healthcheck-ag…
…gr-app Bump yaci store version and custom health check for aggr app
- Loading branch information
Showing
3 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...rdanofoundation/ledgersync/aggregation/app/healthcheck/CustomHealthStatusServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.cardanofoundation.ledgersync.aggregation.app.healthcheck; | ||
|
||
import com.bloxbean.cardano.yaci.store.core.service.BlockFetchService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.cardanofoundation.ledgersync.healthcheck.HealthCheckProperties; | ||
import org.cardanofoundation.ledgersync.healthcheck.model.HealthStatus; | ||
import org.cardanofoundation.ledgersync.healthcheck.model.Message; | ||
import org.cardanofoundation.ledgersync.healthcheck.service.HealthCheckCachingService; | ||
import org.cardanofoundation.ledgersync.healthcheck.service.HealthStatusService; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CustomHealthStatusServiceImpl implements HealthStatusService { | ||
private final HealthCheckCachingService healthCheckCachingService; | ||
private final HealthCheckProperties healthCheckProperties; | ||
private final BlockFetchService blockFetchService; | ||
|
||
@Override | ||
public HealthStatus getHealthStatus() { | ||
final var latestEventTime = healthCheckCachingService.getLatestEventTime(); | ||
final var latestSlotNo = healthCheckCachingService.getLatestSlotNo(); | ||
final var latestBlockTime = healthCheckCachingService.getLatestBlockTime(); | ||
final var stopSlot = healthCheckProperties.getStopSlot(); | ||
if (blockFetchService.isScheduledToStop()) { | ||
return new HealthStatus(Boolean.TRUE, CustomMessage.IS_SCHEDULED_TO_STOP.getCode(), | ||
CustomMessage.IS_SCHEDULED_TO_STOP.getDesc()); | ||
} | ||
|
||
if (!isInThreshold(healthCheckProperties.getEventTimeThreshold(), latestEventTime)) { | ||
if (stopSlot > 0 && latestSlotNo >= stopSlot) { | ||
return new HealthStatus(Boolean.TRUE, Message.STOP_SLOT_HAS_REACHED); | ||
} | ||
return new HealthStatus(Boolean.FALSE, Message.IS_BAD); | ||
} | ||
|
||
if (healthCheckProperties.isBlockTimeCheckEnabled() && latestBlockTime != null && | ||
isInThreshold(healthCheckProperties.getBlockTimeThreshold(), latestBlockTime)) { | ||
return new HealthStatus(Boolean.TRUE, Message.BLOCK_HAS_REACHED_TIP); | ||
} | ||
|
||
return new HealthStatus(Boolean.TRUE, Message.IS_GOOD); | ||
} | ||
|
||
private boolean isInThreshold(Long threshold, LocalDateTime time) { | ||
long value = ChronoUnit.SECONDS.between(time, LocalDateTime.now(ZoneOffset.UTC)); | ||
return value <= threshold; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...main/java/org/cardanofoundation/ledgersync/aggregation/app/healthcheck/CustomMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.cardanofoundation.ledgersync.aggregation.app.healthcheck; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum CustomMessage { | ||
IS_SCHEDULED_TO_STOP("IS_SCHEDULED_TO_STOP", "Service is scheduled to stop"); | ||
|
||
private final String code; | ||
private final String desc; | ||
|
||
CustomMessage(String code, String desc) { | ||
this.code = code; | ||
this.desc = desc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters