Skip to content

Commit

Permalink
Merge pull request #160 from cardano-foundation/custom-healthcheck-ag…
Browse files Browse the repository at this point in the history
…gr-app

Bump yaci store version and custom health check for aggr app
  • Loading branch information
Sotatek-HuyLe3a authored Apr 9, 2024
2 parents b94a986 + aa2de2a commit e5ff4c3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
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;
}
}
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;
}
}
12 changes: 6 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[libraries]
yaci-store-starter="com.bloxbean.cardano:yaci-store-spring-boot-starter:0.1.0-rc2-a863cfe-SNAPSHOT"
yaci-store-utxo-starter="com.bloxbean.cardano:yaci-store-utxo-spring-boot-starter:0.1.0-rc2-a863cfe-SNAPSHOT"
yaci-store-account-starter="com.bloxbean.cardano:yaci-store-account-spring-boot-starter:0.1.0-rc2-a863cfe-SNAPSHOT"
yaci-store-remote-starter="com.bloxbean.cardano:yaci-store-remote-spring-boot-starter:0.1.0-rc2-a863cfe-SNAPSHOT"
yaci-store-governance-starter="com.bloxbean.cardano:yaci-store-governance-spring-boot-starter:0.1.0-rc2-a863cfe-SNAPSHOT"
yaci-store-events="com.bloxbean.cardano:yaci-store-events:0.1.0-rc2-a863cfe-SNAPSHOT"
yaci-store-starter="com.bloxbean.cardano:yaci-store-spring-boot-starter:0.1.0-rc2-87c7beb-SNAPSHOT"
yaci-store-utxo-starter="com.bloxbean.cardano:yaci-store-utxo-spring-boot-starter:0.1.0-rc2-87c7beb-SNAPSHOT"
yaci-store-account-starter="com.bloxbean.cardano:yaci-store-account-spring-boot-starter:0.1.0-rc2-87c7beb-SNAPSHOT"
yaci-store-remote-starter="com.bloxbean.cardano:yaci-store-remote-spring-boot-starter:0.1.0-rc2-87c7beb-SNAPSHOT"
yaci-store-governance-starter="com.bloxbean.cardano:yaci-store-governance-spring-boot-starter:0.1.0-rc2-87c7beb-SNAPSHOT"
yaci-store-events="com.bloxbean.cardano:yaci-store-events:0.1.0-rc2-87c7beb-SNAPSHOT"

cardano-client-lib="com.bloxbean.cardano:cardano-client-lib:0.5.1"
snakeyaml="org.yaml:snakeyaml:2.0"
Expand Down

0 comments on commit e5ff4c3

Please sign in to comment.