File size: 1,795 Bytes
f2e524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# ⚡ 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](https://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`