Skip to content

Commit

Permalink
Migrate from WSGI to ASGI
Browse files Browse the repository at this point in the history
  • Loading branch information
ttys0dev committed May 4, 2023
1 parent f242381 commit 3fe0584
Show file tree
Hide file tree
Showing 11 changed files with 628 additions and 47 deletions.
10 changes: 5 additions & 5 deletions cl/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def api_index(request: HttpRequest) -> HttpResponse:
)


def replication_docs(request: HttpRequest) -> HttpResponse:
async def replication_docs(request: HttpRequest) -> HttpResponse:
return render(request, "replication.html", {"private": False})


Expand Down Expand Up @@ -159,7 +159,7 @@ def coverage_data(request, version, court):
)


def get_result_count(request, version, day_count):
async def get_result_count(request, version, day_count):
"""Get the count of results for the past `day_count` number of days
GET parameters will be a complete search string
Expand Down Expand Up @@ -199,7 +199,7 @@ def get_result_count(request, version, day_count):
return JsonResponse({"count": response.result.numFound}, safe=True)


def deprecated_api(request, v):
async def deprecated_api(request, v):
return JsonResponse(
{
"meta": {
Expand All @@ -213,12 +213,12 @@ def deprecated_api(request, v):
)


def webhooks_getting_started(request):
async def webhooks_getting_started(request):
context = {"private": False}
return render(request, "webhooks-getting-started.html", context)


def webhooks_docs(request, version=None):
async def webhooks_docs(request, version=None):
"""Show the correct version of the webhooks docs"""

context = {"private": False}
Expand Down
7 changes: 7 additions & 0 deletions cl/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cl.settings")

application = get_asgi_application()
4 changes: 2 additions & 2 deletions cl/search/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from django.core.management import call_command
from django.db import IntegrityError, transaction
from django.http import HttpRequest
from django.test import RequestFactory, override_settings
from django.test import AsyncRequestFactory, override_settings
from django.urls import reverse
from lxml import etree, html
from rest_framework.status import HTTP_200_OK
Expand Down Expand Up @@ -941,7 +941,7 @@ def setUp(self) -> None:
"--noinput",
]
call_command("cl_update_index", *args)
self.factory = RequestFactory()
self.factory = AsyncRequestFactory()

def test_grouped_queries(self) -> None:
"""When we have a cluster with multiple opinions, do results get
Expand Down
3 changes: 3 additions & 0 deletions cl/settings/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
ROOT_URLCONF = "cl.urls"

