Muttered3 commited on
Commit
356e2ca
·
verified ·
1 Parent(s): 2814d35

Update worker.py

Browse files
Files changed (1) hide show
  1. worker.py +37 -41
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
- # Reduced printing saves massive CPU cycles
 
 
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(5) # Deep sleep when paused or stopped
13
  continue
14
 
15
- # 2. Get next word
16
- word = await db.pop_word()
17
-
18
- # 3. Handle empty queue
19
  if not word:
20
- # No words left? Sleep longer to save CPU
21
- await asyncio.sleep(10)
22
  continue
23
 
24
- # 4. Perform the check using the NEW function name
25
- status = await scraper.check_fragment(word)
 
 
 
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
- # If database is full or connection drops, sleep 5 seconds
38
- err = str(e).lower()
39
- if "max number of clients" in err or "too many connections" in err:
40
- await asyncio.sleep(5)
41
- else:
42
- await asyncio.sleep(2)
43
 
44
- # RENAMED from run_workers() to run_worker() to match main.py
45
- async def run_worker():
46
- print("⚙️ Workers initializing...")
47
- while True:
48
- try:
49
- # Fetch desired speed from DB
50
- concurrency = await db.get_concurrency()
51
-
52
- # Start the requested number of workers
53
- tasks = [asyncio.create_task(start_worker(i)) for i in range(concurrency)]
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(run_worker())
 
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())