readiness.sh


Overview

`readiness.sh` is a lightweight Bash script designed to serve as a readiness probe typically used in containerized environments such as Kubernetes. Its primary purpose is to determine if a dependent service, specifically a Blockbook API server, is ready to handle requests. The readiness check involves:

This script enables orchestration platforms or monitoring systems to make informed decisions about routing traffic or restarting pods based on the Blockbook service's readiness.


Detailed Explanation

Script Behavior and Flow

  1. Check for Disable Flag

    • The script looks for the presence of a file at /data/disable_readiness.

    • If this file exists, the script prints "readiness probe disabled" and exits with code 0 (success), effectively bypassing all readiness checks.

  2. Query Blockbook API

    • The script performs an HTTP GET request to http://localhost:8001/api/v2 using curl with silent and fail flags (-sf).

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

  3. Parse JSON Response

    • The output from the API is parsed using jq to extract the .blockbook.inSync boolean value.

  4. Determine Sync Status

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

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


Step-by-Step Breakdown

Step

Command / Logic

Description

1

[[[ -f "$DISABLE_READINESS_PROBE" ]]](/projects/291/68846)

Check if readiness probe is disabled

2

curl -sf http://localhost:8001/api/v2

Query Blockbook API

3

jq -r '.blockbook.inSync'

Extract `inSync` status from JSON response

4

Condition check on [IN_SYNC](/projects/291/69231)

Decide readiness based on sync status

5

`exit 0` or `exit 1`

Exit with appropriate status code


Parameters and Return Values

This script does **not** accept any command-line arguments or parameters. It relies on:

**Exit Codes:**


Usage Example

Assuming the script is executable (`chmod +x readiness.sh`), it can be run directly:

./readiness.sh

**Possible outputs:**

**Integration Example (Kubernetes readinessProbe):**

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

Implementation Details


Interactions with Other System Components


Mermaid Diagram

This flowchart illustrates the control flow and decision points within `readiness.sh`:

flowchart TD
    A[Start] --> B{Is /data/disable_readiness file present?}
    B -- Yes --> C[Print "readiness probe disabled"]
    C --> D[Exit 0]
    B -- No --> E[Run curl to fetch Blockbook API]
    E --> F{Did curl succeed?}
    F -- No --> G[Exit 1]
    F -- Yes --> H[Parse JSON to get .blockbook.inSync]
    H --> I{Is 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 yet effective readiness probe script that:

This script is a critical part of ensuring that dependent services only receive traffic when fully ready, thus improving overall system stability and reliability.