readiness.sh

Overview

`readiness.sh` is a lightweight shell script designed as a readiness probe for a service component named **blockbook**. Its primary purpose is to determine whether the blockbook service is ready to accept traffic by checking its synchronization status. The script is intended to be used in container orchestration environments (like Kubernetes) as a readiness check endpoint.

The script performs the following key functions:

This readiness check helps orchestrators decide if the service can receive traffic or if it should wait until synchronization is complete.


Detailed Explanation

Script Logic

The script performs a series of conditional checks and makes an HTTP request to the blockbook API using `curl`, then parses the JSON response with `jq`.


Variables


Workflow & Steps

  1. Check for readiness disable file

    if [[ -f "$DISABLE_READINESS_PROBE" ]]; then
      echo "readiness probe disabled"
      exit 0
    fi
    

    If the file /data/disable_readiness exists, the script exits with code 0 (success), indicating readiness is disabled and the pod/service is considered ready.

  2. Check blockbook API availability and get status

    STATUS=$(curl -sf http://localhost:8001/api/v2) || exit 1
    

    Performs a silent, fail-fast HTTP GET request to the blockbook API endpoint at localhost:8001/api/v2. If the request fails, the script exits with code 1 (not ready).

  3. Parse synchronization status

    IN_SYNC=$(echo $STATUS | jq -r '.blockbook.inSync')
    

    Parses the JSON response to extract the .blockbook.inSync field, which indicates whether blockbook is synced.

  4. Evaluate synchronization result

    if [[ $IN_SYNC == "true" ]]; then
        echo "blockbook is synced"
        exit 0
    fi
    
    echo "blockbook is still syncing"
    exit 1
    

    If blockbook is synced, the script returns success (exit 0), otherwise returns failure (exit 1) indicating the service is not ready.


Exit Codes


Usage Example

This script is typically invoked by container orchestrators as a readiness probe command:

./readiness.sh

Important Implementation Details


Interaction with Other System Components


Mermaid Flowchart Diagram

flowchart TD
    A[Start readiness.sh] --> B{File /data/disable_readiness exists?}
    B -- Yes --> C[Print "readiness probe disabled"]
    C --> D[Exit 0 (Ready)]
    B -- No --> E[HTTP GET http://localhost:8001/api/v2]
    E --> F{Request successful?}
    F -- No --> G[Exit 1 (Not ready)]
    F -- Yes --> H[Parse .blockbook.inSync from JSON]
    H --> I{inSync == "true"?}
    I -- Yes --> J[Print "blockbook is synced"]
    J --> D
    I -- No --> K[Print "blockbook is still syncing"]
    K --> G

Summary

`readiness.sh` is a simple but effective readiness probe script that checks if the blockbook service is synced and ready. It provides an easy toggle to disable readiness checks through a file, uses robust HTTP and JSON parsing tools, and integrates smoothly with container orchestration environments to improve service reliability and traffic routing decisions.