Category Archives: activerecord

How to handle a polling thread when using Apache and Passenger

In my rails application, I have a poller which should indefinitely run.

def start_poll do
    # poll

After this thread remained idle for 300 seconds (Apache's default TIMEOUT value), Apache killed the thread and hence the polling would stop. In order to get around this, I used a forked process of Passenger

PhusionPassenger.on_event(:starting_worker_process) do |_forked|

By using Passenger's spawning mechanism, the polling thread defined in start_poll runs indefinitely.

However, the Thread loses its database connection (MySQL using ActiveRecord) after 8 hours of inactivity on the thread. (8 hours is the default wait_timeout for MySQL).

How can I solve this issue? Is there a way I can not use Passenger's spawning and let Apache kill the idle polling thread after 300 seconds, but then initialize a new thread at the same time?

I am using Apache, PhusionPassenger, Rails 3, ActiveRecord and MySQL.