我正在开发一个 Rails 3.2 项目,尽管我认为项目规模不大,但最近几个月资产增加了不少。资产由 JS(无咖啡脚本)和 SASS 文件组成;我们有不少图片,但自早期以来它们出现的次数越来越少,所以我认为它们不是一个重要因素。我们可能有十几个库,大多数都很小,最大的是 Jquery UI JS。部署是通过 Capistrano 完成的,并且开始变得很明显,部署到登台比部署到生产要快得多。为了说明,同时避免有关不同服务器和网络影响的因素,我只是在我的笔记本电脑上按顺序运行以下三个命令,如下所示:
$ time RAILS_ENV=production bundle exec rake assets:precompile
^Crake aborted!
[Note I aborted this run as I felt it was getting stupidly long...]
real 52m33.656s
user 50m48.993s
sys 1m42.165s
$ time RAILS_ENV=staging bundle exec rake assets:precompile
real 0m41.685s
user 0m38.808s
sys 0m2.803s
$ time RAILS_ENV=development bundle exec rake assets:precompile
real 0m12.157s
user 0m10.567s
sys 0m1.531s
所以我只能摸不着头脑。为什么不同环境之间存在如此巨大的差异?我可以理解开发和登台之间的差距,但是我们的登台和生产配置是相同的。(我应该指出,生产编译将在大约 2 小时后完成!)
虽然最终结果是让我的预编译更快,但我想通过了解所有时间的去向以及为什么 Rails 环境之间存在如此大的差异来实现这一点。我看过其他关于使用不同压缩器等的帖子,但我找不到任何关于如何调试这些 rake 任务以找出时间花费在哪里并确定哪些设置可能导致如此巨大差异的信息。
我不知道人们可能需要什么其他信息,所以如果有评论询问,我会更新。TIA
更新:下面提供的其他信息
config/environments/production.rb
和config/environments/staging.rb
(它们完全相同):
MyRailsApp::Application.configure do
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
config.static_cache_control = "public, max-age=31536000"
config.action_controller.asset_host = "//#{MyRailsApp::CONFIG[:cdn]}"
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
基本的 config/application.rb 是:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module MyRailsApp
CONFIG = YAML.load_file(File.join(File.dirname(__FILE__), 'config.yml'))[Rails.env]
class Application < Rails::Application
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += %W(#{config.root}/app/workers)
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable the asset pipeline
config.assets.enabled = true
# Stop precompile from looking for the database
config.assets.initialize_on_precompile = false
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
# Fix fonts in assets pipeline
# http://stackoverflow.com/questions/6510006/add-a-new-asset-path-in-rails-3-1
config.assets.paths << Rails.root.join('app','assets','fonts')
config.middleware.insert 0, 'Rack::Cache', {
:verbose => true,
:metastore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/meta"),
:entitystore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/body")
} # unless Rails.env.production? ## uncomment this 'unless' in Rails 3.1,
## because it already inserts Rack::Cache in production
config.middleware.insert_after 'Rack::Cache', 'Dragonfly::Middleware', :images
config.action_mailer.default_url_options = { :host => CONFIG[:email][:host] }
config.action_mailer.asset_host = 'http://' + CONFIG[:email][:host]
end
end
宝石文件:
source 'http://rubygems.org'
gem 'rails', '3.2.13'
gem 'mysql2'
gem 'dragonfly', '>= 0.9.14'
gem 'rack-cache', :require => 'rack/cache'
gem 'will_paginate'
gem 'dynamic_form'
gem 'amazon_product' # for looking up Amazon ASIN codes of books
gem 'geoip'
gem 'mobile-fu'
gem 'airbrake'
gem 'newrelic_rpm'
gem 'bartt-ssl_requirement', '~>1.4.0', :require => 'ssl_requirement'
gem 'dalli' # memcache for api_cache
gem 'api_cache'
gem 'daemons'
gem 'delayed_job_active_record'
gem 'attr_encrypted'
gem 'rest-client'
gem 'json', '>= 1.7.7'
gem 'carrierwave' # simplify file uploads
gem 'net-scp'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'therubyracer'
gem 'sass-rails', '~> 3.2.3'
gem 'compass', '~> 0.12.alpha'
gem 'uglifier', '>= 1.0.3'
gem 'jquery-fileupload-rails'
end
gem 'jquery-rails'
gem 'api_bee', :git => 'git://github.com/ismasan/ApiBee.git', :ref => '3cff959fea5963cf46b3d5730d68927cebcc59a8'
gem 'httparty', '>= 0.10.2'
gem 'twitter'
# Auth providers
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem 'omniauth-google-oauth2'
gem 'omniauth-identity'
gem 'omniauth-readmill'
gem 'bcrypt-ruby', "~> 3.0.0" # required for omniauth-identity
gem 'mail_view'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# Deploy with Capistrano
group :development do
gem 'capistrano'
gem 'capistrano-ext'
gem 'capistrano_colors'
gem 'rvm-capistrano'
# requirement for Hoof, Linux equivalent of Pow
gem 'unicorn'
end
group :test, :development do
gem 'rspec-rails'
gem 'pry'
gem 'pry-rails'
end
group :test do
gem 'factory_girl_rails'
gem 'capybara'
gem 'cucumber-rails'
gem 'database_cleaner'
gem 'launchy'
gem 'ruby-debug19'
# Pretty printed test output
gem 'shoulda-matchers'
gem 'simplecov', :require => false
gem 'email_spec'
gem 'show_me_the_cookies'
gem 'vcr'
gem 'webmock', '1.6'
end