1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2024-12-26 01:27:07 +00:00

Fix postgresql reconnection #2071

This commit is contained in:
Marian Moravcik 2023-02-27 13:31:38 +01:00
parent 2acf2451b4
commit 2e8203f0d4

View file

@ -1,24 +1,22 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import ast
import os
import atexit import atexit
import json import json
import ast import logging
import os
import time import time
from datetime import datetime from datetime import datetime
from peewee import Model, AutoField, TextField, IntegerField, ForeignKeyField, BlobField, BooleanField, BigIntegerField, \
DateTimeField
from playhouse.sqliteq import SqliteQueueDatabase
from playhouse.migrate import SqliteMigrator, migrate
from playhouse.sqlite_ext import RowIDField
from dogpile.cache import make_region from dogpile.cache import make_region
from utilities.path_mappings import path_mappings from peewee import Model, AutoField, TextField, IntegerField, ForeignKeyField, BlobField, BooleanField, BigIntegerField, \
DateTimeField, OperationalError, PostgresqlDatabase
from peewee import PostgresqlDatabase
from playhouse.migrate import PostgresqlMigrator 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 utilities.path_mappings import path_mappings
from .config import settings, get_array_from from .config import settings, get_array_from
from .get_args import args from .get_args import args
@ -29,14 +27,21 @@ postgresql = settings.postgresql.getboolean('enabled')
region = make_region().configure('dogpile.cache.memory') region = make_region().configure('dogpile.cache.memory')
if postgresql: if postgresql:
class ReconnectPostgresqlDatabase(ReconnectMixin, PostgresqlDatabase):
reconnect_errors = (
(OperationalError, 'server closed the connection unexpectedly'),
)
logger.debug( logger.debug(
f"Connecting to PostgreSQL database: {settings.postgresql.host}:{settings.postgresql.port}/{settings.postgresql.database}") f"Connecting to PostgreSQL database: {settings.postgresql.host}:{settings.postgresql.port}/{settings.postgresql.database}")
database = PostgresqlDatabase(settings.postgresql.database, database = ReconnectPostgresqlDatabase(settings.postgresql.database,
user=settings.postgresql.username, user=settings.postgresql.username,
password=settings.postgresql.password, password=settings.postgresql.password,
host=settings.postgresql.host, host=settings.postgresql.host,
port=settings.postgresql.port, port=settings.postgresql.port,
autoconnect=True autocommit=True,
autoconnect=True,
) )
migrator = PostgresqlMigrator(database) migrator = PostgresqlMigrator(database)
else: else:
@ -62,6 +67,7 @@ class UnknownField(object):
class BaseModel(Model): class BaseModel(Model):
class Meta: class Meta:
database = database database = database
model_metadata_class = ThreadSafeDatabaseMetadata
class System(BaseModel): class System(BaseModel):