INSTALLED_APPS = [
"daphne",
"django.contrib.admin",
"django.contrib.admindocs",
"django.contrib.contenttypes",
Expand Down Expand Up @@ -178,6 +179,8 @@
INSTALLED_APPS.append("django_extensions")
MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")

ASGI_APPLICATION = "cl.asgi.application"


################
# Misc. Django #
Expand Down
26 changes: 13 additions & 13 deletions cl/simple_pages/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@
path("faq/", faq, name="faq"),
path("feeds/", feeds, name="feeds_info"),
path("podcasts/", podcasts, name="podcasts"),
path("contribute/", contribute, name="contribute"),
path("contribute/", contribute, name="contribute"), # type: ignore[arg-type]
path("contact/", contact, name="contact"),
path("contact/thanks/", contact_thanks, name="contact_thanks"),
path("contact/thanks/", contact_thanks, name="contact_thanks"), # type: ignore[arg-type]
# Help pages
path("help/", help_home, name="help_home"),
path("help/", help_home, name="help_home"), # type: ignore[arg-type]
path("help/coverage/", coverage_graph, name="coverage"),
path(
"help/coverage/financial-disclosures/",
coverage_fds,
name="coverage_fds",
),
path("help/markdown/", markdown_help, name="markdown_help"),
path("help/markdown/", markdown_help, name="markdown_help"), # type: ignore[arg-type]
path("help/alerts/", alert_help, name="alert_help"),
path("help/donations/", donation_help, name="donation_help"),
path("help/delete-account/", delete_help, name="delete_help"),
path("help/tags-notes/", tag_notes_help, name="tag_notes_help"),
path("help/search-operators/", advanced_search, name="advanced_search"),
path("help/recap/email/", recap_email_help, name="recap_email_help"),
path("help/broken-email/", broken_email_help, name="broken_email_help"),
path("help/donations/", donation_help, name="donation_help"), # type: ignore[arg-type]
path("help/delete-account/", delete_help, name="delete_help"), # type: ignore[arg-type]
path("help/tags-notes/", tag_notes_help, name="tag_notes_help"), # type: ignore[arg-type]
path("help/search-operators/", advanced_search, name="advanced_search"), # type: ignore[arg-type]
path("help/recap/email/", recap_email_help, name="recap_email_help"), # type: ignore[arg-type]
path("help/broken-email/", broken_email_help, name="broken_email_help"), # type: ignore[arg-type]
# Added 2018-10-23
path(
"search/advanced-techniques/",
Expand All @@ -64,10 +64,10 @@
"coverage/financial-disclosures/",
RedirectView.as_view(pattern_name="coverage_fds", permanent=True),
),
path("terms/v/<int:v>/", old_terms, name="old_terms"),
path("terms/", latest_terms, name="terms"),
path("terms/v/<int:v>/", old_terms, name="old_terms"), # type: ignore[arg-type]
path("terms/", latest_terms, name="terms"), # type: ignore[arg-type]
# Robots
path("robots.txt", robots, name="robots"),
# SEO-related stuff
path("mywot8f5568174e171ff0acff.html", validate_for_wot),
path("mywot8f5568174e171ff0acff.html", validate_for_wot), # type: ignore[arg-type]
]
32 changes: 17 additions & 15 deletions cl/simple_pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
logger = logging.getLogger(__name__)


def about(request: HttpRequest) -> HttpResponse:
async def about(request: HttpRequest) -> HttpResponse:
"""Loads the about page"""
return TemplateResponse(request, "about.html", {"private": False})

Expand Down Expand Up @@ -80,7 +80,7 @@ def faq(request: HttpRequest) -> HttpResponse:
)


def help_home(request: HttpRequest) -> HttpResponse:
async def help_home(request: HttpRequest) -> HttpResponse:
return TemplateResponse(request, "help/index.html", {"private": False})


Expand Down Expand Up @@ -119,35 +119,35 @@ def alert_help(request: HttpRequest) -> HttpResponse:
return TemplateResponse(request, "help/alert_help.html", context)


def donation_help(request: HttpRequest) -> HttpResponse:
async def donation_help(request: HttpRequest) -> HttpResponse:
return TemplateResponse(
request, "help/donation_help.html", {"private": False}
)


def delete_help(request: HttpRequest) -> HttpResponse:
async def delete_help(request: HttpRequest) -> HttpResponse:
return TemplateResponse(
request, "help/delete_account_help.html", {"private": False}
)


def markdown_help(request: HttpRequest) -> HttpResponse:
async def markdown_help(request: HttpRequest) -> HttpResponse:
return TemplateResponse(
request, "help/markdown_help.html", {"private": False}
)


def tag_notes_help(request: HttpRequest) -> HttpResponse:
async def tag_notes_help(request: HttpRequest) -> HttpResponse:
return TemplateResponse(request, "help/tags_help.html", {"private": False})


def recap_email_help(request: HttpRequest) -> HttpResponse:
async def recap_email_help(request: HttpRequest) -> HttpResponse:
return TemplateResponse(
request, "help/recap_email_help.html", {"private": False}
)


def broken_email_help(request: HttpRequest) -> HttpResponse:
async def broken_email_help(request: HttpRequest) -> HttpResponse:
return TemplateResponse(
request,
"help/broken_email_help.html",
Expand Down Expand Up @@ -306,7 +306,7 @@ def podcasts(request: HttpRequest) -> HttpResponse:
)


