【问题标题】:Rails 3.2.1, assets precompiled twice on deploy?Rails 3.2.1,资产在部署时预编译了两次?
【发布时间】:2012-02-27 02:32:31
【问题描述】:

我有这个 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

【问题讨论】:

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


    【解决方案1】:

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

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

    edit:默认情况下,当您将摘要设置为 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

    【讨论】:

    • 我在问题中提到我知道这个 capistrano 任务。我将其关闭是因为 1) 我在使用它和精灵时遇到问题 2) 我想清楚地检查发生了什么。
    • 谢谢;尽管为每个“情况”运行一项任务对我来说似乎很愚蠢,但为什么在地球上的轨道上无法检查选项并采取相应的行动只运行一项任务它超出了我的范围......如果你有任何其他关于什么摘要、非摘要、主要或为什么的信息有人想要非摘要和东西,请分享。
    • 凯恩我同意。我觉得更明智的行为是预编译一次并使用该单一输出创建摘要和非摘要文件。这显然会使任务的速度加倍。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2017-12-12
    相关资源
    最近更新 更多