SOURCES: php-fcgi.sysconfig, php-fcgi.init - almost zero (perhaps ...

glen glen at pld-linux.org
Thu Dec 14 19:42:22 CET 2006


Author: glen                         Date: Thu Dec 14 18:42:22 2006 GMT
Module: SOURCES                       Tag: HEAD
---- Log message:
- almost zero (perhaps true?) downtime in case restart and using local sockets
- you must upgrade you config to use new vars for this to take effect (old config is fine too just no new features)

---- Files affected:
SOURCES:
   php-fcgi.sysconfig (1.7 -> 1.8) , php-fcgi.init (1.10 -> 1.11) 

---- Diffs:

================================================================
Index: SOURCES/php-fcgi.sysconfig
diff -u SOURCES/php-fcgi.sysconfig:1.7 SOURCES/php-fcgi.sysconfig:1.8
--- SOURCES/php-fcgi.sysconfig:1.7	Thu Dec 14 14:21:12 2006
+++ SOURCES/php-fcgi.sysconfig	Thu Dec 14 19:42:16 2006
@@ -1,10 +1,19 @@
 # $Id$
-
-# Bind to tcp port 1026 on localhost, running as http:http
-#SPAWNARGS="-p 1026 -u http -g http"
+#
+# User and Group to use for spawned FCGI app.
+SPAWN_UID=http
+SPAWN_GID=http
 
 # Bind to unix-domain socket, running as http:http
-SPAWNARGS="-s /var/run/php/fcgi.sock -u http -g http"
+SPAWN_SOCKET="/var/run/php/fcgi.sock"
+
+# Bind to tcp port 1026 on localhost.
+# Address myst be IPv4 not name.
+#SPAWN_PORT="1026"
+#SPAWN_ADDR="127.0.0.1"
+
+# Additional args not covered here.
+#SPAWNARGS=""
 
 # for PHP 5.x
 PHP_FCGI_BINARY=/usr/bin/php.fcgi

================================================================
Index: SOURCES/php-fcgi.init
diff -u SOURCES/php-fcgi.init:1.10 SOURCES/php-fcgi.init:1.11
--- SOURCES/php-fcgi.init:1.10	Thu Dec 14 09:12:08 2006
+++ SOURCES/php-fcgi.init	Thu Dec 14 19:42:16 2006
@@ -33,17 +33,68 @@
 	PHP_FCGI_BINARY=/usr/bin/php.fcgi
 fi
 
+checkconfig() {
+	if [ -n "$SPAWN_PORT" -a -n "$SPAWN_SOCKET" ]; then
+		echo >&2 "$0: port and socket can not be used simulatenously"
+		exit 1
+	fi
+
+	if [ -n "$SPAWN_ADDR" -a -z "$SPAWN_PORT" ]; then
+		echo >&2 "$0: bind address specified but no port"
+		exit 1
+	fi
+
+	if [ -n "$SPAWN_ADDR" ] && [[ $SPAWN_ADDR != [0-9]*.[0-9]*.[0-9]*.[0-9] ]]; then
+		echo >&2 "$0: bind address not valid ipv6 address: $SPAWN_ADDR"
+		exit 1
+	fi
+
+	if [ -n "$SPAWN_PORT" ] && [[ $SPAWN_PORT != [0-9]* ]]; then
+		echo >&2 "$0: spawn port not numeric: $SPAWN_PORT"
+		exit 1
+	fi
+}
+
+# Spawns FCGI process.
+
+# Sets $RETVAL
+# Creates subsys lock.
+fcgi_spawn() {
+	export PHP_FCGI_MAX_REQUESTS
+	export FCGI_WEB_SERVER_ADDRS
+	local args
+
+	# user/group
+	args="$args ${SPAWN_UID:+-u $SPAWN_UID}"
+	args="$args ${SPAWN_GID:+-g $SPAWN_GID}"
+
+	# bind address
+	args="$args ${SPAWN_ADDR:+-a $SPAWN_ADDR}"
+	args="$args ${SPAWN_PORT:+-p $SPAWN_PORT}"
+	args="$args ${SPAWN_SOCKET:+-s $SPAWN_SOCKET}"
+	args="$args $SPAWNARGS"
+
+	msg_starting "PHP FastCGI"
+	checkconfig
+	daemon /usr/sbin/spawn-fcgi -P /var/run/php-fcgi.pid $args -f $PHP_FCGI_BINARY -C $PHP_FCGI_CHILDREN
+	RETVAL=$?
+	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/php-fcgi
+}
+
+# Stops FCGI.
+#
+# Removes lockfile.
+# RETVAL is not set
+fcgi_stop() {
+	msg_stopping "PHP FastCGI"
+	killproc --pidfile php-fcgi.pid ${PHP_FCGI_BINARY##*/}
+	rm -f /var/lock/subsys/php-fcgi >/dev/null 2>&1
+}
+
 start() {
 	# Start daemons.
 	if [ ! -f /var/lock/subsys/php-fcgi ]; then
-		export PHP_FCGI_MAX_REQUESTS
-		export FCGI_WEB_SERVER_ADDRS
-
-		msg_starting "PHP FastCGI"
-		daemon /usr/sbin/spawn-fcgi -P /var/run/php-fcgi.pid $SPAWNARGS -f $PHP_FCGI_BINARY -C $PHP_FCGI_CHILDREN
-
-		RETVAL=$?
-		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/php-fcgi
+		fcgi_spawn
 	else
 		msg_already_running "PHP FastCGI"
 	fi
@@ -52,27 +103,69 @@
 stop() {
 	# Stop daemons.
 	if [ -f /var/lock/subsys/php-fcgi ]; then
-		msg_stopping "PHP FastCGI"
-		killproc --pidfile php-fcgi.pid ${PHP_FCGI_BINARY##*/}
-		rm -f /var/lock/subsys/php-fcgi >/dev/null 2>&1
+		fcgi_stop
 	else
 		msg_not_running "PHP FastCGI"
 	fi
 }
 
+restart() {
+
+	local stop socket oldpid newpid
+
+	# we need to know about stopping before we start
+	if [ -f /var/lock/subsys/php-fcgi ]; then
+		stop=1
+		oldpid=$(cat /var/run/php-fcgi.pid)
+	fi
+
+	# so if we're using local sockets, we can create the new processes before
+	# stopping old ones, thus causing almost no downtime during restart
+	if [ "$SPAWN_SOCKET" ]; then
+		socket="$SPAWN_SOCKET"
+		SPAWN_SOCKET=$(mktemp -p ${SPAWN_SOCKET%/*} ${SPAWN_SOCKET##*/}.XXXXXX) || exit 1
+		[ "$SPAWN_UID" ] && chown $SPAWN_UID $SPAWN_SOCKET
+		[ "$SPAWN_GID" ] && chgrp $SPAWN_GID $SPAWN_SOCKET
+
+		fcgi_spawn
+		if [ $RETVAL = 0 ]; then
+			# on success switch the socket and we can kill the old processes
+			mv -f "$SPAWN_SOCKET" "$socket"
+
+			# to prevent killing the newly spawned process we restore pid of old fcgi
+			newpid=$(cat /var/run/php-fcgi.pid)
+			echo "$oldpid" > /var/run/php-fcgi.pid
+		fi
+	fi
+
+	# Stop daemons.
+	if [ "$stop" = 1 ]; then
+		fcgi_stop
+		if [ -n "$newpid" ]; then
+			echo "$newpid" > /var/run/php-fcgi.pid
+			touch /var/lock/subsys/php-fcgi
+		fi
+	fi
+
+	# if we used socket, the new process was started before killing old one
+	if [ -z "$SPAWN_SOCKET" ]; then
+		fcgi_spawn
+	fi
+}
+
 RETVAL=0
 # See how we were called.
 case "$1" in
 start)
+	checkconfig
 	start
 	;;
 stop)
 	stop
 	;;
 restart)
-	stop
-	start
-	exit $?
+	checkconfig
+	restart
 	;;
 status)
 	status php-fcgi $PHP_FCGI_BINARY
@@ -81,7 +174,7 @@
 reload|force-reload)
 	if [ -f /var/lock/subsys/php-fcgi ]; then
 		msg_reloading "PHP FastCGI"
-		killproc php.fcgi -HUP
+		killproc --pidfile php-fcgi.pid ${PHP_FCGI_BINARY##*/}
 		RETVAL=$?
 	else
 		msg_not_running "PHP FastCGI"
================================================================

---- CVS-web:
    http://cvs.pld-linux.org/SOURCES/php-fcgi.sysconfig?r1=1.7&r2=1.8&f=u
    http://cvs.pld-linux.org/SOURCES/php-fcgi.init?r1=1.10&r2=1.11&f=u



More information about the pld-cvs-commit mailing list