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.5 KB
Raw Blame
import { KafkaProducer } from '~/common/kafka/producer'
import { logger } from '~/common/utils/logger'

/**
 * Typed producer registry that holds pre-created Kafka producers by name.
 *
 * Generic over `P` so the set of valid producer names is known at compile time.
 * Producers are created by `KafkaProducerRegistryBuilder` and passed as a Record
 * to the constructor — the registry itself only manages access and shutdown.
 */
export class KafkaProducerRegistry<P extends string> {
    constructor(private producers: Record<P, KafkaProducer>) {}

    /**
     * Get a producer by its typed name.
     */
    getProducer(name: P): KafkaProducer {
        return this.producers[name]
    }

    /**
     * Flush and disconnect all producers.
     *
     * Continues on failure to ensure all producers are attempted. Throws after all
     * disconnects complete if any failed.
     */
    async disconnectAll(): Promise<void> {
        const entries = Object.entries<KafkaProducer>(this.producers)
        const errors: [string, unknown][] = []
        for (const [name, producer] of entries) {
            logger.info('🔌', `Disconnecting producer "${name}"`)
            try {
                await producer.disconnect()
            } catch (err) {
                logger.error('🔌', `Failed to disconnect producer "${name}"`, { err })
                errors.push([name, err])
            }
        }
        if (errors.length > 0) {
            throw new Error(`Failed to disconnect producers: ${errors.map(([n]) => n).join(', ')}`)
        }
    }
}