Skip to content

: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.

fix(signals): keep the ranking sweep pause across deploys (#108199)

posthog[bot]99cc6d681949
diff --git a/products/signals/backend/ranking/schedule.py b/products/signals/backend/ranking/schedule.py
index fe4118a3b1bf..3236644ad229 100644
--- a/products/signals/backend/ranking/schedule.py
+++ b/products/signals/backend/ranking/schedule.py
@@ -42,6 +42,9 @@ async def create_inbox_ranking_scoring_schedule(client: Client) -> None:
policy=SchedulePolicy(overlap=ScheduleOverlapPolicy.SKIP),
)
if await a_schedule_exists(client, INBOX_RANKING_SCORING_SCHEDULE_ID):
+ # Keep the live state, so a deploy does not resume a schedule that an operator paused.
+ description = await client.get_schedule_handle(INBOX_RANKING_SCORING_SCHEDULE_ID).describe()
+ schedule.state = description.schedule.state
await a_update_schedule(client, INBOX_RANKING_SCORING_SCHEDULE_ID, schedule)
else:
await a_create_schedule(client, INBOX_RANKING_SCORING_SCHEDULE_ID, schedule, trigger_immediately=False)
diff --git a/products/signals/backend/ranking/sweep.py b/products/signals/backend/ranking/sweep.py
index b3b0322517e8..0834cb9136a8 100644
--- a/products/signals/backend/ranking/sweep.py
+++ b/products/signals/backend/ranking/sweep.py
@@ -218,11 +218,13 @@ def reports_due_for_scoring(
def score_inbox_reports(limit: int | None = None) -> ScoreInboxReportsResult:
if not settings.INBOX_RANKING_SCORING_ENABLED:
+ logger.info("inbox_ranking_sweep_skipped", skipped_reason="disabled")
return ScoreInboxReportsResult(skipped_reason="disabled")
deadline = time.monotonic() + _TIME_BUDGET.total_seconds()
# A served model that does not load raises here and aborts the run.
serving = load_serving_set()
if serving is None:
+ logger.info("inbox_ranking_sweep_skipped", skipped_reason="no manifest")
return ScoreInboxReportsResult(skipped_reason="no manifest")
manifest_version = serving.manifest.manifest_version
now = timezone.now()
@@ -235,6 +237,13 @@ def score_inbox_reports(limit: int | None = None) -> ScoreInboxReportsResult:
ids_by_team: dict[int, list[str]] = defaultdict(list)
for candidate in candidates:
ids_by_team[candidate.team_id].append(candidate.report_id)
+ # A pass that the worker kills logs no finish, so the start log is the only trace of it.
+ logger.info(
+ "inbox_ranking_sweep_started",
+ candidates=len(candidates),
+ teams=len(ids_by_team),
+ manifest_version=manifest_version,
+ )
scored = no_vector = failed_teams = deferred_teams = 0
with ph_scoped_capture() as capture:
@@ -253,6 +262,9 @@ def score_inbox_reports(limit: int | None = None) -> ScoreInboxReportsResult:
logger.exception("inbox_ranking_sweep_team_failed", team_id=team_id)
failed_teams += 1
continue
+ finally:
+ # Send the events of each team when it ends, so a killed pass keeps the events of the teams it persisted.
+ capture.flush()
scored += sum(1 for outcome in outcomes if outcome.score is not None)
no_vector += sum(1 for outcome in outcomes if outcome.reason == scorer.NO_VECTOR)
diff --git a/products/signals/backend/test/test_ranking_schedule.py b/products/signals/backend/test/test_ranking_schedule.py
new file mode 100644
index 000000000000..d565a9658e87
--- /dev/null
+++ b/products/signals/backend/test/test_ranking_schedule.py
@@ -0,0 +1,30 @@
+import pytest
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from temporalio.client import Client, ScheduleState
+
+from products.signals.backend.ranking import schedule as ranking_schedule
+from products.signals.backend.ranking.schedule import (
+ INBOX_RANKING_SCORING_SCHEDULE_ID,
+ create_inbox_ranking_scoring_schedule,
+)
+
+
+async def test_an_update_keeps_an_operator_pause() -> None:
+ client = MagicMock(spec=Client)
+ state = ScheduleState(paused=True, note="Paused by an operator")
+ client.get_schedule_handle.return_value.describe = AsyncMock(
+ return_value=MagicMock(schedule=MagicMock(state=state))
+ )
+ with (
+ patch.object(ranking_schedule, "a_schedule_exists", return_value=True),
+ patch.object(ranking_schedule, "a_create_schedule") as create,
+ patch.object(ranking_schedule, "a_update_schedule") as update,
+ ):
+ await create_inbox_ranking_scoring_schedule(client)
+
+ create.assert_not_awaited()
+ client.get_schedule_handle.assert_called_once_with(INBOX_RANKING_SCORING_SCHEDULE_ID)
+ assert update.await_args is not None
+ assert update.await_args.args[2].state == state
diff --git a/products/signals/backend/test/test_ranking_sweep.py b/products/signals/backend/test/test_ranking_sweep.py
index 5c282175dd32..0822ac59e995 100644
--- a/products/signals/backend/test/test_ranking_sweep.py
+++ b/products/signals/backend/test/test_ranking_sweep.py
@@ -172,6 +172,7 @@ class TestScoreInboxReports(SimpleTestCase):
self.capture = MagicMock()
self.capture_scopes = 0
self._patch(sweep, "ph_scoped_capture", new=self._capture_scope)
+ self.logger = self._patch(sweep, "logger")
self.failing_teams: dict[int, Exception] = {}
settings_override = override_settings(INBOX_RANKING_SCORING_ENABLED=True)
settings_override.enable()
@@ -219,6 +220,7 @@ class TestScoreInboxReports(SimpleTestCase):
result = score_inbox_reports()
assert result.skipped_reason == "disabled"
+ self.logger.info.assert_called_once_with("inbox_ranking_sweep_skipped", skipped_reason="disabled")
self.load_serving_set.assert_not_called()
self.due.assert_not_called()
self.score_reports.assert_not_called()
@@ -229,6 +231,7 @@ class TestScoreInboxReports(SimpleTestCase):
result = score_inbox_reports()
assert result.skipped_reason == "no manifest"
+ self.logger.info.assert_called_once_with("inbox_ranking_sweep_skipped", skipped_reason="no manifest")
self.due.assert_not_called()
self.score_reports.assert_not_called()
@@ -244,7 +247,10 @@ class TestScoreInboxReports(SimpleTestCase):
call.kwargs["serving"] is self.serving and call.kwargs["capture"] is self.capture
for call in self.score_reports.call_args_list
)
- assert (self.load_serving_set.call_count, self.capture_scopes) == (1, 1)
+ assert (self.load_serving_set.call_count, self.capture_scopes, self.capture.flush.call_count) == (1, 1, 2)
+ self.logger.info.assert_any_call(
+ "inbox_ranking_sweep_started", candidates=3, teams=2, manifest_version=MANIFEST
+ )
assert (result.candidates, result.scored, result.no_vector, result.teams, result.failed_teams) == (
3,
2,
@@ -261,6 +267,7 @@ class TestScoreInboxReports(SimpleTestCase):
result = score_inbox_reports()
assert (result.scored, result.teams, result.failed_teams) == (1, 2, 1)
+ assert self.capture.flush.call_count == 2
def test_teams_left_when_the_time_budget_runs_out_are_deferred_to_the_next_tick(self) -> None:
self._candidates((1, "a"), (2, "b"), (3, "c"))