Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xli
Created April 24, 2011 06:53
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 xli/939381 to your computer and use it in GitHub Desktop.
Save xli/939381 to your computer and use it in GitHub Desktop.
autoload threadsafe
# 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