From ae920bab5177482973e5e78b0ee3fee7a2eade40 Mon Sep 17 00:00:00 2001 From: Alexandre PALO Date: Wed, 14 Feb 2018 19:33:43 +0100 Subject: [PATCH 1/2] working Celery RabbitMQ behavior --- borgia/__init__.py | 7 ++++ borgia/celery.py | 38 ++++++++++++++++++++ borgia/settings.py | 6 +++- celerybeat-schedule.db | Bin 0 -> 16384 bytes requirements.txt | 2 ++ users/tasks.py | 78 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 borgia/celery.py create mode 100644 celerybeat-schedule.db create mode 100644 users/tasks.py diff --git a/borgia/__init__.py b/borgia/__init__.py index e69de29b..71ac8661 100644 --- a/borgia/__init__.py +++ b/borgia/__init__.py @@ -0,0 +1,7 @@ +"""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']""" diff --git a/borgia/celery.py b/borgia/celery.py new file mode 100644 index 00000000..56b9e977 --- /dev/null +++ b/borgia/celery.py @@ -0,0 +1,38 @@ +from __future__ import absolute_import, unicode_literals + +import os +from celery import Celery, chain +from celery.schedules import crontab +from users.tasks import scan_for_expired_users + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'borgia.settings') +"""import django +from django.conf import settings +settings.configure() +django.setup()""" + +# set the default Django settings module for the 'celery' program. +app = Celery('borgia', backend='rpc://', broker='pyamqp://') + +# 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() + +# Console log +@app.task(bind=True) +def debug_task(self): + print('Request: {0!r}'.format(self.request)) + + +@app.on_after_configure.connect +def setup_periodic_tasks(sender, **kwargs): + sender.add_periodic_task( + crontab(hour=18, minute=33), + scan_for_expired_users.s(), + name='Alert mailing' + ) diff --git a/borgia/settings.py b/borgia/settings.py index fe01b123..fd8b278a 100644 --- a/borgia/settings.py +++ b/borgia/settings.py @@ -46,7 +46,8 @@ 'modules', 'stocks', 'graphene_django', - 'static_precompiler' + 'static_precompiler', + 'django_celery_results' ] MIDDLEWARE_CLASSES = [ @@ -221,3 +222,6 @@ CENTER_NAME = "Center Name" DEFAULT_TEMPLATE = "light" #Default template, en minuscule + +# Celery +CELERY_RESULT_BACKEND = 'django-db' diff --git a/celerybeat-schedule.db b/celerybeat-schedule.db new file mode 100644 index 0000000000000000000000000000000000000000..022a4d13ad5a07c8443a6da53adf2e4064414e99 GIT binary patch literal 16384 zcmeI%J8#oa6bJB|I%!g;joZ=}g+juVBE>3IR|ZruSunUDd9xh*LZUiOk9~tkB}?6>^%2T8kTzQX^{{D5P$##AOHafKmY;| zfB*y_aA5>W;+OHm_-=eNJ{up655`XAZe^!(Vai9L5P$##AOHafKmY;|fB*#kkHA1n zNz|QXj^+hHLq3ynzahQ&#Pt(Dj~)yAgMs$K>?z-sk=G1mFwh>FYJZ?P1rE7o-Z3{w zn6ArllXf?9-EpLqlL^CqqtlI}cJy?Ua=tG^F2rm~Pw6{;O9s7A-j7t=*J+~O$~k3z zU3tlFVk()aP15|pQ&P1%Qa@Tyep@eonWz~0TH)neD)@VSwXeI6A8qS2xo%VOfJ!z^ zS>iBTGES7}e%y_e*VIX`TV-p`$^L!4eXP-O6El4I9v7;r9j_g7>Hn(S`TGscxy4a> z3Hz9%juPLC+$Y`GmCyIuu?*bdP0F9vVqw(ce2rtcG<+@d~ z=0`2kbc+u~Gd5LHGMeit&89S`Xx^qP&Jrg%{4k!sE2F^mL+M4mJz8k-XIbFmu%PN{ z@%4sC}Jfq$dy7}PU>qJr2 zrdrRGyaU=YIcsyBvKKiT1_U4g0SG_<0uX=z1Rwwb2tWV=mt5fIZwv@P00Izz00bZa Q0SG_<0uX=z1TMS4AK`1=2mk;8 literal 0 HcmV?d00001 diff --git a/requirements.txt b/requirements.txt index 3c374a0a..3095bce0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,3 +22,5 @@ six==1.10.0 typing==3.6.2 django-static-precompiler==1.6 openpyxl==2.4.9 +celery +django-celery-results==1.0.1 diff --git a/users/tasks.py b/users/tasks.py new file mode 100644 index 00000000..d7d47766 --- /dev/null +++ b/users/tasks.py @@ -0,0 +1,78 @@ +from __future__ import absolute_import, unicode_literals + +from celery import Celery, chain, current_app +from celery.schedules import crontab + +# set the default Django settings module for the 'celery' program. +#app = Celery('borgia', backend='rpc://', broker='pyamqp://') +app = current_app + +@app.task +def scan_for_expired_users(): + for user in get_expired_users(): + deactivating_process = chain(deactivate_account.s(user), send_expiration_email.s()) + deactivating_process() + + + +@app.task +def deactivate_account(user): + # do some stuff + print(f'deactivating account: {user}') + return user + '_deactivated' + + +@app.task +def send_expiration_email(user): + # do some other stuff + print(f'sending expiration email to: {user}') + return True + +def get_expired_users(): + # Set up Django for models + import django + django.setup() + from settings_data.models import Setting + from users.models import User + + + return (f'user_{i}' for i in range(5)) + + +""" + +from __future__ import absolute_import, unicode_literals + +from celery import Celery, chain +from celery.schedules import crontab +#from settings_data.models import Setting + +# set the default Django settings module for the 'celery' program. +app = Celery('borgia', backend='rpc://', broker='pyamqp://') + +@app.task +def scan_for_expired_users(): + for user in get_expired_users(): + deactivating_process = chain(deactivate_account.s(user), send_expiration_email.s()) + deactivating_process() + + +@app.task +def deactivate_account(user): + # do some stuff + print(f'deactivating account: {user}') + return user + '_deactivated' + + +@app.task +def send_expiration_email(user): + # do some other stuff + print(f'sending expiration email to: {user}') + return True + + +def get_expired_users(): + return (f'user_{i}' for i in range(5)) + + +""" From b9df71678d1dd7509fe904b40b700003b54ef507 Mon Sep 17 00:00:00 2001 From: Alexandre PALO Date: Wed, 14 Feb 2018 20:15:54 +0100 Subject: [PATCH 2/2] wip mailing --- borgia/celery.py | 6 +- celerybeat-schedule.db | Bin 16384 -> 16384 bytes users/tasks.py | 66 ++++++++++++------ users/templates/users/mail_alert_balance.html | 1 + users/templates/users/mail_alert_balance.txt | 1 + 5 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 users/templates/users/mail_alert_balance.html create mode 100644 users/templates/users/mail_alert_balance.txt diff --git a/borgia/celery.py b/borgia/celery.py index 56b9e977..771496fb 100644 --- a/borgia/celery.py +++ b/borgia/celery.py @@ -3,7 +3,7 @@ import os from celery import Celery, chain from celery.schedules import crontab -from users.tasks import scan_for_expired_users +from users.tasks import scan_mailing_balance_alert os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'borgia.settings') """import django @@ -32,7 +32,7 @@ def debug_task(self): @app.on_after_configure.connect def setup_periodic_tasks(sender, **kwargs): sender.add_periodic_task( - crontab(hour=18, minute=33), - scan_for_expired_users.s(), + 5, + scan_mailing_balance_alert.s(), name='Alert mailing' ) diff --git a/celerybeat-schedule.db b/celerybeat-schedule.db index 022a4d13ad5a07c8443a6da53adf2e4064414e99..4b71fc74e62e4c1c7e38d0446860d8cfce798fa2 100644 GIT binary patch delta 322 zcmZo@U~Fh$+#n#pCeO#oC(pO>B0KM9Sy?8=$-#2IjB1k?%8AtHCT8Yj=B39cCFUgN zC8x$G=A;&t6tX0zB$lL>Wag%F!5M|D&Rpz|nD~Twtr)pJ7qTT6RF+h6#fOGCa}}~{ z1{HF&6>Tp zypqHuu0k#iZ*FhF2u2152CW9>4!zQn7HMtmV#azSND6<2&{ zh%;9qyJk=!M_VChP$8E$J07QLa%*@idJ93^R>+e9ge8T%L4|y^h5SjHwS@w;g@Pr8 zLP3SXlQraJQbaPCG%`eh5N5T46#J2Fg`yciyTpPD#l0CaxHZ6r85Bx@X_G=pkf9)B rrGg5jYYSyS#>&