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(replay): move replay vision and scoring schedules off minute zero (#106990)

Sandy Spicer7943ff89740f
diff --git a/posthog/temporal/schedule.py b/posthog/temporal/schedule.py
index 9198bdd49234..c95349976d0c 100644
--- a/posthog/temporal/schedule.py
+++ b/posthog/temporal/schedule.py
@@ -645,10 +645,10 @@ async def create_purge_deleted_recording_metadata_schedule(client: Client):
)
-async def create_replay_count_metrics_schedule(client: Client):
+async def create_replay_count_metrics_schedule(client: Client) -> None:
"""Create or update the schedule for the replay count metrics workflow.
- This schedule runs hourly at minute 0, matching the previous Celery schedule.
+ This schedule runs hourly at minute zero to preserve adjacent one-hour metric windows.
"""
replay_count_metrics_schedule = Schedule(
action=ScheduleActionStartWorkflow(
diff --git a/posthog/temporal/session_replay/surfacing_scoring_sweep/schedule.py b/posthog/temporal/session_replay/surfacing_scoring_sweep/schedule.py
index aaf549e3dce5..66cbcaf47732 100644
--- a/posthog/temporal/session_replay/surfacing_scoring_sweep/schedule.py
+++ b/posthog/temporal/session_replay/surfacing_scoring_sweep/schedule.py
@@ -30,6 +30,7 @@ from temporalio.client import (
)
from temporalio.common import SearchAttributePair, TypedSearchAttributes
+from posthog.scheduling.jitter import deterministic_offset
from posthog.temporal.common.client import async_connect
from posthog.temporal.common.schedule import a_create_schedule, a_delete_schedule, a_schedule_exists, a_update_schedule
from posthog.temporal.common.search_attributes import POSTHOG_SCHEDULE_TYPE_KEY
@@ -55,7 +56,13 @@ def _build_schedule() -> Schedule:
execution_timeout=WORKFLOW_EXECUTION_TIMEOUT,
retry_policy=common.RetryPolicy(maximum_attempts=1),
),
- spec=ScheduleSpec(intervals=[ScheduleIntervalSpec(every=SCHEDULE_INTERVAL)]),
+ spec=ScheduleSpec(
+ intervals=[
+ ScheduleIntervalSpec(
+ every=SCHEDULE_INTERVAL, offset=deterministic_offset(SCHEDULE_ID, SCHEDULE_INTERVAL)
+ )
+ ]
+ ),
policy=SchedulePolicy(
overlap=ScheduleOverlapPolicy.SKIP,
catchup_window=SCHEDULE_INTERVAL,
diff --git a/products/replay_vision/backend/temporal/estimates.py b/products/replay_vision/backend/temporal/estimates.py
index f2d05665f270..c643c8f3faf6 100644
--- a/products/replay_vision/backend/temporal/estimates.py
+++ b/products/replay_vision/backend/temporal/estimates.py
@@ -7,6 +7,7 @@ from temporalio import workflow
from temporalio.common import RetryPolicy
from temporalio.exceptions import ApplicationError
+from posthog.scheduling.jitter import deterministic_offset
from posthog.temporal.common.base import PostHogWorkflow
from products.replay_vision.backend.temporal.constants import (
@@ -93,5 +94,6 @@ async def create_replay_vision_estimates_schedule(client: "Client") -> None:
workflow_id=ESTIMATES_WORKFLOW_ID,
inputs=RefreshScannerEstimatesInputs(),
interval=ESTIMATES_REFRESH_INTERVAL,
+ offset=deterministic_offset(ESTIMATES_SCHEDULE_ID, ESTIMATES_REFRESH_INTERVAL),
execution_timeout=ESTIMATES_EXECUTION_TIMEOUT,
)
diff --git a/products/replay_vision/backend/temporal/gemini_cleanup_sweep/schedule.py b/products/replay_vision/backend/temporal/gemini_cleanup_sweep/schedule.py
index bc22a33d6ae9..2b62f09e241a 100644
--- a/products/replay_vision/backend/temporal/gemini_cleanup_sweep/schedule.py
+++ b/products/replay_vision/backend/temporal/gemini_cleanup_sweep/schedule.py
@@ -1,6 +1,7 @@
from temporalio.client import Client
from temporalio.common import SearchAttributePair, TypedSearchAttributes
+from posthog.scheduling.jitter import deterministic_offset
from posthog.temporal.common.search_attributes import POSTHOG_SCHEDULE_TYPE_KEY
from products.replay_vision.backend.temporal.gemini_cleanup_sweep.constants import (
@@ -23,6 +24,7 @@ async def create_replay_vision_gemini_cleanup_sweep_schedule(client: Client) ->
workflow_id=WORKFLOW_ID,
inputs=CleanupSweepInputs(),
interval=SCHEDULE_INTERVAL,
+ offset=deterministic_offset(SCHEDULE_ID, SCHEDULE_INTERVAL),
execution_timeout=WORKFLOW_EXECUTION_TIMEOUT,
search_attributes=TypedSearchAttributes(
search_attributes=[SearchAttributePair(key=POSTHOG_SCHEDULE_TYPE_KEY, value=SCHEDULE_TYPE)]
diff --git a/products/replay_vision/backend/temporal/jev_watch_rank/schedule.py b/products/replay_vision/backend/temporal/jev_watch_rank/schedule.py
index c20f23117cf9..77a3c8714dc3 100644
--- a/products/replay_vision/backend/temporal/jev_watch_rank/schedule.py
+++ b/products/replay_vision/backend/temporal/jev_watch_rank/schedule.py
@@ -1,3 +1,5 @@
+from datetime import timedelta
+
from temporalio.client import Client
from temporalio.common import SearchAttributePair, TypedSearchAttributes
@@ -23,6 +25,8 @@ async def create_replay_vision_jev_watch_rank_schedule(client: Client) -> None:
workflow_id=WORKFLOW_ID,
inputs=JevWatchRankSweepInputs(),
interval=SCHEDULE_INTERVAL,
+ offset=timedelta(minutes=2),
+ jitter=timedelta(minutes=10),
execution_timeout=WORKFLOW_EXECUTION_TIMEOUT,
search_attributes=TypedSearchAttributes(
search_attributes=[SearchAttributePair(key=POSTHOG_SCHEDULE_TYPE_KEY, value=SCHEDULE_TYPE)]
diff --git a/products/replay_vision/backend/temporal/read_meter.py b/products/replay_vision/backend/temporal/read_meter.py
index 4a3648ee1784..45ab0a7eb109 100644
--- a/products/replay_vision/backend/temporal/read_meter.py
+++ b/products/replay_vision/backend/temporal/read_meter.py
@@ -5,6 +5,7 @@ hourly buckets on each scanner row. The sweep consults the trailing-24h sum befo
stretches its effective cadence when a scanner exceeds its read budget (see `sweep_throttle_factor`).
"""
+import datetime as dt
from typing import TYPE_CHECKING
from temporalio import workflow
@@ -56,5 +57,7 @@ async def create_replay_vision_read_meter_schedule(client: "Client") -> None:
workflow_id=READ_METER_WORKFLOW_ID,
inputs=MeterScannerReadsInputs(),
interval=READ_METER_INTERVAL,
+ offset=dt.timedelta(minutes=2),
+ jitter=dt.timedelta(minutes=10),
execution_timeout=READ_METER_EXECUTION_TIMEOUT,
)
diff --git a/products/replay_vision/backend/temporal/reconciler.py b/products/replay_vision/backend/temporal/reconciler.py
index 3762c9a0f9b1..c8daba8c5cf9 100644
--- a/products/replay_vision/backend/temporal/reconciler.py
+++ b/products/replay_vision/backend/temporal/reconciler.py
@@ -10,6 +10,7 @@ from temporalio import workflow
from temporalio.common import RetryPolicy
from temporalio.exceptions import ApplicationError
+from posthog.scheduling.jitter import deterministic_offset
from posthog.temporal.common.base import PostHogWorkflow
from products.replay_vision.backend.temporal.constants import (
@@ -215,5 +216,6 @@ async def create_replay_vision_reconciler_schedule(client: "Client") -> None:
workflow_id=RECONCILER_WORKFLOW_ID,
inputs=ReconcileScannerSchedulesInputs(),
interval=RECONCILER_INTERVAL,
+ offset=deterministic_offset(RECONCILER_SCHEDULE_ID, RECONCILER_INTERVAL),
execution_timeout=RECONCILER_EXECUTION_TIMEOUT,
)
diff --git a/products/replay_vision/backend/temporal/schedule.py b/products/replay_vision/backend/temporal/schedule.py
index 92f79218d0fa..89081850e59f 100644
--- a/products/replay_vision/backend/temporal/schedule.py
+++ b/products/replay_vision/backend/temporal/schedule.py
@@ -21,6 +21,7 @@ from temporalio.client import (
from temporalio.common import SearchAttributePair, TypedSearchAttributes
from temporalio.service import RPCError, RPCStatusCode
+from posthog.scheduling.jitter import deterministic_offset
from posthog.sync import database_sync_to_async
from posthog.temporal.common.client import async_connect
from posthog.temporal.common.schedule import (
@@ -79,12 +80,10 @@ def compute_schedule_fingerprint(snapshot: dict[str, Any] | None) -> str:
def compute_schedule_offset(entity_id: UUID, interval: dt.timedelta) -> dt.timedelta:
"""Stagger per-entity schedules across their interval so they don't all fire on the same boundary.
- UUID.int is stable across processes; modulo distributes fires uniformly across the window. Without
- this, sibling schedules tick together and each reads the same in-flight counts before any of them
- has persisted a row.
+ Without this, sibling schedules tick together and each reads the same in-flight counts before any
+ of them has persisted a row.
"""
- interval_s = max(1, int(interval.total_seconds()))
- return dt.timedelta(seconds=entity_id.int % interval_s)
+ return deterministic_offset(str(entity_id), interval)
def _build_schedule(scanner_id: UUID, team_id: int) -> Schedule:
@@ -132,9 +131,10 @@ async def upsert_interval_schedule(
workflow_id: str,
inputs: BaseModel,
interval: dt.timedelta,
+ offset: dt.timedelta,
execution_timeout: dt.timedelta,
search_attributes: TypedSearchAttributes | None = None,
- offset: dt.timedelta | None = None,
+ jitter: dt.timedelta | None = None,
) -> None:
"""Create or update a singleton interval schedule with SKIP overlap; first creation triggers immediately."""
schedule = Schedule(
@@ -146,7 +146,7 @@ async def upsert_interval_schedule(
execution_timeout=execution_timeout,
retry_policy=common.RetryPolicy(maximum_attempts=1),
),
- spec=ScheduleSpec(intervals=[ScheduleIntervalSpec(every=interval, offset=offset)]),
+ spec=ScheduleSpec(intervals=[ScheduleIntervalSpec(every=interval, offset=offset)], jitter=jitter),
policy=SchedulePolicy(overlap=ScheduleOverlapPolicy.SKIP, catchup_window=interval),
)
if await a_schedule_exists(client, schedule_id):
diff --git a/products/replay_vision/backend/temporal/search_suggestions.py b/products/replay_vision/backend/temporal/search_suggestions.py
index 06616668354c..b73c5e5a9669 100644
--- a/products/replay_vision/backend/temporal/search_suggestions.py
+++ b/products/replay_vision/backend/temporal/search_suggestions.py
@@ -1,6 +1,7 @@
"""Periodic batch refresh of the per-scanner and per-team search suggestions shown on the Search tab's empty state."""
import asyncio
+import datetime as dt
from typing import TYPE_CHECKING
from temporalio import workflow
@@ -88,5 +89,7 @@ async def create_replay_vision_search_suggestions_schedule(client: "Client") ->
workflow_id=SEARCH_SUGGESTIONS_WORKFLOW_ID,
inputs=RefreshSearchSuggestionsInputs(),
interval=SEARCH_SUGGESTIONS_REFRESH_INTERVAL,
+ offset=dt.timedelta(minutes=2),
+ jitter=dt.timedelta(minutes=10),
execution_timeout=SEARCH_SUGGESTIONS_EXECUTION_TIMEOUT,
)