From fe6c131ebbac99b50969d28d19442f70257e12dc Mon Sep 17 00:00:00 2001 From: Esther Date: Thu, 29 Apr 2021 18:35:58 +0300 Subject: [PATCH] add waffle swtich to disable tasks for maintenance apply changes from code review --- devsite/devsite/settings.py | 1 + devsite/devsite/test_settings.py | 1 + devsite/requirements/ginkgo.txt | 1 + devsite/requirements/hawthorn_base.txt | 1 + devsite/requirements/juniper_base.txt | 1 + figures/tasks.py | 14 ++++++++++++++ tests/tasks/test_daily_tasks.py | 25 +++++++++++++++++++++++++ 7 files changed, 44 insertions(+) diff --git a/devsite/devsite/settings.py b/devsite/devsite/settings.py index 02459d03..4fc535f0 100644 --- a/devsite/devsite/settings.py +++ b/devsite/devsite/settings.py @@ -73,6 +73,7 @@ 'debug_toolbar', 'webpack_loader', 'organizations', + 'waffle', 'devsite', 'figures', diff --git a/devsite/devsite/test_settings.py b/devsite/devsite/test_settings.py index 3e03d0b8..f5628838 100644 --- a/devsite/devsite/test_settings.py +++ b/devsite/devsite/test_settings.py @@ -62,6 +62,7 @@ def root(*args): 'rest_framework', 'django_countries', 'django_filters', + 'waffle', 'devsite', 'webpack_loader', 'figures', diff --git a/devsite/requirements/ginkgo.txt b/devsite/requirements/ginkgo.txt index 16279179..e8e8b405 100644 --- a/devsite/requirements/ginkgo.txt +++ b/devsite/requirements/ginkgo.txt @@ -38,6 +38,7 @@ django-model-utils==2.3.1 django-environ==0.4.5 django-celery==3.2.1 jsonfield==1.0.3 # Version used in Ginkgo. Hawthorn uses version 2.0.2 +django-waffle==0.12.0 ## ## Documentation (Sphinx) dependencies diff --git a/devsite/requirements/hawthorn_base.txt b/devsite/requirements/hawthorn_base.txt index cfbe7365..7dd3f92c 100644 --- a/devsite/requirements/hawthorn_base.txt +++ b/devsite/requirements/hawthorn_base.txt @@ -21,6 +21,7 @@ pytz==2016.10 Django==1.11.29 djangorestframework==3.6.3 django-countries==4.6.1 +django-waffle==0.12.0 # edx-platform hawthorn does not use django-extensions django-extensions==1.5.9 diff --git a/devsite/requirements/juniper_base.txt b/devsite/requirements/juniper_base.txt index 7a839335..ea68e9f6 100644 --- a/devsite/requirements/juniper_base.txt +++ b/devsite/requirements/juniper_base.txt @@ -32,6 +32,7 @@ django-webpack-loader==0.7.0 django-model-utils==4.0.0 django-filter==2.3.0 django-environ==0.4.5 +django-waffle==0.18.0 jsonfield==2.1.1 diff --git a/figures/tasks.py b/figures/tasks.py index bba37868..3facbe16 100644 --- a/figures/tasks.py +++ b/figures/tasks.py @@ -9,6 +9,7 @@ import time import six +import waffle from django.contrib.sites.models import Site from django.utils.timezone import utc @@ -42,6 +43,8 @@ # TODO: Make this configurable in the settings # logger.setLevel('INFO') +WAFFLE_DISABLE_PIPELINE = 'figures.disable_pipeline' + @shared_task def populate_single_cdm(course_id, date_for=None, force_update=False): @@ -167,12 +170,18 @@ def populate_daily_metrics(date_for=None, force_update=False): This function will get reworked so that each site runs in its own """ + if waffle.switch_is_active(WAFFLE_DISABLE_PIPELINE): + logger.warning('Figures pipeline is disabled due to %s being active.', + WAFFLE_DISABLE_PIPELINE) + return + # The date_for handling is very similar to the new rule we ahve in # `figures.pipeline.helpers.pipeline_data_for_rule` # The difference is the following code does not set 'date_for' as yesterday # So we likely want to rework the pipeline rule function and this code # so that we have a generalized date_for rule that can take an optional # transform function, like `prev_day` + today = datetime.datetime.utcnow().replace(tzinfo=utc).date() # TODO: Decide if/how we want any special logging if we get an exception # on 'casting' the date_for argument as a datetime.date object @@ -378,6 +387,11 @@ def run_figures_monthly_metrics(): """ Populate monthly metrics for all sites. """ + if waffle.switch_is_active(WAFFLE_DISABLE_PIPELINE): + logger.info('Figures pipeline is disabled due to %s being active.', + WAFFLE_DISABLE_PIPELINE) + return + logger.info('Starting figures.tasks.run_figures_monthly_metrics...') for site in get_sites(): populate_monthly_metrics_for_site.delay(site_id=site.id) diff --git a/tests/tasks/test_daily_tasks.py b/tests/tasks/test_daily_tasks.py index 04de3130..904b30d1 100644 --- a/tests/tasks/test_daily_tasks.py +++ b/tests/tasks/test_daily_tasks.py @@ -58,9 +58,12 @@ """ from __future__ import absolute_import from datetime import date +import logging import pytest from six.moves import range from django.contrib.sites.models import Site +from waffle.testutils import override_switch + from figures.helpers import as_date, as_datetime from figures.models import (CourseDailyMetrics, SiteDailyMetrics) @@ -103,6 +106,28 @@ def mock_cdm_load(self, date_for, **kwargs): assert as_date(CourseDailyMetrics.objects.first().date_for) == as_date(date_for) +@pytest.mark.django_db +def test_disable_populate_daily_metrics(caplog): + """Test figures.tasks.populate_daily_metrics + + Tests that when WAFFLE_DISABLE_PIPELINE is active, the disabled warning msg is logged + """ + with override_switch('figures.disable_pipeline', active=True): + populate_daily_metrics() + assert 'disabled' in caplog.text + + +@pytest.mark.django_db +def test_enable_populate_daily_metrics(caplog): + """Test figures.tasks.populate_daily_metrics + + Tests that when WAFFLE_DISABLE_PIPELINE is not active, the disabled warning msg is not logged + """ + with override_switch('figures.disable_pipeline', active=False): + populate_daily_metrics() + assert 'disabled' not in caplog.text + + def test_populate_single_sdm(transactional_db, monkeypatch): """Test figures.tasks.populate_single_sdm