refactor backend
This commit is contained in:
+94
-339
@@ -1,22 +1,15 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
import threading
|
||||
import traceback
|
||||
from dataclasses import asdict, replace
|
||||
from typing import Any, Dict, List, Optional, TypeVar, Tuple
|
||||
from typing import Any, Callable, Dict, List, Optional
|
||||
|
||||
from utils import sigchld_handler
|
||||
from dotenv import load_dotenv
|
||||
from fastapi import FastAPI, Response, WebSocket, WebSocketDisconnect
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
from enironment import Environment, wrap_in_cached, SharedStateHolder, SharedStateConflictError, SharedState, get_step
|
||||
from processing import reset_caches
|
||||
from secondary import SecondaryManager
|
||||
from enironment import Environment, wrap_in_cached, SharedStateHolder, get_step
|
||||
from steps.git import GitClone
|
||||
from steps.step import CachingStep
|
||||
|
||||
@@ -29,317 +22,14 @@ load_dotenv("local.env")
|
||||
load_dotenv('/run/secrets/brencher-secrets')
|
||||
|
||||
|
||||
class DataclassJSONEncoder(json.JSONEncoder):
|
||||
def default(self, o: Any) -> Any:
|
||||
if hasattr(o, '__dataclass_fields__'):
|
||||
return asdict(o)
|
||||
if isinstance(o, BaseException):
|
||||
return str(o)
|
||||
try:
|
||||
return super().default(o)
|
||||
except TypeError:
|
||||
return str(o)
|
||||
|
||||
|
||||
def custom_json_dumps(obj: Any) -> str:
|
||||
return json.dumps(obj, cls=DataclassJSONEncoder)
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
FRONTEND_DIR = os.path.join(os.path.dirname(__file__), '../frontend')
|
||||
|
||||
# In-memory state
|
||||
environments: Dict[str, Environment] = {}
|
||||
state_lock = threading.Lock()
|
||||
|
||||
# Active WebSocket connections
|
||||
ws_connections: Dict[WebSocket, Dict[str, Any]] = {}
|
||||
|
||||
# Event loop reference for thread-safe async calls
|
||||
_event_loop: Optional[asyncio.AbstractEventLoop] = None
|
||||
|
||||
|
||||
environment_update_event = threading.Event()
|
||||
|
||||
|
||||
# --- Static File Serving ---
|
||||
|
||||
@app.get("/")
|
||||
async def serve_index() -> FileResponse:
|
||||
return FileResponse(os.path.join(FRONTEND_DIR, 'index.html'))
|
||||
|
||||
|
||||
@app.get("/state")
|
||||
async def serve_state() -> Response:
|
||||
return Response(content=custom_json_dumps(get_global_envs_to_emit()), media_type="application/json")
|
||||
|
||||
|
||||
@app.get("/branches")
|
||||
async def serve_branches_route() -> Response:
|
||||
return Response(content=custom_json_dumps(get_global_branches_to_emit()), media_type="application/json")
|
||||
|
||||
|
||||
@app.get("/{path:path}")
|
||||
async def serve_static(path: str) -> FileResponse:
|
||||
file_path = os.path.join(FRONTEND_DIR, path)
|
||||
if os.path.exists(file_path) and os.path.isfile(file_path):
|
||||
return FileResponse(file_path)
|
||||
return FileResponse(os.path.join(FRONTEND_DIR, 'index.html'))
|
||||
|
||||
|
||||
# --- Utility Functions ---
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
|
||||
def merge_dicts(a: Dict[str, T], b: Dict[str, T]) -> Dict[str, T]:
|
||||
result: Dict[str, T] = {}
|
||||
for k in (a.keys() | b.keys()):
|
||||
if k not in a or k not in b:
|
||||
result[k] = a.get(k, b.get(k)) # type: ignore[assignment]
|
||||
continue
|
||||
if (
|
||||
isinstance(a[k], dict)
|
||||
and isinstance(b[k], dict)
|
||||
):
|
||||
result[k] = merge_dicts(a[k], b[k]) # type: ignore[arg-type,assignment]
|
||||
else:
|
||||
result[k] = b[k]
|
||||
return result
|
||||
|
||||
|
||||
def get_local_envs_to_emit() -> Dict[str, Dict[str, Any]]:
|
||||
env_dtos: Dict[str, Dict[str, Any]] = {}
|
||||
for env in environments.values():
|
||||
pipeline_state: List[Dict[str, Any]] = []
|
||||
for r in env.pipeline:
|
||||
try:
|
||||
if isinstance(r, CachingStep):
|
||||
result = r._result
|
||||
else:
|
||||
result = r.progress()
|
||||
|
||||
if isinstance(result, BaseException):
|
||||
stack = traceback.format_exception(type(result), result, result.__traceback__)
|
||||
pipeline_state.append({
|
||||
"name": r.name,
|
||||
"status": [str(result), stack],
|
||||
"error": True,
|
||||
"is_running": True,
|
||||
})
|
||||
else:
|
||||
pipeline_state.append({
|
||||
"name": r.name,
|
||||
"status": result,
|
||||
"is_running": False,
|
||||
})
|
||||
except BaseException as e:
|
||||
stack = traceback.format_exception(type(e), e, e.__traceback__)
|
||||
pipeline_state.append({
|
||||
"name": r.name,
|
||||
"status": [str(e), stack],
|
||||
"error": True,
|
||||
"is_running": True,
|
||||
})
|
||||
env_dtos[env.id] = {'id': env.id }
|
||||
env_dtos[env.id]['pipeline'] = pipeline_state
|
||||
try:
|
||||
shared_state = env.state.progress()
|
||||
env_dtos[env.id]['branches'] = shared_state.branches
|
||||
env_dtos[env.id]['branches_token'] = shared_state.token
|
||||
env_dtos[env.id]['dry'] = shared_state.dry
|
||||
except BaseException:
|
||||
pass
|
||||
return env_dtos
|
||||
|
||||
|
||||
def get_global_envs_to_emit() -> Any:
|
||||
global environments
|
||||
local_envs = get_local_envs_to_emit()
|
||||
merged: Dict[str, Any] = local_envs
|
||||
for connector in secondaryManager or []:
|
||||
v = connector.environments
|
||||
common_keys = set(merged.keys()) & set(v.keys())
|
||||
if len(common_keys) > 0:
|
||||
_schedule_async(broadcast_error({'message': f"Conflict: both master and secondary have environment with id {common_keys}"}))
|
||||
merged = merge_dicts(merged, v)
|
||||
return merged
|
||||
|
||||
|
||||
|
||||
def get_local_branches_to_emit() -> Dict[str, Dict[str, List[Any]]]:
|
||||
global environments
|
||||
branches: Dict[str, Dict[str, List[Any]]] = {}
|
||||
for k, env in environments.items():
|
||||
branches[k] = {}
|
||||
try:
|
||||
step = get_step(env.pipeline, GitClone)
|
||||
branches[k] = {**step.get_branches()}
|
||||
except BaseException as e:
|
||||
stack = traceback.format_exception(type(e), e, e.__traceback__)
|
||||
logger.error(f"Error fetching branches for environment {env.id}: {str(e)}\n{''.join(stack)}")
|
||||
|
||||
return branches
|
||||
|
||||
|
||||
|
||||
def get_global_branches_to_emit() -> Dict[str, Dict[str, List[Any]]]:
|
||||
local_branches: Dict[str, Dict[str, List[Any]]] = get_local_branches_to_emit()
|
||||
merged: Dict[str, Dict[str, List[Any]]] = local_branches
|
||||
for connector in secondaryManager or []:
|
||||
merged = merge_dicts(merged, connector.branches)
|
||||
return merged
|
||||
|
||||
|
||||
# --- Async Helper for Calling from Sync Threads ---
|
||||
|
||||
def _schedule_async(coro: Any) -> None:
|
||||
"""Schedule a coroutine from a sync thread using the saved event loop."""
|
||||
if _event_loop is not None and _event_loop.is_running():
|
||||
asyncio.run_coroutine_threadsafe(coro, _event_loop)
|
||||
|
||||
|
||||
# --- WebSocket Broadcasting Functions ---
|
||||
|
||||
async def broadcast(event: str, data: Any) -> None:
|
||||
"""Broadcast a message to all connected WebSocket clients that have not seen this data yet."""
|
||||
disconnected = set()
|
||||
message = custom_json_dumps({event: data})
|
||||
|
||||
for websocket in ws_connections.keys():
|
||||
if ws_connections[websocket].get(event) == message:
|
||||
continue
|
||||
try:
|
||||
await websocket.send_text(message)
|
||||
ws_connections[websocket][event] = message
|
||||
except Exception as e:
|
||||
logger.error(f"Error sending to websocket: {e}")
|
||||
disconnected.add(websocket)
|
||||
|
||||
for ws in disconnected:
|
||||
ws_connections.pop(ws, None)
|
||||
|
||||
async def broadcast_all() -> None:
|
||||
await broadcast("branches", get_global_branches_to_emit())
|
||||
await broadcast("environments", get_global_envs_to_emit())
|
||||
|
||||
|
||||
async def broadcast_branches(data: Any) -> None:
|
||||
await broadcast("branches", data)
|
||||
|
||||
|
||||
async def broadcast_environments(data: Any) -> None:
|
||||
await broadcast("environments", data)
|
||||
|
||||
|
||||
async def broadcast_error(data: Any) -> None:
|
||||
await broadcast("error", data)
|
||||
|
||||
|
||||
# --- Single WebSocket Endpoint ---
|
||||
|
||||
@app.websocket("/ws")
|
||||
async def websocket_endpoint(websocket: WebSocket) -> None:
|
||||
await websocket.accept()
|
||||
ws_connections[websocket] = {}
|
||||
|
||||
try:
|
||||
# Send initial state on connect and record it so duplicate broadcasts are suppressed
|
||||
branches_payload = custom_json_dumps({"branches": get_global_branches_to_emit()})
|
||||
envs_payload = custom_json_dumps({"environments": get_global_envs_to_emit()})
|
||||
await websocket.send_text(branches_payload)
|
||||
ws_connections[websocket]["branches"] = branches_payload
|
||||
await websocket.send_text(envs_payload)
|
||||
ws_connections[websocket]["environments"] = envs_payload
|
||||
|
||||
while True:
|
||||
data = await websocket.receive_text()
|
||||
message = json.loads(data)
|
||||
|
||||
if "update" in message:
|
||||
update_data = message.get("update") or {}
|
||||
logger.info(f"Received environment update: {update_data}")
|
||||
if secondaryManager:
|
||||
await secondaryManager.send({"update": update_data})
|
||||
id = update_data.get('id', '')
|
||||
if id == '':
|
||||
reset_caches(list(environments.values()))
|
||||
elif id not in environments.keys() and id not in { j for it in secondaryManager or [] for j in it.environments.keys()}:
|
||||
logger.warning(f"Received update for unknown environment id {id}")
|
||||
continue
|
||||
elif id in environments.keys():
|
||||
env = environments.get(id, None)
|
||||
expected_token = update_data.get('token', '')
|
||||
if 'branches' in update_data:
|
||||
if not env:
|
||||
raise RuntimeError(f"Unknown env {update_data.get('id', '')}")
|
||||
env.state.set_branches(update_data.get('branches', []), expected_token=expected_token)
|
||||
if 'dry' in update_data:
|
||||
if not env:
|
||||
raise RuntimeError(f"Unknown env {update_data.get('id', '')}")
|
||||
env.state.set_dry(bool(update_data['dry']), expected_token)
|
||||
|
||||
if env:
|
||||
p = get_step(env.pipeline, type(env.state))
|
||||
if isinstance(p, CachingStep):
|
||||
p.reset()
|
||||
logger.info(f"Updated environment {env.id} branches to {update_data.get('branches')}, dry={update_data.get('dry')}")
|
||||
environment_update_event.set()
|
||||
|
||||
await broadcast_environments(get_global_envs_to_emit())
|
||||
|
||||
except WebSocketDisconnect as e:
|
||||
ws_connections.pop(websocket, None)
|
||||
except Exception as e:
|
||||
logger.error(f"WebSocket error: {e}:{traceback.format_exc()}")
|
||||
await broadcast_error({'message': f'{e}:{traceback.format_exc()}'})
|
||||
# await websocket.close()
|
||||
# ws_connections.pop(websocket, None)
|
||||
|
||||
|
||||
# --- SIGCHLD Handler ---
|
||||
|
||||
def sigchld_handler(signum: int, frame: Any) -> None:
|
||||
"""Reap zombie processes."""
|
||||
while True:
|
||||
try:
|
||||
pid, status = os.waitpid(-1, os.WNOHANG)
|
||||
if pid == 0:
|
||||
break
|
||||
except ChildProcessError:
|
||||
break
|
||||
|
||||
|
||||
# --- Startup Event ---
|
||||
|
||||
secondaryManager: Optional[SecondaryManager] = None
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event() -> None:
|
||||
global _event_loop, secondaryManager
|
||||
_event_loop = asyncio.get_event_loop()
|
||||
secondaryManager = SecondaryManager(
|
||||
urls = os.getenv("SECONDARY_BRENCHER", ""),
|
||||
on_update=lambda: broadcast_all(),
|
||||
)
|
||||
|
||||
|
||||
# --- App Class (used by CLI and integration tests) ---
|
||||
|
||||
class App:
|
||||
|
||||
def __init__(self, cli_env_ids_str: str, dry_run: bool = False) -> None:
|
||||
global environments
|
||||
signal.signal(signal.SIGCHLD, sigchld_handler)
|
||||
|
||||
self.environments: Dict[str, Environment] = {}
|
||||
self.state_lock = threading.Lock()
|
||||
self.environment_update_event = threading.Event()
|
||||
self.emit_callback: Optional[Callable[[], None]] = None
|
||||
|
||||
import configs.brencher
|
||||
import configs.brencher2
|
||||
@@ -359,7 +49,7 @@ class App:
|
||||
configs.registry.registry,
|
||||
configs.gmail_mcp.gmail_mcp,
|
||||
]
|
||||
environments = {e.id: e for e in environments_l}
|
||||
self.environments = {e.id: e for e in environments_l}
|
||||
|
||||
logger.info(f"cli_env_ids {cli_env_ids_str}")
|
||||
if len(cli_env_ids_str) > 0 and cli_env_ids_str[0] == '-':
|
||||
@@ -367,7 +57,7 @@ class App:
|
||||
cli_env_ids = [x for x in cli_env_ids if len(x) > 0]
|
||||
logger.info(f"cli_env_ids (minus) {cli_env_ids}")
|
||||
if cli_env_ids and len(cli_env_ids) > 0:
|
||||
environments = {k: e for k, e in environments.items() if k not in cli_env_ids}
|
||||
self.environments = {k: e for k, e in self.environments.items() if k not in cli_env_ids}
|
||||
else:
|
||||
cli_env_pairs = {x[0]: x[1] if len(x) > 1 else None for x in [x.split(":")
|
||||
for x in cli_env_ids_str.split(',')
|
||||
@@ -376,11 +66,11 @@ class App:
|
||||
}
|
||||
logger.info(f"cli_env_ids {cli_env_pairs}")
|
||||
if cli_env_pairs and len(cli_env_pairs) > 0:
|
||||
environments = {k: e for k, e in environments.items() if k in cli_env_pairs.keys()}
|
||||
self.environments = {k: e for k, e in self.environments.items() if k in cli_env_pairs.keys()}
|
||||
for k, v in cli_env_pairs.items():
|
||||
if v is not None:
|
||||
if k in environments:
|
||||
env = environments[k]
|
||||
if k in self.environments:
|
||||
env = self.environments[k]
|
||||
for step in env.pipeline:
|
||||
resolve_step = step
|
||||
if isinstance(step, CachingStep):
|
||||
@@ -393,39 +83,99 @@ class App:
|
||||
else:
|
||||
logger.info(f"No branch override for environment {k}")
|
||||
if dry_run:
|
||||
for id, e in environments.items():
|
||||
for id, e in self.environments.items():
|
||||
e.state.set_dry(True)
|
||||
|
||||
logger.info(f"Resulting profiles {environments.keys()}")
|
||||
logger.info(f"Resulting profiles {self.environments.keys()}")
|
||||
|
||||
environments = {id: wrap_in_cached(e) for id, e in environments.items()}
|
||||
self.environments = {id: wrap_in_cached(e) for id, e in self.environments.items()}
|
||||
|
||||
def get_local_envs_to_emit(self) -> Dict[str, Dict[str, Any]]:
|
||||
env_dtos: Dict[str, Dict[str, Any]] = {}
|
||||
for env in self.environments.values():
|
||||
pipeline_state: List[Dict[str, Any]] = []
|
||||
for r in env.pipeline:
|
||||
try:
|
||||
if isinstance(r, CachingStep):
|
||||
result = r._result
|
||||
else:
|
||||
result = r.progress()
|
||||
|
||||
if isinstance(result, BaseException):
|
||||
stack = traceback.format_exception(type(result), result, result.__traceback__)
|
||||
pipeline_state.append({
|
||||
"name": r.name,
|
||||
"status": [str(result), stack],
|
||||
"error": True,
|
||||
"is_running": True,
|
||||
})
|
||||
else:
|
||||
pipeline_state.append({
|
||||
"name": r.name,
|
||||
"status": result,
|
||||
"is_running": False,
|
||||
})
|
||||
except BaseException as e:
|
||||
stack = traceback.format_exception(type(e), e, e.__traceback__)
|
||||
pipeline_state.append({
|
||||
"name": r.name,
|
||||
"status": [str(e), stack],
|
||||
"error": True,
|
||||
"is_running": True,
|
||||
})
|
||||
env_dtos[env.id] = {'id': env.id }
|
||||
env_dtos[env.id]['pipeline'] = pipeline_state
|
||||
try:
|
||||
shared_state = env.state.progress()
|
||||
env_dtos[env.id]['branches'] = shared_state.branches
|
||||
env_dtos[env.id]['branches_token'] = shared_state.token
|
||||
env_dtos[env.id]['dry'] = shared_state.dry
|
||||
except BaseException:
|
||||
pass
|
||||
return env_dtos
|
||||
|
||||
def get_local_branches_to_emit(self) -> Dict[str, Dict[str, List[Any]]]:
|
||||
branches: Dict[str, Dict[str, List[Any]]] = {}
|
||||
for k, env in self.environments.items():
|
||||
branches[k] = {}
|
||||
try:
|
||||
step = get_step(env.pipeline, GitClone)
|
||||
branches[k] = {**step.get_branches()}
|
||||
except BaseException as e:
|
||||
stack = traceback.format_exception(type(e), e, e.__traceback__)
|
||||
logger.error(f"Error fetching branches for environment {env.id}: {str(e)}\n{''.join(stack)}")
|
||||
|
||||
return branches
|
||||
|
||||
def processing_thread(self) -> None:
|
||||
while True:
|
||||
import processing
|
||||
|
||||
def emit_envs() -> None:
|
||||
_schedule_async(broadcast_branches(get_global_branches_to_emit()))
|
||||
_schedule_async(broadcast_environments(get_global_envs_to_emit()))
|
||||
emit = self.emit_callback or (lambda: None)
|
||||
|
||||
with state_lock:
|
||||
with self.state_lock:
|
||||
logger.info(f"Processing")
|
||||
if processing.process_all_jobs(list(environments.values()), lambda: emit_envs()):
|
||||
environment_update_event.wait(timeout=1 * 5)
|
||||
if processing.process_all_jobs(list(self.environments.values()), emit):
|
||||
self.environment_update_event.wait(timeout=1 * 5)
|
||||
else:
|
||||
environment_update_event.wait(timeout=1 * 60)
|
||||
environment_update_event.clear()
|
||||
self.environment_update_event.wait(timeout=1 * 60)
|
||||
self.environment_update_event.clear()
|
||||
|
||||
def run(self) -> None:
|
||||
processing = threading.Thread(target=self.processing_thread)
|
||||
processing.daemon = True
|
||||
processing.start()
|
||||
|
||||
def runWeb(self, port: int) -> None:
|
||||
self.run()
|
||||
import uvicorn
|
||||
uvicorn.run(app, host='0.0.0.0', port=port, log_level='info')
|
||||
def runHeadless(self) -> None:
|
||||
"""Run the processing loop on the current thread; blocks forever."""
|
||||
self.processing_thread()
|
||||
|
||||
def runWeb(self, port: int) -> None:
|
||||
import web
|
||||
web_app = web.WebApp(core=self, port=port)
|
||||
self.emit_callback = web_app.emit_envs
|
||||
self.run()
|
||||
web_app.start()
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli_env_ids_list = sys.argv[1:]
|
||||
@@ -435,5 +185,10 @@ if __name__ == '__main__':
|
||||
else:
|
||||
cli_env_ids_str = cli_env_ids_list[0]
|
||||
|
||||
signal.signal(signal.SIGCHLD, sigchld_handler)
|
||||
app1 = App(cli_env_ids_str, 'dry' in sys.argv[1:])
|
||||
app1.runWeb(port=(5007 if 'noweb' in sys.argv[1:] else 5001))
|
||||
|
||||
if 'noweb' in sys.argv[1:]:
|
||||
app1.runHeadless()
|
||||
else:
|
||||
app1.runWeb(5001)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import json
|
||||
from dataclasses import asdict
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
|
||||
class DataclassJSONEncoder(json.JSONEncoder):
|
||||
def default(self, o: Any) -> Any:
|
||||
if hasattr(o, '__dataclass_fields__'):
|
||||
return asdict(o)
|
||||
if isinstance(o, BaseException):
|
||||
return str(o)
|
||||
try:
|
||||
return super().default(o)
|
||||
except TypeError:
|
||||
return str(o)
|
||||
|
||||
|
||||
def custom_json_dumps(obj: Any) -> str:
|
||||
return json.dumps(obj, cls=DataclassJSONEncoder)
|
||||
|
||||
def sigchld_handler(signum: int, frame: Any) -> None:
|
||||
"""Reap zombie processes."""
|
||||
while True:
|
||||
try:
|
||||
pid, status = os.waitpid(-1, os.WNOHANG)
|
||||
if pid == 0:
|
||||
break
|
||||
except ChildProcessError:
|
||||
break
|
||||
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import traceback
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any, AsyncIterator, Dict, List, Optional, TypeVar
|
||||
|
||||
from fastapi import FastAPI, Response, WebSocket, WebSocketDisconnect
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
from app import App
|
||||
from enironment import get_step
|
||||
from utils import custom_json_dumps
|
||||
from processing import reset_caches
|
||||
from secondary import SecondaryManager
|
||||
from steps.step import CachingStep
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
FRONTEND_DIR = os.path.join(os.path.dirname(__file__), '../frontend')
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
|
||||
def _merge_dicts(a: Dict[str, T], b: Dict[str, T]) -> Dict[str, T]:
|
||||
result: Dict[str, T] = {}
|
||||
for k in (a.keys() | b.keys()):
|
||||
if k not in a or k not in b:
|
||||
result[k] = a.get(k, b.get(k)) # type: ignore[assignment]
|
||||
continue
|
||||
if (
|
||||
isinstance(a[k], dict)
|
||||
and isinstance(b[k], dict)
|
||||
):
|
||||
result[k] = _merge_dicts(a[k], b[k]) # type: ignore[arg-type,assignment]
|
||||
else:
|
||||
result[k] = b[k]
|
||||
return result
|
||||
|
||||
|
||||
class WebApp:
|
||||
|
||||
def __init__(self, core: App, port: int) -> None:
|
||||
self.core = core
|
||||
self.port = port
|
||||
|
||||
self.ws_connections: Dict[WebSocket, Dict[str, Any]] = {}
|
||||
self._event_loop: Optional[asyncio.AbstractEventLoop] = None
|
||||
self.secondaryManager: Optional[SecondaryManager] = None
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(fastapi_app: FastAPI) -> AsyncIterator[None]:
|
||||
self._event_loop = asyncio.get_event_loop()
|
||||
self.secondaryManager = SecondaryManager(
|
||||
urls=os.getenv("SECONDARY_BRENCHER", ""),
|
||||
on_update=lambda: self.broadcast_all(),
|
||||
)
|
||||
yield
|
||||
|
||||
self.app = FastAPI(lifespan=lifespan)
|
||||
self.app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
self.app.get("/")(self.serve_index)
|
||||
self.app.get("/state")(self.serve_state)
|
||||
self.app.get("/branches")(self.serve_branches_route)
|
||||
self.app.get("/{path:path}")(self.serve_static)
|
||||
self.app.websocket("/ws")(self.websocket_endpoint)
|
||||
|
||||
# --- State assembly (local + secondary) ---
|
||||
|
||||
def get_global_envs_to_emit(self) -> Any:
|
||||
local_envs = self.core.get_local_envs_to_emit()
|
||||
merged: Dict[str, Any] = local_envs
|
||||
for connector in self.secondaryManager or []:
|
||||
v = connector.environments
|
||||
common_keys = set(merged.keys()) & set(v.keys())
|
||||
if len(common_keys) > 0:
|
||||
self._schedule_async(self.broadcast_error({'message': f"Conflict: both master and secondary have environment with id {common_keys}"}))
|
||||
merged = _merge_dicts(merged, v)
|
||||
return merged
|
||||
|
||||
def get_global_branches_to_emit(self) -> Dict[str, Dict[str, List[Any]]]:
|
||||
local_branches: Dict[str, Dict[str, List[Any]]] = self.core.get_local_branches_to_emit()
|
||||
merged: Dict[str, Dict[str, List[Any]]] = local_branches
|
||||
for connector in self.secondaryManager or []:
|
||||
merged = _merge_dicts(merged, connector.branches)
|
||||
return merged
|
||||
|
||||
# --- Async scheduling from sync threads ---
|
||||
|
||||
def _schedule_async(self, coro: Any) -> None:
|
||||
if self._event_loop is not None and self._event_loop.is_running():
|
||||
asyncio.run_coroutine_threadsafe(coro, self._event_loop)
|
||||
|
||||
# --- Broadcasting ---
|
||||
|
||||
async def broadcast(self, event: str, data: Any) -> None:
|
||||
"""Broadcast a message to all connected WebSocket clients that have not seen this data yet."""
|
||||
disconnected = set()
|
||||
message = custom_json_dumps({event: data})
|
||||
|
||||
for websocket in self.ws_connections.keys():
|
||||
if self.ws_connections[websocket].get(event) == message:
|
||||
continue
|
||||
try:
|
||||
await websocket.send_text(message)
|
||||
self.ws_connections[websocket][event] = message
|
||||
except Exception as e:
|
||||
logger.error(f"Error sending to websocket: {e}")
|
||||
disconnected.add(websocket)
|
||||
|
||||
for ws in disconnected:
|
||||
self.ws_connections.pop(ws, None)
|
||||
|
||||
async def broadcast_all(self) -> None:
|
||||
await self.broadcast("branches", self.get_global_branches_to_emit())
|
||||
await self.broadcast("environments", self.get_global_envs_to_emit())
|
||||
|
||||
async def broadcast_branches(self, data: Any) -> None:
|
||||
await self.broadcast("branches", data)
|
||||
|
||||
async def broadcast_environments(self, data: Any) -> None:
|
||||
await self.broadcast("environments", data)
|
||||
|
||||
async def broadcast_error(self, data: Any) -> None:
|
||||
await self.broadcast("error", data)
|
||||
|
||||
def emit_envs(self) -> None:
|
||||
"""Sync callback used by App's processing thread to push updates to clients."""
|
||||
self._schedule_async(self.broadcast_branches(self.get_global_branches_to_emit()))
|
||||
self._schedule_async(self.broadcast_environments(self.get_global_envs_to_emit()))
|
||||
|
||||
# --- Routes ---
|
||||
|
||||
async def serve_index(self) -> FileResponse:
|
||||
return FileResponse(os.path.join(FRONTEND_DIR, 'index.html'))
|
||||
|
||||
async def serve_state(self) -> Response:
|
||||
return Response(content=custom_json_dumps(self.get_global_envs_to_emit()), media_type="application/json")
|
||||
|
||||
async def serve_branches_route(self) -> Response:
|
||||
return Response(content=custom_json_dumps(self.get_global_branches_to_emit()), media_type="application/json")
|
||||
|
||||
async def serve_static(self, path: str) -> FileResponse:
|
||||
file_path = os.path.join(FRONTEND_DIR, path)
|
||||
if os.path.exists(file_path) and os.path.isfile(file_path):
|
||||
return FileResponse(file_path)
|
||||
return FileResponse(os.path.join(FRONTEND_DIR, 'index.html'))
|
||||
|
||||
async def websocket_endpoint(self, websocket: WebSocket) -> None:
|
||||
await websocket.accept()
|
||||
self.ws_connections[websocket] = {}
|
||||
|
||||
try:
|
||||
# Send initial state on connect and record it so duplicate broadcasts are suppressed
|
||||
branches_payload = custom_json_dumps({"branches": self.get_global_branches_to_emit()})
|
||||
envs_payload = custom_json_dumps({"environments": self.get_global_envs_to_emit()})
|
||||
await websocket.send_text(branches_payload)
|
||||
self.ws_connections[websocket]["branches"] = branches_payload
|
||||
await websocket.send_text(envs_payload)
|
||||
self.ws_connections[websocket]["environments"] = envs_payload
|
||||
|
||||
while True:
|
||||
data = await websocket.receive_text()
|
||||
message = json.loads(data)
|
||||
|
||||
if "update" in message:
|
||||
update_data = message.get("update") or {}
|
||||
logger.info(f"Received environment update: {update_data}")
|
||||
if self.secondaryManager:
|
||||
await self.secondaryManager.send({"update": update_data})
|
||||
id = update_data.get('id', '')
|
||||
if id == '':
|
||||
reset_caches(list(self.core.environments.values()))
|
||||
elif id not in self.core.environments.keys() and id not in {j for it in self.secondaryManager or [] for j in it.environments.keys()}:
|
||||
logger.warning(f"Received update for unknown environment id {id}")
|
||||
continue
|
||||
elif id in self.core.environments.keys():
|
||||
env = self.core.environments.get(id, None)
|
||||
expected_token = update_data.get('token', '')
|
||||
if 'branches' in update_data:
|
||||
if not env:
|
||||
raise RuntimeError(f"Unknown env {update_data.get('id', '')}")
|
||||
env.state.set_branches(update_data.get('branches', []), expected_token=expected_token)
|
||||
if 'dry' in update_data:
|
||||
if not env:
|
||||
raise RuntimeError(f"Unknown env {update_data.get('id', '')}")
|
||||
env.state.set_dry(bool(update_data['dry']), expected_token)
|
||||
|
||||
if env:
|
||||
p = get_step(env.pipeline, type(env.state))
|
||||
if isinstance(p, CachingStep):
|
||||
p.reset()
|
||||
logger.info(f"Updated environment {env.id} branches to {update_data.get('branches')}, dry={update_data.get('dry')}")
|
||||
self.core.environment_update_event.set()
|
||||
|
||||
await self.broadcast_environments(self.get_global_envs_to_emit())
|
||||
|
||||
except WebSocketDisconnect:
|
||||
self.ws_connections.pop(websocket, None)
|
||||
except Exception as e:
|
||||
logger.error(f"WebSocket error: {e}:{traceback.format_exc()}")
|
||||
await self.broadcast_error({'message': f'{e}:{traceback.format_exc()}'})
|
||||
|
||||
# --- Run ---
|
||||
|
||||
def start(self) -> None:
|
||||
import uvicorn
|
||||
uvicorn.run(self.app, host='0.0.0.0', port=self.port, log_level='info')
|
||||
Reference in New Issue
Block a user