fix: Add sync_mode for Sidekiq/Resque compatibility#112
Conversation
Send events synchronously on the calling thread instead of queuing them for a background worker. Follows the same pattern as the Python SDK: in sync_mode, events bypass the queue entirely and are sent inline via Transport#send. Retries are capped at 3 in sync mode (vs 10 in async) to avoid blocking the calling thread for extended periods during API outages. test_mode takes precedence over sync_mode to prevent accidental network calls in test environments. Usage: PostHog::Client.new(api_key: 'key', sync_mode: true) Fixes #10
850b75f to
0cba1cd
Compare
dustinbyrne
left a comment
There was a problem hiding this comment.
Seems totally reasonable to me. The Copilot comment regarding exceptions in example.call is nit-picky but seems worth considering.
As I told Copilot, this is a false positive. RSpec's |
There was a problem hiding this comment.
Pull request overview
Adds an opt-in sync_mode to the Ruby client so event capture can send immediately on the calling thread (bypassing the queue/worker), improving reliability in forking job systems like Sidekiq/Resque.
Changes:
- Add
sync_modeoption toPostHog::Clientthat sends each event inline viaTransport#send, with locking for in-flight sends andflushas a no-op (wait-only) in sync mode. - Add Rails initializer support for
config.sync_mode. - Add worker
shutdownplumbing (noop + send worker) and bump gem version to3.6.0, plus new specs for sync-mode behavior.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| spec/posthog/client_spec.rb | Adds specs covering sync-mode inline send, error handling, serialization failure, flush behavior, and test_mode precedence. |
| posthog-rails/lib/posthog/rails/railtie.rb | Exposes sync_mode configuration via Rails init config wrapper. |
| lib/posthog/client.rb | Implements sync_mode (inline send path, transport init, flush/shutdown coordination) and documents the new option. |
| lib/posthog/send_worker.rb | Adds shutdown method used by client shutdown flow. |
| lib/posthog/noop_worker.rb | Adds shutdown no-op to match worker interface. |
| lib/posthog/version.rb | Bumps version to 3.6.0. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
sync_modeoption that sends events synchronously on the calling thread, bypassing the background queue entirelyTransport#sendat capture timeUsage
Or with Rails:
Design
sync_modebypasses the queue and worker entirely — noSyncWorkerclass neededenqueuecallssend_syncwhich creates a single-itemMessageBatchand sends it immediatelyflushis a no-op in sync mode (nothing to flush)test_modetakes precedence oversync_modeTest plan
Fixes: #10
Note
Issue #10 is a blocker for some customers to adopt our library rather than rolling their own code.