[Answered ]-Running Django command /generating index with manage.py

1👍

You can implement a management command [Django-doc] to do this. In your app you create the management/ and commands/ directories:

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

Then in the make_es_index.py, you can define that command:

# 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()

Next we can schedule the task in Heroku, for example with the Heroku Scheduler. We install the add-on with:

heroku addons:create scheduler:standard

next we create a task in bin/refresh-es-index:

#!/bin/bash

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

Next we can schedule the refresh-es-index task with:

heroku addons:open scheduler

and set the time interval when we need to run the job.

Leave a comment