Update keep_alive.py
Browse files- keep_alive.py +20 -16
keep_alive.py
CHANGED
|
@@ -1,22 +1,13 @@
|
|
| 1 |
-
import os, requests,
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
SPACE_HOST = os.getenv("SPACE_HOST") or os.getenv("CONTAINER_HOST")
|
| 6 |
|
| 7 |
-
# If still missing, check if we have a custom override
|
| 8 |
if not SPACE_HOST:
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
CF_TOKEN = os.getenv("CLOUDFLARE_API_TOKEN") # Unified token
|
| 12 |
-
CF_ACCOUNT_ID = os.getenv("CF_ACCOUNT_ID")
|
| 13 |
-
|
| 14 |
-
if not all([CF_TOKEN, CF_ACCOUNT_ID, SPACE_HOST]):
|
| 15 |
-
print("⏭️ Keep-alive skipped: Missing CF credentials or SPACE_HOST.")
|
| 16 |
-
print(f" DEBUG: SPACE_HOST={SPACE_HOST}, CF_TOKEN={'Set' if CF_TOKEN else 'Missing'}, CF_ACCOUNT_ID={'Set' if CF_ACCOUNT_ID else 'Missing'}")
|
| 17 |
exit(0)
|
| 18 |
|
| 19 |
-
# Ensure https:// prefix
|
| 20 |
if not SPACE_HOST.startswith("http"):
|
| 21 |
SPACE_HOST = f"https://{SPACE_HOST}"
|
| 22 |
|
|
@@ -32,18 +23,22 @@ addEventListener('scheduled', event => {{
|
|
| 32 |
event.waitUntil(fetch('{URL}').then(r => console.log('Ping status:', r.status)));
|
| 33 |
}});
|
| 34 |
"""
|
|
|
|
| 35 |
try:
|
| 36 |
res = requests.put(
|
| 37 |
f"https://api.cloudflare.com/client/v4/accounts/{CF_ACCOUNT_ID}/workers/scripts/{WORKER_NAME}",
|
| 38 |
headers=HEADERS, data=script
|
| 39 |
)
|
| 40 |
res.raise_for_status()
|
| 41 |
-
print("✅ Worker
|
| 42 |
except Exception as e:
|
| 43 |
print(f"❌ Worker deploy failed: {e}")
|
| 44 |
exit(1)
|
| 45 |
|
| 46 |
-
# 2.
|
|
|
|
|
|
|
|
|
|
| 47 |
cron_payload = {"schedules": [{"cron": "*/10 * * * *"}]}
|
| 48 |
try:
|
| 49 |
res = requests.put(
|
|
@@ -51,7 +46,16 @@ try:
|
|
| 51 |
headers={"Authorization": f"Bearer {CF_TOKEN}", "Content-Type": "application/json"},
|
| 52 |
json=cron_payload
|
| 53 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
res.raise_for_status()
|
| 55 |
print("✅ Cron trigger set (every 10 mins).")
|
| 56 |
except Exception as e:
|
| 57 |
-
print(f"
|
|
|
|
|
|
| 1 |
+
import os, requests, time
|
| 2 |
|
| 3 |
+
CF_TOKEN = os.getenv("CLOUDFLARE_API_TOKEN")
|
| 4 |
+
CF_ACCOUNT_ID = os.getenv("CF_ACCOUNT_ID")
|
| 5 |
SPACE_HOST = os.getenv("SPACE_HOST") or os.getenv("CONTAINER_HOST")
|
| 6 |
|
|
|
|
| 7 |
if not SPACE_HOST:
|
| 8 |
+
print("⏭️ Keep-alive skipped: SPACE_HOST not detected.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
exit(0)
|
| 10 |
|
|
|
|
| 11 |
if not SPACE_HOST.startswith("http"):
|
| 12 |
SPACE_HOST = f"https://{SPACE_HOST}"
|
| 13 |
|
|
|
|
| 23 |
event.waitUntil(fetch('{URL}').then(r => console.log('Ping status:', r.status)));
|
| 24 |
}});
|
| 25 |
"""
|
| 26 |
+
|
| 27 |
try:
|
| 28 |
res = requests.put(
|
| 29 |
f"https://api.cloudflare.com/client/v4/accounts/{CF_ACCOUNT_ID}/workers/scripts/{WORKER_NAME}",
|
| 30 |
headers=HEADERS, data=script
|
| 31 |
)
|
| 32 |
res.raise_for_status()
|
| 33 |
+
print("✅ Worker script deployed.")
|
| 34 |
except Exception as e:
|
| 35 |
print(f"❌ Worker deploy failed: {e}")
|
| 36 |
exit(1)
|
| 37 |
|
| 38 |
+
# 2. Wait briefly for propagation
|
| 39 |
+
time.sleep(2)
|
| 40 |
+
|
| 41 |
+
# 3. Set Cron Trigger
|
| 42 |
cron_payload = {"schedules": [{"cron": "*/10 * * * *"}]}
|
| 43 |
try:
|
| 44 |
res = requests.put(
|
|
|
|
| 46 |
headers={"Authorization": f"Bearer {CF_TOKEN}", "Content-Type": "application/json"},
|
| 47 |
json=cron_payload
|
| 48 |
)
|
| 49 |
+
if res.status_code == 400:
|
| 50 |
+
# Sometimes 400 means the worker isn't ready yet, retry once
|
| 51 |
+
time.sleep(5)
|
| 52 |
+
res = requests.put(
|
| 53 |
+
f"https://api.cloudflare.com/client/v4/accounts/{CF_ACCOUNT_ID}/workers/scripts/{WORKER_NAME}/schedules",
|
| 54 |
+
headers={"Authorization": f"Bearer {CF_TOKEN}", "Content-Type": "application/json"},
|
| 55 |
+
json=cron_payload
|
| 56 |
+
)
|
| 57 |
res.raise_for_status()
|
| 58 |
print("✅ Cron trigger set (every 10 mins).")
|
| 59 |
except Exception as e:
|
| 60 |
+
print(f"⚠️ Cron setup failed (non-critical): {e}")
|
| 61 |
+
print(" The worker is deployed but may not ping automatically. Check CF Dashboard.")
|