def contribute(request: HttpRequest) -> HttpResponse:
async def contribute(request: HttpRequest) -> HttpResponse:
return TemplateResponse(request, "contribute.html", {"private": False})


Expand Down Expand Up @@ -373,17 +373,17 @@ def contact(
return TemplateResponse(request, template_path, template_data)


def contact_thanks(request: HttpRequest) -> HttpResponse:
async def contact_thanks(request: HttpRequest) -> HttpResponse:
return TemplateResponse(request, "contact_thanks.html", {"private": True})


def advanced_search(request: HttpRequest) -> HttpResponse:
async def advanced_search(request: HttpRequest) -> HttpResponse:
return TemplateResponse(
request, "help/advanced_search.html", {"private": False}
)


def old_terms(request: HttpRequest, v: str) -> HttpResponse:
async def old_terms(request: HttpRequest, v: str) -> HttpResponse:
return TemplateResponse(
request,
f"terms/{v}.html",
Expand All @@ -395,7 +395,7 @@ def old_terms(request: HttpRequest, v: str) -> HttpResponse:
)


def latest_terms(request: HttpRequest) -> HttpResponse:
async def latest_terms(request: HttpRequest) -> HttpResponse:
return TemplateResponse(
request,
"terms/latest.html",
Expand All @@ -415,11 +415,13 @@ def robots(request: HttpRequest) -> HttpResponse:
return response


def validate_for_wot(request: HttpRequest) -> HttpResponse:
async def validate_for_wot(request: HttpRequest) -> HttpResponse:
return HttpResponse("bcb982d1e23b7091d5cf4e46826c8fc0")


def ratelimited(request: HttpRequest, exception: Exception) -> HttpResponse:
async def ratelimited(
request: HttpRequest, exception: Exception
) -> HttpResponse:
return TemplateResponse(
request,
"429.html",
Expand Down
4 changes: 2 additions & 2 deletions cl/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def delete_account(request: AuthenticatedHttpRequest) -> HttpResponse:
)


def delete_profile_done(request: HttpRequest) -> HttpResponse:
async def delete_profile_done(request: HttpRequest) -> HttpResponse:
return TemplateResponse(request, "profile/deleted.html", {"private": True})


Expand All @@ -442,7 +442,7 @@ def take_out(request: AuthenticatedHttpRequest) -> HttpResponse:
)


def take_out_done(request: HttpRequest) -> HttpResponse:
async def take_out_done(request: HttpRequest) -> HttpResponse:
return TemplateResponse(
request,
"profile/take_out_done.html",
Expand Down
7 changes: 3 additions & 4 deletions docker/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,13 @@ CMD python /opt/courtlistener/manage.py migrate && \
#freelawproject/courtlistener:latest-web-prod
FROM python-base as web-prod

CMD gunicorn cl_wsgi:application \
--chdir /opt/courtlistener/docker/django/wsgi-configs/ \
CMD gunicorn cl_asgi:application \
--chdir /opt/courtlistener/docker/django/asgi-configs/ \
--user www-data \
--group www-data \
# Set high number of workers. Docs recommend 2-4× core count`
--workers ${NUM_WORKERS:-48} \
--worker-class gthread \
--threads 10 \
--worker-class uvicorn.workers.UvicornWorker \
# Allow longer queries to solr.
--limit-request-line 6000 \
# Reset each worker once in a while
Expand Down
9 changes: 9 additions & 0 deletions docker/django/asgi-configs/cl_asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os
import sys

from django.core.asgi import get_asgi_application

os.environ["DJANGO_SETTINGS_MODULE"] = "cl.settings"

sys.path.append("/opt/courtlistener")
application = get_asgi_application()
Loading

0 comments on commit 3fe0584

Please sign in to comment.