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
private
Wait for any children to exit, remove the “main” PID, and exit.
-
.load_classes(*paths) ⇒ Object
private
Load classes from given paths - using path/glob pattern.
-
.load_file(file) ⇒ Object
private
Loads a file, tracking its modified time and, if necessary, the classes it declared.
-
.reap_workers(status = 0, sig = "ABRT") ⇒ Object
private
Reap any workers of the spawner process and exit with an appropriate status code.
-
.reload(file) ⇒ Object
private
Reloads the classes in the specified file.
-
.remove_classes_in_file(file) {|file| ... } ⇒ Object
private
Removes all classes declared in the specified file.
-
.remove_constant(const) ⇒ Object
private
Removes the specified class.
-
.run ⇒ Object
Load all classes from Merb’s native load paths.
-
.start_transaction ⇒ Object
private
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, default_framework, finished?, inherited, move_klass
Class Method Details
.exit_gracefully ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Wait for any children to exit, remove the “main” PID, and exit.
Returns
(Does not return.)
603 604 605 606 607 608 609 610 611 |
# File 'lib/merb-core/bootloader.rb', line 603 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 exit end |
.load_classes(*paths) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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
798 799 800 801 802 803 804 805 806 807 808 809 810 |
# File 'lib/merb-core/bootloader.rb', line 798 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Loads a file, tracking its modified time and, if necessary, the classes it declared.
Parameters
- file<String>
-
The file to load.
Returns
nil
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
# File 'lib/merb-core/bootloader.rb', line 762 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 load 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 = "ABRT") ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
Returns
(Does not return.)
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 |
# File 'lib/merb-core/bootloader.rb', line 730 def reap_workers(status = 0, sig = "ABRT") 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 |
.reload(file) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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
827 828 829 830 831 832 833 |
# File 'lib/merb-core/bootloader.rb', line 827 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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
849 850 851 852 853 854 855 856 857 |
# File 'lib/merb-core/bootloader.rb', line 849 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 |
# File 'lib/merb-core/bootloader.rb', line 873 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
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/merb-core/bootloader.rb', line 555 def run # Add models, controllers, helpers and lib to the load path unless @ran $LOAD_PATH.unshift Merb.dir_for(:model) $LOAD_PATH.unshift Merb.dir_for(:controller) $LOAD_PATH.unshift Merb.dir_for(:lib) $LOAD_PATH.unshift Merb.dir_for(:helper) end @ran = true # 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 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 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 |
# File 'lib/merb-core/bootloader.rb', line 627 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("ABRT", 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') {} Merb.trap('ABRT') { reap_workers } Merb.trap('HUP') { reap_workers(128) } end end |