PERDYPTO commited on
Rename app/main.py to main.py
Browse files- app/main.py +0 -506
- main.py +23 -0
app/main.py
DELETED
|
@@ -1,506 +0,0 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
-
import os
|
| 3 |
-
import secrets
|
| 4 |
-
import threading
|
| 5 |
-
import time
|
| 6 |
-
from contextlib import asynccontextmanager, suppress
|
| 7 |
-
from dataclasses import dataclass
|
| 8 |
-
from datetime import datetime, timezone
|
| 9 |
-
from typing import Callable, Dict, List, Literal, Optional
|
| 10 |
-
|
| 11 |
-
from fastapi import Body, Depends, FastAPI, HTTPException, Query
|
| 12 |
-
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
| 13 |
-
from pydantic import BaseModel, Field
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def _now() -> float:
|
| 17 |
-
return time.time()
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def iso_from_ts(ts: float) -> str:
|
| 21 |
-
return (
|
| 22 |
-
datetime.fromtimestamp(ts, tz=timezone.utc)
|
| 23 |
-
.isoformat(timespec="seconds")
|
| 24 |
-
.replace("+00:00", "Z")
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def generate_job_id() -> str:
|
| 29 |
-
return "job_" + secrets.token_hex(6)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
@dataclass
|
| 33 |
-
class Settings:
|
| 34 |
-
github_shared_secret: str = os.getenv("GITHUB_SHARED_SECRET", "")
|
| 35 |
-
desktop_api_key: str = os.getenv("DESKTOP_API_KEY", "")
|
| 36 |
-
claim_timeout_buffer: float = float(os.getenv("CLAIM_TIMEOUT_BUFFER_SECONDS", "60"))
|
| 37 |
-
liveness_window: float = float(os.getenv("LIVENESS_WINDOW_SECONDS", "60"))
|
| 38 |
-
max_retries: int = int(os.getenv("MAX_RETRIES", "3"))
|
| 39 |
-
retention_seconds: float = float(os.getenv("RESULT_RETENTION_SECONDS", "3600"))
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
class Environment(BaseModel):
|
| 43 |
-
platform: str
|
| 44 |
-
os_version: str
|
| 45 |
-
python_versions: List[str]
|
| 46 |
-
notes: Optional[str] = None
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
class WorkerRegisterRequest(BaseModel):
|
| 50 |
-
worker_id: int
|
| 51 |
-
url: str
|
| 52 |
-
environment: Environment
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
class PollRequest(BaseModel):
|
| 56 |
-
worker_id: int
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
class DoneRequest(BaseModel):
|
| 60 |
-
worker_id: int
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
class JobSubmission(BaseModel):
|
| 64 |
-
job_id: Optional[str] = None
|
| 65 |
-
python_version: str
|
| 66 |
-
platform: str = "windows"
|
| 67 |
-
entry_point: str
|
| 68 |
-
files: Dict[str, str]
|
| 69 |
-
dependencies: List[str] = Field(default_factory=list)
|
| 70 |
-
network_required: bool = False
|
| 71 |
-
stdin: Optional[str] = None
|
| 72 |
-
timeout: float
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
Category = Literal[
|
| 76 |
-
"success",
|
| 77 |
-
"timeout",
|
| 78 |
-
"dependency_install_failed",
|
| 79 |
-
"infra_failure",
|
| 80 |
-
"checker_error",
|
| 81 |
-
"not_applicable",
|
| 82 |
-
]
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
class ResultObject(BaseModel):
|
| 86 |
-
job_id: str
|
| 87 |
-
stdout: str
|
| 88 |
-
stderr: str
|
| 89 |
-
exit_code: int
|
| 90 |
-
timed_out: bool
|
| 91 |
-
category: Category
|
| 92 |
-
failure_reason: Optional[str] = None
|
| 93 |
-
network_activity_detected: Optional[bool] = None
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
class MemoryStore:
|
| 97 |
-
def __init__(self, settings: Settings, clock: Callable[[], float] = _now):
|
| 98 |
-
self.settings = settings
|
| 99 |
-
self.clock = clock
|
| 100 |
-
self.lock = threading.RLock()
|
| 101 |
-
self.workers: Dict[int, dict] = {}
|
| 102 |
-
self.jobs: Dict[str, dict] = {}
|
| 103 |
-
|
| 104 |
-
def register_worker(self, worker_id: int, url: str, environment: dict) -> dict:
|
| 105 |
-
with self.lock:
|
| 106 |
-
now = self.clock()
|
| 107 |
-
worker = self.workers.get(worker_id)
|
| 108 |
-
|
| 109 |
-
if worker is None:
|
| 110 |
-
worker = {
|
| 111 |
-
"worker_id": worker_id,
|
| 112 |
-
"url": url,
|
| 113 |
-
"environment": environment,
|
| 114 |
-
"registered_at": now,
|
| 115 |
-
"last_seen_at": now,
|
| 116 |
-
"reported_done": False,
|
| 117 |
-
}
|
| 118 |
-
self.workers[worker_id] = worker
|
| 119 |
-
else:
|
| 120 |
-
worker["url"] = url
|
| 121 |
-
worker["environment"] = environment
|
| 122 |
-
worker["last_seen_at"] = now
|
| 123 |
-
worker["registered_at"] = now
|
| 124 |
-
worker["reported_done"] = False
|
| 125 |
-
|
| 126 |
-
return self._worker_public_locked(worker_id, now)
|
| 127 |
-
|
| 128 |
-
def poll_job(self, worker_id: int):
|
| 129 |
-
with self.lock:
|
| 130 |
-
worker = self.workers.get(worker_id)
|
| 131 |
-
if worker is None:
|
| 132 |
-
raise KeyError("worker not registered")
|
| 133 |
-
|
| 134 |
-
now = self.clock()
|
| 135 |
-
old_status = self._status_locked(worker_id, now)
|
| 136 |
-
worker["last_seen_at"] = now
|
| 137 |
-
|
| 138 |
-
if old_status != "idle":
|
| 139 |
-
return None
|
| 140 |
-
|
| 141 |
-
eligible = []
|
| 142 |
-
for job in self.jobs.values():
|
| 143 |
-
if job["status"] != "queued":
|
| 144 |
-
continue
|
| 145 |
-
if job["platform"] != worker["environment"].get("platform"):
|
| 146 |
-
continue
|
| 147 |
-
if job["python_version"] in worker["environment"].get("python_versions", []):
|
| 148 |
-
eligible.append(job)
|
| 149 |
-
|
| 150 |
-
if not eligible:
|
| 151 |
-
return None
|
| 152 |
-
|
| 153 |
-
eligible.sort(key=lambda j: j["submitted_at"])
|
| 154 |
-
job = eligible[0]
|
| 155 |
-
|
| 156 |
-
job["status"] = "claimed"
|
| 157 |
-
job["claimed_by"] = worker_id
|
| 158 |
-
job["claimed_at"] = now
|
| 159 |
-
job["claim_deadline"] = (
|
| 160 |
-
now + job["timeout"] + self.settings.claim_timeout_buffer
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
return self._public_job(job)
|
| 164 |
-
|
| 165 |
-
def report_result(self, worker_id: int, result: ResultObject) -> None:
|
| 166 |
-
with self.lock:
|
| 167 |
-
job = self.jobs.get(result.job_id)
|
| 168 |
-
if job is None:
|
| 169 |
-
raise KeyError("job not found")
|
| 170 |
-
if job.get("claimed_by") != worker_id:
|
| 171 |
-
raise PermissionError("job is not claimed by this worker")
|
| 172 |
-
|
| 173 |
-
now = self.clock()
|
| 174 |
-
job["status"] = "complete"
|
| 175 |
-
job["result"] = result.model_dump()
|
| 176 |
-
job["completed_at"] = now
|
| 177 |
-
job["claimed_by"] = None
|
| 178 |
-
job["claimed_at"] = None
|
| 179 |
-
job["claim_deadline"] = None
|
| 180 |
-
|
| 181 |
-
worker = self.workers.get(worker_id)
|
| 182 |
-
if worker is not None:
|
| 183 |
-
worker["last_seen_at"] = now
|
| 184 |
-
|
| 185 |
-
def report_done(self, worker_id: int) -> List[str]:
|
| 186 |
-
with self.lock:
|
| 187 |
-
worker = self.workers.get(worker_id)
|
| 188 |
-
if worker is None:
|
| 189 |
-
return []
|
| 190 |
-
|
| 191 |
-
now = self.clock()
|
| 192 |
-
worker["reported_done"] = True
|
| 193 |
-
worker["last_seen_at"] = now
|
| 194 |
-
|
| 195 |
-
released = []
|
| 196 |
-
for job in list(self.jobs.values()):
|
| 197 |
-
if job["status"] == "claimed" and job["claimed_by"] == worker_id:
|
| 198 |
-
self._abandon_claim_locked(job, "worker reported done", now)
|
| 199 |
-
released.append(job["job_id"])
|
| 200 |
-
|
| 201 |
-
return released
|
| 202 |
-
|
| 203 |
-
def submit_job(self, job_data: JobSubmission) -> str:
|
| 204 |
-
with self.lock:
|
| 205 |
-
job_id = job_data.job_id or generate_job_id()
|
| 206 |
-
if job_id in self.jobs:
|
| 207 |
-
raise ValueError("job_id already exists")
|
| 208 |
-
|
| 209 |
-
now = self.clock()
|
| 210 |
-
self.jobs[job_id] = {
|
| 211 |
-
"job_id": job_id,
|
| 212 |
-
"python_version": job_data.python_version,
|
| 213 |
-
"platform": job_data.platform,
|
| 214 |
-
"entry_point": job_data.entry_point,
|
| 215 |
-
"files": job_data.files,
|
| 216 |
-
"dependencies": job_data.dependencies,
|
| 217 |
-
"network_required": job_data.network_required,
|
| 218 |
-
"stdin": job_data.stdin,
|
| 219 |
-
"timeout": job_data.timeout,
|
| 220 |
-
"status": "queued",
|
| 221 |
-
"submitted_at": now,
|
| 222 |
-
"retry_count": 0,
|
| 223 |
-
"claimed_by": None,
|
| 224 |
-
"claimed_at": None,
|
| 225 |
-
"claim_deadline": None,
|
| 226 |
-
"result": None,
|
| 227 |
-
"completed_at": None,
|
| 228 |
-
"last_abandon_reason": None,
|
| 229 |
-
}
|
| 230 |
-
return job_id
|
| 231 |
-
|
| 232 |
-
def get_job(self, job_id: str):
|
| 233 |
-
with self.lock:
|
| 234 |
-
job = self.jobs.get(job_id)
|
| 235 |
-
if job is None:
|
| 236 |
-
return None, None
|
| 237 |
-
|
| 238 |
-
if job["status"] == "complete":
|
| 239 |
-
return "complete", job["result"]
|
| 240 |
-
|
| 241 |
-
matching = self._matching_worker_ids_locked(job)
|
| 242 |
-
return "pending", {
|
| 243 |
-
"retry_count": job["retry_count"],
|
| 244 |
-
"matching_worker_ids": matching,
|
| 245 |
-
"last_abandon_reason": job.get("last_abandon_reason"),
|
| 246 |
-
"submitted_at": iso_from_ts(job["submitted_at"]),
|
| 247 |
-
}
|
| 248 |
-
|
| 249 |
-
def list_workers(self) -> List[dict]:
|
| 250 |
-
with self.lock:
|
| 251 |
-
now = self.clock()
|
| 252 |
-
return [
|
| 253 |
-
self._worker_public_locked(worker["worker_id"], now)
|
| 254 |
-
for worker in self.workers.values()
|
| 255 |
-
]
|
| 256 |
-
|
| 257 |
-
def list_jobs(self) -> List[dict]:
|
| 258 |
-
with self.lock:
|
| 259 |
-
result = []
|
| 260 |
-
for job in self.jobs.values():
|
| 261 |
-
item = {
|
| 262 |
-
"job_id": job["job_id"],
|
| 263 |
-
"status": job["status"],
|
| 264 |
-
"submitted_at": iso_from_ts(job["submitted_at"]),
|
| 265 |
-
"python_version": job["python_version"],
|
| 266 |
-
"platform": job["platform"],
|
| 267 |
-
"retry_count": job["retry_count"],
|
| 268 |
-
"claimed_by": job.get("claimed_by"),
|
| 269 |
-
"claim_deadline": (
|
| 270 |
-
iso_from_ts(job["claim_deadline"])
|
| 271 |
-
if job.get("claim_deadline")
|
| 272 |
-
else None
|
| 273 |
-
),
|
| 274 |
-
"matching_worker_ids": self._matching_worker_ids_locked(job),
|
| 275 |
-
"last_abandon_reason": job.get("last_abandon_reason"),
|
| 276 |
-
}
|
| 277 |
-
if job["status"] == "complete" and job.get("result"):
|
| 278 |
-
item["result"] = job["result"]
|
| 279 |
-
result.append(item)
|
| 280 |
-
return result
|
| 281 |
-
|
| 282 |
-
def process_expired_claims(self) -> None:
|
| 283 |
-
with self.lock:
|
| 284 |
-
now = self.clock()
|
| 285 |
-
for job in list(self.jobs.values()):
|
| 286 |
-
if (
|
| 287 |
-
job["status"] == "claimed"
|
| 288 |
-
and job.get("claim_deadline")
|
| 289 |
-
and now >= job["claim_deadline"]
|
| 290 |
-
):
|
| 291 |
-
self._abandon_claim_locked(job, "claim timeout", now)
|
| 292 |
-
|
| 293 |
-
def cleanup_completed(self) -> None:
|
| 294 |
-
with self.lock:
|
| 295 |
-
now = self.clock()
|
| 296 |
-
for job_id in list(self.jobs):
|
| 297 |
-
job = self.jobs[job_id]
|
| 298 |
-
if (
|
| 299 |
-
job["status"] == "complete"
|
| 300 |
-
and job.get("completed_at")
|
| 301 |
-
and (now - job["completed_at"]) > self.settings.retention_seconds
|
| 302 |
-
):
|
| 303 |
-
del self.jobs[job_id]
|
| 304 |
-
|
| 305 |
-
def _abandon_claim_locked(self, job: dict, reason: str, now: float) -> None:
|
| 306 |
-
if job.get("retry_count", 0) >= self.settings.max_retries:
|
| 307 |
-
job["status"] = "complete"
|
| 308 |
-
job["result"] = {
|
| 309 |
-
"job_id": job["job_id"],
|
| 310 |
-
"stdout": "",
|
| 311 |
-
"stderr": "",
|
| 312 |
-
"exit_code": 1,
|
| 313 |
-
"timed_out": False,
|
| 314 |
-
"category": "infra_failure",
|
| 315 |
-
"failure_reason": (
|
| 316 |
-
f"job abandoned more than {self.settings.max_retries} times "
|
| 317 |
-
f"({reason})"
|
| 318 |
-
),
|
| 319 |
-
"network_activity_detected": None,
|
| 320 |
-
}
|
| 321 |
-
job["completed_at"] = now
|
| 322 |
-
job["claimed_by"] = None
|
| 323 |
-
job["claimed_at"] = None
|
| 324 |
-
job["claim_deadline"] = None
|
| 325 |
-
else:
|
| 326 |
-
job["retry_count"] = job.get("retry_count", 0) + 1
|
| 327 |
-
job["status"] = "queued"
|
| 328 |
-
job["claimed_by"] = None
|
| 329 |
-
job["claimed_at"] = None
|
| 330 |
-
job["claim_deadline"] = None
|
| 331 |
-
job["last_abandon_reason"] = reason
|
| 332 |
-
|
| 333 |
-
def _matching_worker_ids_locked(self, job: dict) -> List[int]:
|
| 334 |
-
matching = []
|
| 335 |
-
for worker_id, worker in self.workers.items():
|
| 336 |
-
if job.get("platform") != worker["environment"].get("platform"):
|
| 337 |
-
continue
|
| 338 |
-
if job.get("python_version") in worker["environment"].get("python_versions", []):
|
| 339 |
-
matching.append(worker_id)
|
| 340 |
-
return matching
|
| 341 |
-
|
| 342 |
-
def _status_locked(self, worker_id: int, now: float) -> str:
|
| 343 |
-
worker = self.workers.get(worker_id)
|
| 344 |
-
if worker is None:
|
| 345 |
-
return "unresponsive"
|
| 346 |
-
|
| 347 |
-
if worker.get("reported_done") or (now - worker["last_seen_at"]) > self.settings.liveness_window:
|
| 348 |
-
return "unresponsive"
|
| 349 |
-
|
| 350 |
-
for job in self.jobs.values():
|
| 351 |
-
if job["status"] == "claimed" and job.get("claimed_by") == worker_id:
|
| 352 |
-
return "busy"
|
| 353 |
-
|
| 354 |
-
return "idle"
|
| 355 |
-
|
| 356 |
-
def _worker_public_locked(self, worker_id: int, now: float) -> dict:
|
| 357 |
-
worker = self.workers[worker_id]
|
| 358 |
-
return {
|
| 359 |
-
"worker_id": worker["worker_id"],
|
| 360 |
-
"url": worker["url"],
|
| 361 |
-
"environment": worker["environment"],
|
| 362 |
-
"registered_at": iso_from_ts(worker["registered_at"]),
|
| 363 |
-
"last_seen_at": iso_from_ts(worker["last_seen_at"]),
|
| 364 |
-
"status": self._status_locked(worker_id, now),
|
| 365 |
-
}
|
| 366 |
-
|
| 367 |
-
@staticmethod
|
| 368 |
-
def _public_job(job: dict) -> dict:
|
| 369 |
-
return {
|
| 370 |
-
"job_id": job["job_id"],
|
| 371 |
-
"python_version": job["python_version"],
|
| 372 |
-
"platform": job["platform"],
|
| 373 |
-
"entry_point": job["entry_point"],
|
| 374 |
-
"files": job["files"],
|
| 375 |
-
"dependencies": job["dependencies"],
|
| 376 |
-
"network_required": job["network_required"],
|
| 377 |
-
"stdin": job["stdin"],
|
| 378 |
-
"timeout": job["timeout"],
|
| 379 |
-
}
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
async def _background_loop(store: MemoryStore, settings: Settings) -> None:
|
| 383 |
-
while True:
|
| 384 |
-
await asyncio.sleep(1)
|
| 385 |
-
store.process_expired_claims()
|
| 386 |
-
store.cleanup_completed()
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
def create_app(
|
| 390 |
-
settings: Optional[Settings] = None,
|
| 391 |
-
enable_background_tasks: bool = True,
|
| 392 |
-
clock: Optional[Callable[[], float]] = None,
|
| 393 |
-
) -> FastAPI:
|
| 394 |
-
settings = settings or Settings()
|
| 395 |
-
store = MemoryStore(settings, clock=clock or _now)
|
| 396 |
-
security = HTTPBearer(auto_error=False)
|
| 397 |
-
|
| 398 |
-
def worker_auth(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 399 |
-
expected = settings.github_shared_secret
|
| 400 |
-
if (
|
| 401 |
-
not expected
|
| 402 |
-
or not credentials
|
| 403 |
-
or credentials.scheme.lower() != "bearer"
|
| 404 |
-
or not secrets.compare_digest(
|
| 405 |
-
credentials.credentials.encode(), expected.encode()
|
| 406 |
-
)
|
| 407 |
-
):
|
| 408 |
-
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 409 |
-
|
| 410 |
-
def desktop_auth(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 411 |
-
expected = settings.desktop_api_key
|
| 412 |
-
if (
|
| 413 |
-
not expected
|
| 414 |
-
or not credentials
|
| 415 |
-
or credentials.scheme.lower() != "bearer"
|
| 416 |
-
or not secrets.compare_digest(
|
| 417 |
-
credentials.credentials.encode(), expected.encode()
|
| 418 |
-
)
|
| 419 |
-
):
|
| 420 |
-
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 421 |
-
|
| 422 |
-
@asynccontextmanager
|
| 423 |
-
async def lifespan(app: FastAPI):
|
| 424 |
-
if enable_background_tasks:
|
| 425 |
-
task = asyncio.create_task(_background_loop(store, settings))
|
| 426 |
-
try:
|
| 427 |
-
yield
|
| 428 |
-
finally:
|
| 429 |
-
task.cancel()
|
| 430 |
-
with suppress(asyncio.CancelledError):
|
| 431 |
-
await task
|
| 432 |
-
else:
|
| 433 |
-
yield
|
| 434 |
-
|
| 435 |
-
app = FastAPI(
|
| 436 |
-
title="HF Space Worker Pool Coordinator",
|
| 437 |
-
version="1.0.0",
|
| 438 |
-
lifespan=lifespan,
|
| 439 |
-
)
|
| 440 |
-
app.state.settings = settings
|
| 441 |
-
app.state.store = store
|
| 442 |
-
app.state.enable_background_tasks = enable_background_tasks
|
| 443 |
-
|
| 444 |
-
@app.post("/worker/register", dependencies=[Depends(worker_auth)])
|
| 445 |
-
def worker_register(body: WorkerRegisterRequest):
|
| 446 |
-
return store.register_worker(body.worker_id, body.url, body.environment.model_dump())
|
| 447 |
-
|
| 448 |
-
@app.post("/worker/poll", dependencies=[Depends(worker_auth)])
|
| 449 |
-
def worker_poll(body: PollRequest):
|
| 450 |
-
try:
|
| 451 |
-
job = store.poll_job(body.worker_id)
|
| 452 |
-
return {"job": job}
|
| 453 |
-
except KeyError:
|
| 454 |
-
raise HTTPException(status_code=404, detail="Worker not registered")
|
| 455 |
-
|
| 456 |
-
@app.post("/worker/result", dependencies=[Depends(worker_auth)])
|
| 457 |
-
def worker_result(
|
| 458 |
-
worker_id: int = Query(...),
|
| 459 |
-
result: ResultObject = Body(...),
|
| 460 |
-
):
|
| 461 |
-
try:
|
| 462 |
-
store.report_result(worker_id, result)
|
| 463 |
-
return {"status": "complete", "job_id": result.job_id}
|
| 464 |
-
except KeyError:
|
| 465 |
-
raise HTTPException(status_code=404, detail="Job not found")
|
| 466 |
-
except PermissionError:
|
| 467 |
-
raise HTTPException(status_code=409, detail="Job not claimed by this worker")
|
| 468 |
-
|
| 469 |
-
@app.post("/worker/done", dependencies=[Depends(worker_auth)])
|
| 470 |
-
def worker_done(body: DoneRequest):
|
| 471 |
-
released = store.report_done(body.worker_id)
|
| 472 |
-
return {"released_job_ids": released}
|
| 473 |
-
|
| 474 |
-
@app.post("/desktop/job", dependencies=[Depends(desktop_auth)])
|
| 475 |
-
def desktop_submit_job(body: JobSubmission):
|
| 476 |
-
try:
|
| 477 |
-
job_id = store.submit_job(body)
|
| 478 |
-
return {"job_id": job_id, "status": "queued"}
|
| 479 |
-
except ValueError:
|
| 480 |
-
raise HTTPException(status_code=409, detail="job_id already exists")
|
| 481 |
-
|
| 482 |
-
@app.get("/desktop/job/{job_id}", dependencies=[Depends(desktop_auth)])
|
| 483 |
-
def desktop_get_result(job_id: str):
|
| 484 |
-
status, data = store.get_job(job_id)
|
| 485 |
-
if status is None:
|
| 486 |
-
raise HTTPException(status_code=404, detail="Job not found")
|
| 487 |
-
if status == "complete":
|
| 488 |
-
return {"status": "complete", "result": data}
|
| 489 |
-
return {"status": "pending", **data}
|
| 490 |
-
|
| 491 |
-
@app.get("/desktop/workers", dependencies=[Depends(desktop_auth)])
|
| 492 |
-
def desktop_list_workers():
|
| 493 |
-
return store.list_workers()
|
| 494 |
-
|
| 495 |
-
@app.get("/desktop/jobs", dependencies=[Depends(desktop_auth)])
|
| 496 |
-
def desktop_list_jobs():
|
| 497 |
-
return store.list_jobs()
|
| 498 |
-
|
| 499 |
-
@app.get("/health")
|
| 500 |
-
def health():
|
| 501 |
-
return {"online":"yes"}
|
| 502 |
-
|
| 503 |
-
return app
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
app = create_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
import pkgutil
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
|
| 6 |
+
app = FastAPI(title="Multi-Project API Relay")
|
| 7 |
+
|
| 8 |
+
# Automatically discover and include routers from /projects
|
| 9 |
+
projects_dir = Path(__file__).parent / "projects"
|
| 10 |
+
if projects_dir.exists():
|
| 11 |
+
for module_info in pkgutil.iter_modules([str(projects_dir)]):
|
| 12 |
+
if module_info.ispkg:
|
| 13 |
+
project_name = module_info.name
|
| 14 |
+
try:
|
| 15 |
+
router_module = importlib.import_module(f"projects.{project_name}.router")
|
| 16 |
+
router = getattr(router_module, "router")
|
| 17 |
+
app.include_router(router, prefix=f"/{project_name}")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"Failed to load project '{project_name}': {e}")
|
| 20 |
+
|
| 21 |
+
@app.get("/health")
|
| 22 |
+
def health():
|
| 23 |
+
return {"online": "yes"}
|