cookiecutter / cookiecutter-django

Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.
https://cookiecutter-django.readthedocs.io
BSD 3-Clause "New" or "Revised" License
12.12k stars 2.9k forks source link

include django-health-check #4428

Open goatwu1993 opened 1 year ago

goatwu1993 commented 1 year ago

Description

  1. Installing the django-health-check.
  2. Add "health_check", "health_check.db" to INSTALLED_APPS
  3. Add "health_check.cache" to INSTALLED_APPS if cache enabled
  4. Add a health check endpoint to URL.
  5. Add Healthcheck to container image by adding this line to Dockerfile: HEALTHCHECK --interval=5m --timeout=3s --start-period=30s --retries=3 CMD python ./manage.py health_check

Not really sure if this sure be included in dev docker image.

Rationale

In a live setup, we use health-checks to see if an app or container is working right. AWS Application Load Balancer (ALB) uses an HTTP call to check health. AWS Elastic Container Service (ECS) does a health-check with a built-in method or a custom script. Other platforms like Kubernetes do similar checks.

akashverma0786 commented 1 year ago

I am new here, can you please tell me what should i check in the function? connection with database or what else? Or Can someone help me here.

goatwu1993 commented 1 year ago

@akashverma0786 possibly create a flag in cookiecutter, include django healthcheck, and add the URL in the urls.py

browniebroke commented 1 year ago

I think that's a pretty standard need when you need to deploy to prod, especially with Docker. One notable exception might be Heroku, maybe?

Not really sure if this sure be included in dev docker image.

Agree, I don't think it's needed for local development.

Side note, I wonder if the Celery checks would help resolve this old issue: #2727

luzfcb commented 1 year ago

@browniebroke I've been using this lib in production and with the recent change in how Sentry charges things, one of the pending tasks for my team is to exclude any logs generated by this lib from being sent to Sentry. I unfortunately haven't had time to work on it yet.

goatwu1993 commented 1 year ago

I think that's a pretty standard need when you need to deploy to prod, especially with Docker. One notable exception might be Heroku, maybe?

Not really sure if this sure be included in dev docker image.

Agree, I don't think it's needed for local development.

Side note, I wonder if the Celery checks would help resolve this old issue: #2727

@browniebroke

It seems Heroku does not have some health check mechanism.

2727 Root cause is celery beat startup without the django_celery_beat_periodictask table. Using docker-compose's depends statement, making celery beat depends on the django service healthy will also do the trick.

     depends_on:
       django:
         condition: service_healthy

However, it is overkilled and over-complicated IMO. It can be fixed by including python manage.py migrate django_celery_beat or python manage.py migrate in celery-beat start up script.

4459 to fix #2727

foarsitter commented 1 year ago

I'm in for adding django-health-check as library but against adding or using it as HEALTHCHECK.

In my opinion Python is to slow for using it in aa healthcheck. 30s start-period is not something that is usable I think.

I found this snippet a while ago but don't remember where. It checks if there is something running at port 5000 and perfectly suitable for rolling updates with docker swarm services.

services:
  django:
    <<: *default-opts    
    command: /start
    healthcheck:
      test: /bin/bash -c "timeout 1 bash -c '</dev/tcp/localhost/5000' 2>/dev/null"
      start_period: 100s
      interval: 30s
      timeout: 30s
      retries: 5
    deploy:
      restart_policy:
        condition: any
        delay: 5s
        max_attempts: 24
      rollback_config:
        parallelism: 0
        order: stop-first
      update_config:
         order: start-first
         failure_action: rollback
         delay: 10s