Task rate limits#3363
Open
mbertrand wants to merge 2 commits into
Open
Conversation
OpenAPI ChangesNo changes detected Unexpected changes? Ensure your branch is up-to-date with |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a reusable task rate-limiting decorator (backed by the configured Redis cache) and applies it to several ETL Celery tasks to prevent repeated invocations within a fixed time window.
Changes:
- Introduces
rate_limited_task(wait_time, key=None)inmain/decorators.pyusing an atomiccache.addon therediscache. - Adds unit tests covering rate-limiting behavior and key selection.
- Applies the decorator to selected ETL tasks in
learning_resources/tasks.py(3600s for MIT edX; 900s for MITxOnline/xPro/OLL).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| main/decorators.py | Adds the Redis-cache-backed rate limit decorator used to skip frequent task invocations. |
| main/decorators_test.py | Adds tests for rate-limiting behavior and cache key selection. |
| learning_resources/tasks.py | Wraps several ETL tasks with the new rate-limiting decorator. |
5f48bee to
04f3099
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What are the relevant tickets?
Closes https://github.com/mitodl/hq/issues/11394
Description (What does it do?)
Adds a
rate_limited_taskdecorator that makes use of redis lock keys to prevent a task from running more than once everynseconds (3600 forget_mit_edx_data, 900 forget_xpro_data,get_oll_data, andget_mitxonline_data).How can this be tested?
docker compose upRun
./manage.py backpopulate_mitxonline_dataTry running it again afterward. You should see this in the logs:
And the command should finish quickly:
Population of MITX Online data finished, took 0.041112 seconds Task skipped (rate-limited). See celery logs for details.Wait 15 minutes and try again. This time it should process normally:
Additional Context