Skip to content

Commit 04f3099

Browse files
committed
feedback
1 parent 7985585 commit 04f3099

7 files changed

Lines changed: 43 additions & 18 deletions

File tree

learning_resources/management/commands/backpopulate_mit_edx_data.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ def handle(self, *args, **options): # noqa: ARG002
6262
self.stdout.write(
6363
f"Population of MIT edX data finished, took {total_seconds} seconds"
6464
)
65-
self.stdout.write(
66-
f"Populated {count} resources. See celery logs for details."
67-
)
65+
if count is None:
66+
self.stdout.write(
67+
"Task skipped (rate-limited). See celery logs for details."
68+
)
69+
else:
70+
self.stdout.write(
71+
f"Populated {count} resources. See celery logs for details."
72+
)

learning_resources/management/commands/backpopulate_mitxonline_data.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ def handle(self, *args, **options): # noqa: ARG002
4848
self.stdout.write(
4949
f"Population of MITX Online data finished, took {total_seconds} seconds"
5050
)
51-
self.stdout.write(
52-
f"Populated {count} resources. See celery logs for details."
53-
)
51+
if count is None:
52+
self.stdout.write(
53+
"Task skipped (rate-limited). See celery logs for details."
54+
)
55+
else:
56+
self.stdout.write(
57+
f"Populated {count} resources. See celery logs for details."
58+
)

learning_resources/management/commands/backpopulate_oll_data.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ def handle(self, *args, **options): # noqa: ARG002
5555
self.stdout.write(
5656
f"Population of oll data finished, took {total_seconds} seconds"
5757
)
58-
self.stdout.write(
59-
f"Populated {count} resources. See celery logs for details."
60-
)
58+
if count is None:
59+
self.stdout.write(
60+
"Task skipped (rate-limited). See celery logs for details."
61+
)
62+
else:
63+
self.stdout.write(
64+
f"Populated {count} resources. See celery logs for details."
65+
)

learning_resources/management/commands/backpopulate_xpro_data.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ def handle(self, *args, **options): # noqa: ARG002
4949
self.stdout.write(
5050
f"Population of xpro data finished, took {total_seconds} seconds"
5151
)
52-
self.stdout.write(
53-
f"Populated {count} resources. See celery logs for details."
54-
)
52+
if count is None:
53+
self.stdout.write(
54+
"Task skipped (rate-limited). See celery logs for details."
55+
)
56+
else:
57+
self.stdout.write(
58+
f"Populated {count} resources. See celery logs for details."
59+
)

learning_resources/tasks.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def get_micromasters_data():
122122
def get_mit_edx_data(
123123
api_course_datafile: str | None = None,
124124
api_program_datafile: str | None = None,
125-
) -> int:
125+
) -> int | None:
126126
"""Task to sync MIT edX data with the database
127127
128128
Args:
@@ -132,7 +132,8 @@ def get_mit_edx_data(
132132
Otherwise, the API is queried directly.
133133
134134
Returns:
135-
int: The number of results that were fetched
135+
int | None: The number of results fetched, or None if the call was
136+
skipped due to rate limiting.
136137
"""
137138
courses = pipelines.mit_edx_courses_etl(api_course_datafile)
138139
programs = pipelines.mit_edx_programs_etl(api_program_datafile)
@@ -142,7 +143,7 @@ def get_mit_edx_data(
142143

143144
@app.task
144145
@rate_limited_task(wait_time=900)
145-
def get_mitxonline_data() -> int:
146+
def get_mitxonline_data() -> int | None:
146147
"""Execute the MITX Online ETL pipeline"""
147148
courses = pipelines.mitxonline_courses_etl()
148149
programs = pipelines.mitxonline_programs_etl()

main/decorators.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010

1111
def rate_limited_task(wait_time: int, key: str | None = None):
1212
"""
13-
Drop calls made within `wait_time` seconds of the last successful invocation.
14-
Uses an atomic `cache.add` so it is safe across Celery workers.
13+
Drop calls made within `wait_time` seconds of the previous invocation.
14+
15+
The lock is acquired before the wrapped function runs and is not released
16+
on exception — failures still count against the rate limit to prevent
17+
retry storms against upstream APIs. Uses an atomic `cache.add` so it is
18+
safe across Celery workers.
1519
1620
Args:
1721
wait_time: Lock duration in seconds.

main/decorators_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def _patch_redis_cache(mocker):
77
"""Patch the redis cache used by rate_limited_task and return its mock."""
88
mock_redis = mocker.Mock()
9-
mocker.patch.dict("main.decorators.caches", {"redis": mock_redis})
9+
mocker.patch("main.decorators.caches", {"redis": mock_redis})
1010
return mock_redis
1111

1212

0 commit comments

Comments
 (0)