-
Notifications
You must be signed in to change notification settings - Fork 730
Expand file tree
/
Copy pathserver.py
More file actions
66 lines (56 loc) · 2.14 KB
/
server.py
File metadata and controls
66 lines (56 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import asyncio
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from fastapi import FastAPI
from loguru import logger
from crowdgit.services import (
CloneService,
CommitService,
LicenseService,
MaintainerService,
QueueService,
SoftwareValueService,
VulnerabilityScannerService,
)
from crowdgit.settings import WORKER_SHUTDOWN_TIMEOUT_SEC
from crowdgit.worker.repository_worker import RepositoryWorker
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
"""FastAPI lifespan context manager"""
logger.info("Starting application lifespan")
clone_service = CloneService()
queue_service = QueueService()
commit_service = CommitService(queue_service=queue_service)
software_value_service = SoftwareValueService()
vulnerability_scanner_service = VulnerabilityScannerService()
maintainer_service = MaintainerService()
license_service = LicenseService()
worker_task = None
worker = RepositoryWorker(
clone_service=clone_service,
commit_service=commit_service,
software_value_service=software_value_service,
vulnerability_scanner_service=vulnerability_scanner_service,
maintainer_service=maintainer_service,
license_service=license_service,
queue_service=queue_service,
)
logger.info("Repo worker initialized")
try:
worker_task = asyncio.create_task(worker.run())
logger.info("RepositoryWorker started successfully")
# Yield control to FastAPI - it will handle signals and trigger shutdown
yield
finally:
await worker.shutdown() # stopping worker to process new repos
try:
await asyncio.wait_for(worker_task, timeout=WORKER_SHUTDOWN_TIMEOUT_SEC)
logger.info("Worker shutdown complete")
except asyncio.TimeoutError:
logger.warning("Worker shutdown timeout, forcing cancellation")
worker_task.cancel()
app = FastAPI(lifespan=lifespan)
@app.get("/")
async def root():
"""Health check endpoint"""
return {"message": "Git Integration Worker Running", "status": "healthy"}