automatically delete users who meet criteria - python

I want to use this query-set to automatically delete registered users who did not activate their account : User.objects.filter(is_active=False).filter(profile__key_expires__lt=timezone.now()).delete(), but i don't know where to put it.
Views and functions need to be called to execute, but i need this code to run continuously or at least in frequent intervals.

You can put your code in a periodic task using Celery.
Take a look at this and this threads about differences between crontab scripts and Celery.

You can create a separate script and run it in background with cron
This is the best way not to overload your Django app. E.g here my script that loads best members and popular tags. Check how I've imported Django in it

Related

Django save model everyday

I have a model and a signal in models.py and this model sends message to discord webhook with how many days left to something. I want to refresh it everyday at 12:00 AM everyday automatically without using django-celery cause it doesnt work for me. My plan is do something like this
time_set = 12
if time_set == timezone.now().hour:
...save model instances...
but i have totally no idea how to do it
And i want to do it this way cause when model instance are saved signal runs
Django doesn't handle this scenario out of the box, hence the need for celery and its ilk. The simplest way is to set a scheduled task on the operating system that calls a custom django management command (which is essentially a python script that can reference your django models and methods etc by calling python manage.py myNewCommand).
You can find more about custom commands at https://docs.djangoproject.com/en/4.0/howto/custom-management-commands/
You can create a custom management command and call it by using a cron entry, set to run every day.
Check Django official documentation for the instructions on creating the custom command.
Instead of calling the save() method each time, I'd create a send_discord_message() on the model, and call it wherever required. If you need to execute it every time an instance is saved, then is preferred to use an override save() method in the model. Signals are a great way to plug and extend different apps together, but they have some caveats and it is simpler to override the save() method.
I'm supposing you are using a Unix-like system. You can check how to configure and create cron jobs.

How to run a function in the future using Django based on a condition?

I would like to run a particular function (let's say to delete a post) at a specific time in the future (e.g.: at 10am) only once based on a condition.
I am using Django, and I was thinking about using cron or python-crontab, but it seems that these task schedulers can only be used when a particular task has to be executed more than once in the future. As I was trying to use the python-crontab with Django, I also did not find any resources that allow me to execute "this task of deleting a post at 10am tomorrow only if a user does a particular action", for example.
Does anyone know if I can still use python-crontab? Or other technology should be used?
I would use:
https://github.com/celery/django-celery-beat
http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html
Celery to run background tasks, and celery beat is a scheduler to kick off the background tasks at the specified times.

Running functions automatically when certain criteria are met. Without user interaction.

I am using Flask.
I am currently using a fabfile to check which users should get a bill and I set up a cron job to run the fabfile every morning at 5am. This automatically creates bills in Stripe and in my database and sends out emails to the users to inform them. This could be used for birthday reminders or anything else similar.
Is setting up a cronjob the standard way of doing this sort of thing? Is there a better way/standard?
I would define "this sort of thing" as. Anything that needs to happen automatically in the app when certain criteria are met without a user interacting with said app.
I could not find much when I googled this.
Using cron is in effect the most straightforward way of doing it. However, there are other kind of services that trigger tasks on a periodic basis and offer some additional control. For instance, Celery's scheduler. There seems to be a tutorial about building periodic tasks with celery here.
What I think you have to ask yourself is:
Is a cron job the most reliable way of billing your customers?
I've written small/simple apps that use an internal timer. e.g: https://bitbucket.org/prologic/irclogger which roates it's irc log files once per day. Is this any better or more reliable? Not really; if the daemon/bot were to die prematurely or the system were to crash; what happens then? In this case it just gets started again and logs continue to rorate at the next "day" interval.
I think two things are important here:
Reliability
Robustness

perform an action at a certain time with the web.py framework

I am currently starting to work on a web server running web.py . If I understand it correctly, the whole thing is implemented as some kind of a loop that listens to the requests at the specified port and reacts accordingly.
I would like the server to perform a certain action at a certain time without a request that triggers it. What is the best way to achieve this? Thank you.
If you want to run it within python you can look at using the threading class.
More information on how to do it here: Executing periodic actions in Python
I think the best way is to set up a cron job in the system, which will execute your script (which can be written in Python or other language).

Splitting a Django project

I have a django project with various apps, which are completely independent. I'd like to make them run each one in their own process, as some of them spawn background threads to precalculate periodically some data and now they are competing for the CPU (the machine has loads of cores, but you know, the GIL and such...)
So, is there an easy way to split automatically the project into different ones, or at least to make each app live in its own process?
You can always have different settings files, but that would be like having multiple projects and even multiple endpoints. With some effort you could configure a reverse proxy to forward to the right Django server, based on the request's path and so on, but I don't think that's what you want and it would be an ugly solution to your problem.
The solution to this is to move the heavy processing to a jobs queue. A lot of people and projects prefer Celery for this.
If that seems like overkill for some reason, you can always implement your own based on simple cron jobs. You can take a look at my small project that does this.
The simplest of the simple is probably to write a custom management command that observes given model (database table) for new entries and processes them. The model is written to by e.g. Django view and the management command is launched periodically from cron (e.g. every 5 minutes).
Example: user registers on the site, but the account creation is an expensive operation (allocating some space, pinging remote services etc.). Therefore you just write a new record to AccountRequest table (AccountRequest.objects.create(...)). Then, cron periodically launches your management script (./manage.py account_creator), which checks for new AccountRequest-s (AccountRequest.objects.filter(unprocessed=True)), does its job and marks those requests as processed.

Categories