Skip to content

Commit

Permalink
Merge pull request #2160 from Sefaria/gunicorn-startup-script
Browse files Browse the repository at this point in the history
fix(helm): move library init code to gunicorn start hook.
  • Loading branch information
nsantacruz authored Dec 9, 2024
2 parents 7d3ceeb + 04ced9f commit 71a2f15
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions helm-chart/sefaria-project/templates/configmap/gunicorn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data:
{{- end }}
loglevel = "warning"
preload_app = True
{{- if .Values.instrumentation.enabled }}
def post_fork(server, worker):
Expand All @@ -33,6 +34,10 @@ data:
{{- end }}
def on_starting(server):
from reader.startup import init_library_cache
init_library_cache()
def combined_logformat(logger, name, event_dict):
if event_dict.get('logger') == "gunicorn.access":
message = event_dict['event']
Expand Down
Empty file.
14 changes: 14 additions & 0 deletions reader/management/commands/runserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Using staticfiles as the base class in order to not overwrite its custom runserver logic
from django.contrib.staticfiles.management.commands.runserver import Command as RunserverCommand
from reader.startup import init_library_cache
import structlog
logger = structlog.get_logger(__name__)


class Command(RunserverCommand):

def get_handler(self, *args, **options):
handler = super(Command, self).get_handler(*args, **options)
logger.info("Starting reader application")
init_library_cache()
return handler
2 changes: 2 additions & 0 deletions reader/startup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@


def init_library_cache():
import django
django.setup()
import structlog
logger = structlog.get_logger(__name__)

Expand Down
8 changes: 4 additions & 4 deletions reader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4609,9 +4609,9 @@ def android_asset_links_json(request):
}]
)

def application_health_api(request):
def rollout_health_api(request):
"""
Defines the /healthz and /health-check API endpoints which responds with
Defines the /healthz-rollout API endpoint which responds with
200 if the application is ready for requests,
500 if the application is not ready for requests
"""
Expand All @@ -4625,9 +4625,9 @@ def application_health_api_nonlibrary(request):
return http.HttpResponse("Healthy", status="200")


def rollout_health_api(request):
def application_health_api(request):
"""
Defines the /healthz-rollout API endpoint which responds with
Defines the /healthz API endpoint which responds with
200 if the services Django depends on, Redis, Multiserver, and NodeJs
are available.
500 if any of the aforementioned services are not available
Expand Down
2 changes: 0 additions & 2 deletions sefaria/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
application = get_wsgi_application()


from reader.startup import init_library_cache
init_library_cache()
# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)

0 comments on commit 71a2f15

Please sign in to comment.