Django – Schedule django-admin command using django-chronograph

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

 

9. Browse the admin page @ http://[hostname]:8000/admin/ and login with the superuser account which is created during syncdb. There would be the django-chronograph widget on the admin web portal.
django-chronograph-setup-1
 

10. Add a new job and you can select all available django-admin commands including our custom command hello_world. Set the Frequency such that it will run minutely.
django-chronograph-setup-2
 

11. You should be able to find your job in the job listing page.
django-chronograph-setup-3
 

12. The job is running~
django-chronograph-setup-4
 

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.

 

Done =)

Reference:

Advertisements

One thought on “Django – Schedule django-admin command using django-chronograph”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s