Reloadable Reloaded: Auto-Reload WebORB on Rails Service Classes
One of the pain points that developers have experienced when building solutions with WebORB on Rails is the need to restart the server after changes have been made to the service class. In the old days people just expected that code updates would require server restarts. In the modern world of server-side programming we live in today however, this is a significant nuisance. This is especially true when building rails apps, because it automatically reloads controllers, models etc. when in development mode so developers expect their services to be reloaded too!
Rails does this by a combination of a couple features. First it provides a module named Reloadable that you can include in your non-active record/controller classes to tell Rails... "Hey can you reload this when I'm in development mode?" I've know about this for a while, but by including "include 'Reloadable'" in my service classes I still wasn't getting any reload love. It turns out that I needed to add a few lines of code to WebORB to bring this feature to life. First you have to set:
Dependencies.mechanism = :load
This is the key condition to executing the reload bits within Rails. Secondly, I needed to change the way WebORB on Rails loads classes. Previously it did this simply by calling the require method on the file path. Now however it uses the rails dynamic require_or_load method.
This feature is only enabled in development mode, as it incurs a performance penalty to reload classes; it was interesting to find out that instead of any kind of file listener on classes that should be reloaded, Rails simply reloads everything that is "reloadable" on every request when in development mode.
The net/net of this is: If you want your service classes to automatically reload in development mode add this line of code to just inside your service class:
include Reloadable
Or, if you get tired of including Reloadable in each class, create a base service class the uses the Reloadable::Subclasses module and have all your services extend it:
class BaseService
include Reloadable::Subclasses
end
Now you can stop wasting that extra 10 to 15 seconds everytime you make a change!! This will make you a more productive programmer. Productive programmers are happy programers. So by using WebORB you will be happy! :-)







3 Comments:
It's great to see this is finally working. I was trying to include Reloadable trick a while back, but was getting exceptions. I look forward to giving this a shot.
1:08 PM
Thanks a lot
2:12 PM
What do you think on this? http://weblog.rubyonrails.org/2006/8/11/reloading-revamped
2:35 PM
Post a Comment
<< Home