From 20096010b49bb96752d091a1d9266e8fe56eb313 Mon Sep 17 00:00:00 2001 From: Alexander Hosking Date: Mon, 13 Dec 2021 15:21:44 -0500 Subject: [PATCH] Add Celery Support This needs to be tested on a linux host - windows not supported --- api/api/__init__.py | 5 +++++ api/api/celery.py | 23 +++++++++++++++++++++++ api/bills/tasks.py | 7 +++++++ 3 files changed, 35 insertions(+) create mode 100644 api/api/celery.py create mode 100644 api/bills/tasks.py diff --git a/api/api/__init__.py b/api/api/__init__.py index e69de29..1e3599b 100644 --- a/api/api/__init__.py +++ b/api/api/__init__.py @@ -0,0 +1,5 @@ +# 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',) \ No newline at end of file diff --git a/api/api/celery.py b/api/api/celery.py new file mode 100644 index 0000000..79b9c4c --- /dev/null +++ b/api/api/celery.py @@ -0,0 +1,23 @@ +import os + +from celery import Celery + +# Set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings') + +app = Celery('api') + + +# 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 apps. +app.autodiscover_tasks() + + +@app.task(bind=True) +def debug_task(self): + print(f'Request: {self.request!r}') \ No newline at end of file diff --git a/api/bills/tasks.py b/api/bills/tasks.py new file mode 100644 index 0000000..b11b48f --- /dev/null +++ b/api/bills/tasks.py @@ -0,0 +1,7 @@ +### Celery Tasks! + +from celery import shared_task + +@shared_task +def get_overdue_bills(): + return('This is a test') \ No newline at end of file