Skip to content
Browse files

Revert "Removed time-based expiry for Heroku PR"

This reverts commit 5513ea9.

Conflicts:
	lib/turbo-sprockets/tasks/assets.rake
  • Loading branch information...
1 parent e327ae2 commit 4990fd0fc6a9cdc254882185df3a17dcaaff95e7 @ndbroadbent committed
Showing with 36 additions and 11 deletions.
  1. +36 −11 lib/turbo-sprockets/tasks/assets.rake
View
47 lib/turbo-sprockets/tasks/assets.rake
@@ -35,6 +35,15 @@ namespace :assets do
end
end
+ # Returns an array of assets recognized by config.assets.digests,
+ # including gzipped assets and manifests
+ def known_assets
+ assets = Rails.application.config.assets.digests.to_a.flatten.map do |asset|
+ [asset, "#{asset}.gz"]
+ end.flatten
+ assets + %w(manifest.yml sources_manifest.yml)
+ end
+
desc "Compile all the assets named in config.assets.precompile"
task :precompile do
invoke_or_reboot_rake_task "assets:precompile:all"
@@ -61,6 +70,18 @@ namespace :assets do
env = Rails.application.assets
target = File.join(::Rails.public_path, config.assets.prefix)
+ # Before first compile, set the mtime of all current assets to current time.
+ # This time reflects the last time the assets were being used.
+ if digest.nil?
+ ::Rails.logger.debug "Updating mtimes for current assets..."
+ known_assets.each do |asset|
+ full_path = File.join(target, asset)
+ if File.exist?(full_path)
+ File.utime(Time.now, Time.now, full_path)
+ end
+ end
+ end
+
# If processing non-digest assets, and compiled digest files are
# present, then generate non-digest assets from existing assets.
# It is assumed that `assets:precompile:nondigest` won't be run manually
@@ -107,26 +128,30 @@ namespace :assets do
invoke_or_reboot_rake_task "assets:clean_expired:all"
end
- # Remove assets that aren't referenced by manifest.yml
+ # Remove assets that haven't been deployed since `config.assets.expire_after` (default 7 days).
+ # The precompile task updates the mtime of the current assets before compiling,
+ # which indicates when they were last in use.
+ #
+ # The current assets are ignored, which is faster than the alternative of
+ # setting their mtimes only to check them again.
namespace :clean_expired do
task :all => ["assets:environment"] do
config = ::Rails.application.config
+ expire_after = config.assets.expire_after || 1.day
public_asset_path = File.join(::Rails.public_path, config.assets.prefix)
- # Build an array of assets recognized by config.assets.digests,
- # including gzipped assets and manifests
- known_assets = Rails.application.config.assets.digests.to_a.flatten.map do |asset|
- [asset, "#{asset}.gz"]
- end.flatten
- known_assets += %w(manifest.yml sources_manifest.yml)
+ @known_assets = known_assets
Dir.glob(File.join(public_asset_path, '**/*')).each do |asset|
next if File.directory?(asset)
logical_path = asset.sub("#{public_asset_path}/", '')
- # Delete asset if not found in known_assets
- unless logical_path.in?(known_assets)
- ::Rails.logger.debug "Removing unreferenced asset: #{logical_path}"
- FileUtils.rm_f asset
+
+ unless logical_path.in?(@known_assets)
+ # Delete asset if not used for more than expire_after seconds
+ if File.mtime(asset) < (Time.now - expire_after)
+ ::Rails.logger.debug "Removing expired asset: #{logical_path}"
+ FileUtils.rm_f asset
+ end
end
end
end

0 comments on commit 4990fd0

Please sign in to comment.
Something went wrong with that request. Please try again.