Adds a rake task, and a task to whenevers schedule, to delete all zips in public/data/zip older than 2 days

This commit is contained in:
Philipp Bayer
2014-06-16 16:13:04 +10:00
parent a6f5dcbc13
commit ba38ccf1dd
2 changed files with 26 additions and 0 deletions

View File

@@ -22,3 +22,7 @@
every :day, at: '1am' do
rake 'dump:full', environment: 'production'
end
every 7.days, at: '1am' do
rake 'dump:clean', environment: 'production'
end

22
lib/tasks/dump_clean.rake Normal file
View File

@@ -0,0 +1,22 @@
namespace :dump do
desc 'clean everything in public/zip older than 2 days'
task clean: :environment do
dir = File.join(Rails.root, 'public/data/zip')
# Don't ever delete the link and what is linked to
datadump = File.join(dir, 'opensnp_datadump.current.zip')
forbidden_files = [datadump]
forbidden_files << File.readlink(datadump) unless not File.file? datadump
Dir.entries(dir).each do |f|
f = File.join(dir, f)
# Has to be older than 2 days, don't delete important files, only delete archives
if (get_file_age_in_days(f) > 2) and (not forbidden_files.include? f) and (f.ends_with? 'zip')
File.delete(f)
end
end
end
end
def get_file_age_in_days(file)
(Time.now - File.mtime(file)) / (60 * 60 * 24)
end