EC2 ran out of memory while capistrano's assets: precompile was running, and as a result of restarting the instance, deploy ended halfway. If the web server runs based on the half-finished compilation result and a runtime error occurs, I can't see it, so I tried to get it back to a clean state.
rails (5.2.4.2)
capistrano (3.11.0)
sprockets (3.7.2)
sprockets-rails (3.2.1)
current_path is this path
shared_path should be this path
In conclusion, this is what you need to do.
There is a caveat.
Capistrano comes standard with commands like deploy: clobber_assets that seem to erase assets.
It means that you can erase it by hand without using it. The reason will be described later.
First, inside the / var / www / app_name of the server deployed by capistrano, the structure is as follows.
├── current (symlink is attached to the latest directory in releases)
├── releases
│   ├── 20200601101105
│   ├── 20200601102714
│   └── 20200601105159
├── repo
├── revisions.log
└── shared
Under this current, that is, under releases / 2006001105159, it will be configured as in your project's repository. There should be app /, config /, and public /.
Under this public /, there are assets / packed with built static files ... pretending to be
/ var / www / app_name / current / public / assets is a symbolic link to / var / www / app_name / shared / assets.
And / var / www / app_name / current / tmp / cache is also a symbolic link to / var / www / app_name / shared / tmp / cache.
[Rails Guide Asset Cache Store](https://railsguides.jp/asset_pipeline.html#%E3%82%A2%E3%82%BB%E3%83%83%E3%83%88%E3%81% AE% E3% 82% AD% E3% 83% A3% E3% 83% 83% E3% 82% B7% E3% 83% A5% E3% 82% B9% E3% 83% 88% E3% 82% A2)
The default Sprockets cache assets in tmp / cache / assets in development and production environments.
As it is written, there is a cache of the compilation result in tmp / cache / assets, so delete it.
If it is capistrano, / var / www / app_name / shared / tmp / cache / assets corresponds to this, so delete it.
Apart from the assets: precompile cache, Sprockets also keeps the deployment result files. The code for Sprockets says something like this:
module Sprockets
  module Exporters
    # Writes a an asset file to disk
    class FileExporter < Exporters::Base
      def skip?(logger)
        if ::File.exist?(target)
          logger.debug "Skipping #{ target }, already exists"
          true
        else
          logger.info "Writing #{ target }"
          false
        end
      end
      def call
        write(target) do |file|
          file.write(asset.source)
        end
      end
    end
  end
end
When deploy: assets: precompile, have you seen a bunch of logs in standard output? It is probably the above code that realizes that.
See logger.info" Writing .
Like this:
      01 I, [2020-06-01T08:56:09.691790 #10361]  INFO -- : Writing /var/www/app_name/releases/20200601085319/public/assets/develop/application-232387090aed00e6b038…
      01 I, [2020-06-01T08:56:09.692557 #10361]  INFO -- : Writing /var/www/app_name/releases/20200601085319/public/assets/develop/application-232387090aed00e6b038…
      01 I, [2020-06-01T08:56:17.418572 #10361]  INFO -- : Writing /var/www/app_name/releases/20200601085319/public/assets/lb-0002/application-d1bedc9937772b59211a…
      01 I, [2020-06-01T08:56:17.418856 #10361]  INFO -- : Writing /var/www/app_name/releases/20200601085319/public/assets/lb-0002/application-d1bedc9937772b59211a…
      01 I, [2020-06-01T08:56:21.414096 #10361]  INFO -- : Writing /var/www/app_name/releases/20200601085319/public/assets/lb-0003/application-5a60e0a26af1e40a9188…
      01 I, [2020-06-01T08:56:21.414368 #10361]  INFO -- : Writing /var/www/app_name/releases/20200601085319/public/assets/lb-0003/application-5a60e0a26af1e40a9188…
If you get stuck in this skip? Judgment.
In other words, in this example, if the file with digest of / var / www / app_name / releases / 20200601085319 / public / assets / develop / application-232387090aed00e6b038… already exists,
Sprockets does not export files.
So, with capistrano, if the same file exists in / var / www / app_name / shared / assets, it will be skipped by this skip ?.
You need to erase it.
capistrano will display all commands with cap -T.
You can find such a command in relation to assets.
cap deploy:cleanup_assets          # Cleanup expired assets
cap deploy:clobber_assets          # Clobber assets
Isn't the command deploy: clobber_assets prepared by default?
clobber means to hit. It seems that they will force you to make new assets! This is good! If you think about it and use it, it will not actually behave as you expected.
$ bundle exec cap review deploy:clobber_assets
00:00 deploy:clobber_assets
      01 bundle exec rake assets:clobber
      01 I, [2020-06-01T11:22:46.062204 #6756]  INFO -- : Removed /var/www/app_name/releases/20200601094959/public/assets
      01 Removed webpack output path directory /var/www/app_name/releases/20200601094959/public/packs
It seems that the assets are deleted together with the directory, but if you look closely, it is just a symbolic link that is removed, and the essential contents are not deleted. Really? And, in the first place, tmp / cache, which is the cache at compile time, is not deleted.
It seems that issue has also been raised about this.
So, as I wrote in the "Conclusion", it seems better to manually execute the command to delete it, or to define the command yourself.