Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Puma #37

Closed
recyclerobot opened this issue Mar 25, 2014 · 7 comments
Closed

Puma #37

recyclerobot opened this issue Mar 25, 2014 · 7 comments

Comments

@recyclerobot
Copy link

How to correctly implement this with a multi instances puma envirnoment?

@calvinfo
Copy link
Contributor

I haven't used Puma, but from looking at their docs, you'll probably want to add something like this to your puma config:

# config/puma.rb
on_worker_boot do
  Analytics = AnalyticsRuby
  Analytics.init({ secret: YOUR_SEGMENTIO_KEY })
end

Let me know if that doesn't work, and I can take a closer look!

@recyclerobot
Copy link
Author

Hi calvinfo!

Thanks for the on_worker_boot

I've got it working using this:

# config/puma.rb
require 'analytics-ruby'

on_worker_boot do
    puts "Init segment.io analytics"
    if defined? AnalyticsRuby
        Analytics = AnalyticsRuby       # Alias for convenience
        Analytics.init({
            secret: 'XXXXXX',          # The write key for
            on_error: Proc.new { |status, msg| print msg }  # Optional error handler
        })
    end
end

adding the /initializers/analytics.rb

Analytics = AnalyticsRuby

and add the -C config/puma.rb flag to your Procfile

bundle exec puma -t 8:32 -w 2 --preload -e $RACK_ENV -b tcp://0.0.0.0:$PORT -C config/puma.rb

Thanks again for the directions!
Ps: maybe you can add it this in the documentation after the unicorn and passenger parts :)

@calvinfo
Copy link
Contributor

Awesome! Yeah, we're about to completely overhaul the docs so I'll help get it in with the new re-write. Glad to hear it worked out :)

@phildionne
Copy link

Hey guys, I'm having trouble sending events in production. The solution outlined in this issue doesn't seem to work anymore.

My understanding of the desired behavior here is that we'd want a global $AnalyticsBackend instance that is shared across threads.

Once I've seen that the worker thread was dead, but haven't been able to replicate it consistently.

@calvinfo is there any official way now to do this? Thanks

Configuration:

  • Rails 5.2.2
  • Ruby 2.5.3
  • Puma 3.12.0
class Analytics
  class_attribute :backend, instance_writer: false, default: $AnalyticsBackend

  def track(event:, properties: {})
    # ...
  end

  # ...
end
# initializers/analytics.rb

$AnalyticsBackend = Segment::Analytics.new({
  write_key: "...",
  on_error: Proc.new { |status, msg| print msg }
})
# config/puma.rb

threads 5, 5

workers 2

on_worker_shutdown do
  Analytics.flush
end

preload_app!

# ...

@ajsharp
Copy link

ajsharp commented May 16, 2019

I'm seeing the same problem. This seems like a pretty serious issue, as puma is probably the most popular ruby app server.

@ajsharp
Copy link

ajsharp commented May 16, 2019

This might be completely off-topic, but in trying to debug what's going on here, I've noticed that I can't send events through from my production rails console if the user_id is a BSON::ObjectId. However, if I to_s that object, it succeeds and I see the event in the segment debugger.

@ajsharp
Copy link

ajsharp commented May 16, 2019

Apologies, it looks like I was running into #180 as well.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants