durusmail: quixote-users: scripts for starting / stopping scgi_server
scripts for starting / stopping scgi_server
2005-08-08
2005-08-08
mso@oz.net (2 parts)
scripts for starting / stopping scgi_server
mso@oz.net
2005-08-08
Al Pacifico wrote:
> Al Pacifico
> Seattle, WA

Another Quixote user in Seattle. :)  I'd be curious to talk with you off
list.  Do you know about our Python users' group, which supposedly meets
this Thursday?

> Currently I'm stopping SCGI by running it in the foreground and using
ctrl-c, but eventually I will want a method I can use within a shell
script.
>
>
> I found some scripts for starting / stopping the SCGI server posted on
the
> web by Dave Kuhlman, but they looked like they predated Quixote-2.0. On
brief review, the QuixoteHandler class in the scgi module looks like
what
> I
> want, since it looks like it writes it's PID to a file, etc.

The QuixoteHandler in the SCGI package is for Quixote 1.  Q2 has its own
QuixoteHandler in scgi_server.py, and it doesn't write a PID file.

There are two answers to your question, one if you're on a Unix-like
system with start-stop-daemon or the equivalent, and one if you don't.
start-stop-daemon is a program that daemonizes a naive application and
takes care of the PID file stuff, and provides a standard interface to the
OS start/stop utilities.  If you have start-stop-daemon, leave your
application alone (since it's already working with SCGI using
scgi_server.run()).  Add an init script like this, adapted for your OS.

==================
#!/sbin/runscript
# This is /etc/init.d/ra on Linux Gentoo.
# Start/stop the RA application server.
#
# Using --background and --make-pidfile because the program does not fork or
# make a PID file itself.

EXECUTABLE_BASE=ra.scgi
EXECUTABLE=/home/mso/ra/$EXECUTABLE_BASE
PID_FILE=/var/run/${EXECUTABLE_BASE}.pid

start() {
        ebegin "Starting RA server"
        start-stop-daemon --start --quiet --chuid mso:users \
                --background --make-pidfile \
                --pidfile $PID_FILE \
                --exec $EXECUTABLE
        eend $? "Failed to start RA server"
}

stop() {
        ebegin "Stopping RA server"
        start-stop-daemon --stop --quiet --pidfile $PID_FILE
        eend $? "Failed to stop RA server"
}
==================

Later I had to make the application portable to Mac OS X 10.3, which
doesn't have start-stop-daemon and the equivalents looked pretty
complicated.  (I think 10.4 is better but I haven't looked at it.)  Plus I
wanted one solution that would work on both Mac and Linux.  So I wrote
this, based on a daemonizing recipe in the Python Cookbook.  It has
command-line options for all the things start-stop-daemon does, and can
run foreground/daemon or scgi/simple server.  I've tested it on Linux but
not fully on the Mac yet.  It's different than yours because I pass in a
root directory but have the common code create the publisher, using logs
and sessions set in my application-specific 'config' module.  Assuming you
adjusted your config object:

==================
import toplevel
server = toplevel.main_part1()   # Parse args and daemonize.
# Set up 'config' here.
toplevel.main_part2(             # Launch specific server.
    administrationDirectory.administrationDirectory(),
    config,
    server)
==================

I split the main function because my 'config' module contains database
connections, which can't be initialized till after daemonizing.  If your
'config' is just constants, you could combine the two steps.  toplevel.py
is attached.  Here's the relevant part of my 'config' structure:

===================
import quixote
class Container:
    def __init__(self, **kw):
        self.__dict__.update(kw)

# For the 'host' variables below, '' means listen on all interfaces.
# 'localhost' means listen only to requests from the same computer.
# Otherwise set a specific domain or IP.

# Options for the Simple server.
simple = Container(
    #host = 'localhost',
    host = '',
    port = 8082)

# Options for the SCGI server.
scgi = Container(
    port = 3002,
    script_name = "/ra",      # Must match Apache's  directive.
    max_children = 1,         # Max number of simultaneous requests.
    access_log = '/var/log/ra-access.log',
    error_log = '/var/log/ra-error.log')

# The application requires a database connection.
# Note: don't share connections between threads.
import MySQLdb
conn = MySQLdb.connect(user='XXX', passwd='XXX', db='XXX')

# Session management.  Use a separate database connection for sessions.
from session2.SessionManager import SessionManager
from session2.store.MySQLSessionStore import MySQLSessionStore
_conn = MySQLdb.connect(user='XXX', passwd='XXX', db='XXX')
_store = MySQLSessionStore(_conn, 'Session')
session = Container(
    manager_type = "sql",
    store = _store,
    manager = SessionManager(_store),
    ttl = 60 * 24,                        # Time to live: minutes.
    )
del _conn, _store
===================

session2 is the enhanced session package at http://quixote.idyll.org/ .
Note that toplevel.py line 42 checks
config.session.store.is_multiprocess_safe directly .

Your init script now becomes shorter:

===================
#!/sbin/runscript
# Start/stop the RA application server.

APP_ROOT=/home/mso/apps/ra
EXE=$APP_ROOT/ra-server.py
PID_FILE=$APP_ROOT/data/pid

start() {
        ebegin "Starting RA server"
        $EXE --scgi --daemon --pidfile=$PID_FILE --user=apache --group=apache
        eend $? "Failed to start RA server"
}

stop() {
        ebegin "Stopping RA server"
        $EXE --stop --pidfile $PID_FILE
        eend $? "Failed to stop RA server"
}

===================

--
-- Mike Orr 


reply