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(ws): prevent schedule state mismatch when re-enabling schemas after credential errors (#57652)

estefaniaf2f839184901
diff --git a/posthog/temporal/common/schedule.py b/posthog/temporal/common/schedule.py
index 916fc4dda7e9..82b1758b2855 100644
--- a/posthog/temporal/common/schedule.py
+++ b/posthog/temporal/common/schedule.py
@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING
from asgiref.sync import async_to_sync
from temporalio.client import Client, Schedule, ScheduleOverlapPolicy, ScheduleUpdate, ScheduleUpdateInput
+from temporalio.service import RPCError, RPCStatusCode
if TYPE_CHECKING:
from temporalio.common import TypedSearchAttributes
@@ -161,14 +162,18 @@ async def schedule_exists(temporal: Client, schedule_id: str) -> bool:
try:
await temporal.get_schedule_handle(schedule_id).describe()
return True
- except:
- return False
+ except RPCError as e:
+ if e.status == RPCStatusCode.NOT_FOUND:
+ return False
+ raise
async def a_schedule_exists(temporal: Client, schedule_id: str) -> bool:
- """Check whether a schedule exists."""
+ """Check whether a schedule exists. See :func:`schedule_exists`."""
try:
await temporal.get_schedule_handle(schedule_id).describe()
return True
- except:
- return False
+ except RPCError as e:
+ if e.status == RPCStatusCode.NOT_FOUND:
+ return False
+ raise
diff --git a/products/data_warehouse/backend/data_load/service.py b/products/data_warehouse/backend/data_load/service.py
index dbb8752fa316..ee06bc28d93d 100644
--- a/products/data_warehouse/backend/data_load/service.py
+++ b/products/data_warehouse/backend/data_load/service.py
@@ -13,6 +13,7 @@ from temporalio.client import (
Client as TemporalClient,
Schedule,
ScheduleActionStartWorkflow,
+ ScheduleAlreadyRunningError,
ScheduleIntervalSpec,
ScheduleOverlapPolicy,
SchedulePolicy,
@@ -153,7 +154,11 @@ def sync_external_data_job_workflow(
schedule = get_sync_schedule(external_data_schema, should_sync=should_sync)
if create:
- create_schedule(temporal, id=str(external_data_schema.id), schedule=schedule, trigger_immediately=True)
+ try:
+ create_schedule(temporal, id=str(external_data_schema.id), schedule=schedule, trigger_immediately=True)
+ except ScheduleAlreadyRunningError:
+ update_schedule(temporal, id=str(external_data_schema.id), schedule=schedule)
+ trigger_schedule(temporal, schedule_id=str(external_data_schema.id))
else:
update_schedule(temporal, id=str(external_data_schema.id), schedule=schedule)
@@ -168,7 +173,13 @@ async def a_sync_external_data_job_workflow(
schedule = get_sync_schedule(external_data_schema, should_sync=should_sync)
if create:
- await a_create_schedule(temporal, id=str(external_data_schema.id), schedule=schedule, trigger_immediately=True)
+ try:
+ await a_create_schedule(
+ temporal, id=str(external_data_schema.id), schedule=schedule, trigger_immediately=True
+ )
+ except ScheduleAlreadyRunningError:
+ await a_update_schedule(temporal, id=str(external_data_schema.id), schedule=schedule)
+ await a_trigger_schedule(temporal, schedule_id=str(external_data_schema.id))
else:
await a_update_schedule(temporal, id=str(external_data_schema.id), schedule=schedule)
diff --git a/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.ts b/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.ts
index e66bd012491c..27fe218dcab5 100644
--- a/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.ts
+++ b/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.ts
@@ -517,7 +517,11 @@ export const sourceSettingsLogic = kea<sourceSettingsLogicType>([
const latestPendingUpdate = schemaUpdateCache.pendingSchemaUpdates[updatedSchema.id]
const inFlightUpdate = pendingSchemaUpdates[updatedSchema.id]
- return !latestPendingUpdate || latestPendingUpdate.revision <= inFlightUpdate.revision
+ return (
+ !latestPendingUpdate ||
+ !inFlightUpdate ||
+ latestPendingUpdate.revision <= inFlightUpdate.revision
+ )
})
if (schemasToApply.length > 0) {