FIX: makes disk_space computation more resilient (#6172)
This commit is contained in:
parent
fa399ce1c5
commit
796639a797
|
@ -0,0 +1,12 @@
|
|||
require 'disk_space'
|
||||
|
||||
module Jobs
|
||||
class UpdateDiskSpace < Jobs::Base
|
||||
sidekiq_options retry: false
|
||||
|
||||
def execute(args)
|
||||
$redis.set(DiskSpace::DISK_SPACE_STATS_CACHE_KEY, DiskSpace.stats.to_json)
|
||||
$redis.set(DiskSpace::DISK_SPACE_STATS_UPDATED_CACHE_KEY, Time.now.to_i)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -43,7 +43,7 @@ class DiskSpace
|
|||
def self.reset_cached_stats
|
||||
$redis.del(DISK_SPACE_STATS_UPDATED_CACHE_KEY)
|
||||
$redis.del(DISK_SPACE_STATS_CACHE_KEY)
|
||||
compute_disk_space
|
||||
Jobs.enqueue(:update_disk_space)
|
||||
end
|
||||
|
||||
def self.cached_stats
|
||||
|
@ -51,7 +51,7 @@ class DiskSpace
|
|||
updated_at = $redis.get(DISK_SPACE_STATS_UPDATED_CACHE_KEY)
|
||||
|
||||
unless updated_at && (Time.now.to_i - updated_at.to_i) < 30.minutes
|
||||
compute_disk_space
|
||||
Jobs.enqueue(:update_disk_space)
|
||||
end
|
||||
|
||||
if stats
|
||||
|
@ -61,15 +61,6 @@ class DiskSpace
|
|||
|
||||
protected
|
||||
|
||||
def self.compute_disk_space
|
||||
Scheduler::Defer.later 'updated stats' do
|
||||
$redis.set(DISK_SPACE_STATS_CACHE_KEY, self.stats.to_json)
|
||||
$redis.set(DISK_SPACE_STATS_UPDATED_CACHE_KEY, Time.now.to_i)
|
||||
end
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
def self.free(path)
|
||||
`df -Pk #{path} | awk 'NR==2 {print $4;}'`.to_i * 1024
|
||||
end
|
||||
|
|
|
@ -105,7 +105,11 @@ describe Backup do
|
|||
s3_bucket.expects(:object).with(b1.filename).returns(s3_object)
|
||||
s3_object.expects(:delete)
|
||||
|
||||
b1.after_remove_hook
|
||||
expect {
|
||||
b1.after_remove_hook
|
||||
}.to change {
|
||||
Jobs::UpdateDiskSpace.jobs.size
|
||||
}.by(0)
|
||||
end
|
||||
|
||||
context "when s3_backup_bucket includes folders path" do
|
||||
|
@ -125,10 +129,23 @@ describe Backup do
|
|||
end
|
||||
end
|
||||
|
||||
it "calls remove_from_s3 if the SiteSetting is false" do
|
||||
SiteSetting.enable_s3_backups = false
|
||||
b1.expects(:remove_from_s3).never
|
||||
b1.after_remove_hook
|
||||
context "when SiteSetting is false" do
|
||||
before do
|
||||
SiteSetting.enable_s3_backups = false
|
||||
end
|
||||
|
||||
it "doesn’t call remove_from_s3" do
|
||||
b1.expects(:remove_from_s3).never
|
||||
b1.after_remove_hook
|
||||
end
|
||||
|
||||
it "refreshes disk space if not backuping on s3" do
|
||||
expect {
|
||||
b1.after_remove_hook
|
||||
}.to change {
|
||||
Jobs::UpdateDiskSpace.jobs.size
|
||||
}.by(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue