Merge pull request #618 from Hestia-Homes/eco-eligiblity-bug

relaxing connections and temp connection for lamnda
This commit is contained in:
KhalimCK 2025-12-14 23:37:49 +08:00 committed by GitHub
commit 00694a200d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 25 deletions

View file

@ -18,8 +18,8 @@ db_string = connection_string.format(
# each lambda doesn't hog all connections
db_engine = create_engine(
db_string,
pool_size=1,
max_overflow=0, # Limit the number of extra connections. With this and pool size, we allow 1 connection per lambda
pool_size=3,
max_overflow=5, # Limit the number of extra connections. With this and pool size, we allow 1 connection per lambda
pool_pre_ping=True,
pool_recycle=300, # Forces SQLAlchemy to close and reopen any connection older than 300 seconds
)

View file

@ -2,7 +2,8 @@ import boto3
import json
import math
import asyncio
import random
from contextlib import contextmanager
from sqlmodel import Session
from datetime import datetime
@ -29,6 +30,19 @@ router = APIRouter(
sqs_client = boto3.client("sqs")
@contextmanager
def db_session():
session = Session(db_engine)
try:
yield session
session.commit()
except Exception:
session.rollback()
raise
finally:
session.close()
@router.post("/trigger", status_code=202)
async def trigger_plan_entrypoint(body: PlanTriggerRequest):
"""
@ -57,28 +71,27 @@ async def trigger_plan_entrypoint(body: PlanTriggerRequest):
scenario_id = data.get("scenario_id")
if not scenario_id:
created_at = datetime.now().isoformat()
session = sessionmaker(bind=db_engine)()
# Create a new scenario
new_scenario = create_scenario(
session=session,
scenario={
"name": body.scenario_name,
"created_at": created_at,
"budget": body.budget,
"portfolio_id": body.portfolio_id,
"housing_type": body.housing_type,
"goal": body.goal,
"goal_value": body.goal_value,
"trigger_file_path": body.trigger_file_path,
"already_installed_file_path": body.already_installed_file_path,
"patches_file_path": body.patches_file_path,
"non_invasive_recommendations_file_path": body.non_invasive_recommendations_file_path,
"exclusions": body.exclusions,
"multi_plan": body.multi_plan
}
)
scenario_id = new_scenario.id
with db_session() as session:
# Create a new scenario
new_scenario = create_scenario(
session=session,
scenario={
"name": body.scenario_name,
"created_at": created_at,
"budget": body.budget,
"portfolio_id": body.portfolio_id,
"housing_type": body.housing_type,
"goal": body.goal,
"goal_value": body.goal_value,
"trigger_file_path": body.trigger_file_path,
"already_installed_file_path": body.already_installed_file_path,
"patches_file_path": body.patches_file_path,
"non_invasive_recommendations_file_path": body.non_invasive_recommendations_file_path,
"exclusions": body.exclusions,
"multi_plan": body.multi_plan
}
)
scenario_id = new_scenario.id
# Insert the scenario ID into the data payload
data["scenario_id"] = scenario_id