readiness.sh


Overview

`readiness.sh` is a lightweight shell script designed to serve as a readiness probe for a service that uses a local Blockbook API. Its primary function is to determine if the Blockbook instance running on `localhost` is fully synchronized ("in sync") and ready to serve traffic. This script is typically used in container orchestration environments (e.g., Kubernetes) to indicate whether the application is ready to receive requests.

The script performs the following key actions:


Detailed Explanation

Script Variables and Constants

Main Logic Flow

  1. Readiness Probe Disable Check

    if [[ -f "$DISABLE_READINESS_PROBE" ]]; then
      echo "readiness probe disabled"
      exit 0
    fi
    
    • If the file /data/disable_readiness exists, the script immediately outputs "readiness probe disabled" and exits with status code 0 indicating readiness (probe disabled means always ready).

    • This allows manual control over readiness status without modifying the service or the probe itself.

  2. Fetch Blockbook Status

    STATUS=$(curl -sf http://localhost:8001/api/v2) || exit 1
    
    • Uses curl to silently (-s) fetch JSON status from Blockbook's local API on port 8001.

    • The -f flag makes curl fail silently on HTTP errors.

    • If the request fails, the script immediately exits with code 1 indicating the service is not ready.

  3. Parse Synchronization Status

    IN_SYNC=$(echo $STATUS | jq -r '.blockbook.inSync')
    
    • Pipes the JSON response to jq to extract the .blockbook.inSync boolean field.

    • The -r flag outputs raw string without quotes.

    • This field indicates if Blockbook is fully synchronized with the blockchain.

  4. Determine Readiness

    if [[ $IN_SYNC == "true" ]]; then
        echo "blockbook is synced"
        exit 0
    fi
    
    echo "blockbook is still syncing"
    exit 1
    
    • If inSync is "true", the script prints "blockbook is synced" and exits with code 0 (ready).

    • Otherwise, it prints "blockbook is still syncing" and exits with code 1 (not ready).


Usage Example

This script is intended to be used as a Kubernetes readiness probe command or similar health check mechanism.

readinessProbe:
  exec:
    command:
      - /bin/bash
      - /path/to/readiness.sh
  initialDelaySeconds: 10
  periodSeconds: 15
bash readiness.sh
# Output: "blockbook is synced" (if ready) or "blockbook is still syncing"
# Exit code: 0 if ready, 1 if not
touch /data/disable_readiness
bash readiness.sh
# Output: "readiness probe disabled"
# Exit code: 0

Implementation Details


Interaction with Other System Components

This script acts as a bridge between the internal Blockbook sync state and external load balancers or orchestrators controlling service availability.


Mermaid Flowchart Diagram

flowchart TD
    A[Start readiness.sh] --> B{Does /data/disable_readiness exist?}
    B -- Yes --> C[Print "readiness probe disabled"]
    C --> D[Exit 0]
    B -- No --> E[Send curl request to http://localhost:8001/api/v2]
    E --> F{Curl success?}
    F -- No --> G[Exit 1]
    F -- Yes --> H[Parse JSON with jq for .blockbook.inSync]
    H --> I{Is inSync == "true"?}
    I -- Yes --> J[Print "blockbook is synced"]
    J --> D[Exit 0]
    I -- No --> K[Print "blockbook is still syncing"]
    K --> G[Exit 1]

Summary

This simple but effective shell script encapsulates the readiness criteria for a Blockbook-based service by:

It is well suited for integration into container orchestration environments to ensure traffic is only routed to fully synced Blockbook instances. The minimal dependencies and straightforward logic make it reliable and easy to maintain.