Skip to content

Commit

Permalink
Wrap asgi application to ignore lifetime requests
Browse files Browse the repository at this point in the history
ASGI servers send "lifetime" requests to django at startup and shutdown.

The ASGI spec allows these to fail, in which case the server ignores them, however
the way django fails them sends an exception to sentry which we don't want.

Overwrite the handler to return a 501 error without throwing an exception
  • Loading branch information
frankh committed Apr 25, 2024
1 parent 8535186 commit 12b49e3
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion posthog/asgi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import os

from django.core.asgi import get_asgi_application
from django.http.response import HttpResponse

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "posthog.settings")
os.environ.setdefault("SERVER_GATEWAY_INTERFACE", "ASGI")

application = get_asgi_application()

def lifetime_wrapper(func):
async def inner(scope, receive, send):
if scope["type"] != "http":
return HttpResponse(status=501)
return func(scope, receive, send)


application = lifetime_wrapper(get_asgi_application())

0 comments on commit 12b49e3

Please sign in to comment.