Execute function at special time using Django - django

I write task manager, and i want to create bot, which must send message to user (maybe user's email or social account).
I want to user can be able choose task and set time, when bot should send message to him.
My proble is that I don't know what i can user for it, I use django framework, and now i think that celery can help me with it.

Celery might be a bit heavy to start with.
You can try the django_cron to schedule your email tasks or other cron jobs that need to run at a specific time.
It's quite easy to set up following its documentation.

Related

Options for running on user demand asynchronous / background tasks in Django?

My Django app generates a complex report that can take upto 5 minutes to create. Therefore it runs once a night using a scheduled management command.
That's been ok, except I now want the user to be able to select the date range for the report, which means the report needs to be created while the user waits.
What are my options for running the tast in the background? So far I've found these:
Celery - might work but is complex
django-background-tasks looks like the right tool for the job but hasn't been updated for years, last supported Django is 2.2
The report/background task could be generated by AWS Lambda, basically in a microservice. Django calls the Microservice which can execute the background task then call the Django app back once finished. This is what I did last time but not sure it would work now as I'd need to send the microservice 10mb of data to process.
Use subprocess.popen which someone here said worked for them but other reports say it doesn't work from Django.
EDIT: Looks like Django 3.1 onwards supports ASync views and may be the simple solution for this.

Scheduling task from Django admin Panel

I want to schedule a task in Django which would run at a paticular time every day.I am thinking of using celery to schedule the tasks so I wanted to know is there anyway through celery-beat I could execute the task through admin panel that is make it active or inactive and give the time at which I want to execute task through my admin site.Instead of writing this in my code
crontab(day_of_week="0,6", hour="0,12", minute=15)
I want to pass it through my admin panel so that in future if I want to execute my task at some other time I could change it directly through admin panel instead of changing my code.
You will need to implement that on your own or use a third-party project. Luckily, someone already created something like that:
https://django-celery-beat.readthedocs.io/
This extension enables you to store the periodic task schedule in the
database.
The periodic tasks can be managed from the Django Admin interface,
where you can create, edit and delete periodic tasks and how often
they should run.

Send Weekly Email Notifications to Users regarding any changes in data

What are ways to send automatic weekly email notifications to users upon any update or changes in the data in a django project ?
I'll describe the simplest solution I'm aware of. There are also much, much more sophisticated approaches to this problem.
Fundamentally, you need three things:
A task runner (I recommend configuring cron jobs with django-kronos)
An SMTP provider (I recommend Mailgun, which is super simple to set up with Django and gives you some test credits out of the box).
Email templates (write yourself & render to string w/ Django -- Mailgun has some good open source templates as well on their blog)
Example: django-kronos provides decorators for registering functions as cron jobs (this assumes your web server is Linux-based). These jobs can be installed as part of your deploy process from the command line:
./manage.py installtasks
For kronos to find tasks, they must be located in cron.py files inside of your apps.
# myapp/cron.py
import kronos
from django.contrib.auth.models import user
from myapp.services import check_for_changes, notify_user_of_change
# Register cron job to run once a week (every Sunday) at midnight
#kronos.register('0 0 * * 0')
def notify_about_changes():
"""Sets up a cron job and runs this service function once a day.
Installed With:
``./manage.py installtasks``
"""
all_my_users = User.objects.all()
for user in all_my_users:
changes = check_for_changes(user)
for change in changes:
notify_user_of_change(user, change)
Note that it's a good practice to keep your task function as "thin" as possible. In your case, you're (a) checking for changes, then (b) notifying about each change. These could each be a function that accepts a user instance as a param, or they could be model methods. In any case, the hard work should be handled elsewhere.
This is a very broad question, so unfortunately the answer is going to be broad as well. The general workflow you're looking for probably starts off with a Signal which you can set up to be activated on update of data.
That signal would then store data about the changes and then use one of many different email options to send email. You could use either built-in Django emails, or use a 3rd party API such as django-mailchimp.
Hopefully this gets you headed in the right direction.

Alternative to django-celery to perform asynchronous task in Django?

In my admin I have a form allowing to upload a file to fill the DB.
Parsing and filling the DB take a long time, so I'd like to do it asynchronously.
As recommended by several SO users I tried to install python-celery, but I can't manage to do it (I'm on Webfaction).
Is there any simple, easy-to-install alternative?
If webfaction supports cron jobs you can create your own pseudo broker. You could save your long running tasks to the db and in a 'tasks' table, this would allow you to return a response to the user instantaneously. Then there could be a cron that goes through very frequently and looks for uncompleteled tasks and processes them.
I believe this is what django mailer does
https://github.com/jtauber/django-mailer/
https://stackoverflow.com/a/1419640/594589
Try Gearman along with it's python client library
It's very easy to setup and run gearman. Try few examples.

Django app for sending periodic emails?

The main functionality of it would be:
check a condition every day
if the condition is true, then send email
Now, I know how to conditionlly send email, but I don`t know how to do make it repeat every day. I have some ideas but not sure about them at all (to add another process? or others?). I appreciate your suggestions and ideas...thx
You could create a custom management command and use a cron job to have it run once a day.
edit:
I have used this before, http://code.google.com/p/django-command-extensions/wiki/JobsScheduling. It is pretty handy if you have multiple tasks to be run on fixed schedules. Instead of having to add a line in crontab for each task you just add new files to the jobs directory.