diff --git a/posthog/apps.py b/posthog/apps.py index 480502e353436..a818b04bd08b8 100644 --- a/posthog/apps.py +++ b/posthog/apps.py @@ -49,7 +49,7 @@ def ready(self): posthoganalytics.disabled = True # load feature flag definitions if not already loaded - if posthoganalytics.feature_flag_definitions() is None: + if not posthoganalytics.disabled and posthoganalytics.feature_flag_definitions() is None: posthoganalytics.default_client.load_feature_flags() if not settings.SKIP_SERVICE_VERSION_REQUIREMENTS: diff --git a/posthog/celery.py b/posthog/celery.py index 1302fe515f021..8bef171f35432 100644 --- a/posthog/celery.py +++ b/posthog/celery.py @@ -19,7 +19,6 @@ from posthog.metrics import pushed_metrics_registry from posthog.redis import get_client from posthog.utils import get_crontab, get_instance_region -from posthoganalytics import Posthog # set the default Django settings module for the 'celery' program. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "posthog.settings") @@ -758,6 +757,7 @@ def calculate_decide_usage() -> None: from posthog.models.feature_flag.flag_analytics import capture_team_decide_usage from posthog.models import Team from django.db.models import Q + from posthoganalytics import Posthog # send EU data to EU, US data to US api_key = "phc_dZ4GK1LRjhB97XozMSkEwPXx7OVANaJEwLErkY1phUF" if get_instance_region() == "EU" else "sTMFPsFhdP1Ssg" @@ -769,7 +769,7 @@ def calculate_decide_usage() -> None: ): capture_team_decide_usage(ph_client, team.id, team.uuid) - ph_client.flush() + ph_client.shutdown() @app.task(ignore_result=True) diff --git a/posthog/models/feature_flag/flag_analytics.py b/posthog/models/feature_flag/flag_analytics.py index 4d5da89540001..a86117cbfb9ea 100644 --- a/posthog/models/feature_flag/flag_analytics.py +++ b/posthog/models/feature_flag/flag_analytics.py @@ -1,8 +1,11 @@ +from typing import TYPE_CHECKING from posthog.redis import redis, get_client import time -from posthoganalytics import Posthog from sentry_sdk import capture_exception +if TYPE_CHECKING: + from posthoganalytics import Posthog + REDIS_LOCK_TOKEN = "posthog:decide_analytics:lock" CACHE_BUCKET_SIZE = 60 * 2 # duration in seconds @@ -21,7 +24,7 @@ def increment_request_count(team_id: int, count: int = 1) -> None: capture_exception(error) -def capture_team_decide_usage(ph_client: Posthog, team_id: int, team_uuid: str) -> None: +def capture_team_decide_usage(ph_client: "Posthog", team_id: int, team_uuid: str) -> None: try: client = get_client() total_request_count = 0