Periodic Jobs in Django / Celery - How do I notify a user on the screen?

Now I have successfully set up Django celery to check my existing tasks, to remind the user via email when the task is completed:

@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 the popup for the user as a reminder?

Twitter bootstrap allows you to create popups and display them from javascript:

 $(this).modal('show'); 

The problem is, how can the celery worker daemon run this javascript in the user's browser? Maybe I'm wrong, and this is impossible. So the question remains, can a cronjob on celery ever be used to receive ui notifications in a browser?

+4
source share
2 answers

Well, you cannot use the Django message structure because the task does not have access to the user request, and you don’t request objects for workers, since they cannot be printed.

But you can definitely use something like django-notifications . You can create notifications in your task and attach them to the corresponding user. You can then get these messages from your view and process them in your templates to your liking. The user will see a notification at the following request (or you can use AJAX polling for real-time notifications or HTML5 real-time webcams , time [see django-websocket ]).

+6
source

Yes, it is possible, but it is not easy. Ways to perform / emulate communication between the server and the client:

polling The most trivial approach is polling a server from javascript. Your celery task can create lines in your database that can be retrieved using a URL such as /updates , which checks for new updates, marks the lines as read, and returns them.

long survey Often referred to as a comet. The client makes a request to the server, which is delayed until the server decides to return something. For example, django-comet .

WebSocket To enable the exchange of data between the server and the client, you need to open a connection with the client on the server. django-socketio and django-websocket are examples of reusable applications that make this possible.

My advice, judging by your question: either do basic polls, or write letters.

+3
source

All Articles