Skip to content

Commit e01511c

Browse files
committed
fix(analytics): flush posthog events on SIGTERM before ECS task termination
1 parent f80be50 commit e01511c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

apps/sim/instrumentation-node.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ async function initializeOpenTelemetry() {
147147
} catch (err) {
148148
logger.error('Error shutting down OpenTelemetry SDK', err)
149149
}
150+
151+
try {
152+
const { getPostHogClient } = await import('@/lib/posthog/server')
153+
await getPostHogClient()?.shutdown()
154+
logger.info('PostHog client shut down successfully')
155+
} catch (err) {
156+
logger.error('Error shutting down PostHog client', err)
157+
}
150158
}
151159

152160
process.on('SIGTERM', shutdownHandler)

apps/sim/lib/posthog/server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const logger = createLogger('PostHogServer')
66
let _client: import('posthog-node').PostHog | null = null
77
let _disabled = false
88

9+
export function getPostHogClient(): import('posthog-node').PostHog | null {
10+
return getClient()
11+
}
12+
913
function getClient(): import('posthog-node').PostHog | null {
1014
if (_disabled) return null
1115
if (_client) return _client

0 commit comments

Comments
 (0)