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_USER="postgres"
POSTGRES_DB="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 FROM python:3
ENV PYTHONUNBUFFERED 1 ENV PYTHONUNBUFFERED 1
RUN mkdir /code RUN mkdir /app
WORKDIR /code WORKDIR /app
COPY requirements.txt /code/ COPY requirements.txt /app/
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
COPY ./fedireads /code COPY ./fedireads /app

View file

@ -6,14 +6,41 @@ services:
env_file: .env env_file: .env
volumes: volumes:
- pgdata:/var/lib/posgresql/data - pgdata:/var/lib/posgresql/data
networks:
- main
web: web:
build: . build: .
command: python manage.py runserver 0.0.0.0:8000 command: python manage.py runserver 0.0.0.0:8000
volumes: volumes:
- .:/code - .:/app
ports: ports:
- "8000:8000" - "8000:8000"
depends_on: depends_on:
- db - 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: volumes:
pgdata: 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! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool('DEBUG', True) DEBUG = env.bool('DEBUG', True)
# TODO: annoying that I keep changing and re-commiting this
DOMAIN = env('DOMAIN') DOMAIN = env('DOMAIN')
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', ['*']) ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', ['*'])
OL_URL = env('OL_URL') OL_URL = env('OL_URL')
# celery/rebbitmq
CELERY_BROKER_URL = env('CELERY_BROKER')
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
@ -33,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.humanize', 'django.contrib.humanize',
'fedireads', 'fedireads',
'celery',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -75,7 +80,7 @@ DATABASES = {
'ENGINE': 'django.db.backends.postgresql_psycopg2', 'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres', 'NAME': 'postgres',
'USER': 'postgres', 'USER': 'postgres',
'PASSWORD': '"%s"' % env('POSTGRES_PASSWORD', 'fedireads'), 'PASSWORD': '%s' % env('POSTGRES_PASSWORD', 'fedireads'),
'HOST': 'db', 'HOST': 'db',
'PORT': 5432 'PORT': 5432
} }

View file

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