Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from contextlib import contextmanager
- from apscheduler.executors.pool import ProcessPoolExecutor, ThreadPoolExecutor
- from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
- from apscheduler.schedulers.asyncio import AsyncIOScheduler
- from apscheduler.schedulers.background import BackgroundScheduler
- from pytz import utc
- from sqlalchemy import create_engine
- from sqlalchemy.orm import declarative_base, sessionmaker
- from services.scheduler.scheduler_service import Reminder
- from settings import (
- APschedulerDataBaseSettings,
- AuroraDataBaseSettings,
- PostgresSettings,
- ReadPostgresSettings,
- WalletPostgresSettings,
- )
- db_settings = PostgresSettings()
- aurora_db_settings = AuroraDataBaseSettings()
- apscheduler_db_settings = APschedulerDataBaseSettings()
- read_db_settings = ReadPostgresSettings()
- wallet_db_settings = WalletPostgresSettings()
- SQLALCHEMY_DATABASE_URL = (
- "postgresql+psycopg2://"
- f"{db_settings.POSTGRES_USER}:{db_settings.POSTGRES_PASSWORD}@"
- f"{db_settings.POSTGRES_HOST}:{db_settings.POSTGRES_PORT}/"
- f"{db_settings.POSTGRES_DB}"
- )
- AURORA_SQLALCHEMY_DATABASE_URL = (
- "postgresql+psycopg2://"
- f"{aurora_db_settings.AURORA_POSTGRES_USER}:{aurora_db_settings.AURORA_POSTGRES_PASSWORD}@"
- f"{aurora_db_settings.AURORA_POSTGRES_HOST}:{aurora_db_settings.AURORA_POSTGRES_PORT}/"
- f"{aurora_db_settings.AURORA_POSTGRES_DB}"
- )
- APSCHEDULER_SQLALCHEMY_DATABASE_URL = (
- "postgresql+psycopg2://"
- f"{apscheduler_db_settings.APSCHEDULER_POSTGRES_USER}:{apscheduler_db_settings.APSCHEDULER_POSTGRES_PASSWORD}@"
- f"{apscheduler_db_settings.APSCHEDULER_POSTGRES_HOST}:{apscheduler_db_settings.APSCHEDULER_POSTGRES_PORT}/"
- f"{apscheduler_db_settings.APSCHEDULER_POSTGRES_DB}"
- )
- SQLALCHEMY_READ_DATABASE_URL = (
- "postgresql+psycopg2://"
- f"{read_db_settings.READ_POSTGRES_USER}:{read_db_settings.READ_POSTGRES_PASSWORD}@"
- f"{read_db_settings.READ_POSTGRES_HOST}:{read_db_settings.READ_POSTGRES_PORT}/"
- f"{read_db_settings.READ_POSTGRES_DB}"
- )
- WALLET_SQLALCHEMY_DATABASE_URL = (
- "postgresql+psycopg2://"
- f"{wallet_db_settings.WALLET_POSTGRES_USER}:{wallet_db_settings.WALLET_POSTGRES_PASSWORD}@"
- f"{wallet_db_settings.WALLET_POSTGRES_HOST}:{wallet_db_settings.WALLET_POSTGRES_PORT}/"
- f"{wallet_db_settings.WALLET_POSTGRES_DB}"
- )
- engine = create_engine(
- SQLALCHEMY_DATABASE_URL,
- pool_size=30,
- max_overflow=30,
- )
- read_engine = create_engine(
- SQLALCHEMY_READ_DATABASE_URL,
- )
- aurora_engine = create_engine(
- AURORA_SQLALCHEMY_DATABASE_URL,
- pool_size=30,
- max_overflow=30,
- )
- wallet_engine = create_engine(
- WALLET_SQLALCHEMY_DATABASE_URL,
- )
- SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
- SessionLocalRead = sessionmaker(autocommit=False, autoflush=False, bind=read_engine)
- SessionLocalAurora = sessionmaker(autocommit=False, autoflush=False, bind=aurora_engine)
- SessionLocalWallet = sessionmaker(autocommit=False, autoflush=False, bind=wallet_engine)
- Base = declarative_base()
- BaseAurora = declarative_base()
- BaseWallet = declarative_base()
- # Schedule Settings
- JOBSTORES = {"default": SQLAlchemyJobStore(url=APSCHEDULER_SQLALCHEMY_DATABASE_URL)}
- EXECUTORS = {"default": ThreadPoolExecutor(20), "processpool": ProcessPoolExecutor(5)}
- JOB_DEFAULTS = {"coalesce": False, "max_instances": 3}
- scheduler = AsyncIOScheduler(
- jobstores=JOBSTORES,
- executors=EXECUTORS,
- job_defaults=JOB_DEFAULTS,
- timezone=utc,
- )
- scheduler.start()
- reminder = Reminder(scheduler)
- def get_db():
- """
- Get a database session.
- Yields:
- The database session
- """
- with SessionLocal() as db: # pragma: no cover
- yield db # pragma: no cover
- @contextmanager
- def get_read_db():
- """
- Get a read database session.
- Yields:
- The database session
- """
- with SessionLocalRead() as db: # pragma: no cover
- yield db # pragma: no cover
- def get_db_aurora():
- """
- Get a aurora database session.
- Yields:
- The database session
- """
- with SessionLocalAurora() as db: # pragma: no cover
- yield db # pragma: no cover
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement