diff --git a/conf/configs/common.py b/conf/configs/common.py index 0b9dc8ca..6a9859e9 100644 --- a/conf/configs/common.py +++ b/conf/configs/common.py @@ -120,6 +120,8 @@ REMINDERS_FREQUENCY_SEC = 30 +EXTERNAL_CALENDAR_REINDEX_FREQUENCY_SEC = 600 + # Application definition SESSION_EXPIRE_AT_BROWSER_CLOSE = True diff --git a/helium/common/utils/metricutils.py b/helium/common/utils/metricutils.py index 2e27f728..f6b8b3c3 100644 --- a/helium/common/utils/metricutils.py +++ b/helium/common/utils/metricutils.py @@ -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') diff --git a/helium/feed/services/icalexternalcalendarservice.py b/helium/feed/services/icalexternalcalendarservice.py index 8dd3686b..228214a5 100644 --- a/helium/feed/services/icalexternalcalendarservice.py +++ b/helium/feed/services/icalexternalcalendarservice.py @@ -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__) @@ -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") diff --git a/helium/feed/tasks.py b/helium/feed/tasks.py new file mode 100644 index 00000000..d3231836 --- /dev/null +++ b/helium/feed/tasks.py @@ -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()) diff --git a/helium/feed/views/apis/externalcalendarresourceviews.py b/helium/feed/views/apis/externalcalendarresourceviews.py index 3641174a..9e907d1f 100644 --- a/helium/feed/views/apis/externalcalendarresourceviews.py +++ b/helium/feed/views/apis/externalcalendarresourceviews.py @@ -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__) diff --git a/helium/planner/tasks.py b/helium/planner/tasks.py index 25a8a05c..23efbe4b 100644 --- a/helium/planner/tasks.py +++ b/helium/planner/tasks.py @@ -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__) @@ -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())