Stay Connected with the ensure_connected gem

Ryan Sonnek bio photo By Ryan Sonnek

This is your application when “MYSQL has gone away”

Most developers working with long running background processes will eventually run into the infamous “MySQL server has gone away” error. This error is thrown when the background processes are idle for an extended period of time and lose their active connection to the database.

There are a few workarounds floating around the interwebs to deal with this exception, but the ensure_connected gem is a clean and programmatic solution to take care of this issue once and for all.

The ensure_connected gem wraps any method with a check to ensure that you are connected to the database before performing work.

class Worker
  def do_something
    #do your work here.
    #you're connected to the database baby!
  end

  ensure_connected :do_something
end

And Resque users have it even better with the resque-ensure-connected plugin. Simply drop this gem into your project and all of your background job invocations will be wrapped with the ensure_connected check. This works with the Backgrounded Resque integration as well!

As always, sourcecode is 100% opensource and available on Github.

https://github.com/wireframe/ensure_connected

https://github.com/wireframe/resque-ensure-connected

Gems are available for download through Gemcutter.