Skip to content

Commit

Permalink
fix: use sync def for non-io bound tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Nov 21, 2023
1 parent 90a4a57 commit 04005bb
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def update_multi_polygon_project_boundary(
raise HTTPException(e) from e


async def preview_tasks(boundary: str, dimension: int):
def preview_tasks(boundary: str, dimension: int):
"""Preview tasks by returning a list of task objects."""
"""Use a lambda function to remove the "z" dimension from each coordinate in the feature's geometry """

Expand Down Expand Up @@ -667,7 +667,7 @@ def get_osm_extracts(boundary: str):
return data


async def split_into_tasks(
def split_into_tasks(
db: Session, outline: str, no_of_buildings: int, has_data_extracts: bool
):
"""Splits a project into tasks.
Expand Down Expand Up @@ -2045,7 +2045,7 @@ async def get_background_task_status(task_id: uuid.UUID, db: Session):


async def insert_background_task_into_database(
db: Session, name: str = None, project_id = None
db: Session, name: str = None, project_id=None
) -> uuid.uuid4:
"""Inserts a new task into the database
Params:
Expand Down
20 changes: 15 additions & 5 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import os
import uuid
from pathlib import Path
from typing import List, Optional, Union
from typing import List, Optional

from fastapi import (
APIRouter,
Expand All @@ -32,7 +32,7 @@
Response,
UploadFile,
)
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse
from fastapi.responses import FileResponse, JSONResponse
from loguru import logger as log
from osm_fieldwork.data_models import data_models_path
from osm_fieldwork.make_data_extract import getChoices
Expand All @@ -42,12 +42,22 @@
from shapely.ops import unary_union
from sqlalchemy.orm import Session

from app.config import settings
from ..central import central_crud
from ..db import database, db_models
from ..models.enums import TILES_FORMATS, TILES_SOURCE
from ..tasks import tasks_crud
from . import project_crud, project_schemas, utils
from .project_crud import check_crs
from app.auth.osm import AuthUser, login_required

from app.projects.project_export import (
export_project_by_id,
export_project_by_id_with_odk,
import_fmtm_project,
import_fmtm_project_with_odk,
load_zip_in_memory,
)

router = APIRouter(
prefix="/projects",
Expand Down Expand Up @@ -452,7 +462,7 @@ async def task_split(
# Validatiing Coordinate Reference System
check_crs(boundary)

result = await project_crud.split_into_tasks(
result = project_crud.split_into_tasks(
db, boundary, no_of_buildings, has_data_extracts
)

Expand Down Expand Up @@ -865,7 +875,7 @@ async def preview_tasks(
# Validatiing Coordinate Reference System
check_crs(boundary)

result = await project_crud.preview_tasks(boundary, dimension)
result = project_crud.preview_tasks(boundary, dimension)
return result


Expand Down Expand Up @@ -1287,4 +1297,4 @@ async def generate_files_janakpur(

return {"Message": project_id, "task_id": background_task_id}

return {"Message": f"{project_id}", "task_id": f"{background_task_id}"}

5 changes: 2 additions & 3 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
#

import uuid
from typing import List, Optional, Union
from typing import List, Optional

from geojson_pydantic import Feature as GeojsonFeature
from geoalchemy2.elements import WKBElement
from pydantic import BaseModel, ConfigDict, Field
from pydantic import BaseModel

from app.db import db_models
from app.models.enums import ProjectPriority, ProjectStatus, TaskSplitType
Expand Down
5 changes: 1 addition & 4 deletions src/backend/app/projects/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import uuid
from typing import Optional

from fastapi import (
Expand Down Expand Up @@ -60,9 +59,7 @@ async def generate_files(
contents = upload

# Create task in db and return uuid
log.debug(
f"Creating generate_files background task for project ID: {project_id}"
)
log.debug(f"Creating generate_files background task for project ID: {project_id}")
background_task_id = await project_crud.insert_background_task_into_database(
db, project_id=project_id
)
Expand Down
4 changes: 2 additions & 2 deletions src/backend/app/submission/submission_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ async def convert_to_osm(db: Session, project_id: int, task_id: int):
submission = xform.getSubmissions(odkid, task_id, None, False, True)
submission = (json.loads(submission))["value"]
else:
submission = await get_all_submissions(db, project_id)
submission = get_all_submissions(db, project_id)

if not submission:
raise HTTPException(status_code=404, detail="Submission not found")
Expand Down Expand Up @@ -438,7 +438,7 @@ def extract_files(zip_file_path):
return final_zip_file_path


async def get_all_submissions(db: Session, project_id):
def get_all_submissions(db: Session, project_id):
project_info = project_crud.get_project(db, project_id)

# ODK Credentials
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/submission/submission_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ async def get_osm_xml(
os.remove(jsoninfile)

# Submission JSON
submission = await submission_crud.get_all_submissions(db, project_id)
submission = submission_crud.get_all_submissions(db, project_id)

# Write the submission to a file
with open(jsoninfile, "w") as f:
Expand Down
2 changes: 1 addition & 1 deletion src/backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from app.db.db_models import DbOrganisation, DbUser
from app.main import api, get_application
from app.projects import project_crud
from app.projects.project_schemas import ProjectUpload, ODKCentral, ProjectInfo
from app.projects.project_schemas import ODKCentral, ProjectInfo, ProjectUpload
from app.users.user_schemas import User

engine = create_engine(settings.FMTM_DB_URL.unicode_string())
Expand Down

0 comments on commit 04005bb

Please sign in to comment.