diff options
author | Marian Moravcik <[email protected]> | 2023-02-27 13:31:38 +0100 |
---|---|---|
committer | Marian Moravcik <[email protected]> | 2023-02-27 13:31:38 +0100 |
commit | 2e8203f0d4f9830a3608db28e6f61f8d9d87715e (patch) | |
tree | 73477e725d7846f2decc96cec76689419911f138 | |
parent | 2acf2451b44e0dd407e299170ed37117b0eeff7b (diff) | |
download | bazarr-2e8203f0d4f9830a3608db28e6f61f8d9d87715e.tar.gz bazarr-2e8203f0d4f9830a3608db28e6f61f8d9d87715e.zip |
Fix postgresql reconnection #2071
-rw-r--r-- | bazarr/app/database.py | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/bazarr/app/database.py b/bazarr/app/database.py index 26096c26e..76741fae7 100644 --- a/bazarr/app/database.py +++ b/bazarr/app/database.py @@ -1,24 +1,22 @@ # -*- coding: utf-8 -*- -import logging -import os +import ast import atexit import json -import ast +import logging +import os import time from datetime import datetime +from dogpile.cache import make_region from peewee import Model, AutoField, TextField, IntegerField, ForeignKeyField, BlobField, BooleanField, BigIntegerField, \ - DateTimeField -from playhouse.sqliteq import SqliteQueueDatabase + DateTimeField, OperationalError, PostgresqlDatabase +from playhouse.migrate import PostgresqlMigrator from playhouse.migrate import SqliteMigrator, migrate +from playhouse.shortcuts import ThreadSafeDatabaseMetadata, ReconnectMixin from playhouse.sqlite_ext import RowIDField +from playhouse.sqliteq import SqliteQueueDatabase -from dogpile.cache import make_region from utilities.path_mappings import path_mappings - -from peewee import PostgresqlDatabase -from playhouse.migrate import PostgresqlMigrator - from .config import settings, get_array_from from .get_args import args @@ -29,15 +27,22 @@ postgresql = settings.postgresql.getboolean('enabled') region = make_region().configure('dogpile.cache.memory') if postgresql: + class ReconnectPostgresqlDatabase(ReconnectMixin, PostgresqlDatabase): + reconnect_errors = ( + (OperationalError, 'server closed the connection unexpectedly'), + ) + + logger.debug( f"Connecting to PostgreSQL database: {settings.postgresql.host}:{settings.postgresql.port}/{settings.postgresql.database}") - database = PostgresqlDatabase(settings.postgresql.database, - user=settings.postgresql.username, - password=settings.postgresql.password, - host=settings.postgresql.host, - port=settings.postgresql.port, - autoconnect=True - ) + database = ReconnectPostgresqlDatabase(settings.postgresql.database, + user=settings.postgresql.username, + password=settings.postgresql.password, + host=settings.postgresql.host, + port=settings.postgresql.port, + autocommit=True, + autoconnect=True, + ) migrator = PostgresqlMigrator(database) else: db_path = os.path.join(args.config_dir, 'db', 'bazarr.db') @@ -62,6 +67,7 @@ class UnknownField(object): class BaseModel(Model): class Meta: database = database + model_metadata_class = ThreadSafeDatabaseMetadata class System(BaseModel): |