Skip to content

Commit 2ca05f2

Browse files
authored
Merge pull request #131 from ropable/master
Tweak DB session handling in geocoder functions, bump minor dependency versions.
2 parents e70c5b0 + 9bd0557 commit 2ca05f2

File tree

6 files changed

+270
-232
lines changed

6 files changed

+270
-232
lines changed

.github/dependabot.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ updates:
44
directory: "/"
55
schedule:
66
interval: "weekly"
7+
target-branch: "master"
78
- package-ecosystem: "github-actions"
8-
# Workflow files stored in the
9-
# default location of `.github/workflows`
109
directory: "/"
1110
schedule:
1211
interval: "weekly"
12+
target-branch: "master"

Dockerfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ RUN addgroup -g ${GID} appuser \
1919
# Install Python libs using Poetry.
2020
FROM builder_base AS python_libs_caddy
2121
# Add system dependencies required to use GDAL
22+
# Ref: https://stackoverflow.com/a/59040511/14508
2223
RUN apk add --no-cache \
2324
gdal \
2425
geos \
2526
proj \
26-
binutils
27+
binutils \
28+
&& ln -s /usr/lib/libproj.so.25 /usr/lib/libproj.so \
29+
&& ln -s /usr/lib/libgdal.so.35 /usr/lib/libgdal.so \
30+
&& ln -s /usr/lib/libgeos_c.so.1 /usr/lib/libgeos_c.so
2731
WORKDIR /app
2832
COPY poetry.lock pyproject.toml ./
2933
ARG POETRY_VERSION=1.8.3

geocoder.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import re
44

55
import ujson
6-
from bottle import Bottle, request, response, static_file
6+
from bottle import Bottle, HTTPResponse, request, response, static_file
77
from sqlalchemy import create_engine
8-
from sqlalchemy.orm import scoped_session, sessionmaker
8+
from sqlalchemy.orm import sessionmaker
99
from sqlalchemy.sql import text
1010

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

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

4747
@app.route("/readyz")
4848
def readiness():
49-
result = session.execute(text("SELECT 1")).fetchone()
50-
51-
if result:
49+
try:
50+
with Session.begin() as session:
51+
session.execute(text("SELECT 1")).fetchone()
5252
return "OK"
53+
except:
54+
return HTTPResponse(status=500, body="Error")
5355

5456

5557
@app.route("/api/<object_id>")
@@ -66,7 +68,8 @@ def detail(object_id):
6668
FROM shack_address
6769
WHERE object_id = :object_id""")
6870
sql = sql.bindparams(object_id=object_id)
69-
result = session.execute(sql).fetchone()
71+
with Session.begin() as session:
72+
result = session.execute(sql).fetchone()
7073

7174
if result:
7275
return ujson.dumps(
@@ -107,7 +110,8 @@ def geocode():
107110
FROM shack_address
108111
WHERE ST_Intersects(boundary, ST_GeomFromEWKT(:ewkt))""")
109112
sql = sql.bindparams(ewkt=ewkt)
110-
result = session.execute(sql).fetchone()
113+
with Session.begin() as session:
114+
result = session.execute(sql).fetchone()
111115

112116
# Serialise and return any query result.
113117
response.content_type = "application/json"
@@ -150,7 +154,8 @@ def geocode():
150154
WHERE tsv @@ to_tsquery(:tsquery)
151155
LIMIT :limit""")
152156
sql = sql.bindparams(tsquery=tsquery, limit=limit)
153-
result = session.execute(sql).fetchall()
157+
with Session.begin() as session:
158+
result = session.execute(sql).fetchall()
154159

155160
# Serialise and return any query results.
156161
response.content_type = "application/json"

kustomize/overlays/prod/kustomization.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ patches:
2121
- path: service_patch.yaml
2222
images:
2323
- name: ghcr.io/dbca-wa/caddy
24-
newTag: 2.3.10
24+
newTag: 2.3.11

0 commit comments

Comments
 (0)