-
Notifications
You must be signed in to change notification settings - Fork 568
feat(Segment Membership): Seed organisations too large for the task processor #8517
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
matthewelwell
wants to merge
1
commit into
main
Choose a base branch
from
feat/seed-segment-membership-command
base: main
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.
+150
−11
Open
Changes from all commits
Commits
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
Empty file.
Empty file.
50 changes: 50 additions & 0 deletions
50
api/segment_membership/management/commands/seed_segment_membership.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,50 @@ | ||
| """Run an organisation's identity seed out of band. | ||
|
|
||
| The task processor is a poor host for an organisation large enough that the | ||
| seed outlives its task timeout: the run is marked failed while its thread keeps | ||
| going, retried alongside the thread that is still running, and re-enqueued | ||
| hourly once the retries are exhausted. Calling the task directly sidesteps all | ||
| of that, so the seed can be run as a standalone ECS task that no deployment | ||
| interrupts and nothing retries. | ||
|
|
||
| flagsmith seed_segment_membership <organisation_id> [--ignore-feature-flag] | ||
|
|
||
| Base the ECS task definition on `flagsmith-task-processor`: it carries the | ||
| ClickHouse URL, the Dynamo table names, and the Flagsmith-on-Flagsmith server | ||
| key that the feature flag check needs. | ||
| """ | ||
|
|
||
| from argparse import ArgumentParser | ||
| from typing import Any | ||
|
|
||
| from django.core.management import BaseCommand | ||
|
|
||
| from segment_membership.tasks import seed_organisation_identities | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Mirror an organisation's Dynamo identities into ClickHouse." | ||
|
|
||
| def add_arguments(self, parser: ArgumentParser) -> None: | ||
| parser.add_argument("organisation_id", type=int) | ||
| parser.add_argument( | ||
| "--ignore-feature-flag", | ||
| action="store_true", | ||
| help=( | ||
| "Seed even though segment_membership_inspection is off for the " | ||
| "organisation, so ClickHouse can be populated before the " | ||
| "feature is exposed to it." | ||
| ), | ||
| ) | ||
|
|
||
| def handle( | ||
| self, | ||
| *args: Any, | ||
| organisation_id: int, | ||
| ignore_feature_flag: bool, | ||
| **options: Any, | ||
| ) -> None: | ||
| seed_organisation_identities( | ||
| organisation_id, | ||
| ignore_feature_flag=ignore_feature_flag, | ||
| ) |
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
60 changes: 60 additions & 0 deletions
60
api/tests/unit/segment_membership/test_unit_segment_membership_commands.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,60 @@ | ||
| from io import StringIO | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from django.core.management import call_command | ||
| from mypy_boto3_dynamodb.service_resource import Table | ||
| from pytest_django.fixtures import SettingsWrapper | ||
| from pytest_mock import MockerFixture | ||
|
|
||
| from projects.models import Project | ||
| from segment_membership.models import SegmentMembershipSeed | ||
| from segments.models import Segment | ||
|
|
||
| COMMAND = "seed_segment_membership" | ||
|
|
||
|
|
||
| def test_seed_segment_membership__organisation_id__runs_the_seed_inline( | ||
| mocker: MockerFixture, | ||
| project: Project, | ||
| ) -> None: | ||
| # Given | ||
| seed = mocker.patch( | ||
| "segment_membership.management.commands." | ||
| "seed_segment_membership.seed_organisation_identities" | ||
| ) | ||
|
|
||
| # When | ||
| call_command(COMMAND, project.organisation_id, stdout=StringIO()) | ||
|
|
||
| # Then the task runs in this process rather than being queued, so no | ||
| # task timeout applies and nothing retries it | ||
| seed.assert_called_once_with(project.organisation_id, ignore_feature_flag=False) | ||
| seed.delay.assert_not_called() | ||
|
|
||
|
|
||
| def test_seed_segment_membership__ignore_feature_flag__seeds_with_flag_off( | ||
| mocker: MockerFixture, | ||
| settings: SettingsWrapper, | ||
| project: Project, | ||
| segment: Segment, | ||
| flagsmith_identities_table: Table, | ||
| ) -> None: | ||
| # Given the organisation has not been let into the beta yet | ||
| settings.CLICKHOUSE_ENABLED = True | ||
| cursor = MagicMock() | ||
| open_cursor = mocker.patch("segment_membership.tasks.open_clickhouse_cursor") | ||
| open_cursor.return_value.__enter__.return_value = cursor | ||
| mocker.patch("segment_membership.tasks.enqueue_membership_refresh") | ||
|
|
||
| # When | ||
| call_command( | ||
| COMMAND, | ||
| project.organisation_id, | ||
| "--ignore-feature-flag", | ||
| stdout=StringIO(), | ||
| ) | ||
|
|
||
| # Then the seed runs to completion regardless | ||
| assert SegmentMembershipSeed.objects.filter( | ||
| organisation=project.organisation, seeded_at__isnull=False | ||
| ).exists() |
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
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.