Django: Getting Django-cron Running

那年仲夏 提交于 2019-11-30 09:49:03

问题


I am trying to get Django-cron running, but it seems to only run after hitting the site once. I am using Virtualenv.

Any ideas why it only runs once?

On my PATH, I added the location of django_cron: '/Users/emilepetrone/Workspace/zapgeo/zapgeo/django_cron'

My cron.py file within my Django app:

from django_cron import cronScheduler, Job

from products.views import index

class GetProducts(Job):

    run_every = 60

    def job(self):
        index()

cronScheduler.register(GetProducts)

class GetLocation(Job):

    run_every = 60

    def job(self):
        index()

cronScheduler.register(GetLocation)

回答1:


The first possible reason

There is a variable in django_cron/base.py:

# how often to check if jobs are ready to be run (in seconds)
# in reality if you have a multithreaded server, it may get checked
# more often that this number suggests, so keep an eye on it...
# default value: 300 seconds == 5 min
polling_frequency = getattr(settings, "CRON_POLLING_FREQUENCY", 300)

So, the minimal interval of checking for time to start your task is polling_frequency. You can change it by setting in settings.py of your project:

CRON_POLLING_FREQUENCY = 100 # use your custom value in seconds here

To start a job hit your server at least one time after starting Django web server.

The second possible reason

Your job has an error and it is not queued (queued flag is set to 'f' if your job raises an exception). In this case it stores in field 'queued' of table 'django_cron_job' string value 'f'. You can test it making the query:

select queued from django_cron_job;

If you change the code of your job the field may stay as 'f'. So, if you correct the error of your job you should manually set in queued field: 't'. Or the flag executing in the table django_cron_cron is 't'. It means that your app. server was stopped when your task was in progress. In this case you should manually set it into 'f'.



来源:https://stackoverflow.com/questions/7127758/django-getting-django-cron-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!