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:
Optionally bypassing readiness verification if a disable flag file exists.
Querying the Blockbook API to check synchronization status.
Returning appropriate exit codes and messages to signal readiness state.
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
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 code0(success), effectively bypassing all readiness checks.
Query Blockbook API
The script performs an HTTP GET request to http://localhost:8001/api/v2 using
curlwith silent and fail flags (-sf).If the
curlrequest fails (e.g., service is down or unreachable), the script immediately exits with code1(failure).
Parse JSON Response
The output from the API is parsed using
jqto extract the.blockbook.inSyncboolean value.
Determine Sync Status
If
.blockbook.inSyncis"true", the script prints"blockbook is synced"and exits with0.Otherwise, it prints
"blockbook is still syncing"and exits with1.
Step-by-Step Breakdown
Step | Command / Logic | Description |
|---|---|---|
1 | [[[ -f "$DISABLE_READINESS_PROBE" ]]](/projects/291/68846) | Check if readiness probe is disabled |
2 | Query Blockbook API | |
3 | 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:
The existence of the file
/data/disable_readinessas a configuration flag.The availability of the Blockbook API at http://localhost:8001/api/v2.
**Exit Codes:**
0(Success): Indicates the service is ready.1(Failure): Indicates the service is not ready or an error occurred.
Usage Example
Assuming the script is executable (`chmod +x readiness.sh`), it can be run directly:
./readiness.sh
**Possible outputs:**
readiness probe disabled— readiness checks are bypassed.blockbook is synced— Blockbook API is ready.blockbook is still syncing— Blockbook API is not ready.
**Integration Example (Kubernetes readinessProbe):**
readinessProbe:
exec:
command:
- /path/to/readiness.sh
initialDelaySeconds: 10
periodSeconds: 5
Implementation Details
Use of
/data/disable_readinessfile:
This file acts as a manual override to disable readiness checks without removing or changing the script. This can be handy during maintenance or debugging.HTTP Request with curl:
The script usescurl -sfto silently fetch the API response and fail gracefully in case of errors (non-2xx responses or connection issues).JSON Parsing with jq:
Parsing JSON is done withjq, a lightweight and powerful command-line JSON processor, extracting the.blockbook.inSyncstatus.Exit Status Logic:
The script’s exit code conveys readiness, allowing container orchestrators or monitoring tools to automate health management.
Interactions with Other System Components
Blockbook API Server (
localhost:8001):
The script directly depends on the Blockbook API being accessible locally on port 8001. This means the script is likely deployed alongside or within the same pod or host as the Blockbook service.Container Orchestration / Monitoring Systems:
This script is meant to be called periodically by readiness probes, health checks, or external monitoring tools to verify the Blockbook service's readiness state.File System (
/data/disable_readiness):
The/datadirectory is used as a shared or injected volume mount or location to control the readiness probe externally.
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:
Provides a manual override mechanism.
Reliably checks the Blockbook API synchronization status.
Returns meaningful exit codes and messages for integration with container orchestration platforms.
Uses standard Linux tools (
curl,jq, bash) for portability and ease of maintenance.
This script is a critical part of ensuring that dependent services only receive traffic when fully ready, thus improving overall system stability and reliability.