diff --git a/app/postgis.py b/app/postgis.py index 1dc8e43..6765303 100644 --- a/app/postgis.py +++ b/app/postgis.py @@ -1,8 +1,12 @@ #!/usr/bin/env python3 + +# aiopg bug workaround from https://github.com/aio-libs/aiopg/issues/837#issuecomment-864899918 +import selectors +selectors._PollLikeSelector.modify = selectors._BaseSelectorImpl.modify + import json import aiopg -import psycopg2 from psycopg2.extras import NamedTupleCursor import sentry_sdk @@ -19,26 +23,25 @@ class PostgisClient: The server is assumed to already be populated, including having the soundscape_tile function installed. """ - def __init__(self, server, user_agent, cache_dir, cache_days, cache_size): - # all the other args are only used by the OverpassClient + def __init__(self, server): self.server = server @sentry_sdk.trace async def query(self, x, y): - async with aiopg.connect(self.server) as conn: - async with conn.cursor(cursor_factory=NamedTupleCursor) as cursor: - response = await self._gentile_async(cursor, x, y) - return response - - # based on https://github.com/microsoft/soundscape/blob/main/svcs/data/gentiles.py - async def _gentile_async(self, cursor, x, y, zoom=ZOOM_DEFAULT): try: - await cursor.execute(tile_query, {'zoom': int(zoom), 'tile_x': x, 'tile_y': y}) - value = await cursor.fetchall() - return { - 'type': 'FeatureCollection', - 'features': list(map(lambda x: x._asdict(), value)) - } - except psycopg2.Error as e: + async with aiopg.connect(self.server) as conn: + async with conn.cursor(cursor_factory=NamedTupleCursor) as cursor: + response = await self._gentile_async(cursor, x, y) + return response + except Exception as e: print(e) raise + + # based on https://github.com/microsoft/soundscape/blob/main/svcs/data/gentiles.py + async def _gentile_async(self, cursor, x, y, zoom=ZOOM_DEFAULT): + await cursor.execute(tile_query, {'zoom': int(zoom), 'tile_x': x, 'tile_y': y}) + value = await cursor.fetchall() + return { + 'type': 'FeatureCollection', + 'features': list(map(lambda x: x._asdict(), value)) + } diff --git a/app/server.py b/app/server.py index 05abc95..e76cdef 100644 --- a/app/server.py +++ b/app/server.py @@ -53,9 +53,7 @@ def backend_client(backend_url, user_agent, cache_dir, cache_days, cache_size): backend_url, user_agent, cache_dir, cache_days, cache_size ) elif url_parts.scheme in ('postgis', 'postgres'): - return PostgisClient( - backend_url, user_agent, cache_dir, cache_days, cache_size - ) + return PostgisClient(backend_url) else: raise ValueError("Unrecognized protocol %r" % url_parts.scheme)