【问题标题】:Running Django command /generating index with manage.py使用 manage.py 运行 Django 命令/生成索引
【发布时间】:2021-05-28 03:39:35
【问题描述】:

我需要向 Heroku 运行一个每天晚上需要为 Elasticsearch 生成索引的网络作业,我想知道是否可能。我需要在 python shell 上自动运行的命令是:

heroku run python3 manage.py shell --app My_app
    
from myapp.search_tasks import delete_index, create_index, index_all;
    
delete_index(); create_index(); index_all();

非常感谢您的帮助。

【问题讨论】:

标签: python django postgresql elasticsearch


【解决方案1】:

您可以实现management command [Django-doc] 来执行此操作。在您的应用中创建management/commands/ 目录:

myapp/
    __init__.py
    management/
        commands/
            make_es_index.py
    models.py
    views.py
    urls.py

然后在make_es_index.py,你可以定义那个命令:

# my_app/management/commanheroku addons:open schedulerds/make_es_index.py

from django.core.management.base import BaseCommand, no_translations

from myapp.search_tasks import delete_index, create_index, index_all

class Command(BaseCommand):

    def handle(self, *args, **options):
        delete_index()
        create_index()
        index_all()

接下来我们可以在 Heroku 中安排任务,例如使用Heroku Scheduler。我们安装插件:

heroku addons:create <b>scheduler:standard</b>

接下来我们在bin/refresh-es-index创建一个任务:

#!/bin/bash

cd 'root/of/django-project'
python3 manage.py make_es_index

接下来我们可以安排refresh-es-index任务:

heroku <b>addons:open scheduler</b>

并设置我们需要运行作业的时间间隔。

【讨论】:

  • 谢谢威廉,正是我需要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多