impr: made analytics run in background for optimal performance#68
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughThis PR replaces direct PostHog usage with an in-memory event queue (pendingEvents). analytics.identify/track now enqueue payloads; analytics.shutdown synchronously serializes queued events and spawns the current executable in a new __analytics mode to deliver them. A new analytics-worker module parses the payload and posts events with a 5s timeout, suppressing errors. The CLI adds a __analytics branch to run the worker and switches to a process.on("exit") handler to call analytics.shutdown. package.json version bumped to 0.3.5. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/lib/analytics.ts`:
- Around line 236-241: The code is leaking queued analytics into the child
process environment by setting EK_ANALYTICS_PAYLOAD to JSON; instead, send the
payload over stdin (or another non-environment channel) when spawning the child.
In the block that builds payload from pendingEvents and calls
Bun.spawn([...getSelfSpawnCommand(), "__analytics"], { env: {...}, ... }),
remove the EK_ANALYTICS_PAYLOAD entry and configure the spawn to use a piped
stdin, then write the JSON payload to the child's stdin (and close it) after
spawn; reference symbols: pendingEvents, payload, getSelfSpawnCommand,
Bun.spawn, and EK_ANALYTICS_PAYLOAD (remove usage).
- Around line 236-248: The code currently clears pendingEvents before attempting
to spawn the analytics worker, risking lost events if Bun.spawn(...) fails;
change the logic in the block that builds payload/starts the worker (the code
using pendingEvents, getSelfSpawnCommand(), Bun.spawn(...), proc.unref(), and
EK_ANALYTICS_PAYLOAD) so you only set pendingEvents.length = 0 after Bun.spawn
completes successfully (and proc.unref() is called); if Bun.spawn throws, catch
the error, log it, and leave pendingEvents intact (or requeue the payload) so
the batch is not lost.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 62869a57-6927-4d77-8aa8-5a44492f55ca
⛔ Files ignored due to path filters (1)
bun.lockis excluded by!**/*.lock
📒 Files selected for processing (4)
package.jsonsrc/cli.tssrc/lib/analytics-worker.tssrc/lib/analytics.ts
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/lib/analytics.ts`:
- Around line 238-245: The code currently exposes PII by passing the JSON
payload as a command-line argument to Bun.spawn; change the spawn usage in the
analytics sending path (where Bun.spawn([...cmd, "__analytics", payload], { ...,
detached: true }) and proc.unref() are used) to omit the payload from argv and
instead open stdin for the child (set stdin to "pipe"), write the payload JSON
into proc.stdin and close it, then unref the child; also update the
analytics-worker to read the payload from stdin (e.g., via Bun.stdin.text())
instead of process.argv[3].
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4aa43cd9-f9fb-4daa-8b63-aed2cab77b26
📒 Files selected for processing (2)
src/lib/analytics-worker.tssrc/lib/analytics.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/lib/analytics-worker.ts
Summary by CodeRabbit
Chores
Refactor
Chores