|
|
The django app will all the site to run code in a cron job like manor without needing to setup a real cron job (Usefull for people hosting on windows or with Hosting companies that do not give you access to setup cron jobs).
To create a Cron Job you create a file called cron.py in your application directory. then from cron import Cron then create a class (name it whatever you want) and have Cron as the inherited class (This notifies the system that there is a job, and where to find it)
Next create a job method that only accepts self. In here do whatever you want your Cron Job to do and return True if it succeeded and False if it failed.
you can set the run_every attribute, it is in hours.
Example Cron Job included in the application
from datetime import datetime
from django.contrib.sessions.models import Session
from cron import Cron
class DeleteSessions(Cron):
run_every = 24
def job(self):
now = datetime.now()
try:
Session.objects.filter(expire_date__lt=now).delete()
return True
except:
return False
