如何调试无法忍受的Rails资产预编译

时间:2013-10-11 18:36:34

标签: ruby-on-rails debugging ruby-on-rails-3.2 rake asset-pipeline

我正在开发一个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.rbconfig/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

base 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

3 个答案:

答案 0 :(得分:32)

这可能不会完全回答你的问题,但我相信这是一个不错的开始。正如您将看到的,具体答案取决于具体应用,宝石版本等。

因此。对于与资产相关的工作,如你所知,Rails使用一个名为Sprockets的库,我相信在较新版本的Rails中,它作为一个Railtie挂钩到Rails。它初始化了一个Sprockets“环境”,它可以执行诸如查看资产清单,加载这些文件,压缩它们,为编译后的资产提供合理的名称等等。

默认情况下,Sprockets::Environment将其活动记录到STDERR,日志级别为FATAL,这在这些情况下并不十分有用。幸运的是,Sprockets::Environment(从2.2.2开始)具有可写的记录器属性,您可以使用初始化程序通过Rails修补。


所以,这就是我的建议,开始:

config/initializers中,创建一个类似asset_logging.rb的文件。在其中,放:

Rails.application.config.assets.logger = Logger.new($stdout)

这将覆盖默认记录器,并将更多信息泄露给STDOUT。完成此设置后,运行资产预编译任务:

rake RAILS_ENV=production assets:precompile

你应该看到稍微更有趣的输出,例如:

...
Compiled jquery.ui.core.js  (0ms)  (pid 66524)
Compiled jquery.ui.widget.js  (0ms)  (pid 66524)
Compiled jquery.ui.accordion.js  (10ms)  (pid 66524)
...

但是,最后,最终答案将取决于:

  • 您希望记录此资产的“深度”
  • 您正在使用的Rails,Sprockets等具体版本
  • 以及沿途发现的内容

正如您已经了解的那样,在Rake任务级别或甚至在Rails级别上记录日志,并没有提供太多信息。甚至让Sprockets本身冗长(见上文)并没有太多告诉你。

如果你想要比Sprockets更深入,你可以修补Sprockets整齐地链接在一起的各种引擎和处理器,以使资产管道工作。例如,您可以查看这些组件的日志记录功能:

  • Sass::Engine(将SASS转换为CSS)
  • Uglifier(JavaScript压缩包装器)
  • ExecJS(在Ruby中运行JavaScript; Sprockets和Uglifier的依赖项)
  • therubyracer(V8嵌入在Ruby中;由ExecJS使用)

但我会把所有这些都视为“读者的练习”。如果有一颗银弹,我当然想知道它!

答案 1 :(得分:2)

这个问题有很多可能的原因。

对于可能的原因,我想知道在上次部署的几个环境中编译资产的时间是如何增加的。这可能表明问题是在环境上还是在资产编译本身内。您可以使用git bisect。我通常会通过jenkins或其他ci系统部署我的应用程序,以便我可以看到部署时间和引入时间的任何变化。

它可能归结为大量使用资源CPU,MEMORY(任何交换?),IO。如果您在生产系统上编译资产,他们可能正忙于为您的应用程序请求提供服务。转到你的系统,为资源做一个top,也许同时有太多文件句柄(lsof对此有利)。

另一件事可能是您为应用程序加载或缓存了一些数据。数据库通常在登台和生产环境中大得多,然后它们在开发框上。您可以在初始化程序或whaterver中添加一些Rails.logger次调用。

答案 2 :(得分:2)

我认为您需要在Prod服务器上查看cpu使用参数。

此外,资产可能会多次预编译。 我建议在capistrano创建的共享目录中创建一个资产目录,复制你的更改并在部署时将其链接到你的应用程序。

继承人我是怎么做的,

  after "deploy:update_code" do
    run "export RAILS_ENV=production"
    run "ln -nfs #{shared_path}/public/assets #{release_path}/public/assets"
    # Also for logs and temp section.
    # run "ln -nfs #{shared_path}/log #{release_path}/log"
    # run "ln -nfs #{shared_path}/tmp #{release_path}/tmp"
    #sudo "chmod -R 0777 #{release_path}/tmp/"
    #sudo "chmod -R 0777 #{release_path}/log/"
  end