Rails 3.2.1,资产在部署时预编译了两次?

时间:2012-02-27 02:32:32

标签: ruby-on-rails capistrano asset-pipeline

我有这个Capistrano任务:

namespace :deploy do
  task :precompile, :role => :app do
    run "cd #{release_path}/ && RAILS_ENV=staging bundle exec rake assets:precompile --trace"
  end
end

after "deploy:finalize_update", "deploy:precompile"

我知道有load 'deploy/assets',但我正在努力了解这里发生了什么。

我正在部署到 Amazon EC2 m1.small 实例,该实例显然具有 50%的cpu窃取时间,已通过top验证。 这导致编译资产的时间增加,但请看一下:

    [23.21.xxx.xx] rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'ruby-1.9.3-p125' -c 'cd /home/ubuntu/apps/myapp-rails/releases/20120227020245/ && RAILS_ENV=staging bundle exec rake assets:precompile --trace'
 ** [out :: 23.21.xxx.xx] ** Invoke assets:precompile (first_time)
 ** [out :: 23.21.xxx.xx] ** Execute assets:precompile
 ** [out :: 23.21.xxx.xx] /home/ubuntu/.rvm/rubies/ruby-1.9.3-p125/bin/ruby /home/ubuntu/apps/myapp-rails/shared/bundle/ruby/1.9.1/bin/rake assets:precompile:all RAILS_ENV=staging RAILS_GROUPS=assets --trace
 ** [out :: 23.21.xxx.xx] ** Invoke assets:precompile:all (first_time)
 ** [out :: 23.21.xxx.xx] ** Execute assets:precompile:all
 ** [out :: 23.21.xxx.xx] ** Invoke assets:precompile:primary (first_time)
 ** [out :: 23.21.xxx.xx] ** Invoke assets:environment (first_time)
 ** [out :: 23.21.xxx.xx] ** Execute assets:environment
 ** [out :: 23.21.xxx.xx] ** Invoke environment (first_time)
 ** [out :: 23.21.xxx.xx] ** Execute environment
 ** [out :: 23.21.xxx.xx] ** Invoke tmp:cache:clear (first_time)
 ** [out :: 23.21.xxx.xx] ** Execute tmp:cache:clear
 ** [out :: 23.21.xxx.xx] ** Execute assets:precompile:primary
 ** [out :: 23.21.xxx.xx] ** Invoke assets:precompile:nondigest (first_time)
 ** [out :: 23.21.xxx.xx] ** Invoke assets:environment (first_time)
 ** [out :: 23.21.xxx.xx] ** Execute assets:environment
 ** [out :: 23.21.xxx.xx] ** Invoke environment (first_time)
 ** [out :: 23.21.xxx.xx] ** Execute environment
 ** [out :: 23.21.xxx.xx] ** Invoke tmp:cache:clear (first_time)
 ** [out :: 23.21.xxx.xx] ** Execute tmp:cache:clear
 ** [out :: 23.21.xxx.xx] ** Execute assets:precompile:nondigest
    command finished in 958131ms

除了疯狂花费在预编译资产上的时间由于某种原因我可以说它正在编译它们两次。为什么呢?

我正在使用Rails 3.2.1。 有人可以提供一些关于这里发生了什么的见解吗?这是打算吗?

staging.rb

  # 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

1 个答案:

答案 0 :(得分:10)

load 'deploy/assets'会在部署的相应部分自动为您预编译资产,因此您无需定义预编译任务。您可以删除预编译任务和after "deploy:finalize_update", "deploy:precompile"

https://github.com/capistrano/capistrano/blob/master/lib/capistrano/recipes/deploy/assets.rb

编辑:默认情况下,当您将摘要设置为true时,Rails将创建指纹文件和非指纹文件。它实际上并没有两次运行整个预编译任务,它只是为每种情况运行一个任务。

如果要完全禁用生成非指纹文件,则可以覆盖assets:precompile:all任务。

Rake::Task['assets:precompile:all'].clear
namespace :assets do
  namespace :precompile do
    task :all do
      Rake::Task['assets:precompile:primary'].invoke
      # ruby_rake_task("assets:precompile:nondigest", false) if Rails.application.config.assets.digest
    end
  end
end

注释掉的行是第66行:

https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/assets.rake