Skip to content

Commit

Permalink
healthcheck for rpc task (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
baabeetaa authored Mar 27, 2022
1 parent 3d76e47 commit 832263c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
36 changes: 36 additions & 0 deletions rpc/healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

BLOCKCHAIN_TIME=$(curl --silent --max-time 3 "http://localhost/status" |jq -r .result.sync_info.latest_block_time)

if [[ ${BLOCKCHAIN_TIME} == "null" ]]; then
echo Status: 502
echo Content-type:text/plain
echo
echo The node is currently not responding.
exit 0
fi

if [[ ! -z "$BLOCKCHAIN_TIME" ]]; then
BLOCKCHAIN_SECS=`date -d $BLOCKCHAIN_TIME +%s`
CURRENT_SECS=`date +%s`

# if within 60 seconds of current time, call it synced and report healthy
BLOCK_AGE=$((${CURRENT_SECS} - ${BLOCKCHAIN_SECS}))

if [[ ${BLOCK_AGE} -le 60 ]]; then
echo Status: 200
echo Content-type:text/plain
echo
echo latest_block_time is less than 60 seconds, this node is considered healthy.
else
echo Status: 503
echo Content-type:text/plain
echo
echo latest_block_time is higher than 60 seconds, this node is responding but not synced
fi
else
echo Status: 502
echo Content-type:text/plain
echo
echo The node is currently not responding.
fi
70 changes: 70 additions & 0 deletions rpc/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

#user http;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

resolver 127.0.0.11 valid=30s; # Docker's DNS server

server {
listen 80;
server_name localhost;
#charset koi8-r;

#access_log logs/host.access.log main;

location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;

proxy_pass http://localhost:26657/;
}

location /healthcheck {
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/healthcheck.sh;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
13 changes: 11 additions & 2 deletions rpc/quicksync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ then
exit
fi

pacman -Syu --noconfirm go git base-devel wget jq $pacman_pkgs
pacman -Syu --noconfirm go git base-devel wget jq nginx spawn-fcgi fcgiwrap $pacman_pkgs

echo "#################################################################################################################"
echo "build from source:"
Expand Down Expand Up @@ -242,5 +242,14 @@ fi
echo "download addrbook..."
curl -Ls $proxy_cache_url$addrbook_url > $node_home/config/addrbook.json

# start chain
echo "#################################################################################################################"
echo "start nginx..."
curl -Ls "https://raw.githubusercontent.com/baabeetaa/cosmosia/main/rpc/nginx.conf" > /etc/nginx/nginx.conf
curl -Ls "https://raw.githubusercontent.com/baabeetaa/cosmosia/main/rpc/healthcheck.sh" > /usr/share/nginx/html/healthcheck.sh
chmod +x /usr/share/nginx/html/healthcheck.sh
spawn-fcgi -s /var/run/fcgiwrap.socket -M 766 /usr/sbin/fcgiwrap
/usr/sbin/nginx

echo "#################################################################################################################"
echo "start chain..."
$HOME/go/bin/$daemon_name start $start_flags

0 comments on commit 832263c

Please sign in to comment.