Home > Software design >  Django app on Digitalocean/Heroku takes ages to load - how to investigate and fix?
Django app on Digitalocean/Heroku takes ages to load - how to investigate and fix?

Time:08-21

I recently deployd a small Django app on Digitalocean's app platform but for some reason the app is super slow.

This is the network debug when hitting a small, static site (the view take some ms in development to execute) which doesn't even hit the db, the view just returns a template, that's it.

The static files seem to load rapidly fast, but the view/document's load time looks weird.

enter image description here

This is the log from Digitalocean server which is basically the same for all pages I'm trying - the worker always times out.

[cherry] [2022-08-21 09:53:58] 10.244.17.174 - - [21/Aug/2022:09:53:58  0000] "GET /search/ HTTP/1.1" 200 5591 "https://cherry.trading/search/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
[cherry] [2022-08-21 09:54:29] [2022-08-21 09:54:29  0000] [1] [CRITICAL] WORKER TIMEOUT (pid:169)
[cherry] [2022-08-21 09:54:29] [2022-08-21 09:54:29  0000] [169] [INFO] Worker exiting (pid: 169)
[cherry] [2022-08-21 09:54:30] [2022-08-21 09:54:30  0000] [177] [INFO] Booting worker with pid: 177

I'm on basic subscription with some 512 mb ram (1 CPU)

Component insights: enter image description here

CodePudding user response:

Do you have logging enabled? If not, enable the file logging backend. From there you can investigate further.

You can add this in settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/path/to/django/debug.log', # Do not forget to change this
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

This way, if a view returns any errors, it will be captured and you can then resolve them.

CodePudding user response:

After some back and forth I finally found the issue. The Tailwind middleware messed up things in production.

This is how I now added it:

# settings.py

DEVELOPMENT_MODE = os.getenv("DEVELOPMENT_MODE", "False") == "True"

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware"
]

if DEVELOPMENT_MODE is True:
    # Tailwind hot reloader / browser reload for development environment
    MIDDLEWARE.append("django_browser_reload.middleware.BrowserReloadMiddleware")
  • Related