allenwoot
allenwoot

Reputation: 961

Image assets not showing for rails 4 app deployed with capistrano 3

I have a very basic rails 4 app that displays static images. I set up deploys using cap 3. Deploys appear to be successful, but the image assets are not showing up on the site. On my server, the /home/deploy/project_name/current/public/assets folder contains the png files, so it looks like the asset pipeline is working in some way. What is the url to the image assets from the host IP address? How can I debug this further?

The nginx error log shows the following when the assets are requested:

2016/01/02 08:16:52 [error] 27418#0: *19 open() "/home/deploy/myapp/public/assets/det1-766379905061a5c18b06fd8bae4d21e69304a6e3cedd5d5665a0f953d5743f0e.png" failed (2: No such file or directory), client: 45.50.73.43, server: localhost, request: "GET /assets/det1-766379905061a5c18b06fd8bae4d21e69304a6e3cedd5d5665a0f953d5743f0e.png HTTP/1.1", host: "52.23.158.11", referrer: "http://[ip]/det"

This file is present in current/public/assets folder with full read permissions, yet nginx is still unable to find it.

Capfile

require 'capistrano/setup'
require 'capistrano/deploy'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/rails'
require 'capistrano/rails/assets' # for asset handling add
require 'capistrano/rails/migrations' # for running migrations
require 'capistrano/puma'

config/deploy.rb

lock '3.4.0'

set :application, 'allenwoot'
set :repo_url, '[email protected]:allenwoot/allenwoot.git'
set :branch, :master
set :deploy_to, '/home/deploy/allenwoot'
set :pty, true
set :linked_files, %w{config/database.yml config/application.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
set :keep_releases, 5
set :rvm_type, :user
set :rvm_ruby_version, 'ruby-2.2.1'

set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"    #accept array for multi-bind
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_threads, [0, 8]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_preload_app, false

EDIT: I noticed that the nginx logs indicate that it was looking for the file at location /home/deploy/myapp/public/assets/det1-766379905061a5c18b06fd8bae4d21e69304a6e3cedd5d5665a0f953d5743f0e.png It is present at /home/deploy/myapp/current/public/assets/det1-766379905061a5c18b06fd8bae4d21e69304a6e3cedd5d5665a0f953d5743f0e.png

I added a post deploy hook in capistrano to copy it over, which does fix it, but it feels hacky. Is there a better way to do this?

Upvotes: 1

Views: 1344

Answers (1)

will_in_wi
will_in_wi

Reputation: 2653

The asset pipeline works by processing and copying the files to the server, so that each file has a hash computed using the contents of the file and some other information. The mapping of filename to hashed filename is stored in public/assets/.sprockets-manifest-[somehash].json.

Thus, if the wrong assets are being referenced, it means that one of the following is most likely true:

  • Sprockets manifest is pointing at the wrong file. This can happen if the sprockets compilation cache is not clearing correctly: delete the contents of tmp/cache/assets, and redeploy/recompile assets. If that fixes it, you probably need to upgrade sprockets, there is a bug in an older version.
  • The server hasn't restarted since deploy and thus hasn't picked up the new files.

Upvotes: 0

Related Questions