readiness.sh


Overview

`readiness.sh` is a lightweight Bash script designed to serve as a Kubernetes readiness probe or any similar health check mechanism. Its primary function is to determine if a backend service, specifically a **Blockbook API** running locally on port 8001, has fully synchronized and is ready to handle requests.

The script performs the following checks in order:

  1. Disabling readiness probe: It first checks for the presence of a sentinel file /data/disable_readiness. If this file exists, the readiness probe is considered disabled, and the script exits successfully.

  2. Service availability: It then attempts to query the local Blockbook API endpoint /api/v2 using curl. If the service is unreachable or returns an error, the script exits with failure.

  3. Sync status: It parses the JSON response to check if the Blockbook instance is "in sync." If synced, it exits successfully; otherwise, it exits with failure.

This script is typically used by container orchestrators to decide whether the container is ready to accept traffic.


Detailed Explanation

Script Workflow

#!/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

Functional Breakdown

1. Disable Readiness Probe Check

2. Retrieve Blockbook API Status

3. Parse Sync State


Parameters

This script does not take command-line parameters. It relies on:


Return Values


Usage Example

This script is intended to be used as a Kubernetes readiness probe command:

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

The orchestrator runs the script periodically; if it exits with `0`, the pod is marked ready; otherwise, it is not.


Implementation Details


Interaction with Other System Components


Mermaid Flowchart Diagram

flowchart TD
    A[Start] --> B{Check /data/disable_readiness file}
    B -- Exists --> C[Print "readiness probe disabled"]
    C --> D[Exit 0]
    B -- Not Exists --> E[Run curl to http://localhost:8001/api/v2]
    E -- Fail --> F[Exit 1]
    E -- Success --> G[Parse .blockbook.inSync field using jq]
    G --> H{Is inSync == "true"?}
    H -- Yes --> I[Print "blockbook is synced"]
    I --> D
    H -- No --> J[Print "blockbook is still syncing"]
    J --> F

Summary

`readiness.sh` is a simple yet effective shell script designed to monitor the readiness of a Blockbook backend service by checking its synchronization status through an HTTP API. It also includes a mechanism to disable the readiness check via a sentinel file. This script integrates well with container orchestration tools to provide dynamic service readiness feedback, improving deployment stability and traffic routing decisions.