Skip to content

Commit

Permalink
Merge pull request #78 from uktrade/feature/resolve-celery
Browse files Browse the repository at this point in the history
Add cache app and improve Celery configuration
  • Loading branch information
hareshkainthdbt authored Dec 5, 2024
2 parents 154fcd4 + cdacc27 commit f7d6e92
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions fbr/cache/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from fbr.search.utils.documents import clear_all_documents


@shared_task(binding=True)
@shared_task(bind=True, max_retries=3)
def rebuild_cache():
try:
start = time.time()
Expand All @@ -19,4 +19,4 @@ def rebuild_cache():
end = time.time()
return {"message": "rebuilt cache", "duration": round(end - start, 2)}
except Exception as e:
return {"message": f"error clearing documents: {e}"}
return {"message": f"error building cache data: {e}"}
4 changes: 2 additions & 2 deletions fbr/config/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from celery.schedules import crontab
from dbt_copilot_python.celery_health_check import healthcheck

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

celery_app = Celery("fbr_celery")

Expand All @@ -18,6 +18,6 @@
celery_app.conf.beat_schedule = {
"schedule-fbr-cache-task": {
"task": "fbr.cache.tasks.rebuild_cache",
"schedule": crontab(hour="1", minute="0"),
"schedule": crontab(hour="1", minute="0"), # Runs daily at 1:00 AM
},
}
6 changes: 6 additions & 0 deletions fbr/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@
CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP = True
CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers.DatabaseScheduler"
CELERY_RESULT_EXTENDED = True
CELERY_TASK_TIME_LIMIT = (
450 # Maximum runtime for a task in seconds (e.g., 7.5 minutes)
)
CELERY_TASK_SOFT_TIME_LIMIT = (
270 # Optional: Grace period before forced termination
)

# Internationalisation
LANGUAGE_CODE = "en-gb"
Expand Down
1 change: 1 addition & 0 deletions fbr/config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
BASE_APPS = [
"whitenoise.runserver_nostatic", # Serve static files via WhiteNoise
"rest_framework",
"fbr.cache",
]

# REST_FRAMEWORK = {
Expand Down

0 comments on commit f7d6e92

Please sign in to comment.