Previously we talked about setting up some custom command in a Django project.
I would like to run the custom command in a regular interval. I could use Linux cronjob but it’s hard to manage and check the run history. It would be great if i could setup and manage these scheduled commands on the Django web portal.
django-chronograph, written by wnielson, is a Python Package which allows us to schedule any django-admin command through the web interface. It works well with the UTC timezone but if you have set the TIME_ZONE in your Django project setting.py, there would be time discrepancy.
For more detail about the bug, you can refer to the Issue #36 – Job doesn’t follow the Django’s TIME_ZONE setting.
Thanks opensource, finally i found the root cause in the source code. Here is the forked repository which could fix the problem. (Pull request)
OK, let’s see how django-chronograph works.
1. The following Python packages are required. Make sure Django >= 1.6.
pip install django python-dateutil pytz
- Django (1.6.5)
- python-dateutil (1.5)
- pytz (2014.4)
2. Also install the forked django-chronograph.
pip install https://bitbucket.org/ykyuen/django-chronograph/get/default.zip
3. Create a new Django project, since i want to schedule my custom django-admin command, i would just start with the following project.
- Django – Create custom django-admin command
4. Include the django-chronograph in settings.py.
# Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'chronograph', 'scheduler', )
5. Also set the Django project TIME_ZONE.
# Internationalization # https://docs.djangoproject.com/en/1.6/topics/i18n/ LANGUAGE_CODE = 'en-us' #TIME_ZONE = 'UTC' # Setting UTC is fine for wnielson/django-chronograph to work TIME_ZONE = 'Asia/Hong_Kong' # This will cause 8 hours delay for the scheduled job if you use wnielson/django-chronograph USE_I18N = True USE_L10N = True USE_TZ = True
6. Apply new changes on database.
python manage.py syncdb
7. Add the Django project cron to crontab.
* * * * * /path/to/your/python/binary /path/to/your/project/manage.py cron # In my example #* * * * * /home/vagrant/.pyenv/versions/django-poc/bin/python /vagrant/custom_command/manage.py cron
8. Start the webserver.
python manage.py runserver 0.0.0.0:8000
13. The above setup works fine with SQLite but if i change to database to PostgreSQL and you have setup more than one django-chronograph jobs, they would not be executed. I am not sure what cause the problem. I guess sth related to the psycopg2 driver as stated in StackOverflow – Forking Django DB connections. Feel free to post me if you have any idea.
If you are using Django <= 1.5, the following notes may help.
- Don’t install pytz as it breaks django / chronograph.
- Need to use cronserver as crontab doesn’t work.
- Bitbucket – wnielson/django-chronograph
- Google project – django-chronograph (deprecated, use the Bitbucket repo above.)
- Welcome to Chronograph’s documentation!
- Issue #36 – Job doesn’t follow the Django’s TIME_ZONE setting
- Bitbucket – ykyuen/django-chronograph
- Bixly’s Blog – Django Chronograph
- StackOverflow – Forking Django DB connections