Rails + xmpp4r + puma. Whem puma run as daemon XMPP disconnects

92 views Asked by At

I've got an initializer which starts xmpp4r client. It works fine when I run puma server as a regular process. But when I start puma as a daemon (-d option) it works for a few seconds and disconnects from the xmpp server. I've got separate thread which handles reconnects, but it doesn't work when puma is a daemon.

def init_reconnection_timer
    timers = Timers::Group.new
    periodic_timer = timers.every(5) do
      if @client.is_disconnected?
        begin
          Rails.logger.info "XmppConnector ##### reconnecting to #{APP_CONFIG['broker_address']} ..."
          connect
          Rails.logger.info "XmppConnector ##### connected!"
          presence
        rescue
        end
      end
    end
    Thread.new do
      loop do
        timers.wait
      end
    end
  end

I've got nothing from this code in log when puma is a daemon. Right after rails app start it works for a few seconds, receives messages, iqs, no errors just as usual. Then silently disconnects. Here is a constructor of my class:

class XmppConnector
  include Singleton

  def initialize
    @jid = Jabber::JID::new(APP_CONFIG['broker_username'] + '@' + APP_CONFIG['broker_address'])
    @jid.resource='rails'
    @client = Jabber::Client::new(@jid)
    connect
    init_presence_callback
    init_message_callback
    init_iq_callback
    init_reconnection_timer
    init_subscription_requests
    presence
    @protocol_interface = RemoteInterface.new
    Rails.logger.info "XmppConnector ##### initialized"
  end

But when puma run without -d option - no problems at all. Same thing in development and production, my workstation and server. ruby 2.0.0 puma 2.9.2 Min threads: 0, max threads: 16 rails 4.2.0beta4

1

There are 1 answers

0
Oleg Antonyan On

There was a bug in puma regarding threads in initializers and now it's fixed https://github.com/puma/puma/issues/617