Class: Merb::BootLoader::LoadClasses

Inherits:
Merb::BootLoader show all
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 =
{}
FILES_LOADED =
{}

Class Method Summary collapse

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_gracefullyObject

Wait for any children to exit, remove the “main” PID, and exit.

Returns

(Does not return.)

:api: private



685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/merb-core/bootloader.rb', line 685

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.message}"
    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



926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/merb-core/bootloader.rb', line 926

def load_classes(*paths)
  orphaned_classes = []
  paths.flatten.each do |path|
    Dir[path].sort.each do |file|
      begin
        load_file file
      rescue NameError => ne
        Merb.logger.verbose! "Stashed file with missing requirements for later reloading: #{file}"
        ne.backtrace.each_with_index { |line, idx| Merb.logger.verbose! "[#{idx}]: #{line}" }
        orphaned_classes.unshift(file)
      end
    end
  end
  load_classes_with_requirements(orphaned_classes)
end

.load_file(file, reload = false) ⇒ 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



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
# File 'lib/merb-core/bootloader.rb', line 872

def load_file(file, reload = false)
  Merb.logger.verbose! "#{reload ? "re" : ""}loading #{file}"
  
  # If we're going to be reloading via constant remove,
  # keep track of what constants were loaded and what files
  # have been added, so that the constants can be removed
  # and the files can be removed from $LOADED_FEAUTRES
  if !Merb::Config[:fork_for_class_load]
    if FILES_LOADED[file]
      FILES_LOADED[file].each {|lf| $LOADED_FEATURES.delete(lf)}
    end
    
    klasses = ObjectSpace.classes.dup
    files_loaded = $LOADED_FEATURES.dup
  end

  # If we're in the midst of a reload, remove the file
  # itself from $LOADED_FEATURES so it will get reloaded
  if reload
    $LOADED_FEATURES.delete(file) if reload
  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.message}"
  ensure
    if Merb::Config[:reload_classes]
      MTIMES[file] = File.mtime(file)
    end
  end

  # If we're reloading via constant remove, store off the details
  # after the file has been loaded
  unless Merb::Config[:fork_for_class_load]
    LOADED_CLASSES[file] = ObjectSpace.classes - klasses
    FILES_LOADED[file] = $LOADED_FEATURES - files_loaded
  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



826
827
828
829
830
831
832
833
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
859
860
861
# File 'lib/merb-core/bootloader.rb', line 826

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.message}"
    end

  end

  Merb.exiting = true unless status == 128

  begin
    if @writer
      @writer.puts(status.to_s)
      @writer.close
    end
  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_signalObject



807
808
809
# File 'lib/merb-core/bootloader.rb', line 807

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



957
958
959
960
961
962
963
# File 'lib/merb-core/bootloader.rb', line 957

def reload(file)
  if Merb::Config[:fork_for_class_load]
    reap_workers(128)
  else
    remove_classes_in_file(file) { |f| load_file(f, true) }
  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

Yields:

  • (file)


979
980
981
982
983
984
985
986
987
# File 'lib/merb-core/bootloader.rb', line 979

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



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/merb-core/bootloader.rb', line 1003

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

.runObject

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



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
# File 'lib/merb-core/bootloader.rb', line 645

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_transactionObject

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



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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
# File 'lib/merb-core/bootloader.rb', line 716

def start_transaction
  Merb.logger.warn! "Parent pid: #{Process.pid}"
  reader, writer = nil, nil

  # Enable REE garbage collection
  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[:daemonize] || Merb::Config[:cluster]

    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.5 of a second
      if select(reader_ary, nil, nil, 0.5)
        begin
          # no open writers
          next if reader.eof?
          msg = reader.readline
          reader.close
          if msg.to_i == 128
            Process.waitpid(pid, Process::WNOHANG)
            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