diff --git a/src/sentry/demo_mode/tasks.py b/src/sentry/demo_mode/tasks.py index 3b9546827a5e81..ec1fe5d2d937ca 100644 --- a/src/sentry/demo_mode/tasks.py +++ b/src/sentry/demo_mode/tasks.py @@ -1,4 +1,4 @@ -from datetime import timedelta +from datetime import datetime, timedelta import sentry_sdk from django.db import router @@ -31,29 +31,30 @@ def sync_debug_artifacts(): if ( - not options.get("sentry.demo_mode.sync_artifact_bundles.enable") + not options.get("sentry.demo_mode.sync_debug_artifacts.enable") or not is_demo_mode_enabled() ): return - source_org_id = options.get("sentry.demo_mode.sync_artifact_bundles.source_org_id") + source_org_id = options.get("sentry.demo_mode.sync_debug_artifacts.source_org_id") source_org = Organization.objects.get(id=source_org_id) target_org = get_demo_org() - lookback_days = options.get("sentry.demo_mode.sync_artifact_bundles.lookback_days") + lookback_days = options.get("sentry.demo_mode.sync_debug_artifacts.lookback_days") + cutoff_date = timezone.now() - timedelta(days=lookback_days) - _sync_artifact_bundles(source_org, target_org, lookback_days) - _sync_project_debug_files(source_org, target_org, lookback_days) - _sync_proguard_artifact_releases(source_org, target_org, lookback_days) + _sync_artifact_bundles(source_org, target_org, cutoff_date) + _sync_project_debug_files(source_org, target_org, cutoff_date) + _sync_proguard_artifact_releases(source_org, target_org, cutoff_date) -def _sync_artifact_bundles(source_org: Organization, target_org: Organization, lookback_days=1): +def _sync_artifact_bundles( + source_org: Organization, target_org: Organization, cutoff_date: datetime +): if not source_org or not target_org: return - cutoff_date = timezone.now() - timedelta(days=lookback_days) - artifact_bundles = ArtifactBundle.objects.filter( Q(organization_id=source_org.id) | Q(organization_id=target_org.id), date_uploaded__gte=cutoff_date, @@ -70,23 +71,34 @@ def _sync_artifact_bundles(source_org: Organization, target_org: Organization, l _sync_artifact_bundle(source_artifact_bundle, target_org) -def _sync_project_debug_files(source_org: Organization, target_org: Organization, lookback_days=1): +def _sync_project_debug_files( + source_org: Organization, target_org: Organization, cutoff_date: datetime +): if not source_org or not target_org: return - source_project_ids = Project.objects.filter( - organization_id=source_org.id, - ).values_list("id", flat=True) + source_project_ids = list( + Project.objects.filter( + organization_id=source_org.id, + ).values_list("id", flat=True) + ) - target_project_ids = Project.objects.filter( - organization_id=target_org.id, - ).values_list("id", flat=True) + target_project_ids = list( + Project.objects.filter( + organization_id=target_org.id, + ).values_list("id", flat=True) + ) + + project_debug_files = ProjectDebugFile.objects.filter( + Q(project_id__in=source_project_ids) | Q(project_id__in=target_project_ids), + date_accessed__gte=cutoff_date, + ) - source_project_debug_files = ProjectDebugFile.objects.filter( + source_project_debug_files = project_debug_files.filter( project_id__in=source_project_ids, ) - target_project_debug_files = ProjectDebugFile.objects.filter( + target_project_debug_files = project_debug_files.filter( project_id__in=target_project_ids, ) @@ -99,13 +111,11 @@ def _sync_project_debug_files(source_org: Organization, target_org: Organization def _sync_proguard_artifact_releases( - source_org: Organization, target_org: Organization, lookback_days=1 + source_org: Organization, target_org: Organization, cutoff_date: datetime ): if not source_org or not target_org: return - cutoff_date = timezone.now() - timedelta(days=lookback_days) - proguard_artifact_releases = ProguardArtifactRelease.objects.filter( Q(organization_id=source_org.id) | Q(organization_id=target_org.id), date_added__gte=cutoff_date, diff --git a/src/sentry/options/defaults.py b/src/sentry/options/defaults.py index 57bc5d6e0e910f..4d5e74cb1e5af9 100644 --- a/src/sentry/options/defaults.py +++ b/src/sentry/options/defaults.py @@ -3140,19 +3140,19 @@ ) register( - "sentry.demo_mode.sync_artifact_bundles.enable", + "sentry.demo_mode.sync_debug_artifacts.enable", type=Bool, default=False, flags=FLAG_PRIORITIZE_DISK | FLAG_AUTOMATOR_MODIFIABLE, ) register( - "sentry.demo_mode.sync_artifact_bundles.source_org_id", + "sentry.demo_mode.sync_debug_artifacts.source_org_id", type=Int, flags=FLAG_PRIORITIZE_DISK | FLAG_AUTOMATOR_MODIFIABLE, ) register( - "sentry.demo_mode.sync_artifact_bundles.lookback_days", - default=1, + "sentry.demo_mode.sync_debug_artifacts.lookback_days", + default=3, flags=FLAG_PRIORITIZE_DISK | FLAG_AUTOMATOR_MODIFIABLE, ) diff --git a/tests/sentry/demo_mode/test_tasks.py b/tests/sentry/demo_mode/test_tasks.py index 6ea8a4d4c99e06..9cbece5bb6bfae 100644 --- a/tests/sentry/demo_mode/test_tasks.py +++ b/tests/sentry/demo_mode/test_tasks.py @@ -75,9 +75,16 @@ def set_up_proguard_artifact_release( ) return proguard_artifact_release + def last_three_days(self): + return timezone.now() - timedelta(days=3) + def test_sync_artifact_bundles_no_bundles(self): - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) assert not ArtifactBundle.objects.all().exists() @@ -88,7 +95,11 @@ def test_sync_artifact_bundles_with_differences(self): assert not ArtifactBundle.objects.filter(organization_id=self.target_org.id).exists() - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) target_artifact_bundles = ArtifactBundle.objects.get(organization_id=self.target_org.id) @@ -98,7 +109,11 @@ def test_sync_artifact_bundles_does_not_touch_other_orgs(self): self.set_up_artifact_bundle(self.source_org, self.source_proj_foo) self.set_up_artifact_bundle(self.unrelated_org, self.unrelated_proj_foo) - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) unrelated_artifact_bundles = ArtifactBundle.objects.filter( organization_id=self.unrelated_org.id @@ -113,7 +128,11 @@ def test_sync_artifact_bundles_with_old_uploads(self): assert not ArtifactBundle.objects.filter(organization_id=self.target_org.id).exists() - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=timezone.now() - timedelta(days=1), + ) assert not ArtifactBundle.objects.filter(organization_id=self.target_org.id).exists() @@ -122,9 +141,21 @@ def test_sync_artifact_bundles_only_once(self): self.source_org, self.source_proj_foo ) - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) target_artifact_bundles = ArtifactBundle.objects.filter(organization_id=self.target_org.id) @@ -134,7 +165,11 @@ def test_sync_artifact_bundles_only_once(self): def test_sync_project_artifact_bundles(self): self.set_up_artifact_bundle(self.source_org, self.source_proj_foo) - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) target_project_artifact_bundle = ProjectArtifactBundle.objects.get( organization_id=self.target_org.id, @@ -149,7 +184,11 @@ def test_sync_release_artifact_bundles(self): self.source_org, self.source_proj_foo ) - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) target_release_bundle = ReleaseArtifactBundle.objects.get( organization_id=self.target_org.id, @@ -163,7 +202,11 @@ def test_sync_release_artifact_bundles(self): def test_sync_artifact_bundles_rolls_back_on_error(self, _): self.set_up_artifact_bundle(self.source_org, self.source_proj_foo) - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) assert not ArtifactBundle.objects.filter(organization_id=self.target_org.id).exists() assert not ProjectArtifactBundle.objects.filter(organization_id=self.target_org.id).exists() @@ -177,7 +220,11 @@ def test_sync_project_debug_files(self): debug_id=source_project_debug_file.debug_id, ).exists() - _sync_project_debug_files(source_org=self.source_org, target_org=self.target_org) + _sync_project_debug_files( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) target_project_debug_file = ProjectDebugFile.objects.get( project_id=self.target_proj_foo.id, @@ -199,7 +246,11 @@ def test_sync_project_debug_files_with_old_uploads(self): debug_id=source_project_debug_file.debug_id, ).exists() - _sync_project_debug_files(source_org=self.source_org, target_org=self.target_org) + _sync_project_debug_files( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) assert ProjectDebugFile.objects.filter( project_id=self.target_proj_foo.id, @@ -217,7 +268,11 @@ def test_sync_proguard_artifact_releases(self): proguard_uuid=source_proguard_artifact_release.proguard_uuid, ).exists() - _sync_proguard_artifact_releases(source_org=self.source_org, target_org=self.target_org) + _sync_proguard_artifact_releases( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) target_proguard_artifact_release = ProguardArtifactRelease.objects.get( organization_id=self.target_org.id, @@ -246,7 +301,11 @@ def test_sync_proguard_artifact_releases_with_old_uploads(self): proguard_uuid=source_proguard_artifact_release.proguard_uuid, ).exists() - _sync_artifact_bundles(source_org=self.source_org, target_org=self.target_org) + _sync_artifact_bundles( + source_org=self.source_org, + target_org=self.target_org, + cutoff_date=self.last_three_days(), + ) assert not ProguardArtifactRelease.objects.filter( organization_id=self.target_org.id,