Search code examples
ruby-on-railsrubyactiverecordresque

ActiveRecord models "losing" their find_by_* methods


We have a relatively standard Ruby on Rails project, which has quite a few background jobs that run under Resque (with Redis as a backend.)

The issue is that very rarely -- perhaps once a month, maybe a little less -- we'll suddenly see floods of exceptions from Resque. The exceptions are all in the following vein:

undefined method `find_by_id` for User():Class
undefined method `find_by_name` for CustomerAccount():Class
undefined method `find_by_id` for Job():Class

It appears that suddenly, all ActiveRecord::Base models lose their find_by_* methods for the entire thread. Restarting the worker fixes the issue.

I know that generically, the answer must be "someone, somewhere -- probably in a gem -- is breaking method_missing somehow." Or perhaps, somehow the constants are getting reassigned to a different class. But before I begin a really thorough investigation, I wanted to check if anyone has run into this problem and solved it already.

This project is running Ruby 2.1.1p76, Rails 3.2.17, Resque 1.25.1.


Solution

  • Closing the loop on this ancient question: it turns out moonfly's comment was indeed the issue, and in long-running workers, a dropped database connection would result in this (somewhat strange) error message.

    Knowing the root cause, we were able to add a periodic conn refresh (to try and keep the connection alive when a worker was idle for too long), and also, to add a detection mechanism for when a db conn was dropped, and to reconnect. So, thanks much @moonfly, if you'd like to turn your comment into an answer I'm happy to award you much-delayed answer credit.