refactored with_timeout helper
This commit is contained in:
@@ -600,13 +600,8 @@ class Post < ActiveRecord::Base
|
|||||||
if tags.blank? && Danbooru.config.blank_tag_search_fast_count
|
if tags.blank? && Danbooru.config.blank_tag_search_fast_count
|
||||||
count = Danbooru.config.blank_tag_search_fast_count
|
count = Danbooru.config.blank_tag_search_fast_count
|
||||||
else
|
else
|
||||||
begin
|
count = Post.with_timeout(500, Danbooru.config.blank_tag_search_fast_count || 1_000_000) do
|
||||||
ActiveRecord::Base.connection.execute("SET statement_timeout = 500")
|
Post.tag_match(tags).undeleted.count
|
||||||
count = Post.tag_match(tags).undeleted.count
|
|
||||||
rescue ActiveRecord::StatementInvalid
|
|
||||||
count = Danbooru.config.blank_tag_search_fast_count || 1_000_000
|
|
||||||
ensure
|
|
||||||
ActiveRecord::Base.connection.execute("SET statement_timeout = 3000")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,11 @@ module PostSetPresenters
|
|||||||
|
|
||||||
def post_previews_html(template)
|
def post_previews_html(template)
|
||||||
html = ""
|
html = ""
|
||||||
|
is_empty = Post.with_timeout(500, false) do
|
||||||
|
posts.empty?
|
||||||
|
end
|
||||||
|
|
||||||
if posts.empty?
|
if is_empty?
|
||||||
return template.render("post_sets/blank")
|
return template.render("post_sets/blank")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -8,14 +8,16 @@ module Danbooru
|
|||||||
connection.execute("SET STATEMENT_TIMEOUT = 0") unless Rails.env == "test"
|
connection.execute("SET STATEMENT_TIMEOUT = 0") unless Rails.env == "test"
|
||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
connection.execute("SET STATEMENT_TIMEOUT = 5000") unless Rails.env == "test"
|
connection.execute("SET STATEMENT_TIMEOUT = 3000") unless Rails.env == "test"
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_timeout(n)
|
def with_timeout(n, default_value)
|
||||||
connection.execute("SET STATEMENT_TIMEOUT = #{n}") unless Rails.env == "test"
|
connection.execute("SET STATEMENT_TIMEOUT = #{n}") unless Rails.env == "test"
|
||||||
yield
|
yield
|
||||||
|
rescue ActiveRecord::StatementInvalid
|
||||||
|
return default_value
|
||||||
ensure
|
ensure
|
||||||
connection.execute("SET STATEMENT_TIMEOUT = 5000") unless Rails.env == "test"
|
connection.execute("SET STATEMENT_TIMEOUT = 3000") unless Rails.env == "test"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user