Spaces:
Sleeping
Sleeping
Fix: Use lazy initialization for services to avoid startup errors
Browse files- student/api.py +34 -10
student/api.py
CHANGED
|
@@ -21,14 +21,38 @@ app = FastAPI(
|
|
| 21 |
version="1.0.0",
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# Initialize services
|
| 25 |
-
code_generator = CodeGenerator()
|
| 26 |
-
github_manager = GitHubManager()
|
| 27 |
-
notification_client = NotificationClient()
|
| 28 |
-
|
| 29 |
# Track ongoing tasks
|
| 30 |
active_tasks: dict[str, dict] = {}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
@app.post("/api/build")
|
| 34 |
async def build_app(request: TaskRequest, background_tasks: BackgroundTasks) -> JSONResponse:
|
|
@@ -108,12 +132,12 @@ async def process_build_request(request: TaskRequest) -> None:
|
|
| 108 |
# Generate code
|
| 109 |
logger.info(f"Generating code for {task_key}")
|
| 110 |
active_tasks[task_key]["status"] = "generating"
|
| 111 |
-
|
| 112 |
|
| 113 |
# Create repo and deploy
|
| 114 |
logger.info(f"Deploying to GitHub for {task_key}")
|
| 115 |
active_tasks[task_key]["status"] = "deploying"
|
| 116 |
-
repo_url, commit_sha, pages_url =
|
| 117 |
request.task, output_dir
|
| 118 |
)
|
| 119 |
|
|
@@ -174,7 +198,7 @@ async def process_update_request(request: TaskRequest) -> None:
|
|
| 174 |
# Generate updated code
|
| 175 |
logger.info(f"Updating code for {task_key}")
|
| 176 |
active_tasks[task_key]["status"] = "generating"
|
| 177 |
-
|
| 178 |
|
| 179 |
# Update repo and redeploy
|
| 180 |
logger.info(f"Redeploying to GitHub for {task_key}")
|
|
@@ -185,11 +209,11 @@ async def process_update_request(request: TaskRequest) -> None:
|
|
| 185 |
repo_name = sanitize_repo_name(request.task)
|
| 186 |
|
| 187 |
try:
|
| 188 |
-
repo_url, commit_sha =
|
| 189 |
pages_url = f"https://{settings.github_username}.github.io/{repo_name}/"
|
| 190 |
except Exception as e:
|
| 191 |
logger.warning(f"Update failed, creating new repo: {e}")
|
| 192 |
-
repo_url, commit_sha, pages_url =
|
| 193 |
request.task, output_dir
|
| 194 |
)
|
| 195 |
|
|
|
|
| 21 |
version="1.0.0",
|
| 22 |
)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Track ongoing tasks
|
| 25 |
active_tasks: dict[str, dict] = {}
|
| 26 |
|
| 27 |
+
# Lazy initialization of services
|
| 28 |
+
_code_generator = None
|
| 29 |
+
_github_manager = None
|
| 30 |
+
_notification_client = None
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def get_code_generator():
|
| 34 |
+
"""Lazy initialization of code generator."""
|
| 35 |
+
global _code_generator
|
| 36 |
+
if _code_generator is None:
|
| 37 |
+
_code_generator = CodeGenerator()
|
| 38 |
+
return _code_generator
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def get_github_manager():
|
| 42 |
+
"""Lazy initialization of GitHub manager."""
|
| 43 |
+
global _github_manager
|
| 44 |
+
if _github_manager is None:
|
| 45 |
+
_github_manager = GitHubManager()
|
| 46 |
+
return _github_manager
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def get_notification_client():
|
| 50 |
+
"""Lazy initialization of notification client."""
|
| 51 |
+
global _notification_client
|
| 52 |
+
if _notification_client is None:
|
| 53 |
+
_notification_client = NotificationClient()
|
| 54 |
+
return _notification_client
|
| 55 |
+
|
| 56 |
|
| 57 |
@app.post("/api/build")
|
| 58 |
async def build_app(request: TaskRequest, background_tasks: BackgroundTasks) -> JSONResponse:
|
|
|
|
| 132 |
# Generate code
|
| 133 |
logger.info(f"Generating code for {task_key}")
|
| 134 |
active_tasks[task_key]["status"] = "generating"
|
| 135 |
+
get_code_generator().generate_app(request, output_dir)
|
| 136 |
|
| 137 |
# Create repo and deploy
|
| 138 |
logger.info(f"Deploying to GitHub for {task_key}")
|
| 139 |
active_tasks[task_key]["status"] = "deploying"
|
| 140 |
+
repo_url, commit_sha, pages_url = get_github_manager().create_and_deploy(
|
| 141 |
request.task, output_dir
|
| 142 |
)
|
| 143 |
|
|
|
|
| 198 |
# Generate updated code
|
| 199 |
logger.info(f"Updating code for {task_key}")
|
| 200 |
active_tasks[task_key]["status"] = "generating"
|
| 201 |
+
get_code_generator().generate_app(request, output_dir)
|
| 202 |
|
| 203 |
# Update repo and redeploy
|
| 204 |
logger.info(f"Redeploying to GitHub for {task_key}")
|
|
|
|
| 209 |
repo_name = sanitize_repo_name(request.task)
|
| 210 |
|
| 211 |
try:
|
| 212 |
+
repo_url, commit_sha = get_github_manager().update_and_redeploy(repo_name, output_dir)
|
| 213 |
pages_url = f"https://{settings.github_username}.github.io/{repo_name}/"
|
| 214 |
except Exception as e:
|
| 215 |
logger.warning(f"Update failed, creating new repo: {e}")
|
| 216 |
+
repo_url, commit_sha, pages_url = get_github_manager().create_and_deploy(
|
| 217 |
request.task, output_dir
|
| 218 |
)
|
| 219 |
|