Skip to content

Commit

Permalink
Add workaround for aiopg "File not found" error
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbro committed Jul 23, 2023
1 parent 346b1ab commit 61775d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
37 changes: 20 additions & 17 deletions app/postgis.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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))
}
4 changes: 1 addition & 3 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 61775d7

Please sign in to comment.