-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestrator.py
More file actions
68 lines (52 loc) · 2.28 KB
/
Copy pathorchestrator.py
File metadata and controls
68 lines (52 loc) · 2.28 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Multi-agent orchestrator for processing review posting jobs."""
from __future__ import annotations
import threading
import time
from core import database
from core.review_poster import post_review
class ReviewAgent(threading.Thread):
"""Worker thread that posts reviews pulled from the job queue."""
def __init__(self, agent_id: int) -> None:
super().__init__(name=f"Agent-{agent_id}")
self.agent_id = agent_id
self.daemon = True
def log(self, message: str) -> None:
print(f"[Agent-{self.agent_id}] {message}")
def run(self) -> None: # pragma: no cover - thread logic
while True:
job = database.fetch_next_job()
if not job:
return
job_id = job["job_id"]
site = job["site_name"]
text = job["review_text"]
attempt = 0
backoff = 1.0
while attempt < 3:
try:
# Actual proxy/account assignment would occur here
post_review(site, text)
database.update_job_status(job_id, "Posted")
database.log_review(text, None, site, job.get("account_id"), job.get("proxy_id"), "Posted")
self.log(f"Posted review to {site} – Job ID {job_id}")
break
except Exception as exc: # pragma: no cover - network/selenium errors
attempt += 1
if attempt >= 3:
database.update_job_status(job_id, "Failed", str(exc))
self.log(f"Failed to post review to {site} – Job ID {job_id}: {exc}")
else:
self.log(f"Error posting to {site} (attempt {attempt}); retrying in {backoff}s")
time.sleep(backoff)
backoff *= 2
class Orchestrator:
"""Spawn worker agents to process queued jobs."""
def __init__(self, max_agents: int = 5) -> None:
self.max_agents = max_agents
def run(self) -> None: # pragma: no cover - thread orchestration
threads = [ReviewAgent(i + 1) for i in range(self.max_agents)]
for t in threads:
t.start()
for t in threads:
t.join()
__all__ = ["Orchestrator", "ReviewAgent"]