feat: creatives and external channels
This commit is contained in:
1
shared/__init__.py
Normal file
1
shared/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Shared utilities package."""
|
||||
@@ -9,18 +9,19 @@ RESET = '\033[0m'
|
||||
BOLD = '\033[1m'
|
||||
|
||||
|
||||
def load_settings(settings_class: type[BaseSettings]) -> BaseSettings:
|
||||
def load_settings[T: BaseSettings](settings_class: type[T]) -> T:
|
||||
try:
|
||||
return settings_class()
|
||||
except ValidationError as e:
|
||||
print(f'{RED}{BOLD}Missing environment variables:{RESET}')
|
||||
|
||||
missing = set()
|
||||
missing: set[str] = set()
|
||||
for err in e.errors():
|
||||
if err['type'] == 'missing' and len(err['loc']) == 1:
|
||||
field = settings_class.model_fields.get(str(err['loc'][0]))
|
||||
if field and hasattr(field.annotation, 'model_fields'):
|
||||
for name, info in field.annotation.model_fields.items():
|
||||
field_info = settings_class.model_fields.get(str(err['loc'][0]))
|
||||
if field_info and field_info.annotation is not None and hasattr(field_info.annotation, 'model_fields'):
|
||||
annotation_fields = getattr(field_info.annotation, 'model_fields', {})
|
||||
for name, info in annotation_fields.items():
|
||||
if info.is_required():
|
||||
missing.add(f'{err["loc"][0]}__{name}'.upper())
|
||||
else:
|
||||
@@ -28,8 +29,8 @@ def load_settings(settings_class: type[BaseSettings]) -> BaseSettings:
|
||||
else:
|
||||
missing.add('__'.join(str(loc).upper() for loc in err['loc']))
|
||||
|
||||
for field in sorted(missing):
|
||||
print(f' {YELLOW}{field}{RESET}')
|
||||
for field_name in sorted(missing):
|
||||
print(f' {YELLOW}{field_name}{RESET}')
|
||||
|
||||
print(f'\n{RED}Check .env file or set required variables{RESET}')
|
||||
sys.exit(1)
|
||||
|
||||
165
shared/datebase_base.py
Normal file
165
shared/datebase_base.py
Normal file
@@ -0,0 +1,165 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from collections.abc import AsyncGenerator
|
||||
from contextlib import asynccontextmanager
|
||||
from contextvars import ContextVar
|
||||
|
||||
import pydantic
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import (
|
||||
AsyncEngine,
|
||||
AsyncSession,
|
||||
async_sessionmaker,
|
||||
create_async_engine,
|
||||
)
|
||||
|
||||
_session_ctx: ContextVar[AsyncSession | None] = ContextVar('session', default=None)
|
||||
|
||||
|
||||
class DatabaseConfig(pydantic.BaseModel):
|
||||
URL: pydantic.PostgresDsn
|
||||
ECHO: bool = False
|
||||
ECHO_POOL: bool = False
|
||||
POOL_SIZE: int = 50
|
||||
MAX_OVERFLOW: int = 10
|
||||
|
||||
|
||||
class DatabaseBase:
|
||||
def __init__(self, config: DatabaseConfig, migrations_path: str = 'migration/versions') -> None:
|
||||
self.config = config
|
||||
self.migrations_path = migrations_path
|
||||
self.engine: AsyncEngine | None = None
|
||||
self.session_factory: async_sessionmaker[AsyncSession] | None = None
|
||||
self._connect_lock = asyncio.Lock()
|
||||
|
||||
async def connect(self) -> None:
|
||||
async with self._connect_lock:
|
||||
if self.engine is not None:
|
||||
logging.warning('Database already connected')
|
||||
return
|
||||
|
||||
self.engine = create_async_engine(
|
||||
url=str(self.config.URL),
|
||||
echo=self.config.ECHO,
|
||||
echo_pool=self.config.ECHO_POOL,
|
||||
pool_size=self.config.POOL_SIZE,
|
||||
max_overflow=self.config.MAX_OVERFLOW,
|
||||
)
|
||||
|
||||
self.session_factory = async_sessionmaker(
|
||||
bind=self.engine,
|
||||
autoflush=False,
|
||||
autocommit=False,
|
||||
expire_on_commit=False,
|
||||
)
|
||||
|
||||
await self.ping()
|
||||
await self._check_migrations()
|
||||
|
||||
logging.info('Database initialized')
|
||||
|
||||
async def ping(self) -> None:
|
||||
if self.session_factory is None:
|
||||
raise RuntimeError('Database not connected. Call connect() first.')
|
||||
|
||||
try:
|
||||
async with self.session_factory() as session:
|
||||
await session.execute(text('SELECT 1'))
|
||||
except Exception as e:
|
||||
logging.error(f'Database ping failed: {e}')
|
||||
|
||||
async def close(self) -> None:
|
||||
async with self._connect_lock:
|
||||
if self.engine is None:
|
||||
logging.warning('Database already closed')
|
||||
return
|
||||
|
||||
await self.engine.dispose()
|
||||
self.engine = None
|
||||
self.session_factory = None
|
||||
|
||||
logging.info('Database closed')
|
||||
|
||||
@property
|
||||
def session(self) -> AsyncSession:
|
||||
session = _session_ctx.get()
|
||||
if session is None:
|
||||
raise RuntimeError('No active transaction. Use transaction() or begin() first.')
|
||||
return session
|
||||
|
||||
async def begin(self) -> None:
|
||||
if _session_ctx.get() is not None:
|
||||
raise RuntimeError('Transaction already started')
|
||||
|
||||
if self.session_factory is None:
|
||||
raise RuntimeError('Database not connected. Call connect() first.')
|
||||
|
||||
session = self.session_factory()
|
||||
_session_ctx.set(session)
|
||||
|
||||
async def commit(self) -> None:
|
||||
session = _session_ctx.get()
|
||||
if session is None:
|
||||
raise RuntimeError('No active transaction')
|
||||
|
||||
try:
|
||||
await session.commit()
|
||||
finally:
|
||||
await session.close()
|
||||
_session_ctx.set(None)
|
||||
|
||||
async def rollback(self) -> None:
|
||||
session = _session_ctx.get()
|
||||
if session is None:
|
||||
raise RuntimeError('No active transaction')
|
||||
|
||||
try:
|
||||
await session.rollback()
|
||||
finally:
|
||||
await session.close()
|
||||
_session_ctx.set(None)
|
||||
|
||||
@asynccontextmanager
|
||||
async def transaction(self) -> AsyncGenerator[None]:
|
||||
await self.begin()
|
||||
try:
|
||||
yield
|
||||
await self.commit()
|
||||
except Exception:
|
||||
await self.rollback()
|
||||
raise
|
||||
|
||||
async def _check_migrations(self) -> None:
|
||||
if self.session_factory is None:
|
||||
raise RuntimeError('Database not connected. Call connect() first.')
|
||||
|
||||
async with self.session_factory() as session:
|
||||
result = await session.execute(
|
||||
text("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'alembic_version')")
|
||||
)
|
||||
if not result.scalar():
|
||||
logging.error('Migrations not applied. Run: alembic upgrade head')
|
||||
return
|
||||
|
||||
result = await session.execute(text('SELECT version_num FROM alembic_version'))
|
||||
current_version = result.scalar()
|
||||
|
||||
if not current_version:
|
||||
logging.error('No migration version found in database. Run: alembic upgrade head')
|
||||
return
|
||||
|
||||
try:
|
||||
from alembic.config import Config
|
||||
from alembic.script import ScriptDirectory
|
||||
|
||||
alembic_cfg = Config('alembic.ini')
|
||||
script = ScriptDirectory.from_config(alembic_cfg)
|
||||
head_revision = script.get_current_head()
|
||||
|
||||
if head_revision and current_version != head_revision:
|
||||
logging.warning(
|
||||
f'Database version {current_version} is outdated (latest: {head_revision}). '
|
||||
f'Run: alembic upgrade head'
|
||||
)
|
||||
except Exception as e:
|
||||
logging.debug(f'Could not check migration head: {e}')
|
||||
@@ -39,11 +39,33 @@ class ColoredConsoleFormatter(logging.Formatter):
|
||||
# Add extra fields
|
||||
extra_parts = []
|
||||
for key, value in record.__dict__.items():
|
||||
if key not in ['name', 'msg', 'args', 'created', 'filename', 'funcName', 'levelname',
|
||||
'levelno', 'lineno', 'module', 'msecs', 'message', 'pathname', 'process',
|
||||
'processName', 'relativeCreated', 'thread', 'threadName', 'exc_info',
|
||||
'exc_text', 'stack_info', 'app_name', 'app_version', 'taskName',
|
||||
'color_message']:
|
||||
if key not in [
|
||||
'name',
|
||||
'msg',
|
||||
'args',
|
||||
'created',
|
||||
'filename',
|
||||
'funcName',
|
||||
'levelname',
|
||||
'levelno',
|
||||
'lineno',
|
||||
'module',
|
||||
'msecs',
|
||||
'message',
|
||||
'pathname',
|
||||
'process',
|
||||
'processName',
|
||||
'relativeCreated',
|
||||
'thread',
|
||||
'threadName',
|
||||
'exc_info',
|
||||
'exc_text',
|
||||
'stack_info',
|
||||
'app_name',
|
||||
'app_version',
|
||||
'taskName',
|
||||
'color_message',
|
||||
]:
|
||||
value_color = self.RED if key == 'error' else self.DARK_YELLOW
|
||||
extra_parts.append(f'{self.DARK_CYAN}{key}{self.RESET}={value_color}{value}{self.RESET}')
|
||||
|
||||
|
||||
@@ -11,15 +11,16 @@ class JSONFormatter(logging.Formatter):
|
||||
'message': record.getMessage(),
|
||||
'module': record.module,
|
||||
'package': record.name,
|
||||
'app_name': record.app_name,
|
||||
'app_version': record.app_version,
|
||||
'app_name': getattr(record, 'app_name', 'unknown'),
|
||||
'app_version': getattr(record, 'app_version', 'unknown'),
|
||||
}
|
||||
|
||||
if record.levelno >= logging.ERROR:
|
||||
log_data['location'] = f'{record.pathname}:{record.lineno}'
|
||||
|
||||
if record.exc_info:
|
||||
log_data['error_type'] = record.exc_info[0].__name__
|
||||
exc_type = record.exc_info[0]
|
||||
log_data['error_type'] = exc_type.__name__ if exc_type else 'Unknown'
|
||||
log_data['error_message'] = str(record.exc_info[1])
|
||||
log_data['traceback'] = self.formatException(record.exc_info)
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ def init(config: LoggerConfig) -> None:
|
||||
|
||||
old_factory = logging.getLogRecordFactory()
|
||||
|
||||
def record_factory(*args, **kwargs) -> logging.LogRecord:
|
||||
def record_factory(*args: object, **kwargs: object) -> logging.LogRecord:
|
||||
record = old_factory(*args, **kwargs)
|
||||
record.app_name = config.APP_NAME
|
||||
record.app_version = config.APP_VERSION
|
||||
@@ -50,6 +50,4 @@ def init(config: LoggerConfig) -> None:
|
||||
|
||||
logging.setLogRecordFactory(record_factory)
|
||||
|
||||
logging.info(
|
||||
'Logger initialized', extra={'app': config.APP_NAME, 'version': config.APP_VERSION}
|
||||
)
|
||||
logging.info('Logger initialized', extra={'app': config.APP_NAME, 'version': config.APP_VERSION})
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
__all__ = ('DatabaseConfig', 'DatabaseHelper')
|
||||
|
||||
from .postgres_helper import DatabaseConfig, DatabaseHelper
|
||||
@@ -1,117 +0,0 @@
|
||||
import logging
|
||||
from collections.abc import AsyncGenerator
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
import pydantic
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import (
|
||||
AsyncEngine,
|
||||
AsyncSession,
|
||||
async_sessionmaker,
|
||||
create_async_engine,
|
||||
)
|
||||
|
||||
|
||||
class DatabaseConfig(pydantic.BaseModel):
|
||||
URL: pydantic.PostgresDsn
|
||||
ECHO: bool = False
|
||||
ECHO_POOL: bool = False
|
||||
POOL_SIZE: int = 50
|
||||
MAX_OVERFLOW: int = 10
|
||||
|
||||
|
||||
class DatabaseHelper:
|
||||
def __init__(self, config: DatabaseConfig, migrations_path: str = 'migration/versions') -> None:
|
||||
self.config = config
|
||||
self.migrations_path = migrations_path
|
||||
self.engine: AsyncEngine | None = None
|
||||
self.session_factory: async_sessionmaker[AsyncSession] | None = None
|
||||
|
||||
async def connect(self) -> None:
|
||||
if self.engine is not None:
|
||||
logging.warning('Database already connected')
|
||||
return
|
||||
|
||||
self.engine = create_async_engine(
|
||||
url=str(self.config.URL),
|
||||
echo=self.config.ECHO,
|
||||
echo_pool=self.config.ECHO_POOL,
|
||||
pool_size=self.config.POOL_SIZE,
|
||||
max_overflow=self.config.MAX_OVERFLOW,
|
||||
)
|
||||
|
||||
self.session_factory = async_sessionmaker(
|
||||
bind=self.engine,
|
||||
autoflush=False,
|
||||
autocommit=False,
|
||||
expire_on_commit=False,
|
||||
)
|
||||
|
||||
await self.ping()
|
||||
await self._check_migrations()
|
||||
|
||||
logging.info('Database initialized')
|
||||
|
||||
async def ping(self) -> None:
|
||||
try:
|
||||
async with self.session_getter() as session:
|
||||
await session.execute(text('SELECT 1'))
|
||||
except Exception as e:
|
||||
logging.error(f'Database ping failed: {e}')
|
||||
|
||||
async def close(self) -> None:
|
||||
if self.engine is None:
|
||||
logging.warning('Database already closed')
|
||||
return
|
||||
|
||||
await self.engine.dispose()
|
||||
self.engine = None
|
||||
self.session_factory = None
|
||||
|
||||
logging.info('Database closed')
|
||||
|
||||
@asynccontextmanager
|
||||
async def session_getter(self) -> AsyncGenerator[AsyncSession]:
|
||||
"""Контекстный менеджер для получения сессии"""
|
||||
if self.session_factory is None:
|
||||
raise RuntimeError('Database not connected. Call connect() first.')
|
||||
|
||||
async with self.session_factory() as session:
|
||||
yield session
|
||||
|
||||
async def _check_migrations(self) -> None:
|
||||
migrations_dir = Path(self.migrations_path)
|
||||
if not migrations_dir.exists():
|
||||
return
|
||||
|
||||
latest_file = next(
|
||||
(
|
||||
f
|
||||
for f in sorted(migrations_dir.glob('*.py'), reverse=True)
|
||||
if f.name != '__init__.py'
|
||||
),
|
||||
None,
|
||||
)
|
||||
if not latest_file:
|
||||
return
|
||||
|
||||
latest_version = latest_file.stem.split('_')[0]
|
||||
|
||||
async with self.session_getter() as session:
|
||||
result = await session.execute(
|
||||
text(
|
||||
"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'alembic_version')"
|
||||
)
|
||||
)
|
||||
if not result.scalar():
|
||||
logging.error('Migrations not applied. Run: alembic upgrade head')
|
||||
return
|
||||
|
||||
result = await session.execute(text('SELECT version_num FROM alembic_version'))
|
||||
current_version = result.scalar()
|
||||
|
||||
if current_version != latest_version:
|
||||
logging.warning(
|
||||
f'Database version {current_version} is outdated (latest: {latest_version}). Run: alembic upgrade head'
|
||||
)
|
||||
@@ -1,3 +0,0 @@
|
||||
from shared.telegram.telegram_bot import TelegramConfig, TelegramHelper
|
||||
|
||||
__all__ = ['TelegramHelper', 'TelegramConfig']
|
||||
@@ -10,11 +10,11 @@ class TelegramConfig(pydantic.BaseModel):
|
||||
DROP_PENDING_UPDATES: bool = True
|
||||
|
||||
|
||||
class TelegramHelper:
|
||||
class TelegramBase:
|
||||
def __init__(self, config: TelegramConfig, *routers: Router) -> None:
|
||||
self.bot: Bot = Bot(token=config.TOKEN)
|
||||
self.dispatcher: Dispatcher = Dispatcher()
|
||||
self._polling_task: asyncio.Task | None = None
|
||||
self._polling_task: asyncio.Task[None] | None = None
|
||||
self._drop_pending_updates: bool = config.DROP_PENDING_UPDATES
|
||||
|
||||
for router in routers:
|
||||
@@ -23,27 +23,43 @@ class TelegramHelper:
|
||||
logging.info('Telegram bot initialized')
|
||||
|
||||
async def start_polling(self) -> None:
|
||||
"""Start bot polling in background"""
|
||||
if self._polling_task is not None:
|
||||
logging.warning('Bot polling already started')
|
||||
return
|
||||
|
||||
self._polling_task = asyncio.create_task(
|
||||
self.dispatcher.start_polling(self.bot, drop_pending_updates=self._drop_pending_updates)
|
||||
self.dispatcher.start_polling(
|
||||
self.bot,
|
||||
drop_pending_updates=self._drop_pending_updates,
|
||||
handle_signals=False, # Don't handle SIGINT/SIGTERM in dispatcher
|
||||
)
|
||||
)
|
||||
|
||||
logging.info('Telegram bot polling started')
|
||||
|
||||
async def stop_polling(self) -> None:
|
||||
"""Stop bot polling and cleanup"""
|
||||
if self._polling_task is None:
|
||||
logging.warning('Bot polling not started')
|
||||
return
|
||||
|
||||
self._polling_task.cancel()
|
||||
# Stop dispatcher polling gracefully
|
||||
await self.dispatcher.stop_polling()
|
||||
|
||||
if self.bot.session is not None:
|
||||
await self.bot.session.close()
|
||||
# Wait for polling task to finish
|
||||
try:
|
||||
await asyncio.wait_for(self._polling_task, timeout=5.0)
|
||||
except TimeoutError:
|
||||
logging.warning('Polling task did not stop in time, cancelling')
|
||||
self._polling_task.cancel()
|
||||
try:
|
||||
await self._polling_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
except asyncio.CancelledError:
|
||||
logging.info('Polling task cancelled')
|
||||
|
||||
# Close bot session
|
||||
await self.bot.session.close()
|
||||
|
||||
self._polling_task = None
|
||||
|
||||
Reference in New Issue
Block a user