Created
April 24, 2011 06:53
-
-
Save xli/939381 to your computer and use it in GitHub Desktop.
autoload threadsafe
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
# Load this file before load anything. | |
# JRuby-Rack will load rack before load rails, so should load this file when JRuby runtime initialized. | |
# Call AutoloadThreadsafe.eager_load_all! at the end of rails environment rb | |
module AutoloadThreadsafe | |
module Recorder | |
def self.included(base) | |
base.class_eval do | |
alias :unsafe_autoload :autoload | |
def autoload(m, filename) | |
AutoloadThreadsafe.record(self, m) | |
unsafe_autoload(m, filename) | |
end | |
end | |
end | |
end | |
def record(parent, constant_name) | |
autoload_constants << "#{parent.to_s == 'main' ? '' : parent}::#{constant_name}" | |
end | |
def autoload_constants | |
@autoload_constants ||= [] | |
end | |
def attach! | |
[Kernel, Module].each do |m| | |
unless m.ancestors.include?(Recorder) | |
m.send(:include, Recorder) | |
end | |
end | |
end | |
def eager_load_all! | |
autoload_constants.delete_if do |f| | |
begin | |
f.constantize | |
rescue LoadError => e | |
# ignore it, no one use it | |
end | |
end | |
end | |
extend self | |
end | |
AutoloadThreadsafe.attach! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment