Skip to content

Commit

Permalink
fix: reduce db connections during project creation
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Feb 25, 2024
1 parent 0bf020a commit d9fe0b6
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,21 +1110,18 @@ def flatten_dict(d, parent_key="", sep="_"):
# NOTE defined as non-async to run in separate thread
def generate_task_files(
db: Session,
project_id: int,
project: db_models.DbProject,
task_id: int,
data_extract: FeatureCollection,
xlsform: str,
form_type: str,
odk_credentials: project_schemas.ODKCentralDecrypted,
):
"""Generate all files for a task."""
project_log = log.bind(task="create_project", project_id=project_id)
project_log = log.bind(task="create_project", project_id=project.id)

project_log.info(f"Generating files for task {task_id}")

get_project_sync = async_to_sync(get_project)
project = get_project_sync(db, project_id)

odk_id = project.odkid
project_name = project.project_name_prefix
category = project.xform_title
Expand All @@ -1133,7 +1130,7 @@ def generate_task_files(
# Create an app user for the task
project_log.info(
f"Creating odkcentral app user ({appuser_name}) "
f"for FMTM task ({task_id}) in FMTM project ({project_id})"
f"for FMTM task ({task_id}) in FMTM project ({project.id})"
)
appuser = OdkAppUser(
odk_credentials.odk_central_url,
Expand Down Expand Up @@ -1198,7 +1195,7 @@ def generate_task_files(

# Commit db transaction
db.commit()
db.refresh(project)
# db.refresh(project)

return True

Expand Down Expand Up @@ -1295,7 +1292,7 @@ def wrap_generate_task_files(task_id):
try:
generate_task_files(
next(get_db()),
project_id,
project,
task_id,
split_extract_dict[task_id],
xlsform,
Expand Down

0 comments on commit d9fe0b6

Please sign in to comment.