-
Notifications
You must be signed in to change notification settings - Fork 29
Slow cyberstorm api endpoints (TS-2516) #1151
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
Open
Roffenlund
wants to merge
6
commits into
master
Choose a base branch
from
slow-cyberstorm-api-endpoints
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4655ca5
Add celery queue and redis storage for markdown
Roffenlund 8ce889e
Implement markdown celery task
Roffenlund bc4d79f
Implement service for rendering markdown
Roffenlund 8fd0510
Refactor markdown views
Roffenlund 627dfd8
Rework markdown service and celery task
Roffenlund db27a96
Remove 404 error from readme API view
Roffenlund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Empty file.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from celery.result import AsyncResult | ||
|
|
||
| from thunderstore.cache.utils import get_cache | ||
| from thunderstore.repository.tasks.markdown import render_markdown_to_html | ||
|
|
||
| cache = get_cache("markdown_render") | ||
|
|
||
|
|
||
| def render_markdown_service(markdown: str, key: str, object_id: int) -> dict: | ||
| if markdown.strip() == "": | ||
| return {"html": ""} | ||
|
|
||
| cache_key = f"rendered_html:{key}:{object_id}" | ||
| status_key = f"rendering_status:{key}:{object_id}" | ||
|
|
||
| if (html := cache.get(cache_key)) is not None: | ||
| return {"html": html} | ||
|
|
||
| if task_id := cache.get(status_key): | ||
| task = AsyncResult(id=task_id) | ||
| else: | ||
| task = render_markdown_to_html.delay(markdown=markdown, cache_key=cache_key) | ||
| cache.set(status_key, task.id, timeout=300) | ||
|
|
||
| try: | ||
| result = task.get(timeout=5) | ||
| cache.delete(status_key) | ||
| return {"html": result} | ||
Roffenlund marked this conversation as resolved.
Show resolved
Hide resolved
Roffenlund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| except TimeoutError: | ||
| cache.delete(status_key) | ||
| raise TimeoutError("Markdown rendering task timed out.") | ||
| except Exception as error: | ||
| cache.delete(status_key) | ||
| raise error | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from celery import shared_task | ||
|
|
||
| from thunderstore.cache.utils import get_cache | ||
| from thunderstore.core.settings import CeleryQueues | ||
| from thunderstore.markdown.templatetags.markdownify import render_markdown | ||
|
|
||
| cache = get_cache("markdown_render") | ||
|
|
||
|
|
||
| @shared_task( | ||
| queue=CeleryQueues.BackgroundMarkdownRender, | ||
| name="thunderstore.repository.tasks.render_markdown", | ||
| ) | ||
| def render_markdown_to_html( | ||
| markdown: str, | ||
| cache_key: str, | ||
| ) -> str: | ||
| cached_html = cache.get(cache_key) | ||
| if cached_html is not None: | ||
| return cached_html | ||
|
|
||
| try: | ||
| html = render_markdown(markdown) | ||
| except Exception as error: | ||
| raise error | ||
|
|
||
| cache.set(cache_key, html) | ||
| return html | ||
36 changes: 36 additions & 0 deletions
36
django/thunderstore/repository/tasks/tests/test_markdown.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from thunderstore.cache.utils import get_cache | ||
| from thunderstore.repository.tasks.markdown import render_markdown_to_html | ||
|
|
||
| RENDER_MARKDOWN_PATH = "thunderstore.markdown.templatetags.markdownify.render_markdown" | ||
|
|
||
|
|
||
| def test_markdown_cache_hit_returns_html(): | ||
| cache = get_cache("markdown_render") | ||
| cache_key = "rendered_html:test_key:1" | ||
| cache.set(cache_key, "<p>Cached HTML</p>") | ||
|
|
||
| result = render_markdown_to_html("", cache_key) | ||
| assert result == "<p>Cached HTML</p>" | ||
|
|
||
|
|
||
| def test_markdown_cache_miss_triggers_rendering_task(): | ||
| cache = get_cache("markdown_render") | ||
| cache_key = "rendered_html:test_key:1" | ||
|
|
||
| result = render_markdown_to_html("test markdown", cache_key) | ||
| assert result == "<p>test markdown</p>\n" | ||
| assert cache.get(cache_key) == "<p>test markdown</p>\n" | ||
|
|
||
|
|
||
| def test_markdown_rendering_exception(): | ||
| cache = get_cache("markdown_render") | ||
| cache_key = "rendered_html:test_key:1" | ||
|
|
||
| with pytest.raises(Exception): | ||
| render_markdown_to_html(None, cache_key) | ||
|
|
||
| assert cache.get(cache_key) is None |
63 changes: 63 additions & 0 deletions
63
django/thunderstore/repository/tests/test_markdown_service.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from thunderstore.cache.utils import get_cache | ||
| from thunderstore.repository.services.markdown import render_markdown_service | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| @pytest.mark.parametrize("markdown, expected_html", [("", ""), (" ", "")]) | ||
| def test_render_markdown_empty_input(markdown, expected_html): | ||
| result = render_markdown_service(markdown, "changelog", 1) | ||
| assert result == {"html": expected_html} | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_render_markdown_cached_html(package_version): | ||
| cache = get_cache("markdown_render") | ||
| cache_key = f"rendered_html:changelog:{package_version.id}" | ||
| cache.set(cache_key, "<p>Cached HTML</p>") | ||
|
|
||
| result = render_markdown_service( | ||
| package_version.changelog, "changelog", package_version.id | ||
| ) | ||
| assert result == {"html": "<p>Cached HTML</p>"} | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_render_markdown_no_cached_html(package_version): | ||
| cache = get_cache("markdown_render") | ||
| cache_key = f"rendered_html:changelog:{package_version.id}" | ||
| cache.delete(cache_key) | ||
|
|
||
| result = render_markdown_service( | ||
| package_version.changelog, "changelog", package_version.id | ||
| ) | ||
| assert result == {"html": "<h1>This is an example changelog</h1>\n"} | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| @pytest.mark.parametrize( | ||
| "side_effect,expected_exception,exception_message", | ||
| [ | ||
| (TimeoutError, TimeoutError, "Markdown rendering task timed out."), | ||
| (Exception, Exception, None), | ||
| ], | ||
| ) | ||
| def test_render_markdown_exceptions( | ||
| package_version, side_effect, expected_exception, exception_message | ||
| ): | ||
| cache = get_cache("markdown_render") | ||
| status_key = f"rendering_status:changelog:{package_version.id}" | ||
| task_id = "mock-task-id" | ||
| cache.set(status_key, task_id) | ||
|
|
||
| get_path = "celery.result.AsyncResult.get" | ||
| id_path = "celery.result.AsyncResult.id" | ||
| with patch(get_path, side_effect=side_effect), patch(id_path, return_value=task_id): | ||
| with pytest.raises(expected_exception, match=exception_message): | ||
| render_markdown_service( | ||
| package_version.changelog, "changelog", package_version.id | ||
| ) | ||
| assert cache.get(status_key) is None |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.