Skip to content

Commit

Permalink
Make sure cache data is refreshed by deleting it before making a requ…
Browse files Browse the repository at this point in the history
…est which will populate it again (mozilla#3176)
  • Loading branch information
mathjazz authored Apr 16, 2024
1 parent 8c4aaf2 commit bfc3f8e
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions pontoon/base/management/commands/warmup_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from urllib.parse import urljoin

from django.core.cache import cache
from django.core.management.base import BaseCommand
from django.urls import reverse

Expand All @@ -27,8 +28,13 @@ def handle(self, *args, **options):
self.warmup_contributors_cache()
self.warmup_insights_cache()

def warmup_url(self, url, is_ajax=False):
def warmup_url(self, url, keys=[], is_ajax=False):
try:
# Make sure cache data is refreshed by deleting it
# before making a request which will populate it again.
for key in keys:
cache.delete(key)

headers = {"x-requested-with": "XMLHttpRequest"} if is_ajax else None
requests.get(url, headers=headers)
except requests.exceptions.RequestException as e:
Expand All @@ -38,7 +44,8 @@ def warmup_contributors_cache(self):
self.stdout.write("Warm up Contributors page.")
path = reverse("pontoon.contributors")
url = urljoin(SITE_URL, path)
self.warmup_url(url)
key = "pontoon.contributors.views.(AND:).None"
self.warmup_url(url, keys=[key])
self.stdout.write("Contributors page warmed up.")

self.stdout.write("Warm up Project Contributors tabs.")
Expand All @@ -47,7 +54,10 @@ def warmup_contributors_cache(self):
"pontoon.projects.ajax.contributors", kwargs={"slug": project.slug}
)
url = urljoin(SITE_URL, path)
self.warmup_url(url)
key = f"pontoon.contributors.views.(AND:('entity__resource__project',<Project:{project}>)).None".replace(
" ", ""
)
self.warmup_url(url, keys=[key])
self.stdout.write("Project Contributors tabs warmed up.")

self.stdout.write("Warm up Team Contributors tabs.")
Expand All @@ -56,7 +66,10 @@ def warmup_contributors_cache(self):
"pontoon.teams.ajax.contributors", kwargs={"locale": locale.code}
)
url = urljoin(SITE_URL, path)
self.warmup_url(url)
key = f"pontoon.contributors.views.(AND:('locale',<Locale:{locale}>)).None".replace(
" ", ""
)
self.warmup_url(url, keys=[key])
self.stdout.write("Team Contributors tabs warmed up.")

# We do not warm up ProjectLocale pages, because there are too many of them and
Expand All @@ -66,7 +79,11 @@ def warmup_insights_cache(self):
self.stdout.write("Warm up Insights page.")
path = reverse("pontoon.insights")
url = urljoin(SITE_URL, path)
self.warmup_url(url)
keys = [
f"/pontoon.insights.views/team_pretranslation_quality",
f"/pontoon.insights.views/project_pretranslation_quality",
]
self.warmup_url(url, keys=keys)
self.stdout.write("Insights page warmed up.")

self.stdout.write("Warm up Project Insights tabs.")
Expand All @@ -75,7 +92,8 @@ def warmup_insights_cache(self):
"pontoon.projects.ajax.insights", kwargs={"slug": project.slug}
)
url = urljoin(SITE_URL, path)
self.warmup_url(url, is_ajax=True)
key = f"/pontoon.projects.views/{project.slug}/insights"
self.warmup_url(url, keys=[key], is_ajax=True)
self.stdout.write("Project Insights tabs warmed up.")

self.stdout.write("Warm up Team Insights tabs.")
Expand All @@ -84,7 +102,8 @@ def warmup_insights_cache(self):
"pontoon.teams.ajax.insights", kwargs={"locale": locale.code}
)
url = urljoin(SITE_URL, path)
self.warmup_url(url, is_ajax=True)
key = f"/pontoon.teams.views/{locale.code}/insights"
self.warmup_url(url, keys=[key], is_ajax=True)
self.stdout.write("Team Insights tabs warmed up.")

# We do not warm up ProjectLocale pages, because there are too many of them and
Expand Down

0 comments on commit bfc3f8e

Please sign in to comment.