Skip to content

Commit

Permalink
chore:add cache management script and improve rebuild process
Browse files Browse the repository at this point in the history
Introduce `manage_cache.py` for handling cache rebuilds, including a new script entry in `pyproject.toml`. Refactor imports and add logging to enhance clarity and maintainability in the codebase. Also, import `django-environ` and update the Makefile for additional cache rebuild command.
  • Loading branch information
hareshkainthdbt committed Nov 29, 2024
1 parent 423ef50 commit f2ce5a0
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,10 @@ isort: # Run isort

secrets-baseline: # Generate a new secrets baseline file
poetry run detect-secrets scan > .secrets.baseline

rebuild_cache_man:
export PYTHONPATH=./fbr && \
export DJANGO_SETTINGS_MODULE='fbr.config.settings.local' && \
export DATABASE_URL=postgres://postgres:postgres@localhost:5432/fbr && \
poetry install && \
poetry run rebuild-cache
9 changes: 3 additions & 6 deletions fbr/cache/legislation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@

import requests # type: ignore

from construction_legislation import ( # noqa: E501
construction_legislation_dataframe
) # noqa: E501

from fbr.search.config import SearchDocumentConfig
from fbr.search.utils.date import convert_date_string_to_obj
from fbr.cache.construction_legislation import construction_legislation_dataframe # noqa: E501
from fbr.search.config import SearchDocumentConfig # noqa: E501
from fbr.search.utils.date import convert_date_string_to_obj # noqa: E501
from fbr.search.utils.documents import ( # noqa: E501
generate_short_uuid,
insert_or_update_document,
Expand Down
27 changes: 27 additions & 0 deletions fbr/cache/manage_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# flake8: noqa
import os
import time

import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")

django.setup()

from fbr.cache.legislation import Legislation
from fbr.cache.public_gateway import PublicGateway
from fbr.search.config import SearchDocumentConfig
from fbr.search.utils.documents import clear_all_documents


def rebuild_cache():
try:
start = time.time()
clear_all_documents()
config = SearchDocumentConfig(search_query="", timeout=20)
Legislation().build_cache(config)
PublicGateway().build_cache(config)
end = time.time()
return {"message": "rebuilt cache", "duration": round(end - start, 2)}
except Exception as e:
return {"message": f"error clearing documents: {e}"}
6 changes: 5 additions & 1 deletion fbr/cache/tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

from celery import shared_task

from fbr.cache.legislation import Legislation
Expand All @@ -9,10 +11,12 @@
@shared_task()
def rebuild_cache():
try:
start = time.time()
clear_all_documents()
config = SearchDocumentConfig(search_query="", timeout=20)
Legislation().build_cache(config)
PublicGateway().build_cache(config)
return {"message": "rebuilt cache"}
end = time.time()
return {"message": "rebuilt cache", "duration": round(end - start, 2)}
except Exception as e:
return {"message": f"error clearing documents: {e}"}
2 changes: 1 addition & 1 deletion fbr/search/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class SearchConfig(AppConfig):
"""

name = "search"
name = "fbr.search"
verbose_name = "Find business regulations application functionality"
default_auto_field = "django.db.models.BigAutoField"
5 changes: 4 additions & 1 deletion fbr/search/utils/documents.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import base64
import logging
import re
import uuid

from numpy.f2py.auxfuncs import throw_error

from django.db.models import QuerySet

from search.models import DataResponseModel, logger
from fbr.search.models import DataResponseModel

logger = logging.getLogger(__name__)


def clear_all_documents():
Expand Down
20 changes: 20 additions & 0 deletions fbr/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from setuptools import find_packages, setup

setup(
name="fbr",
version="0.1",
packages=find_packages(),
install_requires=[
# Add your package dependencies here
"requests",
"pandas",
"django",
"dj_database_url",
],
entry_points={
"console_scripts": [
# Define command-line scripts here if needed
# e.g., 'my-command = fbr.module:function',
],
},
)
18 changes: 17 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ celery = {extras = ["redis"], version = "^5.4.0"}
kombu = {extras = ["redis"], version = "^5.4.2"}
boto3 = "^1.35.71"
django-celery-beat = "^2.7.0"
django-environ = "^0.11.2"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.7.1"
Expand Down Expand Up @@ -101,3 +102,6 @@ ignore_errors = true
[tool.flake8]
max-line-length = 79
max-complexity = 10

[tool.poetry.scripts]
rebuild-cache = "fbr.cache.manage_cache:rebuild_cache"

0 comments on commit f2ce5a0

Please sign in to comment.