Search code examples
ruby-on-railsrubymultithreadingeventmachinetcpsocket

Event Machine chat server stops without error trace


I have modified the SimpleChatServer example of EventMachine to work with a Rails App as a chat server. I initialize the chat server in a seperate thread as follows.

Thread.new { 
        puts "I entered a new thread"
        EventMachine.run do

            puts "I entered a new thread"
            EventMachine.start_server("0.0.0.0", 3100, SimpleChatServer)
        end 
    }

I have hosted my app on a VPS running apache and am using Phusion Passenger to serve the rails app. The chat server works perfect except for one problem: The server stops automatically after a few minutes. When I check the error log i find nothing related to the shutdown. An interesting thing that I have observed however is a strange behavior of the shutdown: During day time in my location (11 am - 5 pm) the chat server stops after a few minutes of starting (My timezone is 10 hours ahead of my server's timezone). However during nighttime at my side the server keeps on running without a shut down. This strange behavior is bogging down my mind.

My own assumption is that during my day time, the VPS has more load to handle and thus it kills the Chat Server thread. Can I somehow avoid that? Also I would love to know if there is any other reason for this strange behavior. Please help me with this

Addition: When I check my error log I see this. "[ 2015-03-06 08:00:20.5859 25041/7f20f1439700 agents/HelperAgent/Main.cpp:722 ]: Disconnecting long-running connections for process 25069" Here 25069 is the PID of my chat server. How can i avoid this? How can I instruct linux not to kill my process ever?


Solution

  • A long while back I found the solution to this in a thread on github. The process gets killed because passenger kills idle applications to save memory. In order to disable this and keep my process running, I needed to set max_pool_idle_time to 0 in my passenger configuration. Here is a link to the original thread: https://www.phusionpassenger.com/documentation/Users%20guide%20Nginx.html#_configuring_phusion_passenger