Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Make resetting the quotas a cron job

Daoud Clarke86d6f66e261b
diff --git a/Dockerfile b/Dockerfile
index ce51bfe..a90a615 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -62,6 +62,7 @@ COPY --from=builder /venv /venv
COPY --from=front-end /front-end/dist /front-end-build
ADD nginx.conf.sigil /app
+COPY manage.py /app
# ADD app.json /app
# Set up a volume where the data will live
diff --git a/app.json b/app.json
index 6fb409a..e036b8a 100644
--- a/app.json
+++ b/app.json
@@ -1,4 +1,11 @@
{
+ "cron": [
+ {
+ "command": "/venv/bin/python /app/manage.py reset_monthly_quotas",
+ "schedule": "0 0 1 * *",
+ "concurrency_policy": "forbid"
+ }
+ ],
"scripts": {
"dokku": {
"predeploy": "rm -rf /app/static/* && cp -r /front-end-build/* /app/static/"
diff --git a/mwmbl/apps.py b/mwmbl/apps.py
index 95815ec..8b0d18d 100644
--- a/mwmbl/apps.py
+++ b/mwmbl/apps.py
@@ -52,19 +52,14 @@ class MwmblConfig(AppConfig):
"""
try:
from background_task.models import Task
- from mwmbl.background import flush_search_counts, reset_monthly_quotas
+ from mwmbl.background import flush_search_counts
FLUSH_TASK = "mwmbl.background.flush_search_counts"
- RESET_TASK = "mwmbl.background.reset_monthly_quotas"
# Flush search counts every 10 minutes (600 seconds)
if not Task.objects.filter(task_name=FLUSH_TASK).exists():
flush_search_counts(repeat=600, repeat_until=None)
- # Reset monthly quotas daily (86400 seconds); only acts on day 1
- if not Task.objects.filter(task_name=RESET_TASK).exists():
- reset_monthly_quotas(repeat=86400, repeat_until=None)
-
except Exception:
# Don't prevent startup if background task scheduling fails
import logging
diff --git a/mwmbl/background.py b/mwmbl/background.py
index 67b4652..9ecedd8 100644
--- a/mwmbl/background.py
+++ b/mwmbl/background.py
@@ -3,11 +3,9 @@ Script that updates data in a background process.
Also contains Django Background Tasks for periodic quota maintenance:
- flush_search_counts: syncs Redis monthly counters → DB every 10 minutes
- - reset_monthly_quotas: resets counters on the 1st of each month (runs daily)
"""
import logging
import sys
-from datetime import datetime
from logging import getLogger, basicConfig
from pathlib import Path
from time import sleep
@@ -109,25 +107,3 @@ def flush_search_counts():
logger.exception("Error flushing search count for key %s", key)
-@background(schedule=0)
-def reset_monthly_quotas():
- """
- Reset all monthly search quotas on the first day of each month.
- Runs daily (86400 seconds) but is a no-op on any day other than the 1st.
- """
- from django.core.cache import cache
- from mwmbl.models import MwmblUser
- from mwmbl.quota import delete_all_monthly_keys
-
- now = datetime.utcnow()
- if now.day != 1:
- return # Only act on the first day of the month
-
- logger.info("Resetting monthly search quotas for %s-%02d", now.year, now.month)
- try:
- delete_all_monthly_keys()
- except Exception:
- logger.exception("Error deleting monthly cache keys during quota reset")
-
- MwmblUser.objects.all().update(monthly_search_count=0)
- logger.info("Monthly search quotas reset complete")