FIX: wraps any query with the slow query guard (#6222)

This commit is contained in:
Joffrey JAFFEUX 2018-08-01 07:39:57 -04:00 committed by GitHub
parent b89906e194
commit 7c7dfa4e5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,17 +52,11 @@ class Report
end end
def self.wrap_slow_query(timeout = 20000) def self.wrap_slow_query(timeout = 20000)
begin ActiveRecord::Base.connection.transaction do
ActiveRecord::Base.connection.transaction do # Set a statement timeout so we can't tie up the server
# Set a statement timeout so we can't tie up the server DB.exec "SET LOCAL statement_timeout = #{timeout}"
DB.exec "SET LOCAL statement_timeout = #{timeout}" yield
yield
end
rescue ActiveRecord::QueryCanceled
return :timeout
end end
nil
end end
def prev_start_date def prev_start_date
@ -144,7 +138,6 @@ class Report
report.average = opts[:average] if opts[:average] report.average = opts[:average] if opts[:average]
report.percent = opts[:percent] if opts[:percent] report.percent = opts[:percent] if opts[:percent]
report.higher_is_better = opts[:higher_is_better] if opts[:higher_is_better] report.higher_is_better = opts[:higher_is_better] if opts[:higher_is_better]
report report
end end
@ -164,12 +157,18 @@ class Report
report = _get(type, opts) report = _get(type, opts)
report_method = :"report_#{type}" report_method = :"report_#{type}"
if respond_to?(report_method) begin
send(report_method, report) wrap_slow_query do
elsif type =~ /_reqs$/ if respond_to?(report_method)
req_report(report, type.split(/_reqs$/)[0].to_sym) send(report_method, report)
else elsif type =~ /_reqs$/
return nil req_report(report, type.split(/_reqs$/)[0].to_sym)
else
return nil
end
end
rescue ActiveRecord::QueryCanceled, PG::QueryCanceled => e
report.error = :timeout
end end
rescue Exception => e rescue Exception => e
report.error = :exception report.error = :exception
@ -605,10 +604,8 @@ class Report
options = { end_date: report.end_date, start_date: report.start_date, limit: report.limit || 8 } options = { end_date: report.end_date, start_date: report.start_date, limit: report.limit || 8 }
result = nil result = nil
report.error = wrap_slow_query do result = IncomingLinksReport.find(:top_referred_topics, options)
result = IncomingLinksReport.find(:top_referred_topics, options) report.data = result.data
report.data = result.data
end
end end
def self.report_top_traffic_sources(report) def self.report_top_traffic_sources(report)
@ -631,10 +628,8 @@ class Report
options = { end_date: report.end_date, start_date: report.start_date, limit: report.limit || 8 } options = { end_date: report.end_date, start_date: report.start_date, limit: report.limit || 8 }
result = nil result = nil
report.error = wrap_slow_query do result = IncomingLinksReport.find(:top_traffic_sources, options)
result = IncomingLinksReport.find(:top_traffic_sources, options) report.data = result.data
report.data = result.data
end
end end
def self.report_trending_search(report) def self.report_trending_search(report)