Preloading a controller just once

I’m looking for a way to have a controller pre-loaded at application
startup and even in the development environment have it loaded just
once.

I’ll make it a bit more concrete. I’m in the process of writing a little
plugin for interrogating the state of a Rails app. Among other things,
it ties into request handling and provides a hook for gathering
statistics. The code for calculating a statistic I’d like to define
within a controller. Currently it looks like this

class StatusController < ApplicationController
include HealthCheck

define_check :database_available do
  ActiveRecord::Base.connection != nil
end

define_statistic :request_count, :accumulator => [0] do |

accumulator, *ignored|
accumulator[0] += 1
end
end

With Rails working as it does, this code is only loaded when the status
controller is accessed for the first time through a request. Only from
then on are the defined statistics updated. In the development
environment the accumulated values are forgotten again on the next
request as classes are reloaded then.

To circumvent these issues, I’d like to load this particular controller
on startup and don’t have it reloaded again. Any suggestions on how to
achieve this?

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/

Hello Michael,

2007/2/2, Michael S. [email protected]:

To circumvent these issues, I’d like to load this particular controller
on startup and don’t have it reloaded again. Any suggestions on how to
achieve this?

Did you try to require or reference StatusController in environment.rb
? That would at least load the controller. Then, the reloading
part…

http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/dependencies.rb#L44

We have a way to unload constants, but not the reverse. Maybe this will
help:
#will_unload?
http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/dependencies.rb#L288

Hope that helps !

François Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/

Hello Michael,

2007/2/2, Michael S. [email protected]:

When I require StatusController in environment.rb, reloading is no
longer a problem. But as I said above, I don’t like that I have to
require ApplicationController in addition. This appears to be causing
other mayhem, too. Just randomly clicking around in the app gave me an
exception: ArgumentError “A copy of AuthenticatedSystem has been
removed from the module tree but is still active!”. I’m pretty sure
this is related to the explicit require for ApplicationController,
which includes AuthenticatedSystem.

Instead of requiring “application”, try to reference
ApplicationController instead.

Bye !

François Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/

On Friday 02 February 2007 18:39, Francois B. wrote:

Hello Michael,

Hi Francois, thanks for helping.

2007/2/2, Michael S. [email protected]:

To circumvent these issues, I’d like to load this particular
controller on startup and don’t have it reloaded again. Any
suggestions on how to achieve this?

Did you try to require or reference StatusController in
environment.rb ? That would at least load the controller.

Yes, either results in an exception as the ApplicationController, the
superclass of StatusController, is not yet loaded and can’t be
autoloaded either, apparently. I can require ‘application’, of course,
but that prevents reloading of ApplicationController even in the
development environment, I gather.

Then, the reloading part…

When I require StatusController in environment.rb, reloading is no
longer a problem. But as I said above, I don’t like that I have to
require ApplicationController in addition. This appears to be causing
other mayhem, too. Just randomly clicking around in the app gave me an
exception: ArgumentError “A copy of AuthenticatedSystem has been
removed from the module tree but is still active!”. I’m pretty sure
this is related to the explicit require for ApplicationController,
which includes AuthenticatedSystem.

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/

On Friday 02 February 2007 22:25, Francois B. wrote:

ApplicationController, which includes AuthenticatedSystem.

Instead of requiring “application”, try to reference
ApplicationController instead.

This immediately results in an exception: “uninitialized constant
ApplicationController (NameError)”. I’ve tried it on a freshly
generated project, too, and got the same exception.

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/