| 1 |
#! /usr/bin/env python |
|---|
| 2 |
"""The CherryPy daemon.""" |
|---|
| 3 |
|
|---|
| 4 |
import cherrypy |
|---|
| 5 |
from cherrypy.process import plugins, servers |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
def start(configfile=None, daemonize=False, environment=None, |
|---|
| 9 |
fastcgi=False, pidfile=None): |
|---|
| 10 |
"""Subscribe all engine plugins and start the engine.""" |
|---|
| 11 |
if configfile: |
|---|
| 12 |
cherrypy.config.update(configfile) |
|---|
| 13 |
|
|---|
| 14 |
engine = cherrypy.engine |
|---|
| 15 |
|
|---|
| 16 |
if environment is not None: |
|---|
| 17 |
cherrypy.config.update({'environment': environment}) |
|---|
| 18 |
|
|---|
| 19 |
# Only daemonize if asked to. |
|---|
| 20 |
if daemonize: |
|---|
| 21 |
# Don't print anything to stdout/sterr. |
|---|
| 22 |
cherrypy.config.update({'log.screen': False}) |
|---|
| 23 |
plugins.Daemonizer(engine).subscribe() |
|---|
| 24 |
|
|---|
| 25 |
if pidfile: |
|---|
| 26 |
plugins.PIDFile(engine, pidfile).subscribe() |
|---|
| 27 |
|
|---|
| 28 |
cherrypy.signal_handler.subscribe() |
|---|
| 29 |
|
|---|
| 30 |
if fastcgi: |
|---|
| 31 |
# turn off autoreload when using fastcgi |
|---|
| 32 |
cherrypy.config.update({'autoreload.on': False}) |
|---|
| 33 |
|
|---|
| 34 |
cherrypy.server.unsubscribe() |
|---|
| 35 |
|
|---|
| 36 |
fastcgi_port = cherrypy.config.get('server.socket_port', 4000) |
|---|
| 37 |
fastcgi_bindaddr = cherrypy.config.get('server.socket_host', '0.0.0.0') |
|---|
| 38 |
bindAddress = (fastcgi_bindaddr, fastcgi_port) |
|---|
| 39 |
f = servers.FlupFCGIServer(application=cherrypy.tree, bindAddress=bindAddress) |
|---|
| 40 |
s = servers.ServerAdapter(engine, httpserver=f, bind_addr=bindAddress) |
|---|
| 41 |
s.subscribe() |
|---|
| 42 |
|
|---|
| 43 |
# Always start the engine; this will start all other services |
|---|
| 44 |
engine.start() |
|---|
| 45 |
engine.block() |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
if __name__ == '__main__': |
|---|
| 49 |
from optparse import OptionParser |
|---|
| 50 |
|
|---|
| 51 |
p = OptionParser() |
|---|
| 52 |
p.add_option('-c', '--config', dest='config', |
|---|
| 53 |
help="specify a config file") |
|---|
| 54 |
p.add_option('-d', action="store_true", dest='daemonize', |
|---|
| 55 |
help="run the server as a daemon") |
|---|
| 56 |
p.add_option('-e', '--environment', dest='environment', default=None, |
|---|
| 57 |
help="apply the given config environment") |
|---|
| 58 |
p.add_option('-f', action="store_true", dest='fastcgi', |
|---|
| 59 |
help="start a fastcgi server instead of the default HTTP server") |
|---|
| 60 |
p.add_option('-p', '--pidfile', dest='pidfile', default=None, |
|---|
| 61 |
help="store the process id in the given file") |
|---|
| 62 |
options, args = p.parse_args() |
|---|
| 63 |
|
|---|
| 64 |
start(options.config, options.daemonize, |
|---|
| 65 |
options.environment, options.fastcgi, options.pidfile) |
|---|
| 66 |
|
|---|