Periodic tasks in Django/Celery - How to notify the user on screen?
celery, django, django-celery
Solution
Well, you can't use the Django messages framework, because the task has no way to access the user's request, and you can't pass request objects to the workers neither, because they're unpickable.
But you could definitely use something like django-notifications. You could create notifications in your task and attach them to the user in question. Then, you could retrieve those messages from your view and handle them in your templates to your liking. The user would see the notification on their next request (or you could use AJAX polling for real-time-ish notifications or HTML5 websockets for real real-time [see django-websocket]).
Problem
I have now succesfully setup Django-celery to check after my existing tasks to remind the user by email when the task is due: ``` @periodic_task(run_every=datetime.timedelta(minutes=1)) def check_for_tasks(): tasks = mdls.Task.objects.all() now = datetime.datetime.utcnow().replace(tzinfo=utc,second=00, microsecond=00) for task in tasks: if task.reminder_date_time == now: sendmail(...) ``` So far so good, however what if I wanted to also display a popup to the user as a reminder? Twitter bootstrap allows creating popups and displaying them from javascript: ``` $(this).modal('show'); ``` The problem is though, how can a celery worker daemon run this javascript on the user's browser? Maybe I am going a complete wrong way and this is not possible at all. Therefore the question remains can a cronjob on celery ever be used to achieve a ui notification on the browser?