From 11f038aae9c993baee9344deb8717fa442a5ae55 Mon Sep 17 00:00:00 2001 From: Nikolai B Date: Tue, 26 May 2020 20:31:08 +0100 Subject: [PATCH 1/2] Fix spec assets:clean only removes assets older than 3600 seconds by default --- test/test_task.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test_task.rb b/test/test_task.rb index 81ee6f7b..bd1ba3e8 100644 --- a/test/test_task.rb +++ b/test/test_task.rb @@ -114,6 +114,8 @@ def test_clean_with_keep_specified FileUtils.cp(path, new_path) assert File.exist?(new_path) + mtime = Time.now - 3601 + File.utime(mtime, mtime, new_path.to_s) digest_path = @assets['foo-modified.js'].digest_path @rake['assets:precompile'].invoke @@ -135,7 +137,7 @@ def test_clean_with_keep_specified @rake['assets:clean'].invoke(0) assert File.exist?("#{@dir}/#{digest_path}") - # refute File.exist?("#{@dir}/#{old_digest_path}") + refute File.exist?("#{@dir}/#{old_digest_path}") ensure FileUtils.rm(new_path) if new_path end From 762cbde2260d63dc5f8c4895b5e0498218e306b0 Mon Sep 17 00:00:00 2001 From: Nikolai B Date: Tue, 26 May 2020 20:54:23 +0100 Subject: [PATCH 2/2] Allow age to be specified in assets:clean Before assets:clean would leave assets with an mtime less than 3600 seconds ago --- README.md | 2 +- lib/sprockets/rails/task.rb | 14 ++++++++++--- test/test_task.rb | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 45d2d35f..047c1025 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Deployment task that compiles any assets listed in `config.assets.precompile` to **`rake assets:clean`** -Only removes old assets (keeps the most recent 3 copies) from `public/assets`. Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled. +Only removes old assets (by default keeps the most recent 3 copies and files created within 3600 ) from `public/assets`. Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled. Use `rake assets:clean[3,3600]` to keep the last 3 assets or created within the last hour or `rake assets:clean[1,0]` to keep only the most recent old assets. This never cleans up the currently used assets. **`rake assets:clobber`** diff --git a/lib/sprockets/rails/task.rb b/lib/sprockets/rails/task.rb index 107db04b..8c22bc4a 100644 --- a/lib/sprockets/rails/task.rb +++ b/lib/sprockets/rails/task.rb @@ -68,10 +68,18 @@ def define end end - desc "Remove old compiled assets" - task :clean, [:keep] => :environment do |t, args| + desc "Remove old compiled assets. Arguments\nkeep: minimum previous copies to keep\nage: all assets with age less than this will be kept" + task :clean, [:keep, :age] => :environment do |_t, args| with_logger do - manifest.clean(Integer(args.keep || self.keep)) + age = + if args.age + args.age + elsif respond_to?(:age) + age + else + 3600 + end + manifest.clean(Integer(args.keep || keep), Integer(age)) end end diff --git a/test/test_task.rb b/test/test_task.rb index bd1ba3e8..d0982801 100644 --- a/test/test_task.rb +++ b/test/test_task.rb @@ -141,4 +141,45 @@ def test_clean_with_keep_specified ensure FileUtils.rm(new_path) if new_path end + + def test_clean_with_age_and_keep_specified + assert !@environment_ran + path = Pathname.new(@assets['foo.js'].filename) + new_path = path.join("../foo-modified.js") + + FileUtils.cp(path, new_path) + + assert File.exist?(new_path) + age = 3000 + mtime = Time.now - age + File.utime(mtime, mtime, new_path.to_s) + digest_path = @assets['foo-modified.js'].digest_path + @rake['assets:precompile'].invoke + assert File.exist?("#{@dir}/#{digest_path}") + assert @environment_ran + + # clean environment + setup + + # modify file + File.open(new_path, "a") {|f| f.write("var Bar;") } + @rake['assets:precompile'].invoke + old_digest_path = digest_path + digest_path = @assets['foo-modified.js'].digest_path + + refute_equal old_digest_path, digest_path + assert File.exist?("#{@dir}/#{old_digest_path}") + assert File.exist?("#{@dir}/#{digest_path}") + + @rake['assets:clean'].invoke(0, age + 500) + assert File.exist?("#{@dir}/#{digest_path}") + assert File.exist?("#{@dir}/#{old_digest_path}") + + @rake['assets:clean'].reenable + @rake['assets:clean'].invoke(0, age - 500) + assert File.exist?("#{@dir}/#{digest_path}") + refute File.exist?("#{@dir}/#{old_digest_path}") + ensure + FileUtils.rm(new_path) if new_path + end end