readiness.sh

Overview

`readiness.sh` is a lightweight Bash script designed as a readiness probe for a service that exposes an HTTP API on `localhost:8001`. Its primary function is to determine whether the service, specifically a component named "blockbook", is ready to serve requests by checking its synchronization status. The script can be used in container orchestration environments (e.g., Kubernetes) or monitoring systems to signal if the service is ready or still initializing.

The script also supports a simple manual override mechanism: if a specific file (`/data/disable_readiness`) exists, the readiness probe is disabled, and the script immediately reports readiness.


Detailed Explanation

Variables


Script Flow and Functions

This script does not define explicit functions or classes since it is a shell script. However, it follows a procedural flow with key steps:

  1. Check for Disable File

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

    • Usage: This allows manual override of readiness checks, effectively signaling the system that the service is ready regardless of actual sync status.

  2. Query the Service API

    STATUS=$(curl -sf http://localhost:8001/api/v2) || exit 1
    
    • Uses curl to silently (-s) fetch the API endpoint at http://localhost:8001/api/v2.

    • The -f flag ensures curl fails silently on server errors (HTTP status >= 400).

    • If the API call fails (e.g., service down or network error), the script exits immediately with status 1 (failure).

    • The JSON response is stored in the variable STATUS.

  3. Parse Synchronization Status

    IN_SYNC=$(echo $STATUS | jq -r '.blockbook.inSync')
    
    • Uses jq (a lightweight JSON processor) to extract the value of .blockbook.inSync from the JSON response.

    • The -r flag outputs raw strings without quotes.

    • Expected values: "true" or "false" (string literals).

  4. Evaluate Sync Status and Exit

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

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


Usage Example

Suppose you want to use this script as a readiness probe in a Kubernetes pod definition:

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

Important Implementation Details


Interaction with Other Components


Mermaid Flowchart Diagram

flowchart TD
    Start([Start])
    CheckDisableFile{Is /data/disable_readiness present?}
    PrintDisabled["Print 'readiness probe disabled'"]
    ExitSuccess0[Exit 0]

    CurlAPI["Run: curl -sf http://localhost:8001/api/v2"]
    CurlFail{Did curl succeed?}
    ExitFailure1[Exit 1]

    ParseSync["Parse '.blockbook.inSync' from JSON"]
    CheckSyncTrue{Is 'inSync' == 'true'?}
    PrintSynced["Print 'blockbook is synced'"]
    PrintSyncing["Print 'blockbook is still syncing'"]

    %% Flow connections
    Start --> CheckDisableFile
    CheckDisableFile -- Yes --> PrintDisabled --> ExitSuccess0
    CheckDisableFile -- No --> CurlAPI
    CurlAPI --> CurlFail
    CurlFail -- No --> ExitFailure1
    CurlFail -- Yes --> ParseSync --> CheckSyncTrue
    CheckSyncTrue -- Yes --> PrintSynced --> ExitSuccess0
    CheckSyncTrue -- No --> PrintSyncing --> ExitFailure1

Summary

This small but critical script provides an automated readiness check for the "blockbook" service by verifying its synchronization status via a local HTTP API. It supports disabling readiness probes via a simple file-based toggle and uses standard Unix utilities (`curl` and `jq`). Its clean exit codes and status messages make it ideal for integration with container orchestrators or monitoring frameworks to improve deployment reliability and observability.