Spaces:
Paused
Paused
Update worker.py
Browse files
worker.py
CHANGED
|
@@ -1,61 +1,57 @@
|
|
| 1 |
import asyncio
|
| 2 |
-
import scraper
|
| 3 |
import db
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
async def start_worker(worker_id):
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
while True:
|
| 8 |
try:
|
| 9 |
-
# 1. Check if we are allowed to run
|
| 10 |
state = await db.get_state()
|
| 11 |
if state.get("running") != "1" or state.get("paused") == "1":
|
| 12 |
-
await asyncio.sleep(
|
| 13 |
continue
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
word = await
|
| 17 |
-
|
| 18 |
-
# 3. Handle empty queue
|
| 19 |
if not word:
|
| 20 |
-
|
| 21 |
-
await asyncio.sleep(10)
|
| 22 |
continue
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
if status:
|
| 28 |
await db.mark_done(word, status)
|
| 29 |
-
# Only print when we find something interesting (saves CPU)
|
| 30 |
-
if status not in ("TAKEN", "UNAVAILABLE"):
|
| 31 |
-
print(f"[HIT] {word} is {status}")
|
| 32 |
-
|
| 33 |
-
# 5. Micro-sleep to prevent rate limits
|
| 34 |
-
await asyncio.sleep(0.5)
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
await asyncio.sleep(5)
|
| 41 |
-
else:
|
| 42 |
-
await asyncio.sleep(2)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Wait for them to finish (they won't, unless crashed)
|
| 56 |
-
await asyncio.gather(*tasks)
|
| 57 |
-
except Exception:
|
| 58 |
-
await asyncio.sleep(10)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
-
asyncio.run(
|
|
|
|
| 1 |
import asyncio
|
|
|
|
| 2 |
import db
|
| 3 |
+
import scraper
|
| 4 |
+
import traceback
|
| 5 |
|
| 6 |
+
async def start_worker(worker_id: int):
|
| 7 |
+
print(f"⚙️ Worker {worker_id} initialized.")
|
| 8 |
+
r = await db.get_redis()
|
| 9 |
+
|
| 10 |
while True:
|
| 11 |
try:
|
|
|
|
| 12 |
state = await db.get_state()
|
| 13 |
if state.get("running") != "1" or state.get("paused") == "1":
|
| 14 |
+
await asyncio.sleep(2)
|
| 15 |
continue
|
| 16 |
|
| 17 |
+
# Pop a word from the right side of the queue
|
| 18 |
+
word = await r.rpop("frag:queue")
|
|
|
|
|
|
|
| 19 |
if not word:
|
| 20 |
+
await asyncio.sleep(2)
|
|
|
|
| 21 |
continue
|
| 22 |
|
| 23 |
+
# Get a random verified proxy
|
| 24 |
+
proxy = await db.get_random_proxy()
|
| 25 |
+
|
| 26 |
+
# Execute stealth scrape
|
| 27 |
+
status = await scraper.check_fragment(word, proxy_url=proxy)
|
| 28 |
|
| 29 |
+
# If the proxy timed out or crashed, put the word back on the queue and rest
|
| 30 |
+
if status == "ERROR":
|
| 31 |
+
await r.lpush("frag:queue", word)
|
| 32 |
+
await asyncio.sleep(1.5)
|
| 33 |
+
continue
|
| 34 |
+
|
| 35 |
+
# Success! Log it and continue
|
| 36 |
if status:
|
| 37 |
await db.mark_done(word, status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"❌ Worker {worker_id} Error: {str(e)}")
|
| 41 |
+
traceback.print_exc()
|
| 42 |
+
await asyncio.sleep(2)
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
async def main():
|
| 45 |
+
print("🚀 Sniper Engine Starting...")
|
| 46 |
+
concurrency = await db.get_concurrency()
|
| 47 |
+
|
| 48 |
+
# Spawn tasks individually so a single worker crash doesn't kill the pool
|
| 49 |
+
tasks = []
|
| 50 |
+
for i in range(concurrency):
|
| 51 |
+
task = asyncio.create_task(start_worker(i))
|
| 52 |
+
tasks.append(task)
|
| 53 |
+
|
| 54 |
+
await asyncio.gather(*tasks, return_exceptions=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
| 57 |
+
asyncio.run(main())
|