Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Designate a queue for running the pipeline tasks #346

Merged
merged 3 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion devsite/devsite/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
update_celerybeat_schedule,
# TODO: https://appsembler.atlassian.net/browse/RED-673
# update_webpack_loader,
update_celery_routes,
)


Expand Down Expand Up @@ -149,6 +150,13 @@ def root(*args):
'FIGURES': {}, # This variable is patched by the Figures' `lms_production.py` settings module.
}

PRJ_SETTINGS = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this variable mean?

'CELERY_ROUTES': "app.celery.routes"
}

FIGURES_PIPELINE_TASKS_ROUTING_KEY = ""

# TODO: https://appsembler.atlassian.net/browse/RED-673
# update_webpack_loader(WEBPACK_LOADER, ENV_TOKENS)
update_celerybeat_schedule(CELERYBEAT_SCHEDULE, ENV_TOKENS)
update_celerybeat_schedule(CELERYBEAT_SCHEDULE, ENV_TOKENS, FIGURES_PIPELINE_TASKS_ROUTING_KEY)
update_celery_routes(PRJ_SETTINGS, ENV_TOKENS, FIGURES_PIPELINE_TASKS_ROUTING_KEY)
44 changes: 42 additions & 2 deletions figures/settings/lms_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
from celery.schedules import crontab


class FiguresRouter(object):

def __init__(self, figures_tasks_queue_name):
self.figures_tasks_queue_name = figures_tasks_queue_name

def route_for_task(self, task):
if task.startswith("figures.tasks."):
return self.figures_tasks_queue_name

return None


def get_build_label(release_line):
if release_line in ['ginkgo', 'hawthorn']:
return 'rb10'
Expand Down Expand Up @@ -35,14 +47,21 @@ def update_webpack_loader(webpack_loader_settings, figures_env_tokens):
})


def update_celerybeat_schedule(celerybeat_schedule_settings, figures_env_tokens):
def update_celerybeat_schedule(
celerybeat_schedule_settings,
figures_env_tokens,
figures_tasks_queue):
"""
Figures pipeline job schedule configuration in CELERYBEAT_SCHEDULE.

Daily metrics pipeline scheduler is on by default
Course MAU metrics pipeline scheduler is off by default

TODO: Language improvement: Change the "IMPORT" to "CAPTURE" or "EXTRACT"

We need to set the celery queue for each scheduled task again here, celery
beat does not check CELERY_ROUTES for tasks scheduling:
https://stackoverflow.com/questions/51631455/how-to-route-tasks-to-different-queues-with-celery-and-django
"""
if figures_env_tokens.get('ENABLE_DAILY_METRICS_IMPORT', True):
celerybeat_schedule_settings['figures-populate-daily-metrics'] = {
Expand All @@ -51,6 +70,7 @@ def update_celerybeat_schedule(celerybeat_schedule_settings, figures_env_tokens)
hour=figures_env_tokens.get('DAILY_METRICS_IMPORT_HOUR', 2),
minute=figures_env_tokens.get('DAILY_METRICS_IMPORT_MINUTE', 0),
),
'options': {'queue': figures_tasks_queue},
}

if figures_env_tokens.get('ENABLE_DAILY_MAU_IMPORT', False):
Expand All @@ -60,15 +80,26 @@ def update_celerybeat_schedule(celerybeat_schedule_settings, figures_env_tokens)
hour=figures_env_tokens.get('DAILY_MAU_IMPORT_HOUR', 0),
minute=figures_env_tokens.get('DAILY_MAU_IMPORT_MINUTE', 0),
),
'options': {'queue': figures_tasks_queue},
}

if figures_env_tokens.get('ENABLE_FIGURES_MONTHLY_METRICS', True):
celerybeat_schedule_settings['figures-monthly-metrics'] = {
'task': 'figures.tasks.run_figures_monthly_metrics',
'schedule': crontab(0, 0, day_of_month=1),
'options': {'queue': figures_tasks_queue},
}


