### Home Classes Methods

### In Files

- lib/phusion_passenger/preloader_shared_helpers.rb

### Methods

- #accept_and_process_next_client
- #init
- #run_main_loop

### Class/Module Index

Quicksearch

- PhusionPassenger
- PhusionPassenger::AbstractInstaller
- PhusionPassenger::AbstractInstaller::Abort
- PhusionPassenger::AbstractInstaller::CommandError
- PhusionPassenger::AdminTools
- PhusionPassenger::AdminTools::MemoryStats
- PhusionPassenger::AdminTools::MemoryStats::Process
- PhusionPassenger::AdminTools::ServerInstance
- PhusionPassenger::AdminTools::ServerInstance::CorruptedDirectoryError
- PhusionPassenger::AdminTools::ServerInstance::GenerationsAbsentError
- PhusionPassenger::AdminTools::ServerInstance::Group
- PhusionPassenger::AdminTools::ServerInstance::Process
- PhusionPassenger::AdminTools::ServerInstance::RoleDeniedError
- PhusionPassenger::AdminTools::ServerInstance::StaleDirectoryError
- PhusionPassenger::AdminTools::ServerInstance::Stats
- PhusionPassenger::AdminTools::ServerInstance::UnsupportedGenerationStructureVersionError
- PhusionPassenger::AnalyticsLogger
- PhusionPassenger::AnalyticsLogger::Connection
- PhusionPassenger::AnalyticsLogger::Lock
- PhusionPassenger::AnalyticsLogger::Log
- PhusionPassenger::ClassicRails
- PhusionPassenger::ClassicRails::CGIFixed
- PhusionPassenger::ClassicRails::ThreadHandlerExtension
- PhusionPassenger::ConsoleTextTemplate
- PhusionPassenger::DebugLogging
- PhusionPassenger::LoaderSharedHelpers
- PhusionPassenger::MessageChannel
- PhusionPassenger::MessageChannel::InvalidHashError
- PhusionPassenger::MessageClient
- PhusionPassenger::NativeSupportLoader
- PhusionPassenger::Packaging
- PhusionPassenger::PlatformInfo
- PhusionPassenger::PlatformInfo::Depcheck
- PhusionPassenger::PlatformInfo::Depcheck::ConsoleRunner
- PhusionPassenger::PlatformInfo::Depcheck::Dependency
- PhusionPassenger::PlatformInfo::RuntimeError
- PhusionPassenger::Plugin
- PhusionPassenger::PreloaderSharedHelpers
- PhusionPassenger::Rack
- PhusionPassenger::Rack::OutOfBandGc
- PhusionPassenger::Rack::ThreadHandlerExtension
- PhusionPassenger::Rails3Extensions
- PhusionPassenger::Rails3Extensions::AnalyticsLogging
- PhusionPassenger::Rails3Extensions::AnalyticsLogging::ACExtension
- PhusionPassenger::Rails3Extensions::AnalyticsLogging::ASBenchmarkableExtension
- PhusionPassenger::Rails3Extensions::AnalyticsLogging::ExceptionLogger
- PhusionPassenger::RequestHandler
- PhusionPassenger::RequestHandler::ThreadHandler
- PhusionPassenger::RequestHandler::ThreadHandler::Interrupted
- PhusionPassenger::SharedConstants
- PhusionPassenger::Standalone
- PhusionPassenger::Standalone::AppFinder
- PhusionPassenger::Standalone::Command
- PhusionPassenger::Standalone::ConfigFile
- PhusionPassenger::Standalone::ConfigFile::DisallowedContextError
- PhusionPassenger::Standalone::HelpCommand
- PhusionPassenger::Standalone::Main
- PhusionPassenger::Standalone::PackageRuntimeCommand
- PhusionPassenger::Standalone::RuntimeInstaller
- PhusionPassenger::Standalone::StartCommand
- PhusionPassenger::Standalone::StatusCommand
- PhusionPassenger::Standalone::StopCommand
- PhusionPassenger::Standalone::Utils
- PhusionPassenger::Standalone::VersionCommand
- PhusionPassenger::Utils
- PhusionPassenger::Utils::AnsiColors
- PhusionPassenger::Utils::FileSystemWatcher
- PhusionPassenger::Utils::FileSystemWatcher::DirInfo
- PhusionPassenger::Utils::FileSystemWatcher::FileInfo
- PhusionPassenger::Utils::HostsFileParser
- PhusionPassenger::Utils::TeeInput
- PhusionPassenger::Utils::TmpIO
- PhusionPassenger::Utils::UnseekableSocket
- CommonLibraryBuilder
- Exception
- GC
- IO
- Object
- Signal

