Django:让 Django-cron 运行

发布于 2024-11-30 15:25:21 字数 630 浏览 1 评论 0原文

我正在尝试让 Django-cron 运行,但它似乎只在点击后运行该网站一次。我正在使用 Virtualenv。

有什么想法为什么它只运行一次吗?

在我的 PATH 中,我添加了 django_cron 的位置: '/Users/emilepetrone/Workspace/zapgeo/zapgeo/django_cron'

我的 Django 应用程序中的 cron.py 文件:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捂风挽笑 2024-12-07 15:25:21

第一个可能的原因

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)

因此,检查启动任务的时间的最小间隔是轮询频率。您可以通过在项目的 settings.py 中进行设置来更改它:

CRON_POLLING_FREQUENCY = 100 # use your custom value in seconds here

要启动作业,请在启动 Django Web 服务器后至少访问您的服务器一次。

第二个可能的原因

您的作业出现错误并且未排队(如果您的作业引发异常,已排队标志将设置为“f” )。在本例中,它将字符串值“f”存储在表“django_cron_job”的“queued”字段中。您可以通过查询来测试它:

select queued from django_cron_job;

如果您更改作业的代码,该字段可能会保留为“f”。因此,如果您更正作业的错误,您应该在排队字段中手动设置:“t”。或者表 django_cron_cron 中的执行标志是“t”。这意味着您的应用程序。当您的任务正在进行时,服务器已停止。在这种情况下,您应该手动将其设置为“f”。

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文