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

Stale PID-Files in development

Somehow in my Development-Environment (Windows with Eclipse) webrick comes up with stale PID-Files.

I stop the Server, start it again and sometimes (not always) i get the message:

A server is already running.

This happend since i updated to rails-Version X.

But now i finally figured out (after googling and googling and finding nothing helpful) how to make it work again.

In application.rb before declaration of your own application insert:


require 'stale_pid_cleaner'
StalePidCleaner.check

StalePidCleaner:


class StalePidCleaner
  def self.check
    fName = "./tmp/pids/server.pid"
    if(File.exists?(fName))
      puts "found pidfile: " + fName.to_s
      pid = File.read(fName).to_i
      if(pid < 1)
        puts "invalid pid? " + pid.to_s
      else
        if(!alive?(pid))
          puts "cleaning stale pidfile"
          File.delete(fName)
        end
      end
    end
  end

  private
  def self.alive?(pid)
    begin
      Process.kill(0, pid)
      true
     rescue Errno::ESRCH, TypeError # Process is dead
       false
     end
   end
end

Hope it helps also someone else

Paperclip 4.0 and non deactivatable MediaType Spoofing

Paperclip 4 has an enhancement to detect MediaType Spoofing.

I’m with them, security is good and it’s better something like this is integrated.

But it was a change that you cannot turn off and when it does not work, you’ve got to live with it.

In my case:

  • Tests started failing, even when type was correctly set
  • The command that was executed did not work / was not found (did not look deeper into it)
  • SWFUpload and Paperclip and Spoofing Detection did not seem to work together.

So till it can be officially turned off (found an issue where it seems they want to integrate this) i came up with this as an solution:


module SpoofingFix
  def self.included(base)
    base.send :alias_method, :original_spoofed?, :spoofed?
    base.send :alias_method, :spoofed?, :fixed_spoofed?
  end

  def fixed_spoofed?
    false
  end

end

Paperclip::MediaTypeSpoofDetector.send :include, SpoofingFix

We just say it is always unspoofed and so we can work like before 😉