No matching classes.

# PhusionPassenger::PreloaderSharedHelpers

Provides shared functions for preloader apps.

### Public Instance Methods

accept_and_process_next_client(server_socket) click to toggle source

    # File lib/phusion_passenger/preloader_shared_helpers.rb, line 46
    def accept_and_process_next_client(server_socket)
            original_pid = Process.pid
            client = server_socket.accept
            client.binmode
            begin
                    command = client.readline
            rescue EOFError
                    return nil
            end
            if command !~ /\n\Z/
                    STDERR.puts "Command must end with a newline"
            elsif command == "spawn\n"
                    while client.readline != "\n"
                            # Do nothing.
                    end

                    # Improve copy-on-write friendliness.
                    GC.start

                    pid = fork
                    if pid.nil?
                            $0 = "#{$0} (forking...)"
                            client.puts "OK"
                            client.puts Process.pid
                            client.flush
                            client.sync = true
                            return [:forked, client]
                    else
                            NativeSupport.detach_process(pid)
                    end
            else
                    STDERR.puts "Unknown command '#{command.inspect}'"
            end
            return nil
    ensure
            if client && Process.pid == original_pid
                    begin
                            client.close
                    rescue Errno::EINVAL
                            # Work around OS X bug.
                            # https://code.google.com/p/phusion-passenger/issues/detail?id=854
                    end
            end
    end

init() click to toggle source

    # File lib/phusion_passenger/preloader_shared_helpers.rb, line 33
    def init
            if !Kernel.respond_to?(:fork)
                    message = "Smart spawning is not available on this Ruby " +
                            "implementation because it does not support `Kernel.fork`. "
                    if ENV['SERVER_SOFTWARE'].to_s =~ /nginx/
                            message << "Please set `passenger_spawn_method` to `direct`."
                    else
                            message << "Please set `PassengerSpawnMethod` to `direct`."
                    end
                    raise(message)
            end
    end

run_main_loop(options) click to toggle source

    # File lib/phusion_passenger/preloader_shared_helpers.rb, line 91
    def run_main_loop(options)
            $0 = "Passenger AppPreloader: #{options['app_root']}"
            client = nil
            original_pid = Process.pid
            socket_filename = "#{options['generation_dir']}/backends/preloader.#{Process.pid}"
            server = UNIXServer.new(socket_filename)
            server.close_on_exec!

            # Update the dump information just before telling the preloader that we're
            # ready because the HelperAgent will read and memorize this information.
            LoaderSharedHelpers.dump_all_information

            puts "!> Ready"
            puts "!> socket: unix:#{socket_filename}"
            puts "!> "

            while true
                    ios = select([server, STDIN])[0]
                    if ios.include?(server)
                            result, client = accept_and_process_next_client(server)
                            if result == :forked
                                    STDIN.reopen(client)
                                    STDOUT.reopen(client)
                                    STDOUT.sync = true
                                    client.close
                                    return :forked
                            end
                    end
                    if ios.include?(STDIN)
                            if STDIN.tty?
                                    begin
                                            # Prevent bash from exiting when we press Ctrl-D.
                                            STDIN.read_nonblock(1)
                                    rescue Errno::EAGAIN
                                            # Do nothing.
                                    end
                            end
                            break
                    end
            end
            return nil
    ensure
            server.close if server
            if original_pid == Process.pid
                    File.unlink(socket_filename) rescue nil
            end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.