Skip to content

Commit 7dcb406

Browse files
JasonHokuclaude
andcommitted
refactor: migrate distribution_routes.py to network gateway
test_worker_connection, notify_workers_to_start, and stop_all_workers now use gateway functions. No more inline urllib imports. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3da8a07 commit 7dcb406

1 file changed

Lines changed: 31 additions & 53 deletions

File tree

distribution_routes.py

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import server
1414
from aiohttp import web
15+
from .network_utils import distribution_get, distribution_post_json
1516

1617

1718
# =============================================================================
@@ -391,39 +392,30 @@ async def test_worker_connection(request):
391392
200 + JSON with worker status on success
392393
200 + JSON with error info on failure (so the UI always gets a parseable response)
393394
"""
394-
import urllib.request
395-
import urllib.error
396-
397395
try:
398396
data = await request.json()
399397
worker_url = data.get("worker_url", "").strip().rstrip("/")
400398

401399
if not worker_url:
402400
return web.json_response({"reachable": False, "error": "Empty URL"})
403401

404-
req = urllib.request.Request(
402+
status, resp_data = distribution_get(
405403
f"{worker_url}/distribution/worker_status",
406-
method="GET"
404+
timeout=5
407405
)
408-
409-
with urllib.request.urlopen(req, timeout=5) as resp:
410-
result = json.loads(resp.read().decode("utf-8"))
411-
return web.json_response({
412-
"reachable": True,
413-
"status": result.get("status", "unknown"),
414-
"worker_id": result.get("worker_id", ""),
415-
"jobs_processed": result.get("jobs_processed", 0)
416-
})
417-
418-
except urllib.error.HTTPError as e:
406+
result = json.loads(resp_data.decode("utf-8"))
419407
return web.json_response({
420-
"reachable": False,
421-
"error": f"HTTP {e.code}"
408+
"reachable": True,
409+
"status": result.get("status", "unknown"),
410+
"worker_id": result.get("worker_id", ""),
411+
"jobs_processed": result.get("jobs_processed", 0)
422412
})
413+
423414
except Exception as e:
415+
error_msg = f"HTTP {e.code}" if hasattr(e, 'code') else str(e)
424416
return web.json_response({
425417
"reachable": False,
426-
"error": str(e)
418+
"error": error_msg
427419
})
428420

429421

@@ -567,9 +559,6 @@ def notify_workers_to_start(worker_urls, master_url, session_name, sync_models_t
567559
Returns:
568560
List of (url, success, message) tuples
569561
"""
570-
import urllib.request
571-
import urllib.error
572-
573562
results = []
574563

575564
for url in worker_urls:
@@ -578,36 +567,30 @@ def notify_workers_to_start(worker_urls, master_url, session_name, sync_models_t
578567
continue
579568

580569
try:
581-
data = json.dumps({
582-
"master_url": master_url,
583-
"session_name": session_name,
584-
"sync_models_to_workers": sync_models_to_workers
585-
}).encode("utf-8")
586-
587-
req = urllib.request.Request(
570+
status, resp_data = distribution_post_json(
588571
f"{url}/distribution/start_worker",
589-
data=data,
590-
headers={"Content-Type": "application/json"},
591-
method="POST"
572+
{
573+
"master_url": master_url,
574+
"session_name": session_name,
575+
"sync_models_to_workers": sync_models_to_workers
576+
},
577+
timeout=10
592578
)
579+
result = json.loads(resp_data.decode("utf-8"))
580+
worker_id = result.get("worker_id", "unknown")
581+
print(f"[Distribution] ✅ Worker started at {url} (id: {worker_id})")
582+
results.append((url, True, f"Started (id: {worker_id})"))
593583

594-
with urllib.request.urlopen(req, timeout=10) as resp:
595-
result = json.loads(resp.read().decode("utf-8"))
596-
worker_id = result.get("worker_id", "unknown")
597-
print(f"[Distribution] ✅ Worker started at {url} (id: {worker_id})")
598-
results.append((url, True, f"Started (id: {worker_id})"))
599-
600-
except urllib.error.HTTPError as e:
601-
if e.code == 409:
584+
except Exception as e:
585+
if hasattr(e, 'code') and e.code == 409:
602586
print(f"[Distribution] ℹ️ Worker at {url} already running")
603587
results.append((url, True, "Already running"))
604-
else:
588+
elif hasattr(e, 'code'):
605589
print(f"[Distribution] ❌ Failed to start worker at {url}: HTTP {e.code}")
606590
results.append((url, False, f"HTTP {e.code}"))
607-
608-
except Exception as e:
609-
print(f"[Distribution] ❌ Failed to contact worker at {url}: {e}")
610-
results.append((url, False, str(e)))
591+
else:
592+
print(f"[Distribution] ❌ Failed to contact worker at {url}: {e}")
593+
results.append((url, False, str(e)))
611594

612595
return results
613596

@@ -619,22 +602,17 @@ def stop_all_workers(worker_urls):
619602
Args:
620603
worker_urls: List of worker base URLs
621604
"""
622-
import urllib.request
623-
import urllib.error
624-
625605
for url in worker_urls:
626606
url = url.strip().rstrip("/")
627607
if not url:
628608
continue
629609

630610
try:
631-
req = urllib.request.Request(
611+
distribution_post_json(
632612
f"{url}/distribution/stop_worker",
633-
data=b"{}",
634-
headers={"Content-Type": "application/json"},
635-
method="POST"
613+
{},
614+
timeout=5
636615
)
637-
urllib.request.urlopen(req, timeout=5)
638616
print(f"[Distribution] 🛑 Stopped worker at {url}")
639617
except Exception:
640618
pass

0 commit comments

Comments
 (0)