Skip to content →

Rails Production Eager Loading Ignored by Rake Tasks

I got bit by the Rails production environment today. Here’s the scenario: I have a set of classes that all mixin the same module.

Upon include the module “registers” the class with another class

Once registered subclasses of ApiCheck  can perform actions using the registry. Those actions are run via rake tasks. In development, when classes are autoloaded (i.e. only loaded when referenced), the rake tasks work fine. In production, however, the tasks fail because, in the context of a rake task,  ApiCheck.registry is empty. None of my classes were registered. This struck me as odd because in production Rails eager loads classes. That is, Rails loads all your classes on application startup, not on demand.  Sure enough if I entered a console session in production and checked the registry it was populated as expected. What was going on? After some digging around I came across this comment in production.rb:

Right there on line 4, eager loading is ignored by rake tasks in production “for performance”. Since classes are not autoloaded in production my Syncable classes would have to be required manually in order for them to register themselves with ApiCheck .

Calling #require_syncables on the first line of my rake tasks ensures they work properly in production. Doing similar may save you from a headache if you’re also experiencing problems with rake tasks in Rails production.

Got questions or feedback? I want it. Drop your thoughts in the comments below or hit me @ccstump. You can also follow me on Twitter to be aware of future articles.

Thanks for reading!

 

Published in dev

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *