Sha256: c64b3a29e5feccf3d94012d81f2e43b0c8008a24f005dc1e9edf4761d53722c8

Contents?: true

Size: 1.49 KB

Versions: 48

Compression:

Stored size: 1.49 KB

Contents

require 'thread'

module Rack
  # Rack::Reloader checks on every request, but at most every +secs+
  # seconds, if a file loaded changed, and reloads it, logging to
  # rack.errors.
  #
  # It is recommended you use ShowExceptions to catch SyntaxErrors etc.

  class Reloader
    def initialize(app, secs=10)
      @app = app
      @secs = secs              # reload every @secs seconds max
      @last = Time.now
    end

    def call(env)
      if Time.now > @last + @secs
        Thread.exclusive {
          reload!(env['rack.errors'])
          @last = Time.now
        }
      end

      @app.call(env)
    end

    def reload!(stderr=STDERR)
      need_reload = $LOADED_FEATURES.find_all { |loaded|
        begin
          if loaded =~ /\A[.\/]/  # absolute filename or 1.9
            abs = loaded
          else
            abs = $LOAD_PATH.map { |path| ::File.join(path, loaded) }.
                             find { |file| ::File.exist? file }
          end

          if abs
            ::File.mtime(abs) > @last - @secs  rescue false
          else
            false
          end
        end
      }

      need_reload.each { |l|
        $LOADED_FEATURES.delete l
      }

      need_reload.each { |to_load|
        begin
          if require to_load
            stderr.puts "#{self.class}: reloaded `#{to_load}'"
          end
        rescue LoadError, SyntaxError => e
          raise e                 # Possibly ShowExceptions
        end
      }

      stderr.flush
      need_reload
    end
  end
end

Version data entries

48 entries across 48 versions & 12 rubygems

Version Path
3mix-castronaut-0.5.0.2 vendor/rack/lib/rack/reloader.rb
kastner-rack-0.3.171 lib/rack/reloader.rb
kastner-rack-0.3.186 lib/rack/reloader.rb
masover-castronaut-0.4.4.4 vendor/rack/lib/rack/reloader.rb
masover-castronaut-0.4.4.5 vendor/rack/lib/rack/reloader.rb
masover-castronaut-0.5.0.1 vendor/rack/lib/rack/reloader.rb
p8-castronaut-0.6.1.1 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.2.0 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.2.5 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.3.0 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.3.5 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.3.6 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.4.1 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.4.2 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.4.3 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.4.4 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.4.5 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.4.6 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.5.0 vendor/rack/lib/rack/reloader.rb
relevance-castronaut-0.5.1 vendor/rack/lib/rack/reloader.rb