readiness.sh


Overview

`readiness.sh` is a lightweight Bash script intended to serve as a readiness probe for a service, particularly one that relies on the "blockbook" API. The script checks whether the service is ready to receive traffic by verifying the synchronization status of blockbook. It exits with status `0` (success) if the service is ready, or `1` (failure) otherwise.

This script is typically used in containerized environments (e.g., Kubernetes) to inform the orchestrator whether the application is ready to handle requests, thus controlling load balancing and deployment rollouts.


Detailed Explanation

Purpose

Script Breakdown

#!/bin/bash
DISABLE_READINESS_PROBE=/data/disable_readiness
if [[ -f "$DISABLE_READINESS_PROBE" ]]; then
  echo "readiness probe disabled"
  exit 0
fi
STATUS=$(curl -sf http://localhost:8001/api/v2) || exit 1
IN_SYNC=$(echo $STATUS | jq -r '.blockbook.inSync')
if [[ $IN_SYNC == "true" ]]; then
    echo "blockbook is synced"
    exit 0
fi
echo "blockbook is still syncing"
exit 1

Usage

Run the script directly in an environment where the blockbook API is accessible locally on port `8001`.

./readiness.sh

Example Integration in Kubernetes

readinessProbe:
  exec:
    command:
      - /bin/bash
      - /path/to/readiness.sh
  initialDelaySeconds: 10
  periodSeconds: 15

Important Implementation Details


Interaction with Other System Components


Mermaid Flowchart Diagram

flowchart TD
    Start([Start])
    CheckDisableFile{File /data/disable_readiness?}
    ExitReady("Exit 0: readiness probe disabled")
    CurlRequest["curl -sf http://localhost:8001/api/v2"]
    CurlFail["curl failed: Exit 1"]
    ParseSync["Parse .blockbook.inSync from JSON"]
    SyncCheck{inSync == "true"?}
    ExitSynced("Exit 0: blockbook is synced")
    ExitSyncing("Exit 1: blockbook is still syncing")

    Start --> CheckDisableFile
    CheckDisableFile -- Yes --> ExitReady
    CheckDisableFile -- No --> CurlRequest
    CurlRequest -- Fail --> CurlFail
    CurlRequest -- Success --> ParseSync
    ParseSync --> SyncCheck
    SyncCheck -- Yes --> ExitSynced
    SyncCheck -- No --> ExitSyncing

Summary

The `readiness.sh` script is a simple yet critical utility script designed to integrate with container orchestration systems by performing a readiness check on the blockbook service. It uses a combination of filesystem flags and API status checks to determine readiness, returning appropriate exit codes for orchestration logic. It requires `curl` and `jq` and assumes the blockbook API is locally reachable. The script helps ensure that traffic is only routed to fully synced and ready service instances.