Created
February 8, 2015 20:42
-
-
Save hundredwatt/6eaca9d2d6c8f5ff437e to your computer and use it in GitHub Desktop.
Apply rails/rails#18846 for Delayed Job workers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Delayed::Worker.lifecycle.before :execute do | |
require 'active_record/relation/warn_on_result_set_size' | |
ActiveRecord::Base.warn_on_result_set_size = 1001 | |
ActiveRecord::Base.logger = Rails.logger | |
ActiveRecord::Base.logger.level = Logger::WARN | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveRecord | |
class Relation | |
module WarnOnResultSetSize | |
# When this module is prepended to `ActiveRecord::Relation` and | |
# `config.active_record.warn_on_result_set_size` is set to an | |
# integer, if the number of records a query returns is greater | |
# than the value of `warn_on_result_set_size`, a warning is | |
# logged. This allows for the dection of queries that return a | |
# large number of records, which could cause memory bloat. | |
def exec_queries | |
super.tap do | |
if logger && warn_on_result_set_size | |
if @records.length >= warn_on_result_set_size | |
logger.warn "Result set size exceeded #{warn_on_result_set_size} (model: #{@klass}, records: #{@records.size})" | |
end | |
end | |
end | |
end | |
end | |
end | |
class Base | |
mattr_accessor :warn_on_result_set_size, instance_writer: false | |
self.warn_on_result_set_size = nil | |
end | |
Relation.prepend Relation::WarnOnResultSetSize | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment