From ec13f6d9bc4bee5755a7b7bc2a0cf010bb1f97c0 Mon Sep 17 00:00:00 2001 From: Prasad Konka Date: Mon, 23 Oct 2023 12:59:46 -0400 Subject: [PATCH] Fix to retry download api --- scripts/save_daily_csv_export.sh | 41 +++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/scripts/save_daily_csv_export.sh b/scripts/save_daily_csv_export.sh index 6424a7a0e..485aeb46c 100644 --- a/scripts/save_daily_csv_export.sh +++ b/scripts/save_daily_csv_export.sh @@ -11,18 +11,41 @@ CURRENT_DATE=$(date +"%m_%d_%Y") FILE_DIR="/lantern-project/onc-open-data/lantern-daily-data/$YEAR/$MONTH" FILE_NAME=${CURRENT_DATE}endpointdata.csv mkdir -p $FILE_DIR - +log_file="/etc/lantern/logs/daily.txt" EXPORTFILE="/etc/lantern/dailyexport/$FILE_NAME" echo $EXPORTFILE #URL="http://127.0.0.1:8989/api/download" URL="https://lantern.healthit.gov/api/daily/download" -curl -o "$EXPORTFILE" "$URL" -mv "$EXPORTFILE" "$FILE_DIR/$FILE_NAME" +expected_response_code=200 +max_attempts=10 # Number of times to attempt the request +current_datetime=$(date +"%Y-%m-%d %H:%M:%S") + +attempts=0 +echo "$current_datetime - Starting daily download.." >> $log_file +while [ $attempts -lt $max_attempts ]; do + response_code=$(curl -s -o "$EXPORTFILE" -w "%{http_code}" "$URL") + + echo "$current_datetime - response code is $response_code" >> $log_file + if [ $response_code -eq $expected_response_code ]; then + current_datetime=$(date +"%Y-%m-%d %H:%M:%S") + echo "$current_datetime - Received a 200 OK response. Writing to GitHub repo." >> $log_file + mv "$EXPORTFILE" "$FILE_DIR/$FILE_NAME" + cd $FILE_DIR + git pull + git checkout main + git add $FILE_NAME + git commit -m "Adding daily export file ${FILE_NAME}" + git push --set-upstream origin main + echo "$current_datetime - Done writing to GitHub repo" >> $log_file + break + else + echo "$current_datetime - Received response code $response_code. Retrying in 3 mins..." >> $log_file + sleep 180 + fi -cd $FILE_DIR + ((attempts++)) +done -git pull -git checkout main -git add $FILE_NAME -git commit -m "Adding daily export file ${FILE_NAME}" -git push --set-upstream origin main \ No newline at end of file +if [ $attempts -eq $max_attempts ]; then + echo "Maximum number of attempts reached. Exiting." +fi