Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1930

Show
Ignore:
Timestamp:
03/18/08 12:47:35
Author:
fumanchu
Message:

Working cherryd daemon script. New FlupFCGIServer wrapper in servers.py. Also added a config arg to cherrypy.Application.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cpconfig.py

    r1887 r1930  
    6565 
    6666    engine:     Controls the 'application engine', including autoreload. 
     67                These can only be declared in the global config. 
     68    tree:       Grafts cherrypy.Application objects onto cherrypy.tree. 
    6769                These can only be declared in the global config. 
    6870    hooks:      Declares additional request-processing functions. 
     
    315317    """Namespace handler for the 'tree' config namespace.""" 
    316318    cherrypy.tree.graft(v, v.script_name) 
    317     cherrypy.engine.log("Mounted: %s on %s" % (v, v.script_name)) 
     319    cherrypy.engine.log("Mounted: %s on %s" % (v, v.script_name or "/")) 
    318320Config.namespaces["tree"] = _tree_namespace_handler 
    319321 
  • trunk/cherrypy/_cptree.py

    r1888 r1930  
    4646    relative_urls = False 
    4747     
    48     def __init__(self, root, script_name=""): 
     48    def __init__(self, root, script_name="", config=None): 
    4949        self.log = _cplogging.LogManager(id(self), cherrypy.log.logger_root) 
    5050        self.root = root 
     
    5757         
    5858        self.config = self.__class__.config.copy() 
     59        if config: 
     60            self.merge(config) 
    5961     
    6062    def __repr__(self): 
  • trunk/cherrypy/cherryd

    r1926 r1930  
    22"""The CherryPy daemon.""" 
    33 
    4 import getopt 
    5 import sys 
    6  
    74import cherrypy 
    8 from cherrypy.process import plugins 
     5from cherrypy.process import plugins, servers 
    96 
    107 
    11 shortopts = ["d", "e"] 
    12 longopts = [] 
    13  
    14 # Help for restd command-line options. 
    15 help = """ 
    16 cherryd. Start the cherrypy daemon. 
    17  
    18 Usage: 
    19     cherryd <config filename> 
    20  
    21 """ 
    22  
    23  
    24 def start(options, configfile, *args): 
     8def 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     
    2514    engine = cherrypy.engine 
    2615     
    27     siteconf = {} 
    28     cherrypy._cpconfig.merge(siteconf, configfile) 
    29      
    30     cherrypy.config.update(configfile) 
    31      
    32     if options.has_key('-e'): 
    33         cherrypy.config.update({'environment': options['-e']}) 
    34      
    35     # TODO: Make sure that log files are configurable from the conf file. 
     16    if environment is not None: 
     17        cherrypy.config.update({'environment': environment}) 
    3618     
    3719    # Only daemonize if asked to. 
    38     if options.has_key('-d')
     20    if daemonize
    3921        # Don't print anything to stdout/sterr. 
    4022        cherrypy.config.update({'log.screen': False}) 
    4123        plugins.Daemonizer(engine).subscribe() 
    4224     
    43     if options.has_key('--pidfile')
    44         plugins.PIDFile(engine, options['--pidfile']).subscribe() 
     25    if pidfile
     26        plugins.PIDFile(engine, pidfile).subscribe() 
    4527     
    4628    cherrypy.signal_handler.subscribe() 
    4729     
    48     # TODO: call a 'site setup' function (probably passing it siteconf). 
    49      
    50     if options.has_key('--fastcgi'): 
     30    if fastcgi: 
    5131        # turn off autoreload when using fastcgi 
    5232        cherrypy.config.update({'autoreload.on': False}) 
     
    5737        fastcgi_bindaddr = cherrypy.config.get('server.socket_host', '0.0.0.0') 
    5838        bindAddress = (fastcgi_bindaddr, fastcgi_port) 
    59         try: 
    60             # Always start the engine; this will start all other services 
    61             engine.start() 
    62              
    63             from flup.server.fcgi import WSGIServer 
    64             engine.log('Serving FastCGI on %s:%d' % bindAddress) 
    65             engine.fcgi = WSGIServer(application=wsgiapp, 
    66                                      bindAddress=bindAddress) 
    67             engine.fcgi.run() 
    68             engine.log('FastCGI Server on %s:%d shut down' % bindAddress) 
    69         finally: 
    70             engine.stop() 
    71     else: 
    72         # Always start the engine; this will start all other services 
    73         s = cherrypy.server 
    74         s.httpserver, s.bind_addr = s.httpserver_from_self() 
    75         s.httpserver.wsgi_app = wsgiapp 
    76         engine.start() 
    77         engine.block() 
     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() 
    7846 
    7947 
    8048if __name__ == '__main__': 
    81     try: 
    82         opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts) 
    83     except getopt.GetoptError: 
    84         print help 
    85         sys.exit(2) 
     49    from optparse import OptionParser 
    8650     
    87     start(dict(opts), *tuple(args)) 
     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 
  • trunk/cherrypy/process/servers.py

    r1830 r1930  
    122122 
    123123 
     124class FlupFCGIServer(object): 
     125    """Adapter for a flup.server.fcgi.WSGIServer.""" 
     126     
     127    def __init__(self, *args, **kwargs): 
     128        from flup.server.fcgi import WSGIServer 
     129        self.fcgiserver = WSGIServer(*args, **kwargs) 
     130        # TODO: report this bug upstream to flup. 
     131        # If we don't set _oldSIGs on Windows, we get: 
     132        #   File "C:\Python24\Lib\site-packages\flup\server\threadedserver.py", 
     133        #   line 108, in run 
     134        #     self._restoreSignalHandlers() 
     135        #   File "C:\Python24\Lib\site-packages\flup\server\threadedserver.py", 
     136        #   line 156, in _restoreSignalHandlers 
     137        #     for signum,handler in self._oldSIGs: 
     138        #   AttributeError: 'WSGIServer' object has no attribute '_oldSIGs' 
     139        self.fcgiserver._oldSIGs = [] 
     140        self.ready = False 
     141     
     142    def start(self): 
     143        """Start the FCGI server.""" 
     144        self.ready = True 
     145        self.fcgiserver.run() 
     146     
     147    def stop(self): 
     148        """Stop the HTTP server.""" 
     149        self.ready = False 
     150        # Forcibly stop the fcgi server main event loop. 
     151        self.fcgiserver._keepGoing = False 
     152        # Force all worker threads to die off. 
     153        self.fcgiserver._threadPool.maxSpare = 0 
     154 
     155 
    124156def client_host(server_host): 
    125157    """Return the host on which a client can connect to the given listener.""" 
  • trunk/cherrypy/scaffold/__init__.py

    r1864 r1930  
    99Change to this directory and run: 
    1010 
    11     python cpdeploy.py --config=example.conf 
     11    python cherrypy\cherryd cherrypy\scaffold\site.conf 
    1212 
    1313""" 
  • trunk/cherrypy/scaffold/example.conf

    r1863 r1930  
    1 [global] 
    2 # Uncomment this when you're done developing 
    3 #environment: "production" 
    4  
    5 server.socket_host: "0.0.0.0" 
    6 server.socket_port: 8088 
    7  
    8  
    91[/] 
    102log.error_file: "error.log" 

Hosted by WebFaction

Log in as guest/cpguest to create tickets