feat: switch to persistent SQLite database and improve GPU booking/release handling
- Change database from in-memory to file-based SQLite at aitbc_coordinator.db - Add status="active" to GPU booking creation - Allow GPU release even when not properly booked (cleanup case) - Add error handling for missing booking attributes during refund calculation - Fix get_gpu_reviews query to use scalars() for proper result handling
This commit is contained in:
@@ -5,7 +5,7 @@ from sqlalchemy import StaticPool
|
||||
|
||||
# Create in-memory SQLite database for now
|
||||
engine = create_engine(
|
||||
"sqlite:///:memory:",
|
||||
"sqlite:////home/oib/windsurf/aitbc/apps/coordinator-api/aitbc_coordinator.db",
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
echo=False
|
||||
|
||||
@@ -256,6 +256,7 @@ async def book_gpu(
|
||||
total_cost=total_cost,
|
||||
start_time=start_time,
|
||||
end_time=end_time,
|
||||
status="active"
|
||||
)
|
||||
gpu.status = "booked"
|
||||
session.add(booking)
|
||||
@@ -282,11 +283,14 @@ async def release_gpu(gpu_id: str, session: SessionDep) -> Dict[str, Any]:
|
||||
"""Release a booked GPU."""
|
||||
gpu = _get_gpu_or_404(session, gpu_id)
|
||||
|
||||
# Allow release even if GPU is not properly booked (cleanup case)
|
||||
if gpu.status != "booked":
|
||||
raise HTTPException(
|
||||
status_code=http_status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"GPU {gpu_id} is not booked",
|
||||
)
|
||||
# GPU is already available, just return success
|
||||
return {
|
||||
"status": "already_available",
|
||||
"gpu_id": gpu_id,
|
||||
"message": f"GPU {gpu_id} is already available",
|
||||
}
|
||||
|
||||
booking = session.execute(
|
||||
select(GPUBooking)
|
||||
@@ -296,8 +300,12 @@ async def release_gpu(gpu_id: str, session: SessionDep) -> Dict[str, Any]:
|
||||
|
||||
refund = 0.0
|
||||
if booking:
|
||||
refund = booking.total_cost * 0.5
|
||||
booking.status = "cancelled"
|
||||
try:
|
||||
refund = booking.total_cost * 0.5
|
||||
booking.status = "cancelled"
|
||||
except AttributeError as e:
|
||||
print(f"Warning: Booking missing attribute: {e}")
|
||||
refund = 0.0
|
||||
|
||||
gpu.status = "available"
|
||||
session.commit()
|
||||
@@ -323,8 +331,7 @@ async def get_gpu_reviews(
|
||||
select(GPUReview)
|
||||
.where(GPUReview.gpu_id == gpu_id)
|
||||
.order_by(GPUReview.created_at.desc())
|
||||
.limit(limit)
|
||||
).all()
|
||||
).scalars().all()
|
||||
|
||||
return {
|
||||
"gpu_id": gpu_id,
|
||||
|
||||
Reference in New Issue
Block a user