Skip to content

Commit

Permalink
feat: check whether orderer is a RAFT leader before starting app
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Wu committed Sep 6, 2023
1 parent f4a9209 commit 6b53bb5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
FROM eclipse-temurin:11-jre-focal

RUN apt-get update \
&& apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/* # Clean up to reduce image size

WORKDIR /app

COPY build/install/blocc-temp-humidity-app/ .
COPY scripts/raft-leader-check.sh .

ENTRYPOINT ["./bin/blocc-temp-humidity-app"]
ENTRYPOINT ["./raft-leader-check.sh "]
31 changes: 31 additions & 0 deletions scripts/raft-leader-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

retries=20

while [[ $retries -gt 0 ]]; do
curl -s http://${FABRIC_ORDERER_ADDRESS}/metrics > /tmp/orderer_metrics 2> /dev/null

if [ $? -ne 0 ]; then
echo "Failed to fetch metrics from orderer. Retrying..."
((retries--))
sleep 3
continue
fi

STATUS=$(grep 'consensus_etcdraft_is_leader' /tmp/orderer_metrics | grep -o '[0-1]$')

if [ "$STATUS" == "1" ]; then
echo "Orderer is RAFT leader. Starting blocc-temp-humidity-app..."
./bin/blocc-temp-humidity-app # Start the app here
break
else
echo "Orderer is not RAFT leader. Retrying..."
((retries--))
sleep 3
fi
done

if [[ $retries -eq 0 ]]; then
echo "Failed to confirm RAFT leader status after several attempts."
exit 1
fi

0 comments on commit 6b53bb5

Please sign in to comment.