notifier/main.py

58 lines
1.6 KiB
Python
Raw Normal View History

2023-11-24 02:18:02 +00:00
import asyncio
2024-02-16 23:56:15 +00:00
import logging
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
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
2023-11-23 22:58:55 +00:00
2023-12-22 09:09:03 +00:00
from resolvers.listener import notifications_worker
2023-11-26 10:18:57 +00:00
from resolvers.schema import schema
2023-12-17 22:20:13 +00:00
from services.rediscache import redis
2024-01-22 19:15:49 +00:00
from settings import DEV_SERVER_PID_FILE_NAME, MODE, SENTRY_DSN
2024-01-22 19:05:19 +00:00
logging.basicConfig(level=logging.DEBUG)
2024-02-16 23:56:15 +00:00
logger = logging.getLogger('\t[main]\t')
2024-01-22 19:05:19 +00:00
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()
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
2024-02-16 23:56:15 +00:00
if MODE == 'dev':
2023-11-23 22:58:55 +00:00
if exists(DEV_SERVER_PID_FILE_NAME):
2024-02-16 23:56:15 +00:00
with open(DEV_SERVER_PID_FILE_NAME, 'w', encoding='utf-8') as f:
2023-11-23 22:58:55 +00:00
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-02-16 23:56:15 +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])
2024-02-16 23:56:15 +00:00
app.mount('/', GraphQL(schema, debug=True))