You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
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:
and here are the methods in my application controller where I'm invoking the gem:
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
The text was updated successfully, but these errors were encountered: