Knowing order of Rails 4 Engine or Railtie Initializer execution and their duration

In Rails 3 i found a nice article to know how to hook in your engine or railtie initializers.

It printed the order of initializer execution. With Rails 4 this did not work any longer.

But i since i did not need it any longer, i removed it.

Now i wanted to know how long did my initializers take? Reason: Somehow it got slower with some changes i did.

So i came up with this little piece of code that prints the name of the executed initializer and how long it took. So now i can detmine in the logs when a initializer was executed and which took too long.


module RailsInitializerTimeLogging
  module TimeLoggedInitializer
    def self.included(base)
      base.send :alias_method, :run_without_timelogging, :run
      base.send :alias_method, :run, :run_with_timelogging
    end

    def run_with_timelogging(*args)
      beginning = Time.now
      begin
        run_without_timelogging(*args)
      ensure
        puts "executed initializer: #{name} took #{Time.now - beginning} seconds"
      end
    end
  end
end

Rails::Initializable::Initializer.send :include, RailsInitializerTimeLogging::TimeLoggedInitializer

And require it before your application:


require 'rails_initializer_time_logging'

module YourApplication
  class Application < Rails::Application
    ....
  end
end