Jeffrey S.
Jeffrey S.

Reputation: 33

django-cron does not work

After installing the django_cron correctly, i write the following codes named as cron.py in django app to call django_cron to execute. But it's so odd that this script does not work at all!

from django_cron import cronScheduler, Job
from mysite.tds.models import machine_list,flavor_list,image_list
from mysite.views import *
import datetime,os

class CheckExpire(Job):
run_every = 60

def job(self):
    mac_list = machine_list.objects.filter(expire_date=datetime.date.today())
    for lst in mac_list:
        delete_vm(lst.servername)
        nagios_delete(lst.servername,lst.machine_ip)
    mac_list.delete()

cronScheduler.register(CheckExpire)

The codes above can be executed successfully if i wrtie them in anther script and run this script, so there is no mistake in these codes.

I post the steps on how i install django-cron for you guys to check if i install it correctly.

  1. Put 'django_cron' into your python path

  2. Add 'django_cron' to INSTALLED_APPS in your settings.py file

  3. Add the following code to the beginning of your urls.py file (just after the imports): import django_cron django_cron.autodiscover()

  4. Create a file called 'cron.py' inside each installed app that you want to add a recurring job to.

  5. update MaxRequestsPerChild in httpd.conf and set it to be 100

  6. update the base.py in django_cron and set polling_frequency 30(less than run_every)

Could someone figure out why django_cron does not work under this condition? Thanks in advance.

Upvotes: 2

Views: 1721

Answers (3)

Andrew Louis
Andrew Louis

Reputation: 390

Sometimes it does not work because of logs. If you got django logs path in settings. Cron would like to use it.

Upvotes: 0

Tickon
Tickon

Reputation: 1068

Some things you should check in your database:

1 - After you sync your DB, the tables: 'django_cron_cron' and 'django_cron_job' should be present.

2 - Make sure django_cron_job.queued is set to 1 (if your cron job crash it will be set to 0 and won't run anymore)

3 - Make sure django_cron_cron.executing is set to 0 (otherwise cron thinks this job is still running)

Upvotes: 0

nOw2
nOw2

Reputation: 666

I'll take a guess that you're running mod_wsgi in daemon mode.

The 4th step suggests that django-cron runs jobs when the entire codebase is reloaded, which mod_python or embedded mod_wsgi (embedded) would do when Apache reloads but mod_wsgi (daemon) does not.

A better solution is just to add your script to the system cron.

If you must use django-cron, you'll need to either set maximum-requests on the DaemonProcess configuration of mod_wsgi or set it to run embedded. Neither of which I can recommend for performance and reliability reasons.

Upvotes: 1

Related Questions