add disk space usage failsafe

This commit is contained in:
Régis Hanol 2013-11-15 16:46:41 +01:00
parent e03ae73c5d
commit 8a83f1a88f
6 changed files with 51 additions and 5 deletions

View File

@ -40,7 +40,8 @@ class AdminDashboardData
site_description_check,
access_password_removal,
site_contact_username_check,
notification_email_check ].compact
notification_email_check,
].compact
end
def self.fetch_stats
@ -147,7 +148,7 @@ class AdminDashboardData
end
def site_description_check
return I18n.t('dashboard.site_description_missing') if !SiteSetting.site_description.present?
I18n.t('dashboard.site_description_missing') if !SiteSetting.site_description.present?
end
def send_consumer_email_check
@ -166,7 +167,6 @@ class AdminDashboardData
I18n.t('dashboard.ruby_version_warning') if RUBY_VERSION == '2.0.0' and RUBY_PATCHLEVEL < 247
end
# TODO: generalize this method of putting i18n keys with expiry in redis
# that should be reported on the admin dashboard:
def access_password_removal

View File

@ -15,7 +15,6 @@ class SiteSetting < ActiveRecord::Base
end
end
def self.call_discourse_hub?
self.enforce_global_nicknames? && self.discourse_org_access_key.present?
end

View File

@ -500,7 +500,8 @@ en:
company_domain: "The domain name owned by the company that runs this site, used in legal documents like the /tos"
queue_jobs: "DEVELOPER ONLY! WARNING! By default, queue jobs in sidekiq. If disabled, your site will be broken."
crawl_images: "Enable retrieving images from third party sources to insert width and height dimensions"
download_remote_images_to_local: "Download a copy of all remote images"
download_remote_images_to_local: "Download a copy of remote images hotlinked in posts"
download_remote_images_threshold: "Amount of minimum available disk space required to download remote images locally (in percent)"
ninja_edit_window: "Number of seconds after posting where edits do not create a new version"
edit_history_visible_to_public: "Allow everyone to see previous versions of an edited post. When disabled, only staff members can view edit history."
delete_removed_posts_after: "Number of hours after which posts removed by the author will be deleted."

View File

@ -253,6 +253,8 @@ files:
default:
test: false
default: true
download_remote_images_threshold:
default: 20
trust:
default_trust_level: 0

View File

@ -221,6 +221,8 @@ class CookedPostProcessor
def pull_hotlinked_images
# is the job enabled?
return unless SiteSetting.download_remote_images_to_local?
# have we enough disk space?
return if disable_if_low_on_disk_space
# we only want to run the job whenever it's changed by a user
return if @post.updated_by == Discourse.system_user
# make sure no other job is scheduled
@ -230,6 +232,18 @@ class CookedPostProcessor
Jobs.enqueue_in(delay.seconds.to_i, :pull_hotlinked_images, post_id: @post.id)
end
def disable_if_low_on_disk_space
if available_disk_space < SiteSetting.download_remote_images_threshold
SiteSetting.download_remote_images_to_local = false
return true
end
false
end
def available_disk_space
100 - `df -l . | tail -1 | tr -s ' ' | cut -d ' ' -f 5`.to_i
end
def dirty?
@dirty
end

View File

@ -275,6 +275,8 @@ describe CookedPostProcessor do
let(:post) { build(:post) }
let(:cpp) { CookedPostProcessor.new(post) }
before { cpp.stubs(:available_disk_space).returns(90) }
it "does not run when download_remote_images_to_local is disabled" do
SiteSetting.stubs(:download_remote_images_to_local).returns(false)
Jobs.expects(:cancel_scheduled_job).never
@ -291,6 +293,13 @@ describe CookedPostProcessor do
cpp.pull_hotlinked_images
end
it "disables download when disk space is low" do
SiteSetting.expects(:download_remote_images_threshold).returns(20)
cpp.expects(:available_disk_space).returns(10)
Jobs.expects(:cancel_scheduled_job).never
cpp.pull_hotlinked_images
end
context "and the post has been updated by a user" do
before { post.id = 42 }
@ -310,4 +319,25 @@ describe CookedPostProcessor do
end
context ".disable_if_low_on_disk_space" do
let(:post) { build(:post) }
let(:cpp) { CookedPostProcessor.new(post) }
before { cpp.expects(:available_disk_space).returns(50) }
it "does nothing when there's enough disk space" do
SiteSetting.expects(:download_remote_images_threshold).returns(20)
SiteSetting.expects(:download_remote_images_to_local).never
cpp.disable_if_low_on_disk_space.should == false
end
it "disables download_remote_images_threshold when there's not enough disk space" do
SiteSetting.expects(:download_remote_images_threshold).returns(75)
cpp.disable_if_low_on_disk_space.should == true
SiteSetting.download_remote_images_to_local.should == false
end
end
end