Skip to content

Instantly share code, notes, and snippets.

@cben
Last active June 14, 2017 08:11
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 cben/006e86d4788f98bb6c70b91e4e909af8 to your computer and use it in GitHub Desktop.
Save cben/006e86d4788f98bb6c70b91e4e909af8 to your computer and use it in GitHub Desktop.
# Put this in initializers/
# Kludge to avoid repeated globs & stats on closely spaced requests, eg. assets.
# Investigating at https://gist.github.com/cben/ca7a0d75734216816b39afdec5865224
# Patches https://github.com/rails/rails/blob/master/activesupport/lib/active_support/file_update_checker.rb
# http://apidock.com/rails/ActiveSupport/FileUpdateChecker
# TODO: make less noisy, accumulate stats, dump stats when actually found a change
# TODO: record times when assumed false, compare max mtime
# TODO: don't assume fresh if last check saw changes only after it's "quiet"?
require 'benchmark'
module RateLimitedUpdateChecker
ASSUME_FRESH_SECONDS = 0.1
def updated?
thread_info = File.readlink('/proc/thread-self') rescue "#{Process.id} / #{Thread.current.object_id}"
since_last_checked = Time.now - (@last_checked_at || Time.at(0))
if since_last_checked.between?(0, ASSUME_FRESH_SECONDS)
puts "[#{thread_info} #{to_s}] SKIPPING FILE UPDATE CHECK, STILL FRESH #{since_last_checked}sec."
false
else
result = nil
t = Benchmark.measure { result = super }
puts "[#{thread_info} #{to_s}] Re-checking for updated files after #{since_last_checked}sec... => #{result}, took #{t.to_s.strip}sec"
@last_checked_at = Time.now
result
end
end
end
ActiveSupport::FileUpdateChecker.prepend RateLimitedUpdateChecker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment