Skip to content

Commit 8e09e0c

Browse files
author
Siddharth
committed
initial
0 parents  commit 8e09e0c

17 files changed

+418
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
venv/*
2+
*.pyc

Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM python:3.7.3-slim-stretch
2+
3+
ENV PYTHONDONTWRITEBYTECODE 1
4+
ENV PYTHONUNBUFFERED 1
5+
6+
RUN mkdir /app
7+
8+
RUN apt update && apt upgrade -y && \
9+
apt install gcc default-libmysqlclient-dev -y && \
10+
pip install --upgrade pip
11+
WORKDIR /app
12+
13+
COPY . /app/
14+
RUN pip install -r requirements.txt
15+
16+
EXPOSE 8000
17+
18+
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Procfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
web: python manage.py runserver 0.0.0.0:8000
2+
cmd: celery -A proj worker -l INFO

README.rst

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
==============================================================
2+
Example Django project using Celery
3+
==============================================================
4+
5+
* For sqlite testing, we can implement migration through the docker file via adding another command. `python manage.py makemigrations` and `python manage.py migrate`
6+
7+
Credits: Originally from https://github.com/celery/celery/tree/master/examples/django
8+
9+
Modified the app file just to add a hello-world end point (/api-check/) for validation.
10+
11+
Contents
12+
========
13+
14+
``proj/``
15+
---------
16+
17+
This is a project in itself, created using
18+
``django-admin.py startproject proj``, and then the settings module
19+
(``proj/settings.py``) was modified to add ``demoapp`` to
20+
``INSTALLED_APPS``
21+
22+
``proj/celery.py``
23+
----------
24+
25+
This module contains the Celery application instance for this project,
26+
we take configuration from Django settings and use ``autodiscover_tasks`` to
27+
find task modules inside all packages listed in ``INSTALLED_APPS``.
28+
29+
``demoapp/``
30+
------------
31+
32+
Example generic app. This is decoupled from the rest of the project by using
33+
the ``@shared_task`` decorator. This decorator returns a proxy that always
34+
points to the currently active Celery instance.
35+
36+
Installing requirements
37+
=======================
38+
39+
The settings file assumes that ``rabbitmq-server`` is running on ``localhost``
40+
using the default ports. More information here:
41+
42+
http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html
43+
44+
In addition, some Python requirements must also be satisfied:
45+
46+
.. code-block:: console
47+
48+
$ pip install -r requirements.txt
49+
50+
Starting the worker
51+
===================
52+
53+
.. code-block:: console
54+
55+
$ celery -A proj worker -l INFO
56+
57+
Running a task
58+
===================
59+
60+
.. code-block:: console
61+
62+
$ python ./manage.py shell
63+
>>> from demoapp.tasks import add, mul, xsum
64+
>>> res = add.delay(2,3)
65+
>>> res.get()
66+
5

demoapp/__init__.py

Whitespace-only changes.

demoapp/migrations/0001_initial.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 2.2.1 on 2019-05-24 21:37
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Widget',
16+
fields=[
17+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('name', models.CharField(max_length=140)),
19+
],
20+
),
21+
]

demoapp/migrations/__init__.py

Whitespace-only changes.

demoapp/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.db import models
2+
3+
4+
class Widget(models.Model):
5+
name = models.CharField(max_length=140)

demoapp/tasks.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Create your tasks here
2+
3+
from demoapp.models import Widget
4+
5+
from celery import shared_task
6+
7+
8+
@shared_task
9+
def add(x, y):
10+
return x + y
11+
12+
13+
@shared_task
14+
def mul(x, y):
15+
return x * y
16+
17+
18+
@shared_task
19+
def xsum(numbers):
20+
return sum(numbers)
21+
22+
23+
@shared_task
24+
def count_widgets():
25+
return Widget.objects.count()
26+
27+
28+
@shared_task
29+
def rename_widget(widget_id, name):
30+
w = Widget.objects.get(id=widget_id)
31+
w.name = name
32+
w.save()

demoapp/views.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Create your views here.
2+
from rest_framework.views import APIView
3+
from rest_framework.response import Response
4+
5+
class HomeAPIView(APIView):
6+
def get(self, request, format=None):
7+
return Response({"desc": "hello world"})

0 commit comments

Comments
 (0)