Skip to content

Commit

Permalink
Merge pull request #335 from appsembler/esther/test-commit
Browse files Browse the repository at this point in the history
add waffle switch to disable tasks for maintenance
  • Loading branch information
estherjsuh authored May 27, 2021
2 parents 5b73f2e + 5f2602d commit 035f9ca
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions devsite/devsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'debug_toolbar',
'webpack_loader',
'organizations',
'waffle',
'devsite',
'figures',

Expand Down
1 change: 1 addition & 0 deletions devsite/devsite/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def root(*args):
'rest_framework',
'django_countries',
'django_filters',
'waffle',
'devsite',
'webpack_loader',
'figures',
Expand Down
1 change: 1 addition & 0 deletions devsite/requirements/ginkgo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions devsite/requirements/hawthorn_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions devsite/requirements/juniper_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions figures/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import time

import six
import waffle

from django.contrib.sites.models import Site
from django.utils.timezone import utc
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
25 changes: 25 additions & 0 deletions tests/tasks/test_daily_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 035f9ca

Please sign in to comment.