Class: PhusionPassenger::AppProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/phusion_passenger/app_process.rb

Overview

Contains various information about an application process.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_root, pid, owner_pipe, server_sockets) ⇒ AppProcess

Creates a new AppProcess instance. The parameters correspond with the attributes of the same names. No exceptions will be thrown.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/phusion_passenger/app_process.rb', line 149

def initialize(app_root, pid, owner_pipe, server_sockets)
	@app_root   = app_root
	@pid        = pid
	@owner_pipe = owner_pipe
	
	# We copy the values like this so one can directly pass
	# AbstractRequestHandler#server_sockets as arguments
	# without having AppProcess store references to the socket
	# IO objects.
	@server_sockets = {}
	server_sockets.each_pair do |name, value|
		@server_sockets[name] = [value[0], value[1]]
	end
end

Instance Attribute Details

#app_rootObject (readonly)

The root directory of this application process.



31
32
33
# File 'lib/phusion_passenger/app_process.rb', line 31

def app_root
  @app_root
end

#owner_pipeObject (readonly)

The owner pipe of the application instance (an IO object). Please see RequestHandler for a description of the owner pipe.



53
54
55
# File 'lib/phusion_passenger/app_process.rb', line 53

def owner_pipe
  @owner_pipe
end

#pidObject (readonly)

This process’s PID.



34
35
36
# File 'lib/phusion_passenger/app_process.rb', line 34

def pid
  @pid
end

#server_socketsObject (readonly)

A hash containing all server sockets that this application process listens on. The hash is in the form of:

{
   name1 => [socket_address1, socket_type1],
   name2 => [socket_address2, socket_type2],
   ...
}

name is a Symbol. socket_addressx is the address of the socket and socket_type1 is the socket’s type (either ‘unix’ or ‘tcp’). There’s guaranteed to be at least one server socket, namely one with the name :main.



49
50
51
# File 'lib/phusion_passenger/app_process.rb', line 49

def server_sockets
  @server_sockets
end

Class Method Details

.detect_framework_version(app_root) ⇒ Object

  • Returns the Ruby on Rails version that the application requires.

  • Returns :vendor if the application has a vendored Rails.

  • Returns nil if the application doesn’t specify a particular version.

Raises VersionNotFound if the required Rails version is not installed.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/phusion_passenger/app_process.rb', line 59

def self.detect_framework_version(app_root)
	if File.directory?("#{app_root}/vendor/rails/railties")
		# NOTE: We must check for 'rails/railties' and not just 'rails'.
		# Typo's vendor directory contains an empty 'rails' directory.
		return :vendor
	end
	
	environment_rb = File.read("#{app_root}/config/environment.rb")
	environment_rb =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
	gem_version_spec = $1
	if gem_version_spec.nil?
		return nil
	end
	
	search_results = search_gem('rails', gem_version_spec)
	found_version = search_results.map do |x|
		x.version.version
	end.sort.last
	if found_version.nil?
		# If this error was reported before, then the cache might be out of
		# date because the Rails version may have been installed now.
		# So we reload the RubyGems cache and try again.
		Gem.clear_paths
		search_results = search_gem('rails', gem_version_spec)
		found_version = search_results.map do |x|
			x.version.version
		end.sort.last
	end
	
	if found_version.nil?
		raise VersionNotFound.new("There is no Ruby on Rails version " <<
			"installed that matches version \"#{gem_version_spec}\"",
			gem_version_spec)
	else
		return found_version
	end
end

.read_from_channel(channel) ⇒ Object

Construct an AppProcess by reading information from the given MessageChannel. The other side of the channel must be writing AppProcess information using AppProcess#write_to_channel.

Might raise SystemCallError, IOError or SocketError.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/phusion_passenger/app_process.rb', line 114

def self.read_from_channel(channel)
	app_root, pid, n_server_sockets = channel.read
	if app_root.nil?
		raise IOError, "Connection closed"
	end
	
	server_sockets = {}
	n_server_sockets.to_i.times do
		message = channel.read
		if message.nil?
			raise IOError, "Connection closed"
		end
		name = message.shift
		server_sockets[name.to_sym] = message
	end
	
	owner_pipe = channel.recv_io
	
	return new(app_root, pid.to_i, owner_pipe, server_sockets)
end

.search_gem(gem_name, gem_version_spec) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/phusion_passenger/app_process.rb', line 97

def self.search_gem(gem_name, gem_version_spec)
	if defined?(Gem::Specification) && Gem::Specification.respond_to?(:find_all_by_name)
		return Gem::Specification.find_all_by_name(gem_name, gem_version_spec)
	elsif Gem.respond_to?(:source_index)
		dep = Gem::Dependency.new(gem_name, gem_version_spec)
		return Gem.source_index.search(dep, true)
	else
		dep = Gem::Dependency.new(gem_name, gem_version_spec)
		return Gem.cache.search(dep, true)
	end
end

Instance Method Details

#closeObject

Close the connection with the application process. If there are no other processes that have connections to this application process, then it will shutdown as soon as possible.

See also AbstractRequestHandler#owner_pipe.



169
170
171
# File 'lib/phusion_passenger/app_process.rb', line 169

def close
	@owner_pipe.close rescue nil
end

#write_to_channel(channel) ⇒ Object

Write this AppProcess’s information over the given MessageChannel. The other side must read the information using AppProces.read_from_channel.

Might raise SystemCallError, IOError or SocketError.



139
140
141
142
143
144
145
# File 'lib/phusion_passenger/app_process.rb', line 139

def write_to_channel(channel)
	channel.write(@app_root, @pid, @server_sockets.size)
	@server_sockets.each_pair do |name, value|
		channel.write(name.to_s, *value)
	end
	channel.send_io(@owner_pipe)
end