forked from Dniel97/artemis
database: add static variables to prevent having multiple sessions
This commit is contained in:
parent
737312ca3d
commit
6c155a5e48
@ -15,6 +15,13 @@ from core.utils import Utils
|
|||||||
|
|
||||||
|
|
||||||
class Data:
|
class Data:
|
||||||
|
current_schema_version = 4
|
||||||
|
engine = None
|
||||||
|
session = None
|
||||||
|
user = None
|
||||||
|
arcade = None
|
||||||
|
card = None
|
||||||
|
base = None
|
||||||
def __init__(self, cfg: CoreConfig) -> None:
|
def __init__(self, cfg: CoreConfig) -> None:
|
||||||
self.config = cfg
|
self.config = cfg
|
||||||
|
|
||||||
@ -24,22 +31,32 @@ class Data:
|
|||||||
else:
|
else:
|
||||||
self.__url = f"{self.config.database.protocol}://{self.config.database.username}:{self.config.database.password}@{self.config.database.host}/{self.config.database.name}?charset=utf8mb4"
|
self.__url = f"{self.config.database.protocol}://{self.config.database.username}:{self.config.database.password}@{self.config.database.host}/{self.config.database.name}?charset=utf8mb4"
|
||||||
|
|
||||||
self.__engine = create_engine(self.__url, pool_recycle=3600)
|
if Data.engine is None:
|
||||||
session = sessionmaker(bind=self.__engine, autoflush=True, autocommit=True)
|
Data.engine = create_engine(self.__url, pool_recycle=3600)
|
||||||
self.session = scoped_session(session)
|
self.__engine = Data.engine
|
||||||
|
|
||||||
self.user = UserData(self.config, self.session)
|
if Data.session is None:
|
||||||
self.arcade = ArcadeData(self.config, self.session)
|
s = sessionmaker(bind=Data.engine, autoflush=True, autocommit=True)
|
||||||
self.card = CardData(self.config, self.session)
|
Data.session = scoped_session(s)
|
||||||
self.base = BaseData(self.config, self.session)
|
|
||||||
self.current_schema_version = 4
|
if Data.user is None:
|
||||||
|
Data.user = UserData(self.config, self.session)
|
||||||
|
|
||||||
|
if Data.arcade is None:
|
||||||
|
Data.arcade = ArcadeData(self.config, self.session)
|
||||||
|
|
||||||
|
if Data.card is None:
|
||||||
|
Data.card = CardData(self.config, self.session)
|
||||||
|
|
||||||
|
if Data.base is None:
|
||||||
|
Data.base = BaseData(self.config, self.session)
|
||||||
|
|
||||||
log_fmt_str = "[%(asctime)s] %(levelname)s | Database | %(message)s"
|
|
||||||
log_fmt = logging.Formatter(log_fmt_str)
|
|
||||||
self.logger = logging.getLogger("database")
|
self.logger = logging.getLogger("database")
|
||||||
|
|
||||||
# Prevent the logger from adding handlers multiple times
|
# Prevent the logger from adding handlers multiple times
|
||||||
if not getattr(self.logger, "handler_set", None):
|
if not getattr(self.logger, "handler_set", None):
|
||||||
|
log_fmt_str = "[%(asctime)s] %(levelname)s | Database | %(message)s"
|
||||||
|
log_fmt = logging.Formatter(log_fmt_str)
|
||||||
fileHandler = TimedRotatingFileHandler(
|
fileHandler = TimedRotatingFileHandler(
|
||||||
"{0}/{1}.log".format(self.config.server.log_dir, "db"),
|
"{0}/{1}.log".format(self.config.server.log_dir, "db"),
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
|
Loading…
Reference in New Issue
Block a user