Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Heroku thread errors #32

Closed
mbrichman opened this issue Feb 12, 2014 · 1 comment
Closed

Heroku thread errors #32

mbrichman opened this issue Feb 12, 2014 · 1 comment

Comments

@mbrichman
Copy link

I'm having problems with the gem causing ThreadError exceptions on heroku.

My application is Rails 3.2.14 and I'm running a Unicorn 4.6.3 web server.

Here is how I'm invoking the gem in my unicorn.rb in the after_fork:

defined?(Analytics) and Analytics.init(secret: ENV['segment_secret_key']) if ENV['segment_secret_key'].present?

and here are the methods in my application controller where I'm invoking the gem:

def analytics_client
    client = nil

    if Settings.segment_secret_key.present?
      client_options = { secret: Settings.segment_secret_key }
      client = AnalyticsRuby::Client.new(client_options)

      identify_options = nil

      if user_signed_in?
        client.alias(from: cookies[:user_uuid], to: current_user.channel_token)

        identify_options = { user_id: current_user.channel_token, created: current_user.created_at.iso8601, name: current_user.name, type: 'user', email: current_user.email }
      elsif salon_signed_in?
        client.alias(from: cookies[:user_uuid], to: current_salon.channel_token)

        identify_options = { user_id: current_salon.channel_token, created: current_salon.created_at.iso8601, name: current_salon.name, type: 'salon', email: current_salon.email }
      else
        identify_options = { user_id: cookies[:user_uuid] }
      end

      client.identify(identify_options)
    end

    client
  end

  def track_event(event_string, properties={})
    analytics ||= analytics_client

    if analytics.present?
      user_id = nil

      if user_signed_in?
        user_id = current_user.channel_token
      elsif salon_signed_in?
        user_id = current_salon.channel_token
      else
        user_id = cookies[:user_uuid]
      end

      options = {
        user_id: user_id,
        event: event_string,
        properties: properties
      }

      analytics.track(options)
    end
  end

Several times a week now I get a ThreadError: can't create thread
when it tries to create a new instance of the Analytics client. This is effectively causing my application to fail as the track_event method is called on most application pages.

I'd love to hear any thoughts on why this may be happening. I realize it could be other parts of my application, but this client is the main things that's creating new threads. The problem has gotten worse as I've added more analytics events.

Alternatively, I'd like to hear your thoughts on the best way to rescue from the ThreadError in my code.

Any help appreciated.

Thanks,
Mark

@calvinfo
Copy link
Contributor

Debugged this over support, closing. For anyone else running into this problem, you'll likely want to initialize the client once and then keep an instance around. Each time a new client is created, a new thread is spawned, so you don't want to be doing it every request. We'll want to revisit that behavior in future versions though, maybe offering a way to pool threads or something.

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

No branches or pull requests

2 participants