简体   繁体   中英

Ruby 1.9.3, Rails 3.2.12, OSX Lion - bootstrapping Rails for command-line scripts slow

Bootstrapping Rails in command-line scripts takes a long time (2.5+ seconds), for example:

timer = Time.now  
ENV["RAILS_ENV"] ||= "development"  
require File.expand_path('../../config/application', __FILE__)  
Rails.application.require_environment!  
puts "Init complete in #{Time.now - timer} sec"  
# -> Init complete in 2.621531 sec  

Am I missing something ? If not, do I really need to bootstrap everything to get access to the models ?

I don't have a lot of experience with this, but perhaps you should take a look at trimming down the number of libraries that are required in application.rb :

# require 'rails/all' # remove this and only require what you need
require 'activerecord/railtie' # this may be all you need

Additionally, take a look at the gems you are loading and see if any of them are taking an inordinate amount of time . If a particular gem is slowing down boot time, simply add:

# you will need to explicitly load the gem when needed,
# but it won't be required at boot time 
gem some_gem, :require => false

You could create a special environment that you run the script in and customize the boot process for that environment.

You should profile that block of code and see where most of the time is spent.

What exactly are you trying to get out of booting rails? Access to the models? the underlying data? Perhaps there's an alternate approach than booting all of rails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM