Skip to content

Commit 9a97d4d

Browse files
committed
feat: advanced keep-alive using playwright for streamlit wake-up
1 parent b3c738c commit 9a97d4d

File tree

2 files changed

+65
-26
lines changed

2 files changed

+65
-26
lines changed

.github/workflows/keep-alive.yml

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,30 @@ jobs:
1212
timeout-minutes: 5
1313

1414
steps:
15-
- name: Ping Backend (Render) with Retry
15+
- uses: actions/checkout@v3
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v3
19+
with:
20+
python-version: "3.10"
21+
22+
- name: Install Playwright
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install playwright requests
26+
playwright install chromium
27+
28+
- name: Ping Backends (Render)
1629
run: |
17-
echo "Pinging Render backend..."
18-
for i in 1 2 3; do
19-
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 90 https://aio-health-backend.onrender.com/healthz)
20-
echo "Attempt $i: HTTP $HTTP_CODE"
21-
if [ "$HTTP_CODE" = "200" ]; then
22-
echo "✅ Backend is UP"
23-
exit 0
24-
fi
25-
echo "Waiting 30s for cold start..."
26-
sleep 30
27-
done
28-
echo "⚠️ Backend may be slow to wake up"
29-
30-
- name: Ping Frontend (Streamlit) with Retry
30+
echo "Pinging primary backend (Fastest/New)..."
31+
# Render wakes up if it receives any request. It might take 50s, so we give it a 30s timeout
32+
# Even if curl times out, the request reached Render and it will start waking up
33+
curl -s -o /dev/null -w "%{http_code}" --max-time 30 https://aio-health-backend-gdzi.onrender.com/healthz || echo "Primary ping sent"
34+
35+
echo "Pinging secondary backend (Fallback)..."
36+
curl -s -o /dev/null -w "%{http_code}" --max-time 30 https://aio-health-backend.onrender.com/healthz || echo "Secondary ping sent"
37+
38+
- name: Ping Frontend (Streamlit Headless)
3139
run: |
32-
echo "Pinging Streamlit frontend..."
33-
for i in 1 2; do
34-
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 60 https://ai-healthcare-system.streamlit.app/)
35-
echo "Attempt $i: HTTP $HTTP_CODE"
36-
if [ "$HTTP_CODE" = "200" ]; then
37-
echo "✅ Frontend is UP"
38-
exit 0
39-
fi
40-
sleep 15
41-
done
42-
echo "⚠️ Frontend may need manual wake-up"
40+
echo "Launching headless browser to check if Streamlit is asleep..."
41+
python scripts/keep_alive_frontend.py

scripts/keep_alive_frontend.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import os
2+
import sys
3+
import time
4+
from playwright.sync_api import sync_playwright
5+
6+
def main():
7+
print("Starting Playwright keep-alive script...")
8+
with sync_playwright() as p:
9+
browser = p.chromium.launch(headless=True)
10+
page = browser.new_page()
11+
print("Navigating to Streamlit app...")
12+
13+
try:
14+
# We don't await full load if it takes too long, we just want to hit the page
15+
page.goto("https://ai-healthcare-system.streamlit.app/", timeout=60000)
16+
print("Page loaded. Checking for sleep overlay...")
17+
18+
# Wait a few seconds for the DOM to settle or the sleep overlay to appear
19+
page.wait_for_timeout(5000)
20+
21+
# Streamlit "app has gone to sleep" button text
22+
button = page.get_by_text("Yes, get this app back up!", exact=True)
23+
24+
if button.count() > 0 and button.first.is_visible():
25+
print("💤 App is asleep! Clicking the wake-up button...")
26+
button.first.click()
27+
print("✅ Clicked. Waiting 10 seconds for boot process to start...")
28+
page.wait_for_timeout(10000)
29+
else:
30+
print("☀️ App appears to be awake (or wake-up button not found).")
31+
32+
except Exception as e:
33+
print(f"❌ Playwright encountered an error: {e}")
34+
35+
finally:
36+
print("Closing browser.")
37+
browser.close()
38+
39+
if __name__ == "__main__":
40+
main()

0 commit comments

Comments
 (0)