class PhusionPassenger::Standalone::AppFinder

Attributes

apps[R]
dirs[RW]

Public Class Methods

looks_like_app_directory?(dir) click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 32
def self.looks_like_app_directory?(dir)
        return File.exist?("#{dir}/config.ru") ||
                File.exist?("#{dir}/config/environment.rb") ||
                File.exist?("#{dir}/passenger_wsgi.py") ||
                File.exist?("#{dir}/app.js") ||
                File.exist?("#{dir}/.meteor")
end
new(dirs, options = {}) click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 40
def initialize(dirs, options = {})
        @dirs = dirs
        @options = options.dup
end

Public Instance Methods

global_options() click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 45
def global_options
        return @options
end
monitor(termination_pipe) { |new_apps| ... } click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 79
def monitor(termination_pipe)
        raise "You must call #scan first" if !@apps
        
        watcher = PhusionPassenger::Utils::FileSystemWatcher.new(@watchlist, termination_pipe)
        if wait_on_io(termination_pipe, 3)
                return
        end
        
        while true
                changed = watcher.wait_for_change
                watcher.close
                if changed
                        old_apps = @apps
                        # The change could be caused by a write to some passenger.conf file.
                        # Wait for a short period so that the write has a chance to finish.
                        if wait_on_io(termination_pipe, 0.25)
                                return
                        end
                        
                        new_apps = scan
                        watcher = PhusionPassenger::Utils::FileSystemWatcher.new(@watchlist, termination_pipe)
                        if old_apps != new_apps
                                yield(new_apps)
                        end
                        
                        # Don't process change events again for a short while,
                        # but do detect changes while waiting.
                        if wait_on_io(termination_pipe, 3)
                                return
                        end
                else
                        return
                end
        end
ensure
        watcher.close if watcher
end
multi_mode?() click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 121
def multi_mode?
        return !single_mode?
end
scan() click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 49
def scan
        apps = []
        watchlist = []
        
        if single_mode?
                app_root = find_app_root
                apps << {
                        :server_names => ["_"],
                        :root => app_root
                }
                watchlist << app_root
                watchlist << "#{app_root}/config" if File.exist?("#{app_root}/config")
                watchlist << "#{app_root}/passenger-standalone.json" if File.exist?("#{app_root}/passenger-standalone.json")
                
                config_filename = File.join(app_root, "passenger-standalone.json")
                if File.exist?(config_filename)
                        global_options = load_config_file!(:global_config, config_filename)
                        @options.merge!(global_options)
                end

                apps.map! do |app|
                        @options.merge(app)
                end
        end

        @apps = apps
        @watchlist = watchlist
        return apps
end
single_mode?() click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 117
def single_mode?
        return true
end

Private Instance Methods

filename_to_server_names(filename) click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 176
def filename_to_server_names(filename)
        basename = File.basename(filename)
        names = [basename]
        if basename !~ /^www\.$/
                names << "www.#{basename}"
        end
        return names
end
find_app_root() click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 131
def find_app_root
        if @dirs.empty?
                return File.expand_path(".")
        else
                return File.expand_path(@dirs[0])
        end
end
load_config_file(context, filename) click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 165
def load_config_file(context, filename)
        return load_config_file!(context, filename)
rescue ConfigLoadError => e
        STDERR.puts "*** Warning: #{e.message}"
        return {}
end
load_config_file!(context, filename) click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 139
def load_config_file!(context, filename)
        PhusionPassenger.require_passenger_lib 'utils/json' if !defined?(PhusionPassenger::Utils::JSON)
        begin
                data = File.open(filename, "r:utf-8") do |f|
                        f.read
                end
        rescue SystemCallError => e
                raise ConfigLoadError, "cannot load config file #{filename} (#{e})"
        end

        begin
                config = PhusionPassenger::Utils::JSON.parse(data)
        rescue => e
                raise ConfigLoadError, "cannot parse config file #{filename} (#{e})"
        end
        if !config.is_a?(Hash)
                raise ConfigLoadError, "cannot parse config file #{filename} (it does not contain an object)"
        end

        result = {}
        config.each_pair do |key, val|
                result[key.to_sym] = val
        end
        return result
end
looks_like_app_directory?(dir) click to toggle source
# File lib/phusion_passenger/standalone/app_finder.rb, line 172
def looks_like_app_directory?(dir)
        return AppFinder.looks_like_app_directory?(dir)
end
wait_on_io(io, timeout) click to toggle source

Wait until the given IO becomes readable, or until the timeout has been reached. Returns true if the IO became readable, false if the timeout has been reached.

# File lib/phusion_passenger/standalone/app_finder.rb, line 188
def wait_on_io(io, timeout)
        return !!select([io], nil, nil, timeout)
end