Skip to content

Instantly share code, notes, and snippets.

@hundredwatt
Created February 8, 2015 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hundredwatt/6eaca9d2d6c8f5ff437e to your computer and use it in GitHub Desktop.
Save hundredwatt/6eaca9d2d6c8f5ff437e to your computer and use it in GitHub Desktop.
Apply rails/rails#18846 for Delayed Job workers
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
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