Skip to content

Commit

Permalink
Adding TTL check and coursepage (#2245)
Browse files Browse the repository at this point in the history
* Adding ttl check rather than checking for exist and specifically checking coursepage rather than course

* if none, page break

* change amount

* empty list angers the homepage
  • Loading branch information
JenniWhitman authored Jun 17, 2024
1 parent e4494dc commit a5450e8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
8 changes: 7 additions & 1 deletion cms/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,15 @@ def create_featured_items():
now = now_in_utc()
end_of_day = now + timedelta(days=1)

valid_coursepages = cms_models.CoursePage.objects.filter(live=True).values_list(
"course_id", flat=True
)

enrollable_courseruns = get_enrollable_courseruns_qs(
end_of_day,
Course.objects.select_related("page").filter(page__live=True, live=True),
Course.objects.select_related("page").filter(
id__in=valid_coursepages, live=True
),
)

# Figure out which courses are self-paced and select 2 at random
Expand Down
2 changes: 2 additions & 0 deletions cms/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
CERTIFICATE_INDEX_SLUG = "certificate"
SIGNATORY_INDEX_SLUG = "signatories"
INSTRUCTOR_INDEX_SLUG = "instructors"

ONE_MINUTE = 60
2 changes: 1 addition & 1 deletion cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ def get_cached_featured_products(self):
now = now_in_utc()
redis_cache = caches["redis"]
cached_featured_products = redis_cache.get("CMS_homepage_featured_courses")
if len(cached_featured_products) > 0:
if cached_featured_products and len(cached_featured_products) > 0:
featured_product_ids = [course.id for course in cached_featured_products]
relevant_run_course_ids = (
CourseRun.objects.filter(live=True)
Expand Down
6 changes: 4 additions & 2 deletions cms/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from mitol.common.decorators import single_task

from cms.api import create_featured_items
from cms.constants import ONE_MINUTE
from cms.models import Page
from main.celery import app
from main.settings import (
Expand Down Expand Up @@ -108,8 +109,9 @@ def refresh_featured_homepage_items():
"""
logger = logging.getLogger("refresh_featured_homepage_items__task")
logger.info("Refreshing featured homepage items...")
featured_courses = cache.get("CMS_homepage_featured_courses")
if featured_courses is not None:
# if the key is not found, the ttl will be 0 per their docs
# https://github.com/jazzband/django-redis?tab=readme-ov-file#get-ttl-time-to-live-from-key
if cache.ttl("CMS_homepage_featured_courses") > (10 * ONE_MINUTE):
logger.info("Featured courses found in cache, moving on")
return
logger.info("No featured courses found in cache, refreshing")
Expand Down

0 comments on commit a5450e8

Please sign in to comment.