Skip to content

Commit

Permalink
Delete attachments that have been unused for 24 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
12joan committed Nov 26, 2023
1 parent 44d3f90 commit d835ae4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/models/clean_up_unused_attachments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module CleanUpUnusedAttachments
def self.perform
S3File
.attachments
.where('became_unused_at < ?', 24.hours.ago)
.where(do_not_delete_unused: false)
.find_each do |s3_file|
# Double check became_unused_at
s3_file.update_unused
s3_file.destroy! if s3_file.unused?
end
end
end
1 change: 1 addition & 0 deletions config/clockwork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Clockwork
isolate_errors { CleanUpUnuploadedS3Files.perform }
isolate_errors { CleanUpUntrackedS3Objects.perform }
isolate_errors { CleanUpBlankDocuments.perform }
isolate_errors { CleanUpUnusedAttachments.perform }
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
content_type { 'image/png' }
end

factory :documents_s3_file do
document
s3_file
end

factory :settings do
user
data { { hello: 'world' }.to_json }
Expand Down
50 changes: 50 additions & 0 deletions test/models/clean_up_unused_attachments_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'test_helper'

class CleanUpUnusedAttachmentsTest < ActiveSupport::TestCase
setup do
@user = create(:user)
@project = create(:project, owner: @user)
@document = create(:document, project: @project)
@s3_file = create(:s3_file, role: 'attachment', became_unused_at: 25.hours.ago, owner: @user)
end

test 'should delete unused attachments' do
perform
assert destroyed?, 'unused attachment should be deleted'
end

test 'should not delete used attachments, even if became_unused_at is wrong' do
create(:documents_s3_file, document: @document, s3_file: @s3_file)
@s3_file.update!(became_unused_at: 25.hours.ago)
perform
assert_not destroyed?, 'used attachment should not be deleted'
end

test 'should not delete recently used attachments' do
@s3_file.update!(became_unused_at: 23.hours.ago)
perform
assert_not destroyed?, 'recently used attachment should not be deleted'
end

test 'should not delete non-attachment s3 files' do
@s3_file.update!(role: 'project-image')
perform
assert_not destroyed?, 'non-attachment s3 file should not be deleted'
end

test 'should not delete attachments marked as do_not_delete_unused' do
@s3_file.update!(do_not_delete_unused: true)
perform
assert_not destroyed?, 'attachment marked as do_not_delete_unused should not be deleted'
end

private

def perform
ignore_s3 { CleanUpUnusedAttachments.perform }
end

def destroyed?
!S3File.find_by(id: @s3_file.id)
end
end

0 comments on commit d835ae4

Please sign in to comment.