Skip to content

Commit b200c16

Browse files
committed
init
0 parents  commit b200c16

24 files changed

+485
-0
lines changed

.env/.dev-sample

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
DEBUG=1
2+
SECRET_KEY=dbaa1_i7%*3r9-=z-+_mz4r-!qeed@(-a_r(g@k8jo8y3r27%m
3+
DJANGO_ALLOWED_HOSTS=*
4+
5+
SQL_ENGINE=django.db.backends.postgresql
6+
SQL_DATABASE=hello_django_dev
7+
SQL_USER=hello_django
8+
SQL_PASSWORD=hello_django
9+
SQL_HOST=db
10+
SQL_PORT=5432
11+
12+
CELERY_BROKER=redis://redis:6379/0
13+
CELERY_BACKEND=redis://redis:6379/0

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
__pycache__
2+
env
3+
*.sqlite3
4+
celerybeat-schedule
5+
celerybeat.pid
6+
celery.log
7+
.coverage
8+
htmlcov

compose/local/django/Dockerfile

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
FROM python:3.9-slim-buster
2+
3+
ENV PYTHONUNBUFFERED 1
4+
ENV PYTHONDONTWRITEBYTECODE 1
5+
6+
RUN apt-get update \
7+
# dependencies for building Python packages
8+
&& apt-get install -y build-essential \
9+
# psycopg2 dependencies
10+
&& apt-get install -y libpq-dev \
11+
# Translations dependencies
12+
&& apt-get install -y gettext \
13+
# cleaning up unused files
14+
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
15+
&& rm -rf /var/lib/apt/lists/*
16+
17+
# Requirements are installed here to ensure they will be cached.
18+
COPY ./requirements.txt /requirements.txt
19+
RUN pip install -r /requirements.txt
20+
21+
COPY ./compose/local/django/entrypoint /entrypoint
22+
RUN sed -i 's/\r$//g' /entrypoint
23+
RUN chmod +x /entrypoint
24+
25+
COPY ./compose/local/django/start /start
26+
RUN sed -i 's/\r$//g' /start
27+
RUN chmod +x /start
28+
29+
COPY ./compose/local/django/celery/worker/start /start-celeryworker
30+
RUN sed -i 's/\r$//g' /start-celeryworker
31+
RUN chmod +x /start-celeryworker
32+
33+
COPY ./compose/local/django/celery/beat/start /start-celerybeat
34+
RUN sed -i 's/\r$//g' /start-celerybeat
35+
RUN chmod +x /start-celerybeat
36+
37+
COPY ./compose/local/django/celery/flower/start /start-flower
38+
RUN sed -i 's/\r$//g' /start-flower
39+
RUN chmod +x /start-flower
40+
41+
WORKDIR /app
42+
43+
ENTRYPOINT ["/entrypoint"]
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
rm -f './celerybeat.pid'
7+
celery -A django_celery_example beat -l INFO
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
worker_ready() {
7+
celery -A django_celery_example inspect ping
8+
}
9+
10+
until worker_ready; do
11+
>&2 echo 'Celery workers not available'
12+
sleep 1
13+
done
14+
>&2 echo 'Celery workers is available'
15+
16+
flower \
17+
--app=django_celery_example \
18+
--broker="${CELERY_BROKER}"
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
celery -A django_celery_example worker -l INFO

compose/local/django/entrypoint

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
# if any of the commands in your code fails for any reason, the entire script fails
4+
set -o errexit
5+
# fail exit if one of your pipe command fails
6+
set -o pipefail
7+
# exits if any of your variables is not set
8+
set -o nounset
9+
10+
postgres_ready() {
11+
python << END
12+
import sys
13+
14+
import psycopg2
15+
16+
try:
17+
psycopg2.connect(
18+
dbname="${SQL_DATABASE}",
19+
user="${SQL_USER}",
20+
password="${SQL_PASSWORD}",
21+
host="${SQL_HOST}",
22+
port="${SQL_PORT}",
23+
)
24+
except psycopg2.OperationalError:
25+
sys.exit(-1)
26+
sys.exit(0)
27+
28+
END
29+
}
30+
until postgres_ready; do
31+
>&2 echo 'Waiting for PostgreSQL to become available...'
32+
sleep 1
33+
done
34+
>&2 echo 'PostgreSQL is available'
35+
36+
exec "$@"

compose/local/django/start

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
set -o pipefail
5+
set -o nounset
6+
7+
python manage.py migrate
8+
python manage.py runserver 0.0.0.0:8000

django_celery_example/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from __future__ import absolute_import, unicode_literals
2+
3+
# This will make sure the app is always imported when
4+
# Django starts so that shared_task will use this app.
5+
from .celery import app as celery_app
6+
7+
__all__ = ('celery_app',)

django_celery_example/asgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for django_celery_example project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_celery_example.settings')
15+
16+
application = get_asgi_application()

django_celery_example/celery.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Celery config file
3+
4+
https://docs.celeryproject.org/en/stable/django/first-steps-with-django.html
5+
6+
"""
7+
from __future__ import absolute_import
8+
import os
9+
import time
10+
11+
from celery import Celery
12+
13+
from django.conf import settings
14+
15+
# this code copied from manage.py
16+
# set the default Django settings module for the 'celery' app.
17+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_celery_example.settings')
18+
19+
# you change change the name here
20+
app = Celery("django_celery_example")
21+
22+
# read config from Django settings, the CELERY namespace would make celery
23+
# config keys has `CELERY` prefix
24+
app.config_from_object('django.conf:settings', namespace='CELERY')
25+
26+
# load tasks.py in django apps
27+
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
28+
29+
30+
@app.task
31+
def add(x, y):
32+
time.sleep(5)
33+
return x / y

django_celery_example/settings.py

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
"""
2+
Django settings for django_celery_example project.
3+
4+
Generated by 'django-admin startproject' using Django 3.1.4.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.1/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/3.1/ref/settings/
11+
"""
12+
13+
import os
14+
from pathlib import Path
15+
16+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
17+
BASE_DIR = Path(__file__).resolve().parent.parent
18+
19+
20+
# Quick-start development settings - unsuitable for production
21+
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
22+
23+
# SECURITY WARNING: keep the secret key used in production secret!
24+
SECRET_KEY = '7vu00+58u%2xj&67%pkymsm1s@im&80ixkmkidfn-iqz!9dy8&'
25+
26+
# SECURITY WARNING: don't run with debug turned on in production!
27+
DEBUG = True
28+
29+
ALLOWED_HOSTS = []
30+
31+
32+
# Application definition
33+
34+
INSTALLED_APPS = [
35+
'django.contrib.admin',
36+
'django.contrib.auth',
37+
'django.contrib.contenttypes',
38+
'django.contrib.sessions',
39+
'django.contrib.messages',
40+
'django.contrib.staticfiles',
41+
]
42+
43+
MIDDLEWARE = [
44+
'django.middleware.security.SecurityMiddleware',
45+
'django.contrib.sessions.middleware.SessionMiddleware',
46+
'django.middleware.common.CommonMiddleware',
47+
'django.middleware.csrf.CsrfViewMiddleware',
48+
'django.contrib.auth.middleware.AuthenticationMiddleware',
49+
'django.contrib.messages.middleware.MessageMiddleware',
50+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
51+
]
52+
53+
ROOT_URLCONF = 'django_celery_example.urls'
54+
55+
TEMPLATES = [
56+
{
57+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
58+
'DIRS': [],
59+
'APP_DIRS': True,
60+
'OPTIONS': {
61+
'context_processors': [
62+
'django.template.context_processors.debug',
63+
'django.template.context_processors.request',
64+
'django.contrib.auth.context_processors.auth',
65+
'django.contrib.messages.context_processors.messages',
66+
],
67+
},
68+
},
69+
]
70+
71+
WSGI_APPLICATION = 'django_celery_example.wsgi.application'
72+
73+
74+
# Database
75+
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
76+
77+
DATABASES = {
78+
"default": {
79+
"ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"),
80+
"NAME": os.environ.get("SQL_DATABASE", os.path.join(BASE_DIR, "db.sqlite3")),
81+
"USER": os.environ.get("SQL_USER", "user"),
82+
"PASSWORD": os.environ.get("SQL_PASSWORD", "password"),
83+
"HOST": os.environ.get("SQL_HOST", "localhost"),
84+
"PORT": os.environ.get("SQL_PORT", "5432"),
85+
}
86+
}
87+
88+
89+
# Password validation
90+
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
91+
92+
AUTH_PASSWORD_VALIDATORS = [
93+
{
94+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
95+
},
96+
{
97+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
98+
},
99+
{
100+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
101+
},
102+
{
103+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
104+
},
105+
]
106+
107+
108+
# Internationalization
109+
# https://docs.djangoproject.com/en/3.1/topics/i18n/
110+
111+
LANGUAGE_CODE = 'en-us'
112+
113+
TIME_ZONE = 'UTC'
114+
115+
USE_I18N = True
116+
117+
USE_L10N = True
118+
119+
USE_TZ = True
120+
121+
122+
# Static files (CSS, JavaScript, Images)
123+
# https://docs.djangoproject.com/en/3.1/howto/static-files/
124+
125+
STATIC_URL = '/static/'
126+
127+
CELERY_BROKER_URL = os.environ.get("CELERY_BROKER", "redis://127.0.0.1:6379/0")
128+
CELERY_RESULT_BACKEND = os.environ.get("CELERY_BACKEND", "redis://127.0.0.1:6379/0")

django_celery_example/urls.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""django_celery_example URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/3.1/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path
18+
19+
urlpatterns = [
20+
path('admin/', admin.site.urls),
21+
]

django_celery_example/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for django_celery_example project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_celery_example.settings')
15+
16+
application = get_wsgi_application()

0 commit comments

Comments
 (0)