This is the use case I am looking for :
User will register on my application
It is a note making and remainder application
While creating remainder users will enter the time my application wants to send user an email at that time
I have to use celery for the same
I read several posts and stack overflow answers but didn't get proper answer for the same.
My application is written in Django.
When a user creates a new reminder just schedule a send email task in celery. Note that this is a simple solution but it will make it hard for the user to change notification time after creating it.
From this answer:
To execute a task at a specified date and time you can use eta attribute of apply_async while calling task as mentioned in docs
your_task.apply_async(kwargs={}, eta="your_send_time")
A different solution is to have a task running every 1 minute which will check in DB if there are any emails to be sent. You don't have to use Celery for that. Cron-like lib should do the trick. For example schedule lib or django-cron or Django Commands
Edit: this guy says you should not use dbader/schedule with Django.
Related
I have created an email functionality which will send access token to the user for verification.
This process takes little time to execute.
So, I want to push this function in background and notify the user with the message "The access token would be mailed to your email id. Please check you email". This is to keep user updated rather than him waiting for the access token notification once the whole task is completed.
I don't want to schedule as it is not required here and Django's background_task module won't help I suppose.
What could be the way-out?
Have you tried celery? I think it is doing exactly what you need if I am not wrong.
My problem is basically that I am currently making a customized management system based on Django(3.1) Python(v3.7.9) in which I am pulling the data from a third-party tool. The tool is not giving me the webhooks of every data that I want for visualization and analysis.
The webhook is giving me bits of information and I have to perform a GET request to their API to fetch the rest details if those are not in my database. They are asking for a successful response of webhook within 5 secs otherwise it will trigger a retry.
If I try to do a get request within the function of webhook the time of 5 second will get exceeded the solutions that I came up with was to Django Middleware or Django Triggers so which would be best suitable for my problem I am bit confused.
Note: I can not lower the Django version as I have to use Async Functions
This would be a good use case for a task scheduler like Celery.
Django-triggers is an interface to the Celery scheduler, so it might be a good fit.
Keep in mind, Celery has to be run as a separate process next to django.
Another popular task scheduler is rq-scheduler.
This offers a simple implementation using Redis as a message queue. *Note that Loadbalanced/multi-instance applications are not easily setup with RQ.
I'm looking for a way to keep track of users that are online/offline. So if I present all users in a list i could have an icon or some kind of flag to show this. Is this built in in Django's default Auth system?
My first thought was to simply have a field in my profiles called last_logout in the models and update it with the date/time each time user logged out.
With this info and the built in last_login I should be able to make some kind of function to determine if the user is loggedin/online right?
Or should I just have a boolean field called "online" that I can change when user logs in and out?
With only django it will be hard to do. For such task async frameworks are more suitable.
For example, tornado.
Users won't do logout explicitly every time they go offline. They just close their browser and that's it. You can't know it with only django auth app. It is not designed for such tasks.
Even if you will check for not expired session, it not gives you all online users, because session can be non-expired for 30 days.
So to get real online users, possible solutions are:
Every user will send some data via javascript to your server, for example every 10 seconds. You can fetch that data on server and put user into cache and set cache key to be alive for 10 seconds. So when you need to know, who are online now, you'll check your cache. But it is not a good solution, because it will need a lot of server resources.
Use async framework (tornado) at server side (you can setup separate process for exact requests). And use websockets (SockJS is a good library for that at client side). It is a more complicated solution, but it is better.
You have to consider what exactly means for the users to be "online". Since any user can close the browser window any time and without the server knowing about that action, you'd end up having lots of false "online" users.
You have two basic options:
Keep track of the user's last activity time. Every time the user loads a page you'd update the value of the timer. To get a list of all online users you'd need to select the ones with an activity before X minutes. This is what is done by some web forums.
Open a websocket, long polling connection or some heartbeat to the server. This is what Facebook chat does. You'd need more than just django, since to keep a connection open another kind of server-side resources are needed.
I want to implement a reminder system, where a reminder email will be sent to the users based on some date field in their userprofile.How can I implement this ? Many suggested me not to use celery. Told me to use cron tab.
This is how I do, is this good way? If not, please suggest me a good way.
If cron:
Planning to run a python management command daily, that will check if there is some reminder for that day, by checking dates of all the users.
I dont think this is good, I think, whenever the user select their reminder date, then that date will be registered some how and job will be run only on that date, but I am not getting how to implement this.
Way I know to implement this:
Create a database table named 'reminderdates', and whenever a reminder is registered, add that date and user_id to that table. So, run a cron job everyday that checks if that day is among the dates in the table. If yes job will be run. Also, I will run another cron job that will delete all the older reminderdates from the table so the table size wont get increased with time.
The simplest solution is to setup a cron job to hit a URL on the site that performs the task. I would recommend Celery though. Here's why (from Django/Celery Quickstart):
Cron has the advantage of simplicity, but it's not not ideal for the job. You have to take steps to ensure that regular users of the site cannot hit those URLs directly. It also forces you to manage an external configuration. What if you forget to perform the configuration on the qa or production servers? It would be safer and easier if the configuration was in the code for the site.
You will find a tutorial on how to implement tasks with celery on that previous link.
As for your reminderdates table idea, it should work. You can simplify it a little bit if you try this:
Adding a column with a sent status. Your task will check for date and sent status. If current day matches reminder date AND status is "unsent", then send the reminder.
You don't need a secondary cron job to delete older reminders. The same task that sends the reminders can send each one and delete it after (in which case the sent/unsent status would not be needed).
I work on a page in Django, where users can set custom reminders for different dates (max. 3 per date). The reminders should send via e-mail. Its similar to Google Calendar, where you can set multiple reminders for each event in x-minutes, x-hour or x-days before the date starts.
I wonder, how I can solve it combined with Django. Since there will be a lot of users and dates, which should of course also run perfomant.
Should I do this with a cron job? Is there a python way?
The other traditional way is to use django-celery: http://pypi.python.org/pypi/django-celery/
You can use the celerybeat command to run periodical tasks. Also you can start pending tasks from a django view.
You can use a cron job. To create a management command: refer to the documentation here
Also, you can create the email generation as a queue based, distributed implementation for enhanced performance. You can use Django-mailer app for the same.