From 4bd624aa0839ac1bb9b754467d114f4891a71e6d Mon Sep 17 00:00:00 2001
From: Anthony Graignic <anthony.graignic@uca.fr>
Date: Fri, 8 Mar 2024 16:04:01 +0100
Subject: [PATCH] Add healthcheck before starting simulation script

---
 scripts/run_simulation_2_nodes.sh | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/scripts/run_simulation_2_nodes.sh b/scripts/run_simulation_2_nodes.sh
index a4104d9..27e8dcc 100755
--- a/scripts/run_simulation_2_nodes.sh
+++ b/scripts/run_simulation_2_nodes.sh
@@ -7,9 +7,27 @@ export MNEMONIC='before exclude interest push zone jealous boat ice chimney juic
 export RPC_URL=http://127.0.0.1:8545;
 export CHAIN_ID=636363;
 
+# Check if RPC node is available before starting the simulation
+MAX_ATTEMPTS=5
+DELAY_BETWEEN_ATTEMPS=3
+attempt=1
+
+while true; do
+  # test=$(curl --fail --max-time 3 --header 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83 }' --location $RPC_URL | grep '"result":"0x0"')
+  response=$(curl -s -o /dev/null --header 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83 }' -w "%{http_code}" $RPC_URL)
+  if [ "$response" == "200" ]; then
+    break
+  fi
+
+  echo "RPC node not ready, let's wait $DELAY_BETWEEN_ATTEMPS seconds"
+  sleep "$DELAY_BETWEEN_ATTEMPS"
+
+  attempt=$((attempt + 1))
+  if [ "$attempt" -gt "$MAX_ATTEMPTS" ]; then
+    exit 1
+  fi
+done
 
-# ./target/release/testnet-injector simulate-bx-from-csv ../movement-data/public-transport/MF_user_mobility_june-to-august-2023_mulhouse_dataset/anonymized_june-to-august-2023_mulhouse_dataset.csv $TOTAL_SIMULATION_TIME &
-# Send BX
 ./target/release/testnet-injector simulate-bx-from-csv ./data/anonymized_june-to-november-2023_france_dataset.csv $TOTAL_SIMULATION_TIME &
 
 echo "TOTAL_SIMULATION_TIME = $TOTAL_SIMULATION_TIME and block time = $BLOCK_TIME"
-- 
GitLab