notifier/main.py
Untone 5ab5928d12
Some checks failed
deploy / deploy (push) Has been cancelled
precreate-tables-fix
2024-01-22 22:05:19 +03:00

74 lines
2.3 KiB
Python

import asyncio
import os
from os.path import exists
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from sentry_sdk.integrations.strawberry import StrawberryIntegration
from sqlalchemy import inspect
from sqlalchemy.engine.create import create_engine
from starlette.applications import Starlette
from strawberry.asgi import GraphQL
from orm.notification import NotificationSeen
from resolvers.listener import notifications_worker
from resolvers.model import Notification
from resolvers.schema import schema
from services.db import Base
from services.rediscache import redis
from settings import DB_URL, DEV_SERVER_PID_FILE_NAME, MODE, SENTRY_DSN
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("\t[main]\t")
logger.setLevel(logging.DEBUG)
async def start_up():
await redis.connect()
engine = create_engine(DB_URL)
# Check if the table already exists
inspector = inspect(engine)
if not inspector.has_table("notification"):
# Create the Notification table
Base.metadata.create_all(bind=engine, tables=[Notification.__table__, NotificationSeen.__table__])
logger.info("Notification table was created.")
else:
logger.info("Notification table already exists.")
task = asyncio.create_task(notifications_worker())
logger.info(task)
if MODE == "dev":
if exists(DEV_SERVER_PID_FILE_NAME):
with open(DEV_SERVER_PID_FILE_NAME, "w", encoding="utf-8") as f:
f.write(str(os.getpid()))
else:
try:
import sentry_sdk
sentry_sdk.init(
SENTRY_DSN,
enable_tracing=True,
integrations=[
StrawberryIntegration(async_execution=True),
SqlalchemyIntegration(),
RedisIntegration(),
AioHttpIntegration(),
],
)
except Exception as e:
logger.error("sentry init error", e)
async def shutdown():
await redis.disconnect()
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown])
app.mount("/", GraphQL(schema, debug=True))