Class: Merb::BootLoader::LoadClasses
- Inherits:
-
Merb::BootLoader
- Object
- Merb::BootLoader
- Merb::BootLoader::LoadClasses
- Defined in:
- lib/merb-core/bootloader.rb
Overview
Load all classes inside the load paths.
This is used in conjunction with Merb::BootLoader::ReloadClasses to track files that need to be reloaded, and which constants need to be removed in order to reload a file.
This also adds the model, controller, and lib directories to the load path, so they can be required in order to avoid load-order issues.
Constant Summary collapse
- LOADED_CLASSES =
{}
- MTIMES =
{}
Class Method Summary collapse
-
.exit_gracefully ⇒ Object
Wait for any children to exit, remove the “main” PID, and exit.
-
.load_classes(*paths) ⇒ Object
Load classes from given paths - using path/glob pattern.
-
.load_file(file) ⇒ Object
Loads a file, tracking its modified time and, if necessary, the classes it declared.
-
.reap_workers(status = 0, sig = reap_workers_signal) ⇒ Object
Reap any workers of the spawner process and exit with an appropriate status code.
- .reap_workers_signal ⇒ Object
-
.reload(file) ⇒ Object
Reloads the classes in the specified file.
-
.remove_classes_in_file(file) {|file| ... } ⇒ Object
Removes all classes declared in the specified file.
-
.remove_constant(const) ⇒ Object
Removes the specified class.
-
.run ⇒ Object
Load all classes from Merb’s native load paths.
-
.start_transaction ⇒ Object
Set up the BEGIN point for fork-based loading and sets up any signals in the parent and child.
Methods inherited from Merb::BootLoader
after, after_app_loads, before, before_app_loads, before_master_shutdown, before_worker_shutdown, default_framework, finished?, inherited, move_klass
Class Method Details
.exit_gracefully ⇒ Object
Wait for any children to exit, remove the “main” PID, and exit.
Returns
(Does not return.)
:api: private
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
# File 'lib/merb-core/bootloader.rb', line 652 def exit_gracefully # wait all workers to exit Process.waitall # remove master process pid Merb::Server.remove_pid("main") # terminate, workers remove their own pids # in on exit hook Merb::BootLoader.before_master_shutdown_callbacks.each do |cb| begin cb.call rescue Exception => e Merb.logger.fatal "before_master_shutdown callback crashed: #{e.}" end end exit end |
.load_classes(*paths) ⇒ Object
Load classes from given paths - using path/glob pattern.
Parameters
- *paths<Array>
-
Array of paths to load classes from - may contain glob pattern
Returns
nil
:api: private
870 871 872 873 874 875 876 877 878 879 880 881 882 |
# File 'lib/merb-core/bootloader.rb', line 870 def load_classes(*paths) orphaned_classes = [] paths.flatten.each do |path| Dir[path].each do |file| begin load_file file rescue NameError => ne orphaned_classes.unshift(file) end end end load_classes_with_requirements(orphaned_classes) end |
.load_file(file) ⇒ Object
Loads a file, tracking its modified time and, if necessary, the classes it declared.
Parameters
- file<String>
-
The file to load.
Returns
nil
:api: private
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 |
# File 'lib/merb-core/bootloader.rb', line 834 def load_file(file) # Don't do this expensive operation unless we need to unless Merb::Config[:fork_for_class_load] klasses = ObjectSpace.classes.dup end # Ignore the file for syntax errors. The next time # the file is changed, it'll be reloaded again begin require file rescue SyntaxError => e Merb.logger.error "Cannot load #{file} because of syntax error: #{e.}" ensure if Merb::Config[:reload_classes] MTIMES[file] = File.mtime(file) end end # Don't do this expensive operation unless we need to unless Merb::Config[:fork_for_class_load] LOADED_CLASSES[file] = ObjectSpace.classes - klasses end nil end |
.reap_workers(status = 0, sig = reap_workers_signal) ⇒ Object
Reap any workers of the spawner process and exit with an appropriate status code.
Note that exiting the spawner process with a status code of 128 when a master process exists will cause the spawner process to be recreated, and the app code reloaded.
Parameters
- status<Integer>
-
The status code to exit with. Defaults to 0.
- sig<String>
-
The signal to send to workers
Returns
(Does not return.)
:api: private
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 |
# File 'lib/merb-core/bootloader.rb', line 791 def reap_workers(status = 0, sig = reap_workers_signal) Merb.logger.info "Executed all before worker shutdown callbacks..." Merb::BootLoader.before_worker_shutdown_callbacks.each do |cb| begin cb.call rescue Exception => e Merb.logger.fatal "before worker shutdown callback crashed: #{e.}" end end Merb.exiting = true unless status == 128 begin @writer.puts(status.to_s) if @writer rescue SystemCallError end threads = [] ($WORKERS || []).each do |p| threads << Thread.new do begin Process.kill(sig, p) Process.wait2(p) rescue SystemCallError end end end threads.each {|t| t.join } exit(status) end |
.reap_workers_signal ⇒ Object
772 773 774 |
# File 'lib/merb-core/bootloader.rb', line 772 def reap_workers_signal Merb::Config[:reap_workers_quickly] ? "KILL" : "ABRT" end |
.reload(file) ⇒ Object
Reloads the classes in the specified file. If fork-based loading is used, this causes the current processes to be killed and and all classes to be reloaded. If class-based loading is not in use, the classes declared in that file are removed and the file is reloaded.
Parameters
- file<String>
-
The file to reload.
Returns
When fork-based loading is used:
(Does not return.)
When fork-based loading is not in use:
nil
:api: private
899 900 901 902 903 904 905 |
# File 'lib/merb-core/bootloader.rb', line 899 def reload(file) if Merb::Config[:fork_for_class_load] reap_workers(128) else remove_classes_in_file(file) { |f| load_file(f) } end end |
.remove_classes_in_file(file) {|file| ... } ⇒ Object
Removes all classes declared in the specified file. Any hashes which use classes as keys will be protected provided they have been added to Merb.klass_hashes. These hashes have their keys substituted with placeholders before the file’s classes are unloaded. If a block is provided, it is called before the substituted keys are reconstituted.
Parameters
- file<String>
-
The file to remove classes for.
- &block
-
A block to call with the file that has been removed before klass_hashes are updated
to use the current values of the constants they used as keys.
Returns
nil
:api: private
921 922 923 924 925 926 927 928 929 |
# File 'lib/merb-core/bootloader.rb', line 921 def remove_classes_in_file(file, &block) Merb.klass_hashes.each { |x| x.protect_keys! } if klasses = LOADED_CLASSES.delete(file) klasses.each { |klass| remove_constant(klass) unless klass.to_s =~ /Router/ } end yield file if block_given? Merb.klass_hashes.each {|x| x.unprotect_keys!} nil end |
.remove_constant(const) ⇒ Object
Removes the specified class.
Additionally, removes the specified class from the subclass list of every superclass that tracks it’s subclasses in an array returned by _subclasses_list. Classes that wish to use this functionality are required to alias the reader for their list of subclasses to _subclasses_list. Plugins for ORMs and other libraries should keep this in mind.
Parameters
- const<Class>
-
The class to remove.
Returns
nil
:api: private
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
# File 'lib/merb-core/bootloader.rb', line 945 def remove_constant(const) # This is to support superclasses (like AbstractController) that track # their subclasses in a class variable. superklass = const until (superklass = superklass.superclass).nil? if superklass.respond_to?(:_subclasses_list) superklass.send(:_subclasses_list).delete(klass) superklass.send(:_subclasses_list).delete(klass.to_s) end end parts = const.to_s.split("::") base = parts.size == 1 ? Object : Object.full_const_get(parts[0..-2].join("::")) object = parts[-1].to_s begin base.send(:remove_const, object) Merb.logger.debug("Removed constant #{object} from #{base}") rescue NameError Merb.logger.debug("Failed to remove constant #{object} from #{base}") end nil end |
.run ⇒ Object
Load all classes from Merb’s native load paths.
If fork-based loading is used, every time classes are loaded this will return in a new spawner process and boot loading will continue from this point in the boot loading process.
If fork-based loading is not in use, this only returns once and does not fork a new process.
Returns
Returns at least once:
nil
:api: plugin
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
# File 'lib/merb-core/bootloader.rb', line 612 def run # process name you see in ps output $0 = "merb#{" : " + Merb::Config[:name] if Merb::Config[:name]} : master" # Log the process configuration user defined signal 1 (SIGUSR1) is received. Merb.trap("USR1") do require "yaml" Merb.logger.fatal! "Configuration:\n#{Merb::Config.to_hash.merge(:pid => $$).to_yaml}\n\n" end if Merb::Config[:fork_for_class_load] && !Merb.testing? start_transaction else Merb.trap('INT') do Merb.logger.warn! "Reaping Workers" reap_workers end end # Load application file if it exists - for flat applications load_file Merb.dir_for(:application) if File.file?(Merb.dir_for(:application)) # Load classes and their requirements Merb.load_paths.each do |component, path| next if path.last.blank? || component == :application || component == :router load_classes(path.first / path.last) end Merb::Controller.send :include, Merb::GlobalHelpers nil end |
.start_transaction ⇒ Object
Set up the BEGIN point for fork-based loading and sets up any signals in the parent and child. This is done by forking the app. The child process continues on to run the app. The parent process waits for the child process to finish and either forks again
Returns
Parent Process:
(Does not return.)
Child Process returns at least once:
nil
:api: private
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
# File 'lib/merb-core/bootloader.rb', line 683 def start_transaction Merb.logger.warn! "Parent pid: #{Process.pid}" reader, writer = nil, nil if GC.respond_to?(:copy_on_write_friendly=) GC.copy_on_write_friendly = true end loop do # create two connected endpoints # we use them for master/workers communication reader, @writer = IO.pipe pid = Kernel.fork # pid means we're in the parent; only stay in the loop if that is case break unless pid # writer must be closed so reader can generate EOF condition @writer.close # master process stores pid to merb.main.pid Merb::Server.store_pid("main") if Merb::Config[:console_trap] Merb.trap("INT") {} else # send ABRT to worker on INT Merb.trap("INT") do Merb.logger.warn! "Reaping Workers" begin Process.kill(reap_workers_signal, pid) rescue SystemCallError end exit_gracefully end end Merb.trap("HUP") do Merb.logger.warn! "Doing a fast deploy\n" Process.kill("HUP", pid) end reader_ary = [reader] loop do # wait for worker to exit and capture exit status # # # WNOHANG specifies that wait2 exists without waiting # if no worker processes are ready to be noticed. if exit_status = Process.wait2(pid, Process::WNOHANG) # wait2 returns a 2-tuple of process id and exit # status. # # We do not care about specific pid here. exit_status[1] && exit_status[1].exitstatus == 128 ? break : exit end # wait for data to become available, timeout in 0.25 of a second if select(reader_ary, nil, nil, 0.25) begin # no open writers next if reader.eof? msg = reader.readline if msg =~ /128/ Process.detach(pid) break else exit_gracefully end rescue SystemCallError exit_gracefully end end end end reader.close # add traps to the worker if Merb::Config[:console_trap] Merb::Server.add_irb_trap at_exit { reap_workers } else Merb.trap('INT') do Merb::BootLoader.before_worker_shutdown_callbacks.each { |cb| cb.call } end Merb.trap('ABRT') { reap_workers } Merb.trap('HUP') { reap_workers(128, "ABRT") } end end |