Keep Streamlit app warm #396
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Keep Streamlit app warm | |
| on: | |
| schedule: | |
| - cron: '17 * * * *' | |
| workflow_dispatch: | |
| jobs: | |
| ping: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Wake and ping app | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| APP_URL="https://mouse-microreactors.streamlit.app" | |
| COOKIE_JAR="$(mktemp)" | |
| HEADERS="$(mktemp)" | |
| STATUS_BODY="$(mktemp)" | |
| curl --fail --silent --show-error --location --max-redirs 20 --max-time 120 \ | |
| --cookie-jar "$COOKIE_JAR" --cookie "$COOKIE_JAR" \ | |
| --retry 3 --retry-delay 10 --retry-all-errors \ | |
| -o /dev/null \ | |
| "$APP_URL/?keepwarm=1" | |
| curl --fail --silent --show-error --location --max-time 60 \ | |
| --cookie-jar "$COOKIE_JAR" --cookie "$COOKIE_JAR" \ | |
| --dump-header "$HEADERS" \ | |
| -o "$STATUS_BODY" \ | |
| "$APP_URL/api/v2/app/status" | |
| echo "Status response: $(cat "$STATUS_BODY")" | |
| if grep -q '"status":12' "$STATUS_BODY"; then | |
| echo "App is suspended; requesting resume." | |
| CSRF_TOKEN="$(awk 'tolower($1) == "x-csrf-token:" { gsub("\r", "", $2); print $2 }' "$HEADERS" | tail -n 1)" | |
| if [ -z "$CSRF_TOKEN" ]; then | |
| echo "No CSRF token returned by Streamlit status endpoint." | |
| exit 1 | |
| fi | |
| curl --fail --silent --show-error --location --max-redirs 20 --max-time 120 \ | |
| --request POST \ | |
| --cookie-jar "$COOKIE_JAR" --cookie "$COOKIE_JAR" \ | |
| --header "x-csrf-token: $CSRF_TOKEN" \ | |
| --header "Origin: $APP_URL" \ | |
| --header "Referer: $APP_URL/?keepwarm=1" \ | |
| -o /dev/null \ | |
| -w "Resume HTTP %{http_code} in %{time_total}s\n" \ | |
| "$APP_URL/api/v2/app/resume" | |
| fi | |
| curl --fail --silent --show-error --location --max-redirs 20 --max-time 600 \ | |
| --cookie-jar "$COOKIE_JAR" --cookie "$COOKIE_JAR" \ | |
| --retry 10 --retry-delay 30 --retry-all-errors \ | |
| -o /dev/null \ | |
| -w "App iframe HTTP %{http_code} in %{time_total}s\n" \ | |
| "$APP_URL/~/+/?keepwarm=1" | |
| curl --fail --silent --show-error --location --max-redirs 20 --max-time 600 \ | |
| --cookie-jar "$COOKIE_JAR" --cookie "$COOKIE_JAR" \ | |
| --retry 10 --retry-delay 30 --retry-all-errors \ | |
| -o /dev/null \ | |
| -w "App health HTTP %{http_code} in %{time_total}s\n" \ | |
| "$APP_URL/~/+/_stcore/health" |