Module: PhusionPassenger::PreloaderSharedHelpers

Extended by:
PreloaderSharedHelpers
Included in:
PreloaderSharedHelpers
Defined in:
lib/phusion_passenger/preloader_shared_helpers.rb

Overview

Provides shared functions for preloader apps.

Instance Method Summary collapse

Instance Method Details

#accept_and_process_next_client(server_socket) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
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
# File 'lib/phusion_passenger/preloader_shared_helpers.rb', line 47

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]
		elsif defined?(NativeSupport)
			NativeSupport.detach_process(pid)
		else
			Process.detach(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

#initObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/phusion_passenger/preloader_shared_helpers.rb', line 34

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/i
			message << "Please set `passenger_spawn_method` to `direct`."
		else
			message << "Please set `PassengerSpawnMethod` to `direct`."
		end
		raise(message)
	end
end

#run_main_loop(options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/phusion_passenger/preloader_shared_helpers.rb', line 94

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
		# We call ::select just in case someone overwrites the global select()
		# function by including ActionView::Helpers in the wrong place.
		# https://code.google.com/p/phusion-passenger/issues/detail?id=915
		ios = Kernel.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