def update_celery_routes(platform_settings, figures_env_tokens, celery_tasks_queue):
"""
https://docs.celeryproject.org/en/3.1/userguide/routing.html#manual-routing
"""
if figures_env_tokens.get('FIGURES_PIPELINE_TASKS_ROUTING_KEY', False):
figures_router = FiguresRouter(celery_tasks_queue)
platform_settings.CELERY_ROUTES = (platform_settings.CELERY_ROUTES, figures_router)


def plugin_settings(settings):
"""
Update the LMS/Production (aka AWS) settings to use Figures properly.
Expand All @@ -90,8 +121,17 @@ def plugin_settings(settings):

"""
settings.ENV_TOKENS.setdefault('FIGURES', {})
figures_tasks_default_queue = settings.ENV_TOKENS['FIGURES'].get(
'FIGURES_PIPELINE_TASKS_ROUTING_KEY',
settings.CELERY_DEFAULT_ROUTING_KEY
)
update_webpack_loader(settings.WEBPACK_LOADER, settings.ENV_TOKENS['FIGURES'])
update_celerybeat_schedule(settings.CELERYBEAT_SCHEDULE, settings.ENV_TOKENS['FIGURES'])
update_celerybeat_schedule(
settings.CELERYBEAT_SCHEDULE,
settings.ENV_TOKENS['FIGURES'],
figures_tasks_default_queue
)
update_celery_routes(settings, settings.ENV_TOKENS['FIGURES'], figures_tasks_default_queue)

settings.CELERY_IMPORTS += (
"figures.tasks",
Expand Down
6 changes: 3 additions & 3 deletions figures/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.contrib.sites.models import Site
from django.utils.timezone import utc

from celery import chord
from celery import chord, group
from celery.app import shared_task
from celery.utils.log import get_task_logger

Expand Down Expand Up @@ -393,5 +393,5 @@ def run_figures_monthly_metrics():
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)
all_sites_jobs = group(populate_monthly_metrics_for_site.s(site.id) for site in get_sites())
all_sites_jobs.delay()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@melvinsoft These two lines I'm really interested to know if they will work. I had tried doing grouping before in early Figures development, but the tasks seemed to have not been executed or died silently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnbaldwin We definitely need to try it out on staging, but I pushed the branch directly on Staging and run the Django management command and I was able to generate data.

I'm not sure how did you tried in the past, but if you look at the first line added, I'm creating a group of signatures. So it's Tasks -> Signature -> Group.

We've a design problem that I'm trying to solve with group, we've a celery task run_figures_monthly_metrics that inside does a loop and trigger a new celery tasks for each site populate_monthly_metrics_for_site. This is not recommended by Celery, and actually, after scratching my head for a couple of days, I found out that the tasks we trigger inside the tasks, do not respecting the routing. So I'd say let's try this approach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not recommended by Celery, and actually, after scratching my head for a couple of days, I found out that the tasks we trigger inside the tasks, do not respecting the routing. So I'd say let's try this approach.

Interesting find! Let's try it and see if it works.

10 changes: 5 additions & 5 deletions tests/tasks/test_monthly_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ def test_run_figures_monthly_metrics_with_faked_subtask(transactional_db, monkey
Faking the subtask for the function under test
"""
expected_sites = Site.objects.all()
assert expected_sites.count()
assert expected_sites
sites_visited = []

def fake_populate_monthly_metrics_for_site(site_id):
sites_visited.append(site_id)
def fake_populate_monthly_metrics_for_site(celery_task_group):
for t in celery_task_group.tasks:
sites_visited.extend(t.args)

monkeypatch.setattr('figures.tasks.populate_monthly_metrics_for_site.delay',
fake_populate_monthly_metrics_for_site)
monkeypatch.setattr('celery.group.delay', fake_populate_monthly_metrics_for_site)

run_figures_monthly_metrics()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_daily_mau_pipeline_flag_enabled(self):
self.settings.ENV_TOKENS['FIGURES'] = { 'ENABLE_DAILY_MAU_IMPORT': True }
plugin_settings(self.settings)
assert self.TASK_NAME in self.settings.CELERYBEAT_SCHEDULE
assert set(['task', 'schedule']) == set(
assert set(['task', 'schedule', 'options']) == set(
self.settings.CELERYBEAT_SCHEDULE[self.TASK_NAME].keys())

def test_daily_mau_pipeline_flag_disabled(self):
Expand Down