Skip to content
Snippets Groups Projects
Commit f32036eb authored by Giovanne Marcelo's avatar Giovanne Marcelo
Browse files

now the task only executes if the size of attachments exceed the limit

parent 75bd1262
No related branches found
No related tags found
No related merge requests found
...@@ -3,20 +3,28 @@ require 'fileutils' ...@@ -3,20 +3,28 @@ require 'fileutils'
namespace :attachment_maintaining_service do namespace :attachment_maintaining_service do
desc 'Removing attachments' desc 'Removing attachments'
task remove_attachments: :environment do task remove_attachments: :environment do |t, args|
sort_directories.first(10).each do |dir| args.with_defaults(limit: 10.gigabytes)
FileUtils.rm_r(dir) path = attachments_path + "/*"
if attachments_size(attachments_path) > args.limit
sort_attachments(path).first(10).each do |dir|
FileUtils.rm_r(dir)
end
end end
end end
private private
def sort_directories def attachments_size(dir)
Dir[dir_path].sort_by{ |f| File.atime(f) } `du -s #{dir} | cut -f 1`.to_i
end end
def dir_path def sort_attachments(attachments_path)
Rails.root.join('public','attachments','*').to_s Dir[attachments_path].sort_by{ |f| File.atime(f) }
end
def attachments_path
Rails.root.join('public','attachments').to_s
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment