【问题标题】:Is there a way to prevent rails to precompile assets on production?有没有办法防止 rails 在生产环境中预编译资产?
【发布时间】:2017-07-09 15:34:50
【问题描述】:

我的项目中有很多资产。服务器中的预编译任务非常慢并且耗尽了主机(CPU 利用率 100%,平均延迟高)。

我的想法是预编译本地主机中的所有资产并将所有已预编译的文件发送到 GIT(主)。

在部署操作(cap production deploy)中,避免预编译任务,在服务器中,阻止任何预编译任务。

服务器使用通过 capistrano 发送的已经预编译的文件,在 GIT 中可用。

有可能吗?如果是,怎么办? 如果不是,是否有另一种解决方案来避免服务器预编译资产?

在我的配置下:

宝石文件

gem 'capistrano-rails', group: :development
gem 'capistrano-faster-assets', '~> 1.0', group: :development

Capfile

require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/faster_assets'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

config/environments/production.rb

config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
config.assets.compile = true
config.assets.digest = true
other assets configs in this file is commented

环境信息

OS: Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-48-generic x86_64)
ruby -v: ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
rails -v: 4.2.3
nginx -v: nginx/1.8.0
passenger -v: 5.0.10

如果您需要更多信息,请在 cmets 上告诉我。

【问题讨论】:

    标签: ruby-on-rails-4 amazon-ec2 deployment capistrano3


    【解决方案1】:

    简答:

    替换

    require 'capistrano/rails'
    

    require 'capistrano/rails/migrations'
    require 'capistrano/bundler'
    

    为什么会这样:

    当您需要capistrano/rails 时,您实际上包括以下内容 (source):

    require 'capistrano/bundler'
    require 'capistrano/rails/assets'
    require 'capistrano/rails/migrations'
    

    每个都可以单独包含以获取这些功能。根据文档:https://github.com/capistrano/rails#usage

    【讨论】:

    • 你好@will_in_wi,谢谢你的回答。如我所见,在cap production deploy 中,不会发生资产预编译。然后我必须在本地预编译资产,发送到git master,然后进行部署,对吗?
    • 正确。如果我理解正确,您想在本地预编译您的资产,将它们提交给 master,然后让 Capistrano 跳过预编译,因为预编译的资产已经提交。我的回答告诉你如何跳过预编译。
    • 感谢您的解释。我将在明天进行测试,届时我将部署一些更改。您知道将config/environments/production.rb 中的config.assets.compile = true 更改为false 是否是个好主意吗?
    • 是的,您的解决方案运行良好!现在我在本地机器上执行rake assets precompile 并将所有已编译的资产发送到git master。我按照您的建议更改了Capfile,并且部署未运行precompile。我将 config/environments/production.rb 中的 config.assets.compile 更改为 FALSE,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2016-10-09
    • 1970-01-01
    • 2016-10-14
    相关资源
    最近更新 更多