Skip to content

Commit

Permalink
Celery task to fetch course roster asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed Aug 29, 2024
1 parent 8399a3a commit 2f403b7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lms/tasks/course_roster.py
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)
29 changes: 29 additions & 0 deletions tests/unit/lms/tasks/course_roster_test.py
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
7 changes: 7 additions & 0 deletions tests/unit/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from lms.services.canvas_api import CanvasAPIClient
from lms.services.canvas_studio import CanvasStudioService
from lms.services.course import CourseService
from lms.services.course_roster import CourseRosterService
from lms.services.d2l_api import D2LAPIClient
from lms.services.dashboard import DashboardService
from lms.services.digest import DigestService
Expand Down Expand Up @@ -67,6 +68,7 @@
"canvas_service",
"canvas_studio_service",
"course_service",
"course_roster_service",
"d2l_api_client",
"dashboard_service",
"digest_service",
Expand Down Expand Up @@ -187,6 +189,11 @@ def course_service(mock_service):
return mock_service(CourseService, service_name="course")


@pytest.fixture
def course_roster_service(mock_service):
return mock_service(CourseRosterService)


@pytest.fixture
def d2l_api_client(mock_service):
return mock_service(D2LAPIClient)
Expand Down

0 comments on commit 2f403b7

Please sign in to comment.