Skip to content

Commit

Permalink
add disk space usage failsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
ZogStriP committed Nov 15, 2013
1 parent e03ae73 commit 8a83f1a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
6 changes: 3 additions & 3 deletions app/models/admin_dashboard_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def problems
site_description_check,
access_password_removal,
site_contact_username_check,
notification_email_check ].compact
notification_email_check,
].compact
end

def self.fetch_stats
Expand Down Expand Up @@ -147,7 +148,7 @@ def title_check
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
Expand All @@ -166,7 +167,6 @@ def ruby_version_check
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
Expand Down
1 change: 0 additions & 1 deletion app/models/site_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
2 changes: 2 additions & 0 deletions config/site_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ files:
default:
test: false
default: true
download_remote_images_threshold:
default: 20

trust:
default_trust_level: 0
Expand Down
14 changes: 14 additions & 0 deletions lib/cooked_post_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ def schemaless(url)
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
Expand All @@ -230,6 +232,18 @@ def pull_hotlinked_images
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
Expand Down
30 changes: 30 additions & 0 deletions spec/components/cooked_post_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@
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
Expand All @@ -291,6 +293,13 @@
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 }
Expand All @@ -310,4 +319,25 @@

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

0 comments on commit 8a83f1a

Please sign in to comment.