readiness.sh


Overview

`readiness.sh` is a lightweight Bash script designed to serve as a readiness probe in a containerized or microservices environment. Its primary purpose is to check whether a backend service, specifically the **Blockbook API**, is fully synchronized and ready to serve requests. This script is typically used by container orchestrators (e.g., Kubernetes) to determine if the application instance should receive traffic.

The script checks for a "disable readiness probe" flag file and, if absent, queries the Blockbook API’s status endpoint. Based on the synchronization status returned, it exits with a success or failure code, signaling readiness or non-readiness.


Detailed Explanation

Script Workflow & Logic

  1. Disable Readiness Probe Check

    • The script first checks if a special file /data/disable_readiness exists.

    • If this file exists, the readiness probe is considered disabled.

    • The script immediately outputs "readiness probe disabled" and exits with status code 0 (success).

  2. Query Blockbook API Status

    • If the disable file is not present, the script sends a GET request to http://localhost:8001/api/v2 using curl.

    • The -s flag silences progress, and -f causes curl to fail if the HTTP response code is 400 or greater.

    • If curl fails (e.g., service is down or endpoint unreachable), the script exits with status 1 (failure).

  3. Parse Synchronization Status

    • The JSON response is parsed using jq to extract the value of .blockbook.inSync.

    • If the value is "true", the script prints "blockbook is synced" and exits with status 0.

    • Otherwise, it prints "blockbook is still syncing" and exits with status 1.


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

Line

Description

1

Shebang to specify Bash shell execution

3

Defines a variable pointing to the disable flag file location

5-8

Checks if disable file exists; if yes, prints message and exits with success

10

Uses `curl` to silently fetch the API status; on failure, exits with failure

11

Parses JSON output with `jq` to read the `.blockbook.inSync` boolean flag

13-17

If `inSync` is true, readiness is confirmed; otherwise, reports syncing and exits with failure


Parameters and Return Values


Usage Example

This script is not intended to be run manually but invoked by container orchestration platforms as a readiness probe command.

Example command to run manually:

./readiness.sh

Possible outputs:


Implementation Details


Interaction with Other System Components


Mermaid Flowchart Diagram

flowchart TD
    Start([Start])
    CheckDisableFile{Does /data/disable_readiness file exist?}
    DisableTrue["Print 'readiness probe disabled' \nExit 0"]
    CurlAPI["Execute curl GET http://localhost:8001/api/v2"]
    CurlFail{Did curl succeed?}
    ExitFail["Exit 1"]
    ParseJSON["Parse JSON with jq: .blockbook.inSync"]
    InSyncTrue{Is inSync == 'true'?}
    Synced["Print 'blockbook is synced' \nExit 0"]
    Syncing["Print 'blockbook is still syncing' \nExit 1"]

    Start --> CheckDisableFile
    CheckDisableFile -- Yes --> DisableTrue
    CheckDisableFile -- No --> CurlAPI
    CurlAPI --> CurlFail
    CurlFail -- No --> ExitFail
    CurlFail -- Yes --> ParseJSON
    ParseJSON --> InSyncTrue
    InSyncTrue -- Yes --> Synced
    InSyncTrue -- No --> Syncing

Summary

`readiness.sh` is a minimal, robust readiness probe script that helps orchestrators determine when the Blockbook backend is ready to serve traffic. It elegantly combines file-based toggling, HTTP status querying, and JSON parsing to provide clear readiness signals, making it a vital utility in cloud-native deployment pipelines.