: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(workflows): bound worker config staleness with a background cache refresh (#71062)
Harley Alexandere8fe709f629b
diff --git a/nodejs/src/cdp/services/hogflows/hogflow-manager.service.test.ts b/nodejs/src/cdp/services/hogflows/hogflow-manager.service.test.tsindex 304e7e06a67e..d91be9505dea 100644--- a/nodejs/src/cdp/services/hogflows/hogflow-manager.service.test.ts+++ b/nodejs/src/cdp/services/hogflows/hogflow-manager.service.test.ts@@ -4,6 +4,7 @@ import { FixtureHogFlowBuilder } from '~/cdp/_tests/builders/hogflow.builder'import { HogFlow } from '~/cdp/schema/hogflow'import { closeHub, createHub } from '~/common/utils/db/hub'import { PostgresUse } from '~/common/utils/db/postgres'+import { waitForExpect } from '~/tests/helpers/expectations'import { forSnapshot } from '~/tests/helpers/snapshots'import { createTeam, getTeam, resetTestDatabase } from '~/tests/helpers/sql'import { Hub } from '~/types'@@ -103,6 +104,47 @@ describe('HogFlowManager', () => {})})+ describe('cache staleness bounds', () => {+ // Own timeout: the polling window below plus DB round-trips can exceed the suite's 2s default+ it('picks up an edit without a reload notification once the background refresh age passes', async () => {+ const baseNow = Date.now()+ try {+ let items = await manager.getHogFlowsForTeam(teamId1)+ expect(items.find((item) => item.id === hogFlows[0].id)?.name).toBe('Test Hog Flow team 1')++ // Edit WITHOUT dispatching the reload notification - the missed-publish case+ await hub.postgres.query(+ PostgresUse.COMMON_WRITE,+ `UPDATE posthog_hogflow SET name='Renamed without notification', updated_at = NOW() WHERE id = $1`,+ [hogFlows[0].id],+ 'testKey'+ )++ // Still within the background refresh age: served from cache+ items = await manager.getHogFlowsForTeam(teamId1)+ expect(items.find((item) => item.id === hogFlows[0].id)?.name).toBe('Test Hog Flow team 1')++ // Past the background age (30s + up to 24s jitter) but under the 2 min hard cap:+ // this get serves stale and kicks off a non-blocking refresh+ jest.spyOn(Date, 'now').mockReturnValue(baseNow + 60_000)+ items = await manager.getHogFlowsForTeam(teamId1)+ expect(items.find((item) => item.id === hogFlows[0].id)?.name).toBe('Test Hog Flow team 1')++ // The background refresh lands and the edit becomes visible - self-healed with no+ // markForRefresh. Guards against reverting to the LazyLoader defaults (5 min+ // blocking-only refresh), which would leave this stale until well past 60s.+ await waitForExpect(async () => {+ const refreshed = await manager.getHogFlowsForTeam(teamId1)+ expect(refreshed.find((item) => item.id === hogFlows[0].id)?.name).toBe(+ 'Renamed without notification'+ )+ }, 1500)+ } finally {+ jest.spyOn(Date, 'now').mockReturnValue(baseNow)+ }+ }, 10000)+ })+describe('getHogFlowIdsForTeam', () => {it('returns function IDs', async () => {const result = await manager.getHogFlowIdsForTeams([teamId1, teamId2])diff --git a/nodejs/src/cdp/services/hogflows/hogflow-manager.service.ts b/nodejs/src/cdp/services/hogflows/hogflow-manager.service.tsindex f91ffc742fcd..b6fd067df976 100644--- a/nodejs/src/cdp/services/hogflows/hogflow-manager.service.ts+++ b/nodejs/src/cdp/services/hogflows/hogflow-manager.service.ts@@ -36,13 +36,29 @@ export class HogFlowManagerService {private postgres: PostgresRouter,private pubSub: PubSub) {+ // The reload-hog-flows pub/sub below is the primary invalidation; these ages bound how+ // stale a worker can run when it misses the publish (pod restart, Redis blip). Live edits+ // are expected to reach in-flight runs, so a hot flow self-heals within ~30s via a+ // non-blocking background refresh, with a 2 minute hard cap - without these, a missed+ // publish serves stale config for the 5 minute default and the expiry refetch blocks the+ // worker's hot path.+ const cacheOptions = {+ refreshAgeMs: 2 * 60 * 1000,+ refreshBackgroundAgeMs: 30 * 1000,+ // Absorb transient Postgres blips inside a single load attempt so failed background+ // refreshes don't re-fire at caller QPS and hard-cap refreshes don't fail the batch.+ loaderRetry: { retryIntervalMs: 250, retryJitterMs: 250, maxElapsedMs: 5000 },+ }+this.lazyLoaderByTeam = new LazyLoader({name: 'hog_flow_manager_by_team',+ ...cacheOptions,loader: async (teamIds) => await this.fetchTeamHogFlows(teamIds),})this.lazyLoader = new LazyLoader({name: 'hog_flow_manager',+ ...cacheOptions,loader: async (ids) => await this.fetchHogFlows(ids),})