2016-09-24 13:52:43 +02:00
|
|
|
module Hbc
|
|
|
|
class CLI
|
2017-05-20 19:08:03 +02:00
|
|
|
class Cleanup < AbstractCommand
|
2016-09-24 13:52:43 +02:00
|
|
|
OUTDATED_DAYS = 10
|
|
|
|
OUTDATED_TIMESTAMP = Time.now - (60 * 60 * 24 * OUTDATED_DAYS)
|
|
|
|
|
|
|
|
def self.help
|
|
|
|
"cleans up cached downloads and tracker symlinks"
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-05-21 00:15:56 +02:00
|
|
|
attr_reader :cache_location
|
2017-05-19 20:27:25 +02:00
|
|
|
|
2018-06-09 09:42:49 +02:00
|
|
|
def initialize(*args, cache_location: Cache.path)
|
2017-05-21 00:15:56 +02:00
|
|
|
super(*args)
|
2016-09-24 13:52:43 +02:00
|
|
|
@cache_location = Pathname.new(cache_location)
|
|
|
|
end
|
2016-08-23 01:43:03 +02:00
|
|
|
|
2017-05-19 20:27:25 +02:00
|
|
|
def run
|
2017-06-13 17:14:01 +02:00
|
|
|
remove_cache_files(*args)
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def cache_files
|
2018-06-09 10:41:14 +02:00
|
|
|
return [] unless cache_location.directory?
|
2016-09-24 13:52:43 +02:00
|
|
|
cache_location.children
|
|
|
|
.map(&method(:Pathname))
|
|
|
|
.reject(&method(:outdated?))
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def outdated?(file)
|
2017-05-21 00:15:56 +02:00
|
|
|
outdated_only? && file && file.stat.mtime > OUTDATED_TIMESTAMP
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def incomplete?(file)
|
|
|
|
file.extname == ".incomplete"
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def cache_incompletes
|
|
|
|
cache_files.select(&method(:incomplete?))
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def cache_completes
|
|
|
|
cache_files.reject(&method(:incomplete?))
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def disk_cleanup_size
|
2017-03-07 18:02:31 +01:00
|
|
|
cache_files.map(&:disk_usage).inject(:+)
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-23 01:43:03 +02:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def remove_cache_files(*tokens)
|
|
|
|
message = "Removing cached downloads"
|
|
|
|
message.concat " for #{tokens.join(", ")}" unless tokens.empty?
|
2017-05-21 00:15:56 +02:00
|
|
|
message.concat " older than #{OUTDATED_DAYS} days old" if outdated_only?
|
2016-09-24 13:52:43 +02:00
|
|
|
ohai message
|
2016-08-23 01:43:03 +02:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
deletable_cache_files = if tokens.empty?
|
2016-10-14 20:11:33 +02:00
|
|
|
cache_files
|
|
|
|
else
|
|
|
|
start_withs = tokens.map { |token| "#{token}--" }
|
|
|
|
|
2016-10-23 14:44:14 +02:00
|
|
|
cache_files.select do |path|
|
2016-10-14 20:11:33 +02:00
|
|
|
path.basename.to_s.start_with?(*start_withs)
|
2016-10-23 14:44:14 +02:00
|
|
|
end
|
2016-10-14 20:11:33 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
delete_paths(deletable_cache_files)
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def delete_paths(paths)
|
|
|
|
cleanup_size = 0
|
|
|
|
processed_files = 0
|
|
|
|
paths.each do |item|
|
|
|
|
next unless item.exist?
|
|
|
|
processed_files += 1
|
2017-05-22 04:15:11 +02:00
|
|
|
|
|
|
|
begin
|
|
|
|
LockFile.new(item.basename).with_lock do
|
|
|
|
puts item
|
2017-07-06 20:59:35 +02:00
|
|
|
cleanup_size += File.size(item)
|
|
|
|
item.rmtree
|
2017-05-22 04:15:11 +02:00
|
|
|
end
|
|
|
|
rescue OperationInProgressError
|
2016-09-24 13:52:43 +02:00
|
|
|
puts "skipping: #{item} is locked"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if processed_files.zero?
|
|
|
|
puts "Nothing to do"
|
|
|
|
else
|
|
|
|
disk_space = disk_usage_readable(cleanup_size)
|
|
|
|
ohai "This operation has freed approximately #{disk_space} of disk space."
|
|
|
|
end
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|