Skip to content

Commit

Permalink
refactor: update the settings to make moving to production easier
Browse files Browse the repository at this point in the history
  • Loading branch information
engineervix committed Jul 20, 2021
1 parent dc0b3e1 commit 4d76d43
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 21 deletions.
38 changes: 38 additions & 0 deletions {{cookiecutter.project_slug}}/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import List

import environ
# from celery.schedules import crontab

ROOT_DIR = environ.Path(__file__) - 3
APPS_DIR = ROOT_DIR.path("{{cookiecutter.project_slug}}")
Expand Down Expand Up @@ -291,3 +292,40 @@
},
}
}

# Celery
# ------------------------------------------------------------------------------
# if USE_TZ:
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-timezone
# CELERY_TIMEZONE = TIME_ZONE
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-broker_url
# CELERY_BROKER_URL = env("CELERY_BROKER_URL")
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-result_backend
# CELERY_RESULT_BACKEND = CELERY_BROKER_URL
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-accept_content
# CELERY_ACCEPT_CONTENT = ["json"]
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-task_serializer
# CELERY_TASK_SERIALIZER = "json"
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-result_serializer
# CELERY_RESULT_SERIALIZER = "json"
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-time-limit
# # TODO: set to whatever value is adequate in your circumstances
# CELERY_TASK_TIME_LIMIT = 5 * 60
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-soft-time-limit
# # TODO: set to whatever value is adequate in your circumstances
# CELERY_TASK_SOFT_TIME_LIMIT = 60
# # https://docs.celeryproject.org/en/v4.4.7/userguide/configuration.html#task-default-queue
# CELERY_DEFAULT_QUEUE = "cmcndola.org"

# # https://docs.celeryproject.org/en/v4.4.7/userguide/periodic-tasks.html#beat-entries
# CELERY_BEAT_SCHEDULE = {
# "get_daily_quote": {
# "task": "task_fetch_daily_quote",
# # Executes everyday at midnight (good for production)
# "schedule": crontab(minute=0, hour=0),
# # Executes every 2 hours (for development)
# # "schedule": crontab(minute=0, hour="*/2"),
# # Executes every 5 minutes (for development)
# # "schedule": crontab(minute="*/5"),
# },
# }
3 changes: 3 additions & 0 deletions {{cookiecutter.project_slug}}/config/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@
"ATOMIC_REBUILD": True,
},
}

# http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-eager-propagates
# CELERY_TASK_EAGER_PROPAGATES = True
67 changes: 46 additions & 21 deletions {{cookiecutter.project_slug}}/config/settings/production.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/"""
"""See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/"""

import logging
from email.utils import formataddr, getaddresses

from email.utils import getaddresses

from .base import * # noqa
import sentry_sdk
# from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.redis import RedisIntegration

from .base import * # noqa

DEBUG = False # just to make sure!

Expand Down Expand Up @@ -53,16 +55,21 @@
# "COMPRESSOR": "django_redis.compressors.zlib.ZlibCompressor",
"COMPRESSOR": "django_redis.compressors.lz4.Lz4Compressor",
},
}
},
"renditions": {
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
"LOCATION": env("MEMCACHED_URL"), # noqa F405
"TIMEOUT": 600,
"OPTIONS": {"MAX_ENTRIES": 1000},
},
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"

WAGTAILFRONTENDCACHE = {
"cloudflare": {
"BACKEND": "wagtail.contrib.frontend_cache.backends.CloudflareBackend",
"EMAIL": env("CLOUDFLARE_EMAIL_ADDRESS"), # noqa F405
"TOKEN": env("CLOUDFLARE_API_TOKEN"), # noqa F405
"BEARER_TOKEN": env("CLOUDFLARE_BEARER_TOKEN"), # noqa F405
"ZONEID": env("CLOUDFLARE_DOMAIN_ZONE_ID"), # noqa F405
},
}
Expand All @@ -75,26 +82,36 @@
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

# setup email backend
EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"
SENDGRID_API_KEY = env("SENDGRID_API_KEY") # noqa F405
# setup email backend via Anymail
# ------------------------------------------------------------------------------
# https://anymail.readthedocs.io/en/stable/installation/#installing-anymail
INSTALLED_APPS += ["anymail"] # noqa F405
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
# https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference
# https://anymail.readthedocs.io/en/stable/esps/sendgrid/
EMAIL_BACKEND = "anymail.backends.sendgrid.EmailBackend"
ANYMAIL = {
"SENDGRID_API_KEY": env("SENDGRID_API_KEY"), # noqa F405
"SENDGRID_GENERATE_MESSAGE_ID": env("SENDGRID_GENERATE_MESSAGE_ID"), # noqa F405
"SENDGRID_MERGE_FIELD_FORMAT": env("SENDGRID_MERGE_FIELD_FORMAT"), # noqa F405
"SENDGRID_API_URL": env( # noqa F405
"SENDGRID_API_URL", default="https://api.sendgrid.com/v3/"
),
}

if len(getaddresses([env("EMAIL_RECIPIENTS")])) == 1: # noqa F405
LIST_OF_EMAIL_RECIPIENTS.append( # noqa F405
getaddresses([env("EMAIL_RECIPIENTS")])[0] # noqa F405
formataddr(getaddresses([env("EMAIL_RECIPIENTS")])[0]) # noqa F405
)
else:
LIST_OF_EMAIL_RECIPIENTS += getaddresses([env("EMAIL_RECIPIENTS")]) # noqa F405
for email_address in getaddresses([env("EMAIL_RECIPIENTS")]): # noqa F405
LIST_OF_EMAIL_RECIPIENTS += formataddr(email_address) # noqa F405

if len(getaddresses([env("DEFAULT_FROM_EMAIL")])) == 1: # noqa F405
DEFAULT_FROM_EMAIL = getaddresses([env("DEFAULT_FROM_EMAIL")])[0] # noqa F405
else:
DEFAULT_FROM_EMAIL = getaddresses([env("DEFAULT_FROM_EMAIL")]) # noqa F405
email_address = getaddresses([env("DEFAULT_FROM_EMAIL")])[0] # noqa F405
DEFAULT_FROM_EMAIL = formataddr(email_address)

# try:
# from .local import *
# except ImportError:
# pass
# https://docs.djangoproject.com/en/dev/ref/settings/#server-email
SERVER_EMAIL = env("DJANGO_SERVER_EMAIL", default=DEFAULT_FROM_EMAIL) # noqa F405

# enable SSL flag, if the data exchange needs to be secure.
# Not needed for small apps
Expand Down Expand Up @@ -148,10 +165,18 @@
level=SENTRY_LOG_LEVEL, # Capture info and above as breadcrumbs
event_level=logging.ERROR, # Send errors as events
)
integrations = [sentry_logging, DjangoIntegration()]
integrations = [
sentry_logging,
DjangoIntegration(),
# CeleryIntegration(),
RedisIntegration(),
]
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=integrations,
environment=env("SENTRY_ENVIRONMENT", default="production"), # noqa F405
traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", default=0.0), # noqa F405
# If you wish to associate users to errors (assuming you are using
# django.contrib.auth) you may enable sending PII data.
send_default_pii=True,
)

0 comments on commit 4d76d43

Please sign in to comment.