Adds celery and rabbitmq

This commit is contained in:
Mouse Reeve 2020-03-22 14:33:26 -07:00
parent 1a6ae00a80
commit dd18433faa
7 changed files with 75 additions and 7 deletions

View file

@ -21,3 +21,6 @@ POSTGRES_PASSWORD="fedireads"
POSTGRES_USER="postgres"
POSTGRES_DB="postgres"
RABBITMQ_DEFAULT_USER=rabbit
RABBITMQ_DEFAULT_PASS=changeme
CELERY_BROKER=amqp://rabbit:changeme@rabbitmq:5672

View file

@ -1,7 +1,7 @@
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY ./fedireads /code
COPY ./fedireads /app

View file

@ -6,14 +6,41 @@ services:
env_file: .env
volumes:
- pgdata:/var/lib/posgresql/data
networks:
- main
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
- .:/app
ports:
- "8000:8000"
depends_on:
- db
- celery_worker
networks:
- main
rabbitmq:
env_file: .env
image: rabbitmq:latest
networks:
- main
ports:
- "5672:5672"
restart: on-failure
celery_worker:
env_file: .env
build: .
networks:
- main
command: celery -A fedireads worker -l info
volumes:
- .:/app
depends_on:
- db
- rabbitmq
restart: on-failure
volumes:
pgdata:
networks:
main:

View file

@ -0,0 +1,8 @@
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',)

24
fedireads/celery.py Normal file
View file

@ -0,0 +1,24 @@
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fedireads.settings')
app = Celery('fedireads')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))

View file

@ -17,11 +17,15 @@ SECRET_KEY = env('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool('DEBUG', True)
# TODO: annoying that I keep changing and re-commiting this
DOMAIN = env('DOMAIN')
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', ['*'])
OL_URL = env('OL_URL')
# celery/rebbitmq
CELERY_BROKER_URL = env('CELERY_BROKER')
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
# Application definition
INSTALLED_APPS = [
@ -33,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
'django.contrib.humanize',
'fedireads',
'celery',
]
MIDDLEWARE = [
@ -75,7 +80,7 @@ DATABASES = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': '"%s"' % env('POSTGRES_PASSWORD', 'fedireads'),
'PASSWORD': '%s' % env('POSTGRES_PASSWORD', 'fedireads'),
'HOST': 'db',
'PORT': 5432
}

View file

@ -1,3 +1,4 @@
celery==4.4.2
Django==3.0.3
django-model-utils==4.0.0
environs==7.2.0