-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: fail gracefully when subscription a query takes too long #39132
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
Merged
+311
−11
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4fedb99
add more verbose logging for easier debugging
andyzzhao 5cf3f41
fix: fail gracefully when subscription exports take too long
andyzzhao 6c6e070
don't filter and mock export_asset
andyzzhao 1e893c0
add url to warn log
andyzzhao 26cf713
move to separete method
andyzzhao 1aaa5fb
Merge branch 'master' into andyzzhao/fix-subscriptions
andyzzhao f224d32
update start_to_close
andyzzhao 9693c68
tag export asset query with asset id
andyzzhao 47b6d40
Merge branch 'master' into andyzzhao/fix-subscriptions
andyzzhao cb4f3a4
move tests and remove tag
andyzzhao 60f7ea5
Update query snapshots
github-actions[bot] 8dfa791
Update E2E screenshots for Playwright
github-actions[bot] feed25a
tag export with asset id
andyzzhao 6948f3c
Merge branch 'andyzzhao/fix-subscriptions' of https://github.com/Post…
andyzzhao 857e2dc
Update E2E screenshots for Playwright
github-actions[bot] 65d1e49
fix query tagging
andyzzhao 9316b02
Merge branch 'andyzzhao/fix-subscriptions' of https://github.com/Post…
andyzzhao 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import pytest | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest_asyncio | ||
from asgiref.sync import sync_to_async | ||
|
@@ -18,6 +19,18 @@ | |
pytestmark = [pytest.mark.asyncio, pytest.mark.django_db(transaction=True)] | ||
|
||
|
||
@pytest_asyncio.fixture(autouse=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mocking this because
we already have tests for |
||
async def mock_export_asset(): | ||
"""Mock export_asset_direct to avoid launching Chrome browser in tests.""" | ||
|
||
def set_content(asset: ExportedAsset) -> None: | ||
asset.content = b"fake image data" | ||
asset.save() | ||
|
||
with patch("ee.tasks.subscriptions.subscription_utils.exporter.export_asset_direct", side_effect=set_content): | ||
yield | ||
|
||
|
||
@pytest_asyncio.fixture | ||
async def organization(): | ||
"""Create a test organization.""" | ||
|
@@ -200,3 +213,100 @@ async def test_async_foreign_key_access_with_real_subscription(team, user, dashb | |
pytest.fail( | ||
"deliver_subscription_report_async raised SynchronousOnlyOperation - foreign key access not properly handled in async context" | ||
) | ||
|
||
|
||
@patch("ee.tasks.subscriptions.subscription_utils.exporter.export_asset_direct") | ||
async def test_async_generate_assets_basic(mock_export: MagicMock, team, user) -> None: | ||
def export_success(asset: ExportedAsset) -> None: | ||
asset.content = b"fake image data" | ||
asset.save() | ||
|
||
mock_export.side_effect = export_success | ||
|
||
dashboard = await sync_to_async(Dashboard.objects.create)(team=team, name="test dashboard", created_by=user) | ||
|
||
for i in range(3): | ||
insight = await sync_to_async(Insight.objects.create)(team=team, short_id=f"insight-{i}", name=f"Insight {i}") | ||
await sync_to_async(DashboardTile.objects.create)(dashboard=dashboard, insight=insight) | ||
|
||
subscription = await sync_to_async(create_subscription)(team=team, dashboard=dashboard, created_by=user) | ||
|
||
subscription = await sync_to_async( | ||
lambda: type(subscription) | ||
.objects.select_related("team", "dashboard", "insight", "created_by") | ||
.get(id=subscription.id) | ||
)() | ||
|
||
insights, assets = await generate_assets_async(subscription) | ||
|
||
assert len(insights) == 3 | ||
assert len(assets) == 3 | ||
assert mock_export.call_count == 3 | ||
assert all(asset.content for asset in assets) | ||
|
||
|
||
@patch("ee.tasks.subscriptions.subscription_utils.asyncio.wait_for") | ||
@patch("posthog.tasks.exporter.export_asset_direct") | ||
async def test_async_generate_assets_timeout_continues_with_partial_results( | ||
mock_export: MagicMock, mock_wait_for: MagicMock, team, user | ||
) -> None: | ||
mock_export.return_value = None | ||
mock_wait_for.side_effect = TimeoutError() | ||
|
||
dashboard = await sync_to_async(Dashboard.objects.create)(team=team, name="test dashboard", created_by=user) | ||
|
||
for i in range(3): | ||
insight = await sync_to_async(Insight.objects.create)(team=team, short_id=f"insight-{i}", name=f"Insight {i}") | ||
await sync_to_async(DashboardTile.objects.create)(dashboard=dashboard, insight=insight) | ||
|
||
subscription = await sync_to_async(create_subscription)(team=team, dashboard=dashboard, created_by=user) | ||
|
||
subscription = await sync_to_async( | ||
lambda: type(subscription) | ||
.objects.select_related("team", "dashboard", "insight", "created_by") | ||
.get(id=subscription.id) | ||
)() | ||
|
||
insights, assets = await generate_assets_async(subscription) | ||
|
||
assert len(insights) == 3 | ||
assert len(assets) == 3 | ||
assert all(asset.content is None and asset.content_location is None for asset in assets) | ||
assert mock_wait_for.called | ||
|
||
|
||
@patch("posthog.tasks.exporter.export_asset_direct") | ||
async def test_async_generate_assets_partial_success(mock_export: MagicMock, team, user) -> None: | ||
call_count = 0 | ||
|
||
def export_with_partial_success(asset: ExportedAsset) -> None: | ||
nonlocal call_count | ||
call_count += 1 | ||
if call_count <= 2: | ||
asset.content = b"fake image data" | ||
asset.save() | ||
|
||
mock_export.side_effect = export_with_partial_success | ||
|
||
dashboard = await sync_to_async(Dashboard.objects.create)(team=team, name="test dashboard", created_by=user) | ||
|
||
for i in range(3): | ||
insight = await sync_to_async(Insight.objects.create)(team=team, short_id=f"insight-{i}", name=f"Insight {i}") | ||
await sync_to_async(DashboardTile.objects.create)(dashboard=dashboard, insight=insight) | ||
|
||
subscription = await sync_to_async(create_subscription)(team=team, dashboard=dashboard, created_by=user) | ||
|
||
subscription = await sync_to_async( | ||
lambda: type(subscription) | ||
.objects.select_related("team", "dashboard", "insight", "created_by") | ||
.get(id=subscription.id) | ||
)() | ||
|
||
insights, assets = await generate_assets_async(subscription) | ||
|
||
assert len(insights) == 3 | ||
assert len(assets) == 3 | ||
assets_with_content = [a for a in assets if a.content or a.content_location] | ||
assets_without_content = [a for a in assets if not a.content and not a.content_location] | ||
assert len(assets_with_content) == 2 | ||
assert len(assets_without_content) == 1 |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this supposed to cover?
It looks like this is used for
deliver_subscription_report_activity
which has to wait to generate all the assets?In the worst case, what happens if one of those asset generations takes the full 10 minutes and fails and has to be retried?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct
If one asset generation takes the full time, we've configured temporal to retry 2 more times. However, on each retry, we regenerate all the assets (potentially up to 6) again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool yeah, I guess we don't want to do that eh? Maybe better to use temporal for the asset generation workflows too so we can run them more independently. Sgtm for now.