Skip to content

Commit

Permalink
Added total running time to gunicorn shutdown hooks (#2365)
Browse files Browse the repository at this point in the history
* Added total running time to gunicorn shutdown hooks

* Formatting with ruff (testing it out)

* Fixing import order
  • Loading branch information
jimleroyer authored Nov 19, 2024
1 parent 56da4f9 commit 467e51a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion gunicorn_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import time
import traceback

import gunicorn # type: ignore
Expand All @@ -15,7 +16,12 @@
# Guincorn sets the server type on our app. We don't want to show it in the header in the response.
gunicorn.SERVER = "Undisclosed"

on_aws = os.environ.get("NOTIFY_ENVIRONMENT", "") in ["production", "staging", "scratch", "dev"]
on_aws = os.environ.get("NOTIFY_ENVIRONMENT", "") in [
"production",
"staging",
"scratch",
"dev",
]
if on_aws:
# To avoid load balancers reporting errors on shutdown instances, see AWS doc
# > We also recommend that you configure the idle timeout of your application
Expand Down Expand Up @@ -46,6 +52,9 @@
graceful_timeout = 85
timeout = 90

# Start timer for total running time
start_time = time.time()


def on_starting(server):
server.log.info("Starting Notifications API")
Expand All @@ -58,7 +67,9 @@ def worker_abort(worker):


def on_exit(server):
elapsed_time = time.time() - start_time
server.log.info("Stopping Notifications API")
server.log.info("Total gunicorn running time: {:.2f} seconds".format(elapsed_time))


def worker_int(worker):
Expand Down

0 comments on commit 467e51a

Please sign in to comment.