notifier/main.py

43 lines
1.1 KiB
Python
Raw Normal View History

2023-11-23 22:58:55 +00:00
import os
2023-11-24 02:18:02 +00:00
import asyncio
2023-11-23 22:58:55 +00:00
from os.path import exists
2023-11-26 10:18:57 +00:00
from strawberry.asgi import GraphQL
2023-11-23 22:58:55 +00:00
from starlette.applications import Starlette
from services.rediscache import redis
2023-11-26 10:18:57 +00:00
from resolvers.listener import start as listener_start, stop as listener_stop
from resolvers.schema import schema
2023-11-23 22:58:55 +00:00
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, MODE
async def start_up():
if MODE == "dev":
if exists(DEV_SERVER_PID_FILE_NAME):
await redis.connect()
return
else:
with open(DEV_SERVER_PID_FILE_NAME, "w", encoding="utf-8") as f:
f.write(str(os.getpid()))
else:
await redis.connect()
2023-11-26 10:18:57 +00:00
notification_service_task = asyncio.create_task(listener_start())
2023-11-24 02:18:02 +00:00
print(f"[main] {notification_service_task}")
2023-11-23 22:58:55 +00:00
try:
import sentry_sdk
sentry_sdk.init(SENTRY_DSN)
except Exception as e:
print("[sentry] init error")
print(e)
async def shutdown():
2023-11-26 10:18:57 +00:00
listener_stop()
2023-11-23 22:58:55 +00:00
await redis.disconnect()
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown])
app.mount("/", GraphQL(schema, debug=True))