Skip to content

Commit

Permalink
Merge pull request #131 from ropable/master
Browse files Browse the repository at this point in the history
Tweak DB session handling in geocoder functions, bump minor dependency versions.
  • Loading branch information
ropable authored Oct 2, 2024
2 parents e70c5b0 + 9bd0557 commit 2ca05f2
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 232 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ updates:
directory: "/"
schedule:
interval: "weekly"
target-branch: "master"
- package-ecosystem: "github-actions"
# Workflow files stored in the
# default location of `.github/workflows`
directory: "/"
schedule:
interval: "weekly"
target-branch: "master"
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ RUN addgroup -g ${GID} appuser \
# Install Python libs using Poetry.
FROM builder_base AS python_libs_caddy
# Add system dependencies required to use GDAL
# Ref: https://stackoverflow.com/a/59040511/14508
RUN apk add --no-cache \
gdal \
geos \
proj \
binutils
binutils \
&& ln -s /usr/lib/libproj.so.25 /usr/lib/libproj.so \
&& ln -s /usr/lib/libgdal.so.35 /usr/lib/libgdal.so \
&& ln -s /usr/lib/libgeos_c.so.1 /usr/lib/libgeos_c.so
WORKDIR /app
COPY poetry.lock pyproject.toml ./
ARG POETRY_VERSION=1.8.3
Expand Down
23 changes: 14 additions & 9 deletions geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import re

import ujson
from bottle import Bottle, request, response, static_file
from bottle import Bottle, HTTPResponse, request, response, static_file
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import text

from caddy.utils import env
Expand All @@ -22,7 +22,7 @@
# https://docs.sqlalchemy.org/en/20/orm/contextual.html#unitofwork-contextual
database_url = env("DATABASE_URL").replace("postgis", "postgresql+psycopg")
db_engine = create_engine(database_url)
session = scoped_session(sessionmaker(bind=db_engine, autoflush=True))()
Session = sessionmaker(bind=db_engine, autoflush=True)

# Regex patterns
LON_LAT_PATTERN = re.compile(r"(?P<lon>-?[0-9]+.[0-9]+),\s*(?P<lat>-?[0-9]+.[0-9]+)")
Expand All @@ -46,10 +46,12 @@ def liveness():

@app.route("/readyz")
def readiness():
result = session.execute(text("SELECT 1")).fetchone()

if result:
try:
with Session.begin() as session:
session.execute(text("SELECT 1")).fetchone()
return "OK"
except:
return HTTPResponse(status=500, body="Error")


@app.route("/api/<object_id>")
Expand All @@ -66,7 +68,8 @@ def detail(object_id):
FROM shack_address
WHERE object_id = :object_id""")
sql = sql.bindparams(object_id=object_id)
result = session.execute(sql).fetchone()
with Session.begin() as session:
result = session.execute(sql).fetchone()

if result:
return ujson.dumps(
Expand Down Expand Up @@ -107,7 +110,8 @@ def geocode():
FROM shack_address
WHERE ST_Intersects(boundary, ST_GeomFromEWKT(:ewkt))""")
sql = sql.bindparams(ewkt=ewkt)
result = session.execute(sql).fetchone()
with Session.begin() as session:
result = session.execute(sql).fetchone()

# Serialise and return any query result.
response.content_type = "application/json"
Expand Down Expand Up @@ -150,7 +154,8 @@ def geocode():
WHERE tsv @@ to_tsquery(:tsquery)
LIMIT :limit""")
sql = sql.bindparams(tsquery=tsquery, limit=limit)
result = session.execute(sql).fetchall()
with Session.begin() as session:
result = session.execute(sql).fetchall()

# Serialise and return any query results.
response.content_type = "application/json"
Expand Down
2 changes: 1 addition & 1 deletion kustomize/overlays/prod/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ patches:
- path: service_patch.yaml
images:
- name: ghcr.io/dbca-wa/caddy
newTag: 2.3.10
newTag: 2.3.11
Loading

0 comments on commit 2ca05f2

Please sign in to comment.