mirror of
https://github.com/discourse/discourse.git
synced 2025-02-27 04:21:52 +08:00
Fix and improve s3:expire_missing_assets
task (#18863)
- Ensure it works with prefixed S3 buckets - Perform a sanity check that all current assets are present on S3 before starting deletion - Remove the lifecycle rule configuration and delete expired assets immediately. This task should be run post-deploy anyway, so adding a 10-day window is not required
This commit is contained in:
parent
4e4302c608
commit
e8570b5cc9
@ -10,11 +10,18 @@ def gzip_s3_path(path)
|
|||||||
"#{path[0..-ext.length]}gz#{ext}"
|
"#{path[0..-ext.length]}gz#{ext}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def existing_assets
|
||||||
|
@existing_assets ||= Set.new(helper.list("assets/").map(&:key))
|
||||||
|
end
|
||||||
|
|
||||||
|
def prefix_s3_path(path)
|
||||||
|
path = File.join(helper.s3_bucket_folder_path, path) if helper.s3_bucket_folder_path
|
||||||
|
path
|
||||||
|
end
|
||||||
|
|
||||||
def should_skip?(path)
|
def should_skip?(path)
|
||||||
return false if ENV['FORCE_S3_UPLOADS']
|
return false if ENV['FORCE_S3_UPLOADS']
|
||||||
@existing_assets ||= Set.new(helper.list("assets/").map(&:key))
|
existing_assets.include?(prefix_s3_path(path))
|
||||||
path = File.join(helper.s3_bucket_folder_path, path) if helper.s3_bucket_folder_path
|
|
||||||
@existing_assets.include?(path)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def upload(path, remote_path, content_type, content_encoding = nil)
|
def upload(path, remote_path, content_type, content_encoding = nil)
|
||||||
@ -197,26 +204,28 @@ end
|
|||||||
task 's3:expire_missing_assets' => :environment do
|
task 's3:expire_missing_assets' => :environment do
|
||||||
ensure_s3_configured!
|
ensure_s3_configured!
|
||||||
|
|
||||||
count = 0
|
assets_to_delete = existing_assets.dup
|
||||||
keep = 0
|
|
||||||
|
|
||||||
in_manifest = asset_paths
|
# Check that all current assets are uploaded, and remove them from the to_delete list
|
||||||
|
asset_paths.each do |current_asset_path|
|
||||||
|
uploaded = assets_to_delete.delete?(prefix_s3_path(current_asset_path))
|
||||||
|
if !uploaded
|
||||||
|
puts "A current asset does not exist on S3 (#{current_asset_path}). Aborting cleanup task."
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
puts "Ensuring AWS assets are tagged correctly for removal"
|
if assets_to_delete.size > 0
|
||||||
helper.list('assets/').each do |f|
|
puts "Found #{assets_to_delete.size} assets to delete..."
|
||||||
if !in_manifest.include?(f.key)
|
assets_to_delete.each do |to_delete|
|
||||||
helper.tag_file(f.key, old: true)
|
if !to_delete.start_with?(prefix_s3_path("assets/"))
|
||||||
count += 1
|
# Sanity check, this should never happen
|
||||||
|
raise "Attempted to delete a non-/asset S3 path (#{to_delete}). Aborting"
|
||||||
|
end
|
||||||
|
puts "Deleting #{to_delete}..."
|
||||||
|
helper.delete_object(to_delete)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
# ensure we do not delete this by mistake
|
puts "No stale assets found"
|
||||||
helper.tag_file(f.key, {})
|
|
||||||
keep += 1
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
puts "#{count} assets were flagged for removal in 10 days (#{keep} assets will be retained)"
|
|
||||||
|
|
||||||
puts "Ensuring AWS rule exists for purging old assets"
|
|
||||||
helper.update_lifecycle("delete_old_assets", 10, tag: { key: 'old', value: 'true' })
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user