:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(tasks): remove flag checks from workflow (#57731)
Joshua Snyder2dfd5f4a62a1
diff --git a/products/tasks/backend/temporal/client.py b/products/tasks/backend/temporal/client.pyindex 30aa837ad247..ef32b98dca93 100644--- a/products/tasks/backend/temporal/client.py+++ b/products/tasks/backend/temporal/client.py@@ -7,12 +7,10 @@ from django.conf import settingsfrom django.db import transactionfrom django.utils import timezone as django_timezone-import posthoganalyticsfrom asgiref.sync import sync_to_asyncfrom temporalio.common import RetryPolicy, WorkflowIDReusePolicyfrom posthog.models.team.team import Team-from posthog.models.user import Userfrom posthog.temporal.common.client import async_connect, sync_connectfrom posthog.temporal.oauth import PosthogMcpScopes@@ -103,9 +101,6 @@ async def execute_task_processing_workflow_async("""Start the task processing workflow asynchronously. Fire-and-forget.Use this from async contexts (e.g., within Temporal activities).-- Args:- skip_user_check: If True, skip user-based feature flag check. Use for automated/system tasks."""logger.info("execute_task_processing_workflow_async_called",@@ -114,51 +109,7 @@ async def execute_task_processing_workflow_async(task_run_for_metrics = await _aget_task_run_for_metrics(run_id)observe_task_run_workflow_start(task_run_for_metrics, outcome="attempted", reason="requested")try:- team = await Team.objects.select_related("organization").aget(id=team_id)-- if skip_user_check:- logger.info("task_processing_skip_user_check", extra={"task_id": task_id, "team_id": team_id})- tasks_enabled = posthoganalytics.feature_enabled(- "tasks",- f"team_{team_id}",- groups={"organization": str(team.organization_id)},- group_properties={"organization": {"id": str(team.organization_id)}},- only_evaluate_locally=False,- send_feature_flag_events=False,- )- else:- if not user_id:- logger.warning("task_processing_missing_user_id", extra={"task_id": task_id})- observe_task_run_workflow_start(task_run_for_metrics, outcome="blocked", reason="missing_user")- await _terminalize_unstarted_task_run_async(run_id, "Failed to start task workflow: missing user id")- return-- logger.info("task_processing_fetching_team_and_user", extra={"team_id": team_id, "user_id": user_id})- user = await User.objects.aget(id=user_id)-- logger.info(- "task_processing_checking_feature_flag",- extra={"distinct_id": user.distinct_id, "organization_id": team.organization_id},- )- tasks_enabled = posthoganalytics.feature_enabled(- "tasks",- user.distinct_id,- groups={"organization": str(team.organization_id)},- group_properties={"organization": {"id": str(team.organization_id)}},- only_evaluate_locally=False,- send_feature_flag_events=False,- )-- logger.info("task_processing_feature_flag_result", extra={"task_id": task_id, "tasks_enabled": tasks_enabled})-- if not tasks_enabled:- logger.warning("task_processing_blocked_feature_flag", extra={"task_id": task_id})- observe_task_run_workflow_start(task_run_for_metrics, outcome="blocked", reason="feature_flag")- await _terminalize_unstarted_task_run_async(- run_id,- "Failed to start task workflow: tasks feature is disabled",- )- return+ await Team.objects.select_related("organization").aget(id=team_id)workflow_id = TaskRun.get_workflow_id(task_id, run_id)slack_context_dict = _normalize_slack_context(slack_thread_context)@@ -188,7 +139,7 @@ async def execute_task_processing_workflow_async(logger.info("task_processing_workflow_started", extra={"task_id": task_id, "run_id": run_id})observe_task_run_workflow_start(task_run_for_metrics, outcome="started", reason="accepted")- except (Team.DoesNotExist, User.DoesNotExist) as e:+ except Team.DoesNotExist as e:observe_task_run_workflow_start(task_run_for_metrics, outcome="failed", reason="permission_validation")logger.exception("task_processing_permission_validation_failed",@@ -223,9 +174,6 @@ def execute_task_processing_workflow("""Start the task processing workflow synchronously. Fire-and-forget.Use this from sync contexts (e.g., API endpoints).-- Args:- skip_user_check: If True, skip user-based feature flag check. Use for automated/system tasks."""task_run_for_metrics = _get_task_run_for_metrics(run_id)observe_task_run_workflow_start(task_run_for_metrics, outcome="attempted", reason="requested")@@ -235,49 +183,7 @@ def execute_task_processing_workflow(extra={"task_id": task_id, "run_id": run_id, "team_id": team_id, "user_id": user_id},)- team = Team.objects.get(id=team_id)-- if settings.DEBUG:- logger.info("task_processing_debug_skip_feature_flag", extra={"task_id": task_id})- tasks_enabled = True- elif skip_user_check:- logger.info("task_processing_skip_user_check", extra={"task_id": task_id, "team_id": team_id})- tasks_enabled = posthoganalytics.feature_enabled(- "tasks",- f"team_{team_id}",- groups={"organization": str(team.organization.id)},- group_properties={"organization": {"id": str(team.organization.id)}},- only_evaluate_locally=False,- send_feature_flag_events=False,- )- else:- if not user_id:- logger.warning("task_processing_missing_user_id", extra={"task_id": task_id})- observe_task_run_workflow_start(task_run_for_metrics, outcome="blocked", reason="missing_user")- _terminalize_unstarted_task_run(run_id, "Failed to start task workflow: missing user id")- return-- user = User.objects.get(id=user_id)-- tasks_enabled = posthoganalytics.feature_enabled(- "tasks",- user.distinct_id,- groups={"organization": str(team.organization.id)},- group_properties={"organization": {"id": str(team.organization.id)}},- only_evaluate_locally=False,- send_feature_flag_events=False,- )-- logger.info("task_processing_feature_flag_result", extra={"task_id": task_id, "tasks_enabled": tasks_enabled})-- if not tasks_enabled:- logger.warning("task_processing_blocked_feature_flag", extra={"task_id": task_id})- observe_task_run_workflow_start(task_run_for_metrics, outcome="blocked", reason="feature_flag")- _terminalize_unstarted_task_run(- run_id,- "Failed to start task workflow: tasks feature is disabled",- )- return+ Team.objects.get(id=team_id)workflow_id = TaskRun.get_workflow_id(task_id, run_id)slack_context_dict = _normalize_slack_context(slack_thread_context)@@ -315,7 +221,7 @@ def execute_task_processing_workflow(logger.info("task_processing_workflow_started", extra={"task_id": task_id, "run_id": run_id})observe_task_run_workflow_start(task_run_for_metrics, outcome="started", reason="accepted")- except (Team.DoesNotExist, User.DoesNotExist) as e:+ except Team.DoesNotExist as e:observe_task_run_workflow_start(task_run_for_metrics, outcome="failed", reason="permission_validation")logger.exception("task_processing_permission_validation_failed",diff --git a/products/tasks/backend/tests/test_task_run_metrics.py b/products/tasks/backend/tests/test_task_run_metrics.pyindex 60dbe1a7622d..72633344a744 100644--- a/products/tasks/backend/tests/test_task_run_metrics.py+++ b/products/tasks/backend/tests/test_task_run_metrics.py@@ -1,6 +1,6 @@from unittest.mock import AsyncMock, MagicMock, patch-from django.test import TestCase, override_settings+from django.test import TestCasefrom parameterized import parameterizedfrom prometheus_client import REGISTRY@@ -69,39 +69,17 @@ class TestTaskRunMetrics(TestCase):@parameterized.expand([- ("started", True, "user", True, None, [("attempted", "requested"), ("started", "accepted")], True),- (- "missing_user",- False,- "none",- True,- None,- [("attempted", "requested"), ("blocked", "missing_user")],- False,- ),- (- "feature_flag",- False,- "user",- False,- None,- [("attempted", "requested"), ("blocked", "feature_flag")],- False,- ),+ ("started", "team", None, [("attempted", "requested"), ("started", "accepted")], True),("permission_validation",- False,- "missing",- True,+ "missing_team",None,[("attempted", "requested"), ("failed", "permission_validation")],False,),("temporal_start",- True,- "user",- True,+ "team",RuntimeError("boom"),[("attempted", "requested"), ("failed", "temporal_start")],True,@@ -111,9 +89,7 @@ class TestTaskRunMetrics(TestCase):def test_workflow_start_increments_outcome_counters(self,_name: str,- debug: bool,- user_kind: str,- feature_enabled: bool,+ team_kind: str,sync_connect_side_effect: Exception | None,expected_outcomes: list[tuple[str, str]],expect_sync_connect_called: bool,@@ -132,21 +108,15 @@ class TestTaskRunMetrics(TestCase):before_by_outcome = [_sample_value("posthog_tasks_task_run_workflow_start_total", labels) for labels in labels_by_outcome]- user_id = {- "user": self.user.id,- "none": None,- "missing": self.user.id + 1,- }[user_kind]+ team_id = {+ "team": self.team.id,+ "missing_team": self.team.id + 9999,+ }[team_kind]temporal_client = MagicMock()temporal_client.start_workflow = AsyncMock()- with (- override_settings(DEBUG=debug),- patch("products.tasks.backend.temporal.client.posthoganalytics.feature_enabled") as mock_feature_enabled,- patch("products.tasks.backend.temporal.client.sync_connect") as mock_sync_connect,- ):- mock_feature_enabled.return_value = feature_enabled+ with patch("products.tasks.backend.temporal.client.sync_connect") as mock_sync_connect:if sync_connect_side_effect is not None:mock_sync_connect.side_effect = sync_connect_side_effectelse:@@ -155,8 +125,8 @@ class TestTaskRunMetrics(TestCase):execute_task_processing_workflow(task_id=str(self.task.id),run_id=str(task_run.id),- team_id=self.team.id,- user_id=user_id,+ team_id=team_id,+ user_id=self.user.id,)for labels, before in zip(labels_by_outcome, before_by_outcome, strict=True):diff --git a/products/tasks/backend/tests/test_temporal_client.py b/products/tasks/backend/tests/test_temporal_client.pyindex 1061d25dff3c..2f6980c2de78 100644--- a/products/tasks/backend/tests/test_temporal_client.py+++ b/products/tasks/backend/tests/test_temporal_client.py@@ -55,26 +55,6 @@ class TestExecuteTaskProcessingWorkflow(TransactionTestCase):async_to_sync(execute_task_processing_workflow_async)(**kwargs)- @parameterized.expand([("sync",), ("async",)])- def test_marks_run_failed_when_user_id_is_missing(self, executor: str) -> None:- run = self._create_run()-- self._execute_workflow(executor, run, None)-- self._assert_run_failed(run, "Failed to start task workflow: missing user id")-- @parameterized.expand([("sync",), ("async",)])- def test_marks_run_failed_when_tasks_feature_is_disabled(self, executor: str) -> None:- run = self._create_run()-- with patch(- "products.tasks.backend.temporal.client.posthoganalytics.feature_enabled", return_value=False- ) as mock_feature_enabled:- self._execute_workflow(executor, run, self.user.id)-- self._assert_run_failed(run, "Failed to start task workflow: tasks feature is disabled")- mock_feature_enabled.assert_called_once()-@parameterized.expand([("sync",), ("async",)])def test_marks_run_failed_when_temporal_start_fails(self, executor: str) -> None:run = self._create_run()@@ -88,16 +68,10 @@ class TestExecuteTaskProcessingWorkflow(TransactionTestCase):)connect_mock = Mock(return_value=client) if executor == "sync" else AsyncMock(return_value=client)- with (- patch(- "products.tasks.backend.temporal.client.posthoganalytics.feature_enabled", return_value=True- ) as mock_feature_enabled,- patch(connect_target, connect_mock),- ):+ with patch(connect_target, connect_mock):self._execute_workflow(executor, run, self.user.id)self._assert_run_failed(run, "Failed to start task workflow: temporal unavailable")- mock_feature_enabled.assert_called_once()@parameterized.expand([("sync",), ("async",)])def test_does_not_overwrite_run_that_already_started(self, executor: str) -> None:@@ -112,16 +86,10 @@ class TestExecuteTaskProcessingWorkflow(TransactionTestCase):)connect_mock = Mock(return_value=client) if executor == "sync" else AsyncMock(return_value=client)- with (- patch(- "products.tasks.backend.temporal.client.posthoganalytics.feature_enabled", return_value=True- ) as mock_feature_enabled,- patch(connect_target, connect_mock),- ):+ with patch(connect_target, connect_mock):self._execute_workflow(executor, run, self.user.id)run.refresh_from_db()self.assertEqual(run.status, TaskRun.Status.IN_PROGRESS)self.assertIsNone(run.error_message)self.assertIsNone(run.completed_at)- mock_feature_enabled.assert_called_once()