Skip to content

Commit

Permalink
Add a variant of liveblog running on RabbitMQ.
Browse files Browse the repository at this point in the history
  • Loading branch information
proofit404 committed Jan 5, 2017
1 parent 4fe2da2 commit 5442457
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 26 deletions.
28 changes: 25 additions & 3 deletions liveblog/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Installation
------------

Manual installation
~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~

Make a new virtualenv for the project, and run::

pip install -r requirements.txt
pip install -r requirements/redis.txt

Then, you'll need Redis running locally; the settings are configured to
point to ``localhost``, port ``6379``, but you can change this in the
Expand All @@ -40,7 +40,7 @@ Finally, run::
python manage.py runserver

Docker installation
~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~

Run the app::

Expand Down Expand Up @@ -73,6 +73,28 @@ to see its posts page.
Now, in the admin, make some new Posts against your blog, and watch them appear
in your new window. Edit them, and they'll update themselves live on the page too.

RabbitMQ
--------

You can try to run this example on RabbitMQ channel layer.

For manual installation use following commands::

pip install -r requirements/rabbitmq.txt
export DJANGO_SETTINGS_MODULE="liveblog.settings.rabbitmq"
export PYTHONPATH=$PWD
django-admin migrate
django-admin createsuperuser
django-admin runworker
daphne -b 0.0.0.0 -p 8000 liveblog.asgi:channel_layer

For docker installation use::

docker-compose -f docker-compose.rabbitmq.yml run --rm web django-admin migrate
docker-compose -f docker-compose.rabbitmq.yml run --rm web django-admin createsuperuser
docker-compose -f docker-compose.rabbitmq.yml up -d

The rest of necessary steps are the same.

Suggested Exercises
-------------------
Expand Down
9 changes: 9 additions & 0 deletions liveblog/compose/Dockerfile.rabbitmq
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.5
ENV PYTHONUNBUFFERED 1
ENV RABBITMQ_HOST "rabbitmq"
ENV DJANGO_SETTINGS_MODULE "liveblog.settings.rabbitmq"
ENV PYTHONPATH "/code"
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN pip install -r requirements/rabbitmq.txt
4 changes: 2 additions & 2 deletions liveblog/Dockerfile → liveblog/compose/Dockerfile.redis
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM python:3.5
ENV PYTHONUNBUFFERED 1
ENV REDIS_HOST "redis"
ENV DJANGO_SETTINGS_MODULE "liveblog.settings.redis"
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN pip install -r requirements.txt
RUN pip install -r requirements/redis.txt
RUN python manage.py migrate

26 changes: 26 additions & 0 deletions liveblog/docker-compose.rabbitmq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: "2"
services:
rabbitmq:
image: rabbitmq:management
ports:
- "15672:15672"
web:
build:
context: .
dockerfile: compose/Dockerfile.rabbitmq
command: daphne -b 0.0.0.0 -p 8000 liveblog.asgi:channel_layer
volumes:
- .:/code
depends_on:
- rabbitmq
ports:
- "8000:8000"
worker:
build:
context: .
dockerfile: compose/Dockerfile.rabbitmq
command: django-admin runworker
volumes:
- .:/code
depends_on:
- rabbitmq
5 changes: 3 additions & 2 deletions liveblog/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
version: "2"

services:
redis:
image: redis:alpine
web:
build: .
build:
context: .
dockerfile: compose/Dockerfile.redis
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
Expand Down
2 changes: 1 addition & 1 deletion liveblog/liveblog/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

import os
from channels.asgi import get_channel_layer
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "liveblog.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "liveblog.settings.redis")
channel_layer = get_channel_layer()
1 change: 1 addition & 0 deletions liveblog/liveblog/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# SECURITY WARNING: keep the secret key used in production secret! And don't use debug=True in production!
SECRET_KEY = 'imasecret'
Expand Down Expand Up @@ -44,21 +44,6 @@

ROOT_URLCONF = 'liveblog.urls'

redis_host = os.environ.get('REDIS_HOST', 'localhost')

# Channel layer definitions
# http://channels.readthedocs.org/en/latest/deploying.html#setting-up-a-channel-backend
CHANNEL_LAYERS = {
"default": {
# This example app uses the Redis channel layer implementation asgi_redis
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": [(redis_host, 6379)],
},
"ROUTING": "liveblog.routing.channel_routing",
},
}

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
Expand Down
17 changes: 17 additions & 0 deletions liveblog/liveblog/settings/rabbitmq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .base import *

rabbitmq_host = os.environ.get('RABBITMQ_HOST', 'localhost')
rabbitmq_url = 'amqp://guest:guest@%s:5672/%%2F' % rabbitmq_host

# Channel layer definitions
# http://channels.readthedocs.org/en/latest/deploying.html#setting-up-a-channel-backend
CHANNEL_LAYERS = {
"default": {
# This example app uses the Rabbitmq channel layer implementation asgi_rabbitmq
"BACKEND": "asgi_rabbitmq.RabbitmqChannelLayer",
"CONFIG": {
"url": rabbitmq_url,
},
"ROUTING": "liveblog.routing.channel_routing",
},
}
16 changes: 16 additions & 0 deletions liveblog/liveblog/settings/redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .base import *

redis_host = os.environ.get('REDIS_HOST', 'localhost')

# Channel layer definitions
# http://channels.readthedocs.org/en/latest/deploying.html#setting-up-a-channel-backend
CHANNEL_LAYERS = {
"default": {
# This example app uses the Redis channel layer implementation asgi_redis
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": [(redis_host, 6379)],
},
"ROUTING": "liveblog.routing.channel_routing",
},
}
2 changes: 1 addition & 1 deletion liveblog/liveblog/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "liveblog.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "liveblog.settings.redis")

application = get_wsgi_application()
2 changes: 1 addition & 1 deletion liveblog/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "liveblog.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "liveblog.settings.redis")

from django.core.management import execute_from_command_line

Expand Down
3 changes: 3 additions & 0 deletions liveblog/requirements/rabbitmq.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Django>=1.9
channels>=0.10
git+https://github.com/proofit404/asgi_rabbitmq.git@524e9f12bad9fcb4f24c7345a1a6cd8a264aa494#egg=asgi_rabbitmq
File renamed without changes.

0 comments on commit 5442457

Please sign in to comment.