import os import asyncio from os.path import exists from sentry_sdk.integrations.strawberry import StrawberryIntegration from strawberry.asgi import GraphQL from starlette.applications import Starlette from services.rediscache import redis from resolvers.listener import reactions_worker from resolvers.schema import schema 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() notification_service_task = asyncio.create_task(reactions_worker()) print(f"[main] {notification_service_task}") try: import sentry_sdk sentry_sdk.init( SENTRY_DSN, enable_tracing=True, integrations=[ StrawberryIntegration( # Set async_execution to True if you have # at least one async resolver async_execution=True ), ], ) except Exception as e: print("[sentry] init error") print(e) async def shutdown(): await redis.disconnect() app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown]) app.mount("/", GraphQL(schema, debug=True))