Advertisement
Mochinov

Untitled

Jan 30th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. from contextlib import contextmanager
  2.  
  3. from apscheduler.executors.pool import ProcessPoolExecutor, ThreadPoolExecutor
  4. from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
  5. from apscheduler.schedulers.asyncio import AsyncIOScheduler
  6. from apscheduler.schedulers.background import BackgroundScheduler
  7. from pytz import utc
  8. from sqlalchemy import create_engine
  9. from sqlalchemy.orm import declarative_base, sessionmaker
  10.  
  11. from services.scheduler.scheduler_service import Reminder
  12. from settings import (
  13. APschedulerDataBaseSettings,
  14. AuroraDataBaseSettings,
  15. PostgresSettings,
  16. ReadPostgresSettings,
  17. WalletPostgresSettings,
  18. )
  19.  
  20. db_settings = PostgresSettings()
  21. aurora_db_settings = AuroraDataBaseSettings()
  22. apscheduler_db_settings = APschedulerDataBaseSettings()
  23. read_db_settings = ReadPostgresSettings()
  24. wallet_db_settings = WalletPostgresSettings()
  25.  
  26. SQLALCHEMY_DATABASE_URL = (
  27. "postgresql+psycopg2://"
  28. f"{db_settings.POSTGRES_USER}:{db_settings.POSTGRES_PASSWORD}@"
  29. f"{db_settings.POSTGRES_HOST}:{db_settings.POSTGRES_PORT}/"
  30. f"{db_settings.POSTGRES_DB}"
  31. )
  32.  
  33. AURORA_SQLALCHEMY_DATABASE_URL = (
  34. "postgresql+psycopg2://"
  35. f"{aurora_db_settings.AURORA_POSTGRES_USER}:{aurora_db_settings.AURORA_POSTGRES_PASSWORD}@"
  36. f"{aurora_db_settings.AURORA_POSTGRES_HOST}:{aurora_db_settings.AURORA_POSTGRES_PORT}/"
  37. f"{aurora_db_settings.AURORA_POSTGRES_DB}"
  38. )
  39.  
  40. APSCHEDULER_SQLALCHEMY_DATABASE_URL = (
  41. "postgresql+psycopg2://"
  42. f"{apscheduler_db_settings.APSCHEDULER_POSTGRES_USER}:{apscheduler_db_settings.APSCHEDULER_POSTGRES_PASSWORD}@"
  43. f"{apscheduler_db_settings.APSCHEDULER_POSTGRES_HOST}:{apscheduler_db_settings.APSCHEDULER_POSTGRES_PORT}/"
  44. f"{apscheduler_db_settings.APSCHEDULER_POSTGRES_DB}"
  45. )
  46.  
  47. SQLALCHEMY_READ_DATABASE_URL = (
  48. "postgresql+psycopg2://"
  49. f"{read_db_settings.READ_POSTGRES_USER}:{read_db_settings.READ_POSTGRES_PASSWORD}@"
  50. f"{read_db_settings.READ_POSTGRES_HOST}:{read_db_settings.READ_POSTGRES_PORT}/"
  51. f"{read_db_settings.READ_POSTGRES_DB}"
  52. )
  53.  
  54. WALLET_SQLALCHEMY_DATABASE_URL = (
  55. "postgresql+psycopg2://"
  56. f"{wallet_db_settings.WALLET_POSTGRES_USER}:{wallet_db_settings.WALLET_POSTGRES_PASSWORD}@"
  57. f"{wallet_db_settings.WALLET_POSTGRES_HOST}:{wallet_db_settings.WALLET_POSTGRES_PORT}/"
  58. f"{wallet_db_settings.WALLET_POSTGRES_DB}"
  59. )
  60.  
  61. engine = create_engine(
  62. SQLALCHEMY_DATABASE_URL,
  63. pool_size=30,
  64. max_overflow=30,
  65. )
  66. read_engine = create_engine(
  67. SQLALCHEMY_READ_DATABASE_URL,
  68. )
  69. aurora_engine = create_engine(
  70. AURORA_SQLALCHEMY_DATABASE_URL,
  71. pool_size=30,
  72. max_overflow=30,
  73. )
  74. wallet_engine = create_engine(
  75. WALLET_SQLALCHEMY_DATABASE_URL,
  76. )
  77.  
  78. SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
  79. SessionLocalRead = sessionmaker(autocommit=False, autoflush=False, bind=read_engine)
  80. SessionLocalAurora = sessionmaker(autocommit=False, autoflush=False, bind=aurora_engine)
  81. SessionLocalWallet = sessionmaker(autocommit=False, autoflush=False, bind=wallet_engine)
  82.  
  83. Base = declarative_base()
  84. BaseAurora = declarative_base()
  85. BaseWallet = declarative_base()
  86.  
  87. # Schedule Settings
  88. JOBSTORES = {"default": SQLAlchemyJobStore(url=APSCHEDULER_SQLALCHEMY_DATABASE_URL)}
  89. EXECUTORS = {"default": ThreadPoolExecutor(20), "processpool": ProcessPoolExecutor(5)}
  90. JOB_DEFAULTS = {"coalesce": False, "max_instances": 3}
  91.  
  92. scheduler = AsyncIOScheduler(
  93. jobstores=JOBSTORES,
  94. executors=EXECUTORS,
  95. job_defaults=JOB_DEFAULTS,
  96. timezone=utc,
  97. )
  98. scheduler.start()
  99. reminder = Reminder(scheduler)
  100.  
  101.  
  102. def get_db():
  103. """
  104. Get a database session.
  105.  
  106. Yields:
  107. The database session
  108. """
  109. with SessionLocal() as db: # pragma: no cover
  110. yield db # pragma: no cover
  111.  
  112.  
  113. @contextmanager
  114. def get_read_db():
  115. """
  116. Get a read database session.
  117.  
  118. Yields:
  119. The database session
  120. """
  121. with SessionLocalRead() as db: # pragma: no cover
  122. yield db # pragma: no cover
  123.  
  124.  
  125. def get_db_aurora():
  126. """
  127. Get a aurora database session.
  128.  
  129. Yields:
  130. The database session
  131. """
  132. with SessionLocalAurora() as db: # pragma: no cover
  133. yield db # pragma: no cover
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement