2015-08-03 52 views
4

我在我的项目中使用pubnub,它订阅了一个频道,并在我的订阅者更新我的数据库。 我做这一切工作在初始化文件中像这样异常:ActiveRecord :: ConnectionNotE建立使用pubnub

$callback_location = (lambda do |envelop| 
    begin 
    case envelop.channel 
     when "iwm_driver_locations" 
     last_location = LatLong.where(driver_id: envelop.message['driver_id']).last 
     if last_location.lat != envelop.message['lng'] and last_location.lng != envelop.message['lat'] 
      l = LatLong.create!(
       lat: envelop.message['lat'], 
       lng: envelop.message['lng'], 
       driver_id: envelop.message['driver_id'] 
     ) 
     end 
     when "iwm_chat" 
     m = Message.create!(
      :author => envelop.message, 
      :message => envelop.message, 
      :timetoken => envelop.timetoken 
     ) 
    end 
    rescue Exception => e 
    Rails.logger.info "****** Exception: #{e}" 
    end 
end) 

$pubnub.subscribe(
    :channel => ['iwm_chat', 'iwm_driver_locations'], 
    :callback => $callback_location 
) unless $pubnub.subscription_running? 

,但我的用户抛出异常在一些尝试ConnectionNotEstablished。但是有时这些代码执行没有问题。

我试图增加数据库超时和池,但同样的问题仍然存在。 任何想法我做错了?

回答

1

尝试添加ActiveRecord::Base.establish_connection之前发射$pubnub.subscribe。它应该有所帮助。

+0

有了这个我的代码在开发中工作正常,但在生产模式下的服务器上,它在每种情况下抛出相同的错误。 –