ports/156723: [patch] www/uwsgi: improved rc.d script
Luká Lalinský
lalinsky at gmail.com
Fri Apr 29 17:50:11 UTC 2011
>Number: 156723
>Category: ports
>Synopsis: [patch] www/uwsgi: improved rc.d script
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-ports-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: change-request
>Submitter-Id: current-users
>Arrival-Date: Fri Apr 29 17:50:11 UTC 2011
>Closed-Date:
>Last-Modified:
>Originator: Luká Lalinský
>Release: 8.2
>Organization:
>Environment:
8.2-RELEASE FreeBSD 8.2-RELEASE #0: Thu Mar 3 11:29:07 UTC 2011
>Description:
I needed to run multiple uWSGI instances and defining a number of "profiles" seemed like the best option. This is similar to what databases/slony1v2 uses. For example, in /etc/rc.conf I can have the following settings:
uwsgi_enable="YES"
uwsgi_profiles=yserver1 server2"
uwsgi_server1_flags="..."
uwsgi_server2_flags="..."
And the rc.d script will now manage two uWSGI instances, each of them can be individually restarted/reloaded/etc. If the uwsgi_profiles is not set, the current variables are used instead.
The attached patch contains the following changes:
- Don't kill all uwsgi processes with SIGKILL, kill only the master
process with SIGINT and let it terminate its children gracefully.
- Use SIGHUP for the reload command and add a new command brutalreload
(I'd have used forcereload but that's already taken) that sends the
SIGTERM signal. http://projects.unbit.it/uwsgi/wiki/uWSGISignals
- Add support for running multiple uwsgi instances with different
configurations. With no profiles, everything works as before. When you
define the uwsgi_profiles variable, it will read the names from there
and try to load also variables uwsgi_<profile>_<option>. The script will
perform specified actions on all profiles, unless you specify an extra
argument in which case it will be performed only on this profile.
For example, the following command will only reload the "django" profile:
I did the changes mainly for myself, but I thought it could be useful to other people.
>How-To-Repeat:
>Fix:
Patch attached with submission follows:
commit d1f9c183abafea9af974b9ee86f8a5ebc40fe776
Author: Lukáš Lalinský <lalinsky at gmail.com>
Date: Fri Apr 29 17:03:39 2011 +0000
Improved init script for uwsgi
- Don't kill all uwsgi processes with SIGKILL, kill only the master
process with SIGINT and let it terminate its children gracefully.
- Use SIGHUP for the reload command and add a new command brutalreload
(I'd have used forcereload but that's already taken) that sends the
SIGTERM signal. http://projects.unbit.it/uwsgi/wiki/uWSGISignals
- Add support for running multiple uwsgi instances with different
configurations. With no profiles, everything works as before. When you
define the uwsgi_profiles variable, it will read the names from there
and try to load also variables uwsgi_<profile>_<option>. The script will
perform specified actions on all profiles, unless you specify an extra
argument in which case it will be performed only on this profile.
For example, the following command will only reload the "django" profile:
$ /usr/local/etc/rc.d/uwsgi reload django
diff --git a/uwsgi/files/uwsgi.in b/uwsgi/files/uwsgi.in
index e40c9af..11ecf37 100644
--- a/uwsgi/files/uwsgi.in
+++ b/uwsgi/files/uwsgi.in
@@ -22,6 +22,15 @@
# Default is 80.
# uwsgi_flags (str): Set the uwsgi command line arguments
# Default is "-M -L".
+#
+# If you would like to have multiple uWSGI instances running, you can
+# define multiple profiles:
+#
+# uwsgi_profiles (str): Set the list of uwsgi profiles
+# Default is "".
+#
+# For each profile you can then define different options (except for
+# uwsgi_enable) using the syntax uwsgi_<profile>_<option>
. /etc/rc.subr
@@ -31,6 +40,7 @@ rcvar=`set_rcvar`
load_rc_config $name
: ${uwsgi_enable="NO"}
+: ${uwsgi_profiles=""}
: ${uwsgi_socket="/tmp/${name}.sock"}
: ${uwsgi_logfile="/var/log/${name}.log"}
: ${uwsgi_pidfile="/var/run/${name}.pid"}
@@ -38,13 +48,48 @@ load_rc_config $name
: ${uwsgi_gid="80"}
: ${uwsgi_flags="-M -L"}
+is_uwsgi_profile() {
+ local profile
+
+ for profile in $uwsgi_profiles; do
+ if [ "$profile" = "$1" ]; then
+ return 0
+ fi
+ done
+
+ return 1
+}
+
+if [ -n "${uwsgi_profiles}" ]; then
+ if [ -n "$2" ]; then
+ profile="$2"
+ if ! is_uwsgi_profile $profile; then
+ echo "$0: no such profile defined in uwsgi_profiles."
+ exit 1
+ fi
+ eval uwsgi_socket=\${uwsgi_${profile}_socket:-"/tmp/${name}-${profile}.sock"}
+ eval uwsgi_logfile=\${uwsgi_${profile}_logfile:-"/var/log/${name}-${profile}.log"}
+ eval uwsgi_pidfile=\${uwsgi_${profile}_pidfile:-"/var/run/${name}-${profile}.pid"}
+ eval uwsgi_uid=\${uwsgi_${profile}_uid:-"${uwsgi_uid}"}
+ eval uwsgi_gid=\${uwsgi_${profile}_gid:-"${uwsgi_uid}"}
+ eval uwsgi_flags=\${uwsgi_${profile}_flags:-"${uwsgi_flags}"}
+ elif [ -n "$1" ]; then
+ for profile in ${uwsgi_profiles}; do
+ echo "Processing ${name} profile: ${profile}"
+ $0 $1 ${profile}
+ done
+ exit 0
+ fi
+fi
+
command=%%PREFIX%%/bin/uwsgi
command_args="--pidfile ${uwsgi_pidfile} -s ${uwsgi_socket} -d ${uwsgi_logfile} --uid ${uwsgi_uid} --gid ${uwsgi_gid} ${uwsgi_flags}"
+pidfile=${uwsgi_pidfile}
stop_postcmd=stop_postcmd
reload_precmd=reload_precmd
-sig_reload="TERM"
-sig_stop="KILL"
-extra_commands="reload"
+brutalreload_cmd=brutalreload_cmd
+sig_stop="INT"
+extra_commands="reload brutalreload"
stop_postcmd()
{
@@ -53,7 +98,16 @@ stop_postcmd()
reload_precmd()
{
- echo "Restarting ${name} gracefully without closing the main sockets."
+ echo "Gracefully reloading ${name} without closing the main sockets."
+}
+
+brutalreload_cmd()
+{
+ echo "Reloading ${name} without closing the main sockets."
+
+ reload_precmd=""
+ sig_reload="TERM"
+ run_rc_command ${rc_prefix}reload $rc_extra_args || return 1
}
run_rc_command "$1"
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the freebsd-ports-bugs
mailing list