You must run process_tasks
command to execute tasks that have been scheduled. There is no other way. Process_tasks
checks the db regularly for scheduled tasks and executes them in the background.
In a local environment
Open a terminal, cd into your app folder and launch the command python manage.py process_tasks
. If you are using a virtualenv make sure you activate it first.
In a production environment
Option 1: Run a cron job
One solution is to launch the process_tasks
command using a cron job
. Beware that cron launches a new process at the scheduled interval without caring about the previous processes. You must make sure the process ends by the time the new cron call is scheduled. Otherwise you will be wasting resources on parallel instances of process_tasks. Happily this check is fairly easy to do.
To launch process_tasks
for a limited time you can use its duration
parameter. For example, to launch it with a life-span of 10 hours you call:
python manage.py process_tasks --duration 36000
Configure the cron job:
In cPanel
you can use the Cron Jobs app. Configure the cron job to run hourly and add the following lines as your cron command:
cd /home/username/appname && /home/username/virtualenv/bin/python /home/username/appname/manage.py process_tasks --duration 3540
This will cd into your app folder and launch the process_tasks
command, taking into consideration your virtual environment and using a life-span of 59 minutes for the process_tasks
command.
Update the paths to you app & virtual environment accordingly! Adjust the cron job interval and duration
parameter to fit your needs!
Benefits:
- If the command fails it will get restarted by the cron job later.
- You don't have to worry about resource/memory leaks too much.
Option 2. Launch command at server startup
Basically you will have to create a script file containing the same launch command and configure your server to run it at startup.
You lose the benefit that your process is restarted from time to time so you will probably have to monitor its health state. If you are going down this path, a good approach is to use supervisord to monitor your process.