-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Celery task to fetch course roster asynchronously
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Celery tasks for fetching course rosters.""" | ||
|
||
from lms.models import LMSCourse | ||
from lms.services.course_roster import CourseRosterService | ||
from lms.tasks.celery import app | ||
|
||
|
||
@app.task( | ||
acks_late=True, | ||
autoretry_for=(Exception,), | ||
max_retries=2, | ||
retry_backoff=3600, | ||
retry_backoff_max=7200, | ||
) | ||
def fetch_roster(*, lms_course_id) -> None: | ||
"""Fetch the roster for one course.""" | ||
|
||
with app.request_context() as request: | ||
roster_service = request.find_service(CourseRosterService) | ||
with request.tm: | ||
lms_course = request.db.get(LMSCourse, lms_course_id) | ||
roster_service.fetch_roster(lms_course) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from contextlib import contextmanager | ||
|
||
import pytest | ||
|
||
from lms.tasks.course_roster import fetch_roster | ||
from tests import factories | ||
|
||
|
||
class TestFetchRoster: | ||
def test_it(self, course_roster_service, db_session): | ||
lms_course = factories.LMSCourse() | ||
db_session.flush() | ||
|
||
fetch_roster(lms_course_id=lms_course.id) | ||
|
||
course_roster_service.fetch_roster.assert_called_once_with(lms_course) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def app(patch, pyramid_request): | ||
app = patch("lms.tasks.course_roster.app") | ||
|
||
@contextmanager | ||
def request_context(): | ||
yield pyramid_request | ||
|
||
app.request_context = request_context | ||
|
||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters