Skip to content

Commit

Permalink
Code for 03
Browse files Browse the repository at this point in the history
  • Loading branch information
robertDouglass committed Jul 28, 2024
1 parent 50d1bf7 commit 930179f
Show file tree
Hide file tree
Showing 24 changed files with 729 additions and 29 deletions.
17 changes: 17 additions & 0 deletions 03_django_redis_celery/.environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Set database environment variables
export DB_HOST="$POSTGRESQL_HOST"
export DB_PORT="$POSTGRESQL_PORT"
export DB_PATH="$POSTGRESQL_PATH"
export DB_USERNAME="$POSTGRESQL_USERNAME"
export DB_PASSWORD="$POSTGRESQL_PASSWORD"
export DB_SCHEME="postgresql"
export DATABASE_URL="${DB_SCHEME}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_PATH}"

# Set Cache environment variables
export CACHE_HOST="$REDIS_HOST"
export CACHE_PORT="$REDIS_PORT"
export CACHE_SCHEME="$REDIS_SCHEME"
export CACHE_URL="${CACHE_SCHEME}://${CACHE_HOST}:${CACHE_PORT}"

# Set Redis environment variables
export REDIS_URL="$CACHE_URL"
75 changes: 75 additions & 0 deletions 03_django_redis_celery/.upsun/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
applications:
uploader:
source:
root: "/"

type: "python:3.12"

relationships:
postgresql:
redis:

mounts:
"media":
source: "storage"
source_path: "media"

web:
commands:
start: "gunicorn -b unix:$SOCKET file_uploader.wsgi --log-file -"
upstream:
socket_family: unix
locations:
"/":
passthru: true

"/static":
allow: true
expires: "1h"
root: "static_root"

"/media":
allow: true
expires: "1h"
root: "media"

variables:
env:
N_PREFIX: "/app/.global"

build:
flavor: none

hooks:
build: |
set -eux
pip install -r requirements.txt
deploy: |
set -eux
python manage.py migrate
workers:
queue:
mounts:
"media":
source: "storage"
source_path: "media"
service: "uploader"
commands:
start: |
celery -A file_uploader worker -B --loglevel=debug
services:
postgresql:
type: postgresql:15
redis:
type: redis:7.0

routes:
"https://{default}/":
type: upstream
upstream: "uploader:http"
"https://www.{default}":
type: redirect
to: "https://{default}/"
5 changes: 5 additions & 0 deletions 03_django_redis_celery/file_uploader/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# file_uploader/__init__.py

from .celery import app as celery_app

__all__ = ('celery_app',)
16 changes: 16 additions & 0 deletions 03_django_redis_celery/file_uploader/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for file_uploader project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'file_uploader.settings')

application = get_asgi_application()
14 changes: 14 additions & 0 deletions 03_django_redis_celery/file_uploader/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# file_uploader/celery.py

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'file_uploader.settings')

app = Celery('file_uploader')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
147 changes: 147 additions & 0 deletions 03_django_redis_celery/file_uploader/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
"""
Django settings for file_uploader project.
Generated by 'django-admin startproject' using Django 5.0.7.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-*iijcw1v+-bix8w!b_(7mr@2e2@s@v_(^=9zw0*q2q(=y2rclv'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_celery_beat',
'uploads',
]

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',
]

ROOT_URLCONF = 'file_uploader.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'file_uploader.wsgi.application'


# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = 'static/'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'root': {
'handlers': ['console'],
'level': 'INFO',
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
'celery': {
'handlers': ['console'],
'level': 'DEBUG',
},
},
}

EMAIL_HOST_USER="[email protected]"

from .settings_psh import *
94 changes: 94 additions & 0 deletions 03_django_redis_celery/file_uploader/settings_psh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#################################################################################
# Upsun-specific configuration

import base64
import json
import os
import sys
from urllib.parse import urlparse
from celery.schedules import crontab

# This variable should always match the primary database relationship name
PLATFORM_DB_RELATIONSHIP = "postgresql"

# Helper function for decoding base64-encoded JSON variables.
def decode(variable):
"""Decodes a Upsun environment variable.
Args:
variable (string):
Base64-encoded JSON (the content of an environment variable).
Returns:
An dict (if representing a JSON object), or a scalar type.
Raises:
JSON decoding error.
"""
try:
if sys.version_info[1] > 5:
return json.loads(base64.b64decode(variable))
else:
return json.loads(base64.b64decode(variable).decode("utf-8"))
except json.decoder.JSONDecodeError:
print("Error decoding JSON, code %d", json.decoder.JSONDecodeError)


# Import some Upsun settings from the environment.
# Read more on Upsun variables at https://docs.upsun.com/development/variables/use-variables.html#use-platformsh-provided-variables
if os.getenv("PLATFORM_APPLICATION_NAME"):
DEBUG = False

if os.getenv("PLATFORM_APP_DIR"):
STATIC_ROOT = os.path.join(os.getenv("PLATFORM_APP_DIR"), "static_root")

if os.getenv("PLATFORM_PROJECT_ENTROPY"):
SECRET_KEY = os.getenv("PLATFORM_PROJECT_ENTROPY")

if os.getenv("PLATFORM_ROUTES"):
platform_routes = decode(os.getenv("PLATFORM_ROUTES"))
ALLOWED_HOSTS = list(map(
lambda key: urlparse(key).hostname,
platform_routes.keys(),
))

# Database service configuration, post-build only.
if os.getenv("PLATFORM_RELATIONSHIPS") and PLATFORM_DB_RELATIONSHIP:
platform_relationships = decode(os.getenv("PLATFORM_RELATIONSHIPS"))
if PLATFORM_DB_RELATIONSHIP in platform_relationships:
db_settings = platform_relationships[PLATFORM_DB_RELATIONSHIP][0]
engine = None
if (
"mariadb" in db_settings["type"]
or "mysql" in db_settings["type"]
or "oracle-mysql" in db_settings["type"]
):
engine = "django.db.backends.mysql"
elif "postgresql" in db_settings["type"]:
engine = "django.db.backends.postgresql"

if engine:
DATABASES = {
"default": {
"ENGINE": engine,
"NAME": db_settings["path"],
"USER": db_settings["username"],
"PASSWORD": db_settings["password"],
"HOST": db_settings["host"],
"PORT": db_settings["port"],
},
}

if 'redis' in platform_relationships:
redis_settings = platform_relationships['redis'][0]
REDIS_HOST = redis_settings['host']
REDIS_PORT = redis_settings['port']
REDIS_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/0"

CELERY_BROKER_URL = CELERY_RESULT_BACKEND = REDIS_URL
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
CELERY_BEAT_SCHEDULE = {
'send-file-report-every-minute': {
'task': 'uploads.tasks.send_file_report',
'schedule': crontab(minute='*'),
},
}


Loading

0 comments on commit 930179f

Please sign in to comment.