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.

3.0 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, key, listeners, path, props } from 'kea'
import { loaders } from 'kea-loaders'

import { projectLogic } from 'scenes/projectLogic'

import { experimentsFlagCleanupTaskRetrieve } from 'products/experiments/frontend/generated/api'
import type { ExperimentFlagCleanupTaskApi } from 'products/experiments/frontend/generated/api.schemas'

export interface FlagCleanupTaskLogicProps {
    experimentId: number
}

const POLL_INTERVAL_MS = 30000

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagCleanupTaskLogicValues {
    currentProjectId: number | null // projectLogic
    cleanupTask: ExperimentFlagCleanupTaskApi | null
    cleanupTaskLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagCleanupTaskLogicActions {
    loadCleanupTask: () => any
    loadCleanupTaskFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadCleanupTaskSuccess: (
        cleanupTask: ExperimentFlagCleanupTaskApi,
        payload?: any
    ) => {
        cleanupTask: ExperimentFlagCleanupTaskApi
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagCleanupTaskLogicMeta {
    key: number
}

export type flagCleanupTaskLogicType = MakeLogicType<
    flagCleanupTaskLogicValues,
    flagCleanupTaskLogicActions,
    FlagCleanupTaskLogicProps,
    flagCleanupTaskLogicMeta
>

export const flagCleanupTaskLogic = kea<flagCleanupTaskLogicType>([
    props({} as FlagCleanupTaskLogicProps),
    key((props) => props.experimentId),
    path((key) => ['scenes', 'experiments', 'flagCleanupTaskLogic', key]),
    connect(() => ({ values: [projectLogic, ['currentProjectId']] })),
    loaders(({ props, values }) => ({
        cleanupTask: [
            null as ExperimentFlagCleanupTaskApi | null,
            {
                loadCleanupTask: async () =>
                    await experimentsFlagCleanupTaskRetrieve(String(values.currentProjectId), props.experimentId),
            },
        ],
    })),
    listeners(({ cache }) => ({
        loadCleanupTaskSuccess: ({ cleanupTask }) => {
            cache.pollFailures = 0
            if (cleanupTask?.is_terminal) {
                cache.disposables.dispose('cleanupTaskPoll')
            }
        },
        loadCleanupTaskFailure: () => {
            // Tolerate transient errors — the task may still finish. Stop only when the
            // endpoint fails persistently.
            cache.pollFailures = (cache.pollFailures ?? 0) + 1
            if (cache.pollFailures >= 3) {
                cache.disposables.dispose('cleanupTaskPoll')
            }
        },
    })),
    afterMount(({ actions, cache }) => {
        actions.loadCleanupTask()
        cache.disposables.add(() => {
            const id = setInterval(() => actions.loadCleanupTask(), POLL_INTERVAL_MS)
            return () => clearInterval(id)
        }, 'cleanupTaskPoll')
    }),
])