notifier/main.py

74 lines
2.3 KiB
Python
Raw Normal View History

2023-11-24 02:18:02 +00:00
import asyncio
2023-12-17 22:20:13 +00:00
import os
2023-11-23 22:58:55 +00:00
from os.path import exists
2023-11-30 06:42:41 +00:00
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
2023-11-26 10:51:06 +00:00
from sentry_sdk.integrations.strawberry import StrawberryIntegration
2024-01-22 19:05:19 +00:00
from sqlalchemy import inspect
from sqlalchemy.engine.create import create_engine
2023-11-23 22:58:55 +00:00
from starlette.applications import Starlette
2023-12-17 22:20:13 +00:00
from strawberry.asgi import GraphQL
2024-01-22 19:05:19 +00:00
from orm.notification import NotificationSeen
2023-11-23 22:58:55 +00:00
2023-12-22 09:09:03 +00:00
from resolvers.listener import notifications_worker
2024-01-22 19:05:19 +00:00
from resolvers.model import Notification
2023-11-26 10:18:57 +00:00
from resolvers.schema import schema
2024-01-22 19:05:19 +00:00
from services.db import Base
2023-12-17 22:20:13 +00:00
from services.rediscache import redis
2024-01-22 19:05:19 +00:00
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)
2023-11-23 22:58:55 +00:00
async def start_up():
2023-12-22 09:09:03 +00:00
await redis.connect()
2024-01-22 19:05:19 +00:00
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.")
2023-12-22 09:09:03 +00:00
task = asyncio.create_task(notifications_worker())
2024-01-22 19:05:19 +00:00
logger.info(task)
2023-12-22 09:09:03 +00:00
2023-11-23 22:58:55 +00:00
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:
2023-11-26 18:21:14 +00:00
try:
import sentry_sdk
sentry_sdk.init(
SENTRY_DSN,
enable_tracing=True,
integrations=[
2023-12-15 12:36:43 +00:00
StrawberryIntegration(async_execution=True),
2023-11-30 06:42:41 +00:00
SqlalchemyIntegration(),
RedisIntegration(),
AioHttpIntegration(),
2023-11-26 18:21:14 +00:00
],
)
except Exception as e:
2024-01-22 19:05:19 +00:00
logger.error("sentry init error", e)
2023-11-23 22:58:55 +00:00
async def shutdown():
await redis.disconnect()
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown])
app.mount("/", GraphQL(schema, debug=True))