Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions samples/django-celery/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM mcr.microsoft.com/devcontainers/python:3.12-bookworm
11 changes: 11 additions & 0 deletions samples/django-celery/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"features": {
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"ghcr.io/devcontainers/features/aws-cli:1": {}
}
}
26 changes: 26 additions & 0 deletions samples/django-celery/.github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Deploy

on:
push:
branches:
- main

jobs:
deploy:
environment: playground
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write

steps:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Deploy
uses: DefangLabs/defang-github-action@v1.2.0
with:
config-env-vars: POSTGRES_PASSWORD SECRET_KEY
env:
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
SECRET_KEY: ${{ secrets.SECRET_KEY }}
63 changes: 63 additions & 0 deletions samples/django-celery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Django Celery

[![1-click-deploy](https://defang.io/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-django-celery-template%26template_owner%3DDefangSamples)

This is a sample Django application that uses Celery for background tasks. It uses Postgres as the database and Redis as the message broker.

## Prerequisites

1. Download [Defang CLI](https://github.com/DefangLabs/defang)
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)

## Development

To run the application locally, you can use the following command:

```bash
docker compose -f compose.dev.yaml up --build
```

## Configuration

For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):

> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.

### `POSTGRES_PASSWORD`
The password for the Postgres database.
```bash
defang config set POSTGRES_PASSWORD
```

### `SECRET_KEY`
The [secret key](https://docs.djangoproject.com/en/5.1/ref/settings/#std-setting-SECRET_KEY) for the Django application.
```bash
defang config set SECRET_KEY
```

## Deployment

> [!NOTE]
> Download [Defang CLI](https://github.com/DefangLabs/defang)

### Defang Playground

Deploy your application to the Defang Playground by opening up your terminal and typing:
```bash
defang compose up
```

### BYOC

If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud).

---

Title: Django Celery

Short Description: A Django application that uses Celery for background tasks, Postgres as the database, and Redis as the message broker.

Tags: Django, Celery, Postgres, Redis

Languages: python, sql
73 changes: 73 additions & 0 deletions samples/django-celery/app/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# Django
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
media/
static/
staticfiles/

# Virtual Environment
venv/
ENV/
.env
.venv

# Version Control
.git
.gitignore
.gitattributes

# IDE/Editor files
.idea/
.vscode/
*.swp
*.swo
*~
.DS_Store

# Testing
.coverage
htmlcov/
.tox/
.pytest_cache/

# Documentation
docs/
README.md
LICENSE

# Docker
Dockerfile*
docker-compose*.yml
.docker/

# Celery
celerybeat-schedule
celerybeat.pid

# Temporary files
*.tmp
.cache/
84 changes: 84 additions & 0 deletions samples/django-celery/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Django #
*.log
*.pot
*.pyc
__pycache__/
local_settings.py
db.sqlite3
db.sqlite3-journal
media

# If you are using PostgreSQL
*.psql
*.pgsql
*.sql

# Python #
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Virtual Environment #
venv/
env/
ENV/
.env

# IDE #
.idea/
.vscode/
*.swp
*.swo
.DS_Store

# Celery #
celerybeat-schedule
celerybeat.pid

# Coverage #
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
coverage.xml
*.cover
.pytest_cache/

# Django stuff:
staticfiles/
staticfiles.json
.static_storage/
.media/

# Jupyter Notebook
.ipynb_checkpoints
43 changes: 43 additions & 0 deletions samples/django-celery/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM python:3.12-slim

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
DJANGO_SETTINGS_MODULE=django_celery.settings

# Create a non-root user
RUN adduser --disabled-password --gecos "" django

# Install system dependencies (merged from both stages)
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
curl \
build-essential \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt \
gunicorn \
whitenoise \
dj-database-url \
psycopg2-binary \
redis

# Copy project files
COPY --chown=django:django . .

# Collect static files
RUN python manage.py collectstatic --noinput

# Switch to non-root user
USER django

# Expose port 8000
EXPOSE 8000

CMD [ "./command.sh" ]
14 changes: 14 additions & 0 deletions samples/django-celery/app/command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Apply database migrations
python manage.py migrate

# Create superuser if not exists
python manage.py createsuperauto

# Start the Django development server if DEBUG is True
if [ "$DEBUG" = "True" ]; then
python manage.py runserver 0.0.0.0:8000
else
gunicorn django_celery.wsgi:application --bind 0.0.0.0:8000 --workers 1 --threads 2 --timeout 120
fi
7 changes: 7 additions & 0 deletions samples/django-celery/app/django_celery/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ('celery_app',)
16 changes: 16 additions & 0 deletions samples/django-celery/app/django_celery/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for django_celery 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.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_celery.settings")

application = get_asgi_application()
17 changes: 17 additions & 0 deletions samples/django-celery/app/django_celery/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_celery.settings')

# create the Celery app
app = Celery('django_celery')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

Loading