2

我的 Rails 4.1.8 应用程序在生产中表现不佳。当我出于某种奇怪的原因部署我的应用程序的新版本时,即使资产没有更改,我的所有资产也会重新指纹。这实质上是在每次部署时都会破坏所有资产的缓存。显然,这首先违背了使用资产管道的目的之一。

我在用着:

  • 导轨 4.1.8,红宝石 2.2.2
  • Capistrano 3.4 用于部署。
  • 链轮导轨 2.3.1

摘自production.rb:

config.assets.digest = true
config.serve_static_assets = true

配置/初始化程序/assets.rb

Rails.application.config.assets.version = '1.0'

头文件

require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rbenv'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

set :rbenv_type, :user 
set :rbenv_ruby, '2.2.2'

/config/deploy.rb

lock '3.4'
set :application, 'myapp'
set :repo_url, 'git@bitbucket.org:user/app.git'
set :deploy_to, '/home/deploy/app'

set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'tmp/uploads', 'vendor/bundle', 'public/system')
set :bundle_binstubs, nil
set :keep_releases, 5

SSHKit.config.command_map[:rake]  = 'bundle exec rake'
SSHKit.config.command_map[:rails] = 'bundle exec rails'

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      within "#{release_path}" do
        execute :bundle, "exec thin restart -O -C /etc/thin/app.yml"
    end
  end
end

after :publishing, :restart

部署时:

Me$: bin/cap production deploy

...
DEBUG [ff4ce05c] Running /usr/bin/env if test ! -d /home/deploy/app/releases/20150618015809; then echo "Directory does not exist '/home/deploy/app/releases/20150618015809'" 1>&2; false; fi as deploy@domain.com
DEBUG [ff4ce05c] Command: if test ! -d /home/deploy/app/releases/20150618015809; then echo "Directory does not exist '/home/deploy/app/releases/20150618015809'" 1>&2; false; fi
DEBUG [ff4ce05c] Finished in 0.370 seconds with exit status 0 (successful).
INFO [4ab0f9c7] Running ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile as deploy@domain.com
DEBUG [4ab0f9c7] Command: cd /home/deploy/app/releases/20150618015809 && ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.2 RAILS_ENV=staging ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile )
DEBUG [4ab0f9c7]    I, [2015-06-18T01:58:31.892846 #16404]  INFO -- : Writing /home/deploy/app/releases/20150618015809/public/assets/proximanova-bold-7c9c7de5d4c12790ac074e1c15637315.eot
DEBUG [4ab0f9c7]    I, [2015-06-18T01:58:31.894641 #16404]  INFO -- : Writing /home/deploy/app/releases/20150618015809/public/assets/proximanova-bold-8252ebfc2dfe42e154d8e8bcedbfd2f6.svg
DEBUG [4ab0f9c7]    I, [2015-06-18T01:58:31.896158 #16404]  INFO -- : Writing /home/deploy/app/releases/20150618015809/public/assets/proximanova-bold-cf546ab4766383966d7b70bd2749c011.ttf
DEBUG [4ab0f9c7]    I, [2015-06-18T01:58:31.897666 #16404]  INFO -- : Writing /home/deploy/app/releases/20150618015809/public/assets/proximanova-bold-4bafadbafe1d7f0915ef05f107e97a25.woff
DEBUG [4ab0f9c7]    I, [2015-06-18T01:58:31.899064 #16404]  INFO -- : Writing /home/deploy/app/releases/20150618015809/public/assets/proximanova-bold-3b37298fc9eb69093953f9885e889090.woff2
...

然后我第二次运行(不改变任何资产)

Me$: bin/cap production deploy

...
DEBUG [cbbe9a55] Running /usr/bin/env if test ! -d /home/deploy/app/releases/20150618020326; then echo "Directory does not exist '/home/deploy/app/releases/20150618020326'" 1>&2; false; fi as deploy@domain.com
DEBUG [cbbe9a55] Command: if test ! -d /home/deploy/app/releases/20150618020326; then echo "Directory does not exist '/home/deploy/app/releases/20150618020326'" 1>&2; false; fi
DEBUG [cbbe9a55] Finished in 0.369 seconds with exit status 0 (successful).
INFO [6e1eeadf] Running ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile as deploy@domain.com
DEBUG [6e1eeadf] Command: cd /home/deploy/app/releases/20150618020326 && ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.2 RAILS_ENV=staging ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile )
DEBUG [6e1eeadf]    I, [2015-06-18T02:03:48.365574 #16929]  INFO -- : Writing /home/deploy/app/releases/20150618020326/public/assets/proximanova-bold-78ec1c34931362f5fc9d1c64477d075f.eot
DEBUG [6e1eeadf]    I, [2015-06-18T02:03:48.367236 #16929]  INFO -- : Writing /home/deploy/app/releases/20150618020326/public/assets/proximanova-bold-6fadee20c88b51ed286db9687b131999.svg
DEBUG [6e1eeadf]    I, [2015-06-18T02:03:48.368709 #16929]  INFO -- : Writing /home/deploy/app/releases/20150618020326/public/assets/proximanova-bold-dc42499d91c401ef2e3452caa1917adf.ttf
DEBUG [6e1eeadf]    I, [2015-06-18T02:03:48.370105 #16929]  INFO -- : Writing /home/deploy/app/releases/20150618020326/public/assets/proximanova-bold-2d17c273eabf9ce851993897dd1fb300.woff
DEBUG [6e1eeadf]    I, [2015-06-18T02:03:48.371464 #16929]  INFO -- : Writing /home/deploy/app/releases/20150618020326/public/assets/proximanova-bold-7562093954d44696b2df7ada1488e2da.woff2
...

显然,所有资产都已重新指纹识别,但情况并非如此。

如果我登录到生产服务器,请删除 /public/assets 中的所有资产并运行以下命令:

RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.2 RAILS_ENV=production ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile

资产已正确编译。如果我再次运行相同的命令,则不会重新生成资产,这也是正确的行为。通过 Capistrano 部署时似乎无法获得正确的行为。

可能在配置中做一些明显不正确的事情,但到目前为止,尽管我尝试了无数的配置选项,但我一直无法缩小范围。

任何想法 Rails 大师?

4

2 回答 2

2

我发现了问题的路径......我的 angular-rails-templates gem - https://github.com/pitr/angular-rails-templates

每次编译 JS 模板时,gem 的 0.1.5 和 0.2.0 版本都会将校验和附加到 config.assets.version 字符串。即使没有资产发生变化,这也会在每次部署时破坏缓存。

临时解决方案是降级到版本 0.1.4,直到可以提供修复。见https://github.com/pitr/angular-rails-templates/issues/104

于 2015-06-29T11:04:53.293 回答
0

您可以尝试下载其中一个文件,重新部署,然后再次下载吗?对两个文件运行校验和(MD5、SHA256 等)并查看它们是否匹配。

校验和基于文件的内容。因此,如果文件得到不同的校验和,它们可能以某种方式具有不同的内容。您可以在其中两个上运行差异,可能是 SVG,因为 XML 的差异会更好。

于 2015-06-22T21:51:31.803 回答