2015-02-04 15:05:17 +08:00
|
|
|
class DiskSpace
|
|
|
|
|
|
|
|
extend ActionView::Helpers::NumberHelper
|
|
|
|
|
2018-10-15 09:43:31 +08:00
|
|
|
DISK_SPACE_STATS_CACHE_KEY ||= 'disk_space_stats'.freeze
|
|
|
|
DISK_SPACE_STATS_UPDATED_CACHE_KEY ||= 'disk_space_stats_updated'.freeze
|
2018-07-25 20:26:30 +08:00
|
|
|
|
2015-02-04 15:05:17 +08:00
|
|
|
def self.uploads_used_bytes
|
2015-02-04 18:37:02 +08:00
|
|
|
# used(uploads_path)
|
|
|
|
# temporary (on our internal setup its just too slow to iterate)
|
|
|
|
Upload.sum(:filesize).to_i
|
2015-02-04 15:05:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.uploads_free_bytes
|
2015-02-04 18:37:02 +08:00
|
|
|
free(uploads_path)
|
2015-02-04 15:05:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.backups_used_bytes
|
|
|
|
used(backups_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.backups_free_bytes
|
|
|
|
free(backups_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.backups_path
|
2018-10-15 09:43:31 +08:00
|
|
|
BackupRestore::LocalBackupStore.base_directory
|
2015-02-04 15:05:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.uploads_path
|
|
|
|
"#{Rails.root}/public/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stats
|
|
|
|
{
|
|
|
|
uploads_used: number_to_human_size(uploads_used_bytes),
|
|
|
|
uploads_free: number_to_human_size(uploads_free_bytes),
|
|
|
|
backups_used: number_to_human_size(backups_used_bytes),
|
|
|
|
backups_free: number_to_human_size(backups_free_bytes)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-07-25 20:26:30 +08:00
|
|
|
def self.reset_cached_stats
|
2018-08-01 21:45:50 +08:00
|
|
|
Discourse.cache.delete(DISK_SPACE_STATS_UPDATED_CACHE_KEY)
|
|
|
|
Discourse.cache.delete(DISK_SPACE_STATS_CACHE_KEY)
|
2018-07-25 20:26:30 +08:00
|
|
|
end
|
|
|
|
|
2015-02-04 15:05:17 +08:00
|
|
|
def self.cached_stats
|
2018-08-01 21:45:50 +08:00
|
|
|
stats = Discourse.cache.read(DISK_SPACE_STATS_CACHE_KEY)
|
|
|
|
updated_at = Discourse.cache.read(DISK_SPACE_STATS_UPDATED_CACHE_KEY)
|
2015-02-04 15:05:17 +08:00
|
|
|
|
|
|
|
unless updated_at && (Time.now.to_i - updated_at.to_i) < 30.minutes
|
2018-07-25 23:04:01 +08:00
|
|
|
Jobs.enqueue(:update_disk_space)
|
2015-02-04 15:05:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if stats
|
|
|
|
JSON.parse(stats)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def self.free(path)
|
2016-01-25 10:22:36 +08:00
|
|
|
`df -Pk #{path} | awk 'NR==2 {print $4;}'`.to_i * 1024
|
2015-02-04 15:05:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.used(path)
|
2016-01-25 10:03:25 +08:00
|
|
|
`du -s #{path}`.to_i * 1024
|
2015-02-04 15:05:17 +08:00
|
|
|
end
|
|
|
|
end
|