Skip to content

Commit

Permalink
Merge pull request #76 from openstreetmap-polska/dev
Browse files Browse the repository at this point in the history
Release to main
  • Loading branch information
Zaczero authored Apr 10, 2024
2 parents ca42454 + 780feb9 commit 7ae53d2
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 10 deletions.
6 changes: 3 additions & 3 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pyproj import Transformer

NAME = 'openaedmap-backend'
VERSION = '2.9.0'
VERSION = '2.9.1'
CREATED_BY = f'{NAME} {VERSION}'
WEBSITE = 'https://openaedmap.org'

Expand All @@ -27,8 +27,8 @@
)

POSTGRES_LOG = os.getenv('POSTGRES_LOG', '0').strip().lower() in ('1', 'true', 'yes')
POSTGRES_URL = 'postgresql+asyncpg://postgres:postgres@127.0.0.1/postgres'
REDIS_URL = os.getenv('REDIS_URL', 'redis://127.0.0.1?protocol=3')
POSTGRES_URL = 'postgresql+asyncpg://postgres:postgres@/postgres?host=/tmp/openaedmap-postgres'
REDIS_URL = os.getenv('REDIS_URL', 'unix:///tmp/openaedmap-redis.sock?protocol=3')

DEFAULT_CACHE_MAX_AGE = timedelta(minutes=1)
DEFAULT_CACHE_STALE = timedelta(minutes=5)
Expand Down
4 changes: 2 additions & 2 deletions config/postgres.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

# disable listening on unix socket
# reason: unused, improved compatibility
unix_socket_directories = ''
unix_socket_directories = '/tmp/openaedmap-postgres'

# adjust memory usage
shared_buffers = 512MB
effective_cache_size = 1GB

# increase max connections
max_connections = 1000
max_connections = 10000

# detect disconnected clients
# reason: safeguard resource usage
Expand Down
12 changes: 12 additions & 0 deletions config/redis.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
# - 2GB RAM
# - 6GB SSD

# listen on socket
# reason: reduce latency
unixsocket /tmp/openaedmap-redis.sock
unixsocketperm 700

# single-database mode
databases 1

# use simple locale collate
# reason: faster sorting without bias
locale-collate C

# disable persistence
# reason: redis is cache only, use postgres for persistence
save ""
Expand Down
2 changes: 1 addition & 1 deletion db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
echo_pool=POSTGRES_LOG,
json_deserializer=JSON_DECODE,
json_serializer=lambda x: JSON_ENCODE(x).decode(),
pool_size=8,
pool_size=10,
max_overflow=-1,
query_cache_size=128,
)
Expand Down
3 changes: 3 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let

entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
set -ex
rm -f data/postgres/postmaster.pid
dev-start
set -o allexport
source "envs/app/${envTag}.env" set
Expand All @@ -42,6 +43,7 @@ with pkgs; dockerTools.buildLayeredImage {

extraCommands = ''
set -e
mkdir tmp
mkdir app && cd app
mkdir -p data/postgres data/photos
cp "${./.}"/*.py .
Expand All @@ -57,6 +59,7 @@ with pkgs; dockerTools.buildLayeredImage {
fakeRootCommands = ''
set -e
${dockerTools.shadowSetup}
chmod 0777 tmp
groupadd --system -g 999 docker
useradd --system --no-create-home -u 999 -g 999 docker
chown -R docker:docker app
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3"

services:
app:
image: backend:${TAG}
Expand Down
5 changes: 5 additions & 0 deletions services/aed_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ async def _update_db_snapshot() -> None:
logging.info('Updating country codes')
await _assign_country_codes(aeds)

logging.info('Updating statistics')
async with db_write() as session:
await session.connection(execution_options={'isolation_level': 'AUTOCOMMIT'})
await session.execute(text(f'ANALYZE "{AED.__tablename__}"'))

logging.info('AED update finished (=%d)', len(aeds))


Expand Down
7 changes: 7 additions & 0 deletions services/country_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from country_code_assigner import CountryCodeAssigner
from db import db_read, db_write
from models.bbox import BBox
from models.db.aed import AED
from models.db.country import Country
from osm_countries import get_osm_countries
from services.state_service import StateService
Expand Down Expand Up @@ -108,6 +109,12 @@ async def _update_db() -> None:
from services.aed_service import AEDService

await AEDService.update_country_codes()

logging.info('Updating statistics')
async with db_write() as session:
await session.connection(execution_options={'isolation_level': 'AUTOCOMMIT'})
await session.execute(text(f'ANALYZE "{AED.__tablename__}", "{Country.__tablename__}"'))

logging.info('Country update finished')


Expand Down
4 changes: 2 additions & 2 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ let
--pwfile=<(echo postgres)
fi
mkdir -p data/supervisor
mkdir -p /tmp/openaedmap-postgres data/supervisor
supervisord -c config/supervisord.conf
echo "Supervisor started"
echo "Waiting for Postgres to start..."
while ! pg_isready -q -h 127.0.0.1 -t 10; do sleep 0.1; done
while ! pg_isready -q -h /tmp/openaedmap-postgres -t 10; do sleep 0.1; done
echo "Postgres started, running migrations"
alembic-upgrade
'')
Expand Down

0 comments on commit 7ae53d2

Please sign in to comment.