dev.altai / KEEP_ALIVE_GUIDE.md
prince1604
Enhance API stability: Add KeepAlive, increase timeout, and optimize crawler threads
f2e524e
|
Raw
History Blame Contribute Delete
1.8 kB

⚡ How to Keep Your API Running 24/7 (Perfect Solution)

You asked for a "perfect solution" to prevent your server from sleeping or stopping. I have updated your code to be more robust, but Hugging Face Spaces (Free Tier) will still sleep after 48 hours or inactivity unless you do this external step.

1. Code Changes I Made (Already Done)

  • Automatic Heartbeat: I added a background system in api.py that logs "System Active" every minute. This helps the server look busy.
  • Increased Timeouts: I increased the server timeout to 1000 seconds. This prevents the API from crashing/stopping when you crawl large websites.
  • Optimized Resources: I balanced the crawler threads to prevent "Out of Memory" crashes.

2. The Final "Perfect" Step (YOU MUST DO THIS)

To effectively "cheat" the sleep timer, you need an external service to ping your API every 5 minutes.

✅ Use UptimeRobot (Free & Reliable)

  1. Go to UptimeRobot.com and create a free account.
  2. Click "Add New Monitor".
  3. Monitor Type: Choose HTTP(s).
  4. Friendly Name: Alt Scraper API.
  5. URL (or IP): Paste your space URL adding /health at the end.
    • Example: https://ubuntu593-alt-scraper-api.hf.space/health
  6. Monitoring Interval: Set to 5 minutes (Important!).
  7. Create Monitor.

Why this works?

  • Every 5 minutes, UptimeRobot hits your /health endpoint.
  • Your API responds "alive".
  • Hugging Face sees this as "active traffic" and resets the sleep timer.
  • My new "Heartbeat" code ensures the internal logs are also updating, so the container looks busy from the inside too.

Now, redeploy your code:

  1. git add .
  2. git commit -m "Add KeepAlive system and optimize timeouts"
  3. git push origin main