【问题标题】:How to make cron time editable for django admin如何让 django 管理员可以编辑 cron 时间
【发布时间】:2019-05-03 07:51:13
【问题描述】:

我必须允许管理员在 django 的管理员视图中设置 cron 时间。 就像我有一个配置模型,管理员可以把时间记录下来

  1. 凌晨 2 点(记录 1)
  2. 下午 4 点(记录 2)

所以我必须在每条记录上运行 cron。 但是cron时间在setting.py中

CRONJOBS = [
    ('*/5 * * * *', 'myapp.cron.my_scheduled_job')
]

https://pypi.org/project/django-crontab/

如何使此设置可供管理员使用。

【问题讨论】:

  • 我决定使用 django_celery_beat 而不是 cron

标签: django django-models cron


【解决方案1】:

我没有看到任何好的方法,因为 vanilla django_crontab 只允许从设置中填充 crontab。你最好找到其他允许你想要的包。但如果你别无选择,我认为以下方法可行:

my_crontab.py

from django_crontab.app_settings import Settings
from django_crontab.crontab import Crontab
from django.conf import settings

# function need to return crontab
# in the same format as settings.py
def populate_from_db():
    # some db magic
    return [('*/5 * * * *', 'myapp.cron.my_scheduled_job')]


class DBCronSettings(Settings):

    def __init__(self, settings):
        super().__init__(settings)
        self.CRONJOBS = populate_from_db() #


class DBCrontab(Crontab):

    def __init__(self, **options):
        super().__init__(**options)
        self.settings = DBCronSettings(settings)

您需要继承 Crontab 和 Settings。让 DBCronSettings 从数据库中读取您的 cron 作业,然后在您的自定义 DBCrontab 中使用此设置。

然后制作你自己的 crontab 命令。处理方法与基本命令中的完全相同,但使用您的 DBCrontab 类而不是原始类。

command.py

from django_crontab.management.commands.crontab import Command as CrontabCommand
from my_crontab import DBCrontab as Crontab


class Command(CrontabCommand):

    def handle(self, *args, **options):
        """
        Dispatches by given subcommand
        """
        if options['subcommand'] == 'add':         # add command
            with Crontab(**options) as crontab:    # initialize a Crontab class with any specified options
                crontab.remove_jobs()              # remove all jobs specified in settings from the crontab
                crontab.add_jobs()                 # and add them back
        elif options['subcommand'] == 'show':      # show command
            # initialize a readonly Crontab class with any specified options
            with Crontab(readonly=True, **options) as crontab:
                crontab.show_jobs()                # list all currently active jobs from crontab
        elif options['subcommand'] == 'remove':    # remove command
            with Crontab(**options) as crontab:    # initialize a Crontab class with any specified options
                crontab.remove_jobs()              # remove all jobs specified in settings from the crontab
        elif options['subcommand'] == 'run':       # run command
            Crontab().run_job(options['jobhash'])  # run the job with the specified hash
        else:
            # output the help string if the user entered something not specified above
            print(self.help)

如果您打算将命令命名为“crontab”,请不要忘记从 INSTALLED_APPS 中删除 django_crontab。

【讨论】:

  • 感谢您的回复。你能分享其他包的链接吗,我找不到任何这样的包。
  • @prakash 看看djangopackages.org/grids/g/cron。特别是 Django Chroniker 似乎是您正在寻找的东西。我没有尝试过任何这些,所以由你决定。
  • 我决定使用 django_celery_beat 而不是 cron
  • @prakash 使用 celery,您可以使用许多强大的功能,例如重试、跨多个主机分配工作人员、高级路由等。但是如果您只有几个简单的周期性任务,那么 celery 可能会有点过头了。至少您将有额外的流程和必要性来支持经纪人。另外我应该提一下,虽然 celery 非常稳定,但您可能会偶尔遇到内存泄漏、挂起。
猜你喜欢
  • 1970-01-01
  • 2014-01-12
  • 1970-01-01
  • 2011-09-28
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多