-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data-warehouse): Add API for cancelling a batch export run (#25514)
Co-authored-by: Tomás Farías Santana <[email protected]>
- Loading branch information
1 parent
7ee4028
commit 61d64b5
Showing
5 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,12 +1,19 @@ | ||
import asyncio | ||
|
||
import pytest | ||
import temporalio.client | ||
from asgiref.sync import async_to_sync | ||
from django.test.client import Client as HttpClient | ||
from rest_framework import status | ||
|
||
from posthog.api.test.batch_exports.conftest import start_test_worker | ||
from posthog.api.test.batch_exports.operations import ( | ||
backfill_batch_export_ok, | ||
cancel_batch_export_run_ok, | ||
create_batch_export_ok, | ||
get_batch_export, | ||
get_batch_export_runs, | ||
get_batch_export_runs_ok, | ||
) | ||
from posthog.api.test.test_organization import create_organization | ||
from posthog.api.test.test_team import create_team | ||
|
@@ -143,3 +150,82 @@ def test_batch_exports_are_partitioned_by_team(client: HttpClient): | |
|
||
response = get_batch_export(client, team.pk, batch_export["id"]) | ||
assert response.status_code == status.HTTP_404_NOT_FOUND, response.json() | ||
|
||
|
||
# TODO - this was in test_delete.py too so maybe extract it out into operations.py? | ||
@async_to_sync | ||
async def wait_for_workflow_executions( | ||
temporal: temporalio.client.Client, query: str, timeout: int = 30, sleep: int = 1 | ||
): | ||
"""Wait for Workflow Executions matching query.""" | ||
workflows = [workflow async for workflow in temporal.list_workflows(query=query)] | ||
|
||
total = 0 | ||
while not workflows: | ||
total += sleep | ||
|
||
if total > timeout: | ||
raise TimeoutError(f"No backfill Workflow Executions after {timeout} seconds") | ||
|
||
await asyncio.sleep(sleep) | ||
workflows = [workflow async for workflow in temporal.list_workflows(query=query)] | ||
|
||
return workflows | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
def test_cancelling_a_batch_export_run(client: HttpClient): | ||
"""Test cancelling a BatchExportRun.""" | ||
temporal = sync_connect() | ||
|
||
destination_data = { | ||
"type": "S3", | ||
"config": { | ||
"bucket_name": "my-production-s3-bucket", | ||
"region": "us-east-1", | ||
"prefix": "posthog-events/", | ||
"aws_access_key_id": "abc123", | ||
"aws_secret_access_key": "secret", | ||
}, | ||
} | ||
batch_export_data = { | ||
"name": "my-production-s3-bucket-destination", | ||
"destination": destination_data, | ||
"interval": "hour", | ||
} | ||
|
||
organization = create_organization("Test Org") | ||
team = create_team(organization) | ||
user = create_user("[email protected]", "Test User", organization) | ||
client.force_login(user) | ||
|
||
with start_test_worker(temporal): | ||
batch_export = create_batch_export_ok( | ||
client, | ||
team.pk, | ||
batch_export_data, | ||
) | ||
batch_export_id = batch_export["id"] | ||
|
||
start_at = "2023-10-23T00:00:00+00:00" | ||
end_at = "2023-10-24T00:00:00+00:00" | ||
backfill_batch_export_ok(client, team.pk, batch_export_id, start_at, end_at) | ||
|
||
# In order for a run to be cancelable we need a running workflow execution | ||
_ = wait_for_workflow_executions(temporal, query=f'TemporalScheduledById="{batch_export_id}"') | ||
|
||
data = get_batch_export_runs_ok(client, team.pk, batch_export_id) | ||
assert len(data["results"]) == 1 | ||
run = data["results"][0] | ||
assert run["status"] == "Running" | ||
|
||
data = cancel_batch_export_run_ok(client, team.pk, batch_export_id, run["id"]) | ||
assert data["cancelled"] is True | ||
|
||
data = get_batch_export_runs_ok(client, team.pk, batch_export_id) | ||
assert len(data["results"]) == 1 | ||
run = data["results"][0] | ||
assert run["status"] == "Cancelled" | ||
|
||
|
||
# TODO - add a test to ensure we can't cancel a completed run? |
This file contains 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 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 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