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.

1.3 KB
Raw Blame
import { initializePrometheusLabels } from '~/common/api/router'
import {
    AiResearchV4ServerConfig,
    buildAiResearchV4ServerConfig,
} from '~/ingestion/pipelines/sessionreplay/ai-research/config'
import { CleanupResources, NodeServer, ServerLifecycle } from '~/servers/base-server'

/** The lifecycle every v4 Kafka consumer server shares: the HTTP server, metric labels, start and stop. */
export abstract class AiResearchV4ConsumerServer implements NodeServer {
    readonly lifecycle: ServerLifecycle
    protected readonly config: AiResearchV4ServerConfig

    constructor(config: Partial<AiResearchV4ServerConfig> = {}) {
        this.config = buildAiResearchV4ServerConfig(config)
        this.lifecycle = new ServerLifecycle(this.config)
    }

    async start(): Promise<void> {
        return this.lifecycle.start(
            () => {
                initializePrometheusLabels(this.config.INGESTION_PIPELINE, this.config.INGESTION_LANE)
                return this.startServices()
            },
            () => this.getCleanupResources()
        )
    }

    async stop(error?: Error): Promise<void> {
        return this.lifecycle.stop(() => this.getCleanupResources(), error)
    }

    protected abstract startServices(): Promise<void>
    protected abstract getCleanupResources(): CleanupResources
}