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'
namespace :attachment_maintaining_service do
desc 'Removing attachments'
task remove_attachments: :environment do
sort_directories.first(10).each do |dir|
FileUtils.rm_r(dir)
task remove_attachments: :environment do |t, args|
args.with_defaults(limit: 10.gigabytes)
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
private
def sort_directories
Dir[dir_path].sort_by{ |f| File.atime(f) }
def attachments_size(dir)
`du -s #{dir} | cut -f 1`.to_i
end
def dir_path
Rails.root.join('public','attachments','*').to_s
def sort_attachments(attachments_path)
Dir[attachments_path].sort_by{ |f| File.atime(f) }
end
def attachments_path
Rails.root.join('public','attachments').to_s
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