Skip to content

Commit

Permalink
Add a scheduled task to reindex stale calendar caches periodically, s…
Browse files Browse the repository at this point in the history
…o they it is not always being refreshed with a page load.
  • Loading branch information
alexdlaird committed Nov 21, 2023
1 parent 822a1d6 commit a4db180
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
2 changes: 2 additions & 0 deletions conf/configs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@

REMINDERS_FREQUENCY_SEC = 30

EXTERNAL_CALENDAR_REINDEX_FREQUENCY_SEC = 600

# Application definition

SESSION_EXPIRE_AT_BROWSER_CLOSE = True
Expand Down
27 changes: 26 additions & 1 deletion helium/common/utils/metricutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,34 @@ def request_stop(metrics, response):
datadog_statsd.increment(metrics['Request-Metric-ID'], tags=DATADOG_TAGS)
datadog_statsd.increment(f"{metrics['Request-Metric-ID']}.{response.status_code}", tags=DATADOG_TAGS)

datadog_statsd.timing(metrics['Request-Metric-ID'], metrics['Request-Timer'].ms, tags=DATADOG_TAGS)
datadog_statsd.timing(metrics['Request-Metric-ID'] + ".time", metrics['Request-Timer'].ms, tags=DATADOG_TAGS)

metrics.pop('Request-Timer')

for name, value in metrics.items():
response._headers[name] = (name, str(value))

def task_start(task_name):
metric_id = "platform.task.{}".format(task_name)
timer = statsd.timer(metric_id, rate=1)
timer.start()

return {
'Task-Timer': timer,
'Task-Metric-ID': metric_id,
'Task-Metric-Start': int(round(timer._start_time * 1000))
}


def task_stop(metrics):
metrics['Task-Timer'].stop()
metrics['Task-Metric-Millis'] = metrics['Task-Timer'].ms

statsd.incr(metrics['Task-Metric-ID'])

if DATADOG_METRICS:
datadog_statsd.increment(metrics['Task-Metric-ID'], tags=DATADOG_TAGS)

datadog_statsd.timing(metrics['Task-Metric-ID'] + ".time", metrics['Task-Timer'].ms, tags=DATADOG_TAGS)

metrics.pop('Task-Timer')
20 changes: 19 additions & 1 deletion helium/feed/services/icalexternalcalendarservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

from helium.common import enums
from helium.common.utils.commonutils import HeliumError
from helium.feed.models import ExternalCalendar
from helium.planner.models import Event
from helium.planner.serializers.eventserializer import EventSerializer

__author__ = "Alex Laird"
__copyright__ = "Copyright 2023, Helium Edu"
__version__ = "1.4.50"
__version__ = "1.4.51"

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -180,3 +181,20 @@ def calendar_to_events(external_calendar, start=None, end=None):
events = _create_events_from_calendar(external_calendar, calendar, start, end)

return events


def reindex_stale_caches():
for external_calendar in ExternalCalendar.objects.all().iterator():
cached = False
cached_value = cache.get(_get_cache_prefix(external_calendar))
if cached_value:
events, cached = _get_events_from_cache(external_calendar, cached_value)

if not cached:
logger.info("Reindexing External Calendar {}".format(external_calendar.pk))

calendar = validate_url(external_calendar.url)

_create_events_from_calendar(external_calendar, calendar)

logger.info("Done reindexing stale caches")
28 changes: 28 additions & 0 deletions helium/feed/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import logging

from django.conf import settings

from conf.celery import app
from helium.common.utils import metricutils
from helium.feed.services import icalexternalcalendarservice

__author__ = "Alex Laird"
__copyright__ = "Copyright 2023, Helium Edu"
__version__ = "1.4.51"

logger = logging.getLogger(__name__)


@app.task(bind=True)
def reindex_external_calendars():
metrics = metricutils.task_start("reindex_external_calendars")

icalexternalcalendarservice.reindex_stale_caches()

metricutils.task_stop(metrics)


@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs): # pragma: no cover
# Add schedule to reindex external calendars periodically
sender.add_periodic_task(settings.EXTERNAL_CALENDAR_REINDEX_FREQUENCY_SEC, reindex_external_calendars.s())
4 changes: 2 additions & 2 deletions helium/feed/views/apis/externalcalendarresourceviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from helium.planner.serializers.eventserializer import EventSerializer

__author__ = "Alex Laird"
__copyright__ = "Copyright 2019, Helium Edu"
__version__ = "1.4.38"
__copyright__ = "Copyright 2023, Helium Edu"
__version__ = "1.4.51"

logger = logging.getLogger(__name__)

Expand Down
8 changes: 4 additions & 4 deletions helium/planner/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from helium.planner.services import reminderservice

__author__ = "Alex Laird"
__copyright__ = "Copyright 2021, Helium Edu"
__version__ = "1.4.46"
__copyright__ = "Copyright 2023, Helium Edu"
__version__ = "1.4.51"

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -203,8 +203,8 @@ def send_email_reminder(email, subject, reminder_id, calendar_item_id, calendar_

@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs): # pragma: no cover
# Add schedule for email reminders every ten seconds
# Add schedule for email reminders periodically
sender.add_periodic_task(settings.REMINDERS_FREQUENCY_SEC, email_reminders.s())

# Add schedule for text reminders every ten seconds
# Add schedule for text reminders periodically
sender.add_periodic_task(settings.REMINDERS_FREQUENCY_SEC, text_reminders.s())

0 comments on commit a4db180

Please sign in to comment.