LeanonHub/hr-saas-candidate
1
1import importlib2import os3import sys4import traceback5from datetime import datetime, timezone6 7REPO_ROOT = os.path.dirname(os.path.abspath(__file__))8IMPLEMENTATION_ROOT = os.path.join(REPO_ROOT, "09_Implementation")9if IMPLEMENTATION_ROOT not in sys.path:10 sys.path.insert(0, IMPLEMENTATION_ROOT)11 12 13SERVICE_MODULES = {14 "recruitment": "platform_runtime",15 "platform": "platform_runtime",16 "identity": "Platform.Identity.main",17 "candidate": "Platform.Candidate.main",18}19 20def _bootstrap_ts() -> str:21 return datetime.now(timezone.utc).isoformat()22 23 24def _bootstrap_print(event: str, **payload) -> None:25 print(event, {"ts": _bootstrap_ts(), **payload}, flush=True)26 27 28def _normalize_service(value: str | None) -> str | None:29 if not value:30 return None31 lowered = value.strip().lower()32 if not lowered:33 return None34 if "candidate" in lowered:35 return "candidate"36 if "identity" in lowered:37 return "identity"38 if "recruitment" in lowered or "platform" in lowered:39 return "platform"40 if lowered in SERVICE_MODULES:41 return "platform" if lowered in {"platform", "recruitment"} else lowered42 return None43 44 45def _resolve_service() -> tuple[str, dict[str, str]]:46 value = os.getenv("SERVICE")47 observed: dict[str, str] = {}48 if value:49 observed["SERVICE"] = value50 service = _normalize_service(value)51 if service:52 return service, observed53 raise RuntimeError(54 "SERVICE environment variable is required and must be one of: candidate, identity, recruitment, platform"55 )56 57 58def _load_app():59 _bootstrap_print("bootstrap_module_import_begin", argv=sys.argv)60 _bootstrap_print("bootstrap_service_resolution_begin", env_keys=["SERVICE"])61 service, observed = _resolve_service()62 module_name = SERVICE_MODULES[service]63 _bootstrap_print(64 "bootstrap_service_resolved",65 service=service,66 module=module_name,67 observed_env=observed,68 )69 _bootstrap_print(70 "bootstrap_entrypoint_begin",71 service=service,72 module=module_name,73 observed_env=observed,74 implementation_root=IMPLEMENTATION_ROOT,75 cwd=os.getcwd(),76 python=sys.version.split()[0],77 )78 try:79 print(80 f"IMPORT_MARKER: before importing service module {service}",81 {"ts": _bootstrap_ts(), "module": module_name},82 flush=True,83 )84 _bootstrap_print("bootstrap_module_import_start", service=service, module=module_name)85 module = importlib.import_module(module_name)86 print(87 f"IMPORT_MARKER: after importing service module {service}",88 {"ts": _bootstrap_ts(), "module": module_name},89 flush=True,90 )91 loaded_app = getattr(module, "app")92 except Exception as exc:93 print(94 "bootstrap_entrypoint_failed",95 {96 "ts": _bootstrap_ts(),97 "service": service,98 "module": module_name,99 "error_type": type(exc).__name__,100 "error": str(exc),101 },102 file=sys.stderr,103 flush=True,104 )105 traceback.print_exc()106 raise107 _bootstrap_print("bootstrap_module_import_complete", service=service, module=module_name)108 _bootstrap_print(109 "bootstrap_entrypoint_complete",110 service=service,111 module=module_name,112 )113 _bootstrap_print("bootstrap_app_resolved", service=service, module=module_name, attr="app")114 return loaded_app115 116 117app = _load_app()118 