Skip to content

Commit

Permalink
feat: rebuild elastic search index after deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna committed Aug 15, 2023
1 parent 5a3d4e7 commit f361da6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
13 changes: 3 additions & 10 deletions basedosdados_api/api/v1/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,8 @@ def rebuild_search_index(modeladmin, request, queryset):


def update_search_index(modeladmin, request, queryset):
for instance in queryset:
try:
search_backend = connections["default"].get_backend()
search_index = search_backend.get_index(
"basedosdados_api.api.v1.models.Dataset"
)
search_index.update_object(instance, using="default")
messages.success(request, "Search index updated successfully")
except ObjectDoesNotExist:
messages.error(request, f"Search index for {instance} update failed")
call_command("update_index", interactive=False, batchsize=100, workers=4)
messages.success(request, "Search index rebuilt successfully")


update_search_index.short_description = "Update search index"
Expand Down Expand Up @@ -423,6 +415,7 @@ class TagAdmin(TabbedTranslationAdmin):
class DatasetAdmin(OrderedInlineModelAdminMixin, TabbedTranslationAdmin):
actions = [
reorder_tables,
update_search_index,
rebuild_search_index,
]

Expand Down
24 changes: 14 additions & 10 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ services:
volumes:
- esdata:/usr/share/elasticsearch/data
ports:
- "127.0.0.1:9200:9200"
- "127.0.0.1:9300:9300"
networks:
- esnet
- 9200:9200
- 9300:9300
healthcheck:
test: ["CMD", "curl --silent --fail http://localhost:9200/_cluster/health"]
interval: 1m
timeout: 20s
retries: 5
start_period: 30s

database:
image: postgres:14
Expand All @@ -29,7 +33,7 @@ services:
ports:
- 5432:5432
volumes:
- vol_database:/var/lib/postgresql/data
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}"]
interval: 1m
Expand All @@ -48,7 +52,10 @@ services:
ports:
- 8080:80
depends_on:
- database
database:
condition: service_healthy
elasticsearch:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost/healthcheck/"]
interval: 1m
Expand All @@ -58,8 +65,5 @@ services:
restart: unless-stopped

volumes:
vol_database:
pgdata:
esdata:

networks:
esnet:
8 changes: 4 additions & 4 deletions start-server.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
# start-server.sh
python manage.py makemigrations && python manage.py migrate
if [ -n "$DJANGO_SUPERUSER_USERNAME" ] && [ -n "$DJANGO_SUPERUSER_PASSWORD" ] ; then
(cd /app; python manage.py createsuperuser --no-input)
fi
(cd /app; python manage.py makemigrations && python manage.py migrate)
(cd /app; gunicorn basedosdados_api.wsgi --user www-data --bind 0.0.0.0:8000 --workers 3 --log-level debug --timeout 180) &
nginx -g "daemon off;"
(cd /app; python manage.py makemigrations)
(cd /app; python manage.py migrate)
(cd /app; python manage.py update_index --remove)
(cd /app; gunicorn basedosdados_api.wsgi --user www-data --bind 0.0.0.0:8000 --workers 3 --log-level debug --timeout 180) & nginx -g "daemon off;"

0 comments on commit f361da6

Please sign in to comment.