changeset 4287:d1be5f6846ca

Aggregated flup-based servers into single file and added server-type config (threaded/forked/single)
author Florian Krupicka <florian.krupicka@googlemail.com>
date Fri, 08 Aug 2008 20:49:12 +0200
parents 4850d477c22f
children 1ec18316ec96
files MoinMoin/web/ajp_interface.py MoinMoin/web/cgi_interface.py MoinMoin/web/flup_frontend.py MoinMoin/web/frontend.py MoinMoin/web/scgi_interface.py wiki/server/moin.ajp wiki/server/moin.cgi wiki/server/moin.scgi
diffstat 8 files changed, 105 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/web/ajp_interface.py	Fri Aug 08 18:51:22 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-"""
-    MoinMoin - AJP interface
-
-    @copyright: 2008 MoinMoin:FlorianKrupicka
-    @license: GNU GPL, see COPYING for details.
-"""
-import optparse
-
-from MoinMoin.web.frontend import ServerFrontEnd
-
-from MoinMoin import log
-logging = log.getLogger(__name__)
-
-class AJPFrontEnd(ServerFrontEnd):
-    def run_server(self, application, options):
-        from flup.server.ajp import WSGIServer
-
-        kwargs = {}
-        if options.port:
-            kwargs['bindAddress'] = (options.interface, options.port)
-        elif options.interface.startswith('/') or \
-                options.interface.startswith('./'):
-            kwargs['bindAddress'] = options.interface
-
-        WSGIServer(application, **kwargs).run()
-
-def run():
-    AJPFrontEnd().run()
-
-if __name__ == '__main__':
-    run()
--- a/MoinMoin/web/cgi_interface.py	Fri Aug 08 18:51:22 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,42 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-"""
-    MoinMoin - CGI/FCGI interface
-
-    @copyright: 2008 MoinMoin:FlorianKrupicka
-    @license: GNU GPL, see COPYING for details.
-"""
-import optparse
-
-from MoinMoin.web.frontend import ServerFrontEnd
-
-from MoinMoin import log
-logging = log.getLogger(__name__)
-
-class CGIFrontEnd(ServerFrontEnd):
-    def run_server(self, application, options):
-        cgi_fallback = False
-
-        try:
-            from flup.server.fcgi import WSGIServer
-        except ImportError:
-            from MoinMoin.web._fallback_cgi import WSGIServer
-            cgi_fallback = True
-
-        kwargs = {}
-        if options.port:
-            kwargs['bindAddress'] = (options.interface, options.port)
-        elif options.interface.startswith('/') or \
-                options.interface.startswith('./'):
-            kwargs['bindAddress'] = options.interface
-        if not cgi_fallback:
-            WSGIServer(application, **kwargs).run()
-        else:
-            if 'bindAddress' in kwargs:
-                logging.warning('Cannot bind to socket when running with CGI fallback')
-                WSGIServer(application).run()
-
-def run():
-    CGIFrontEnd().run()
-
-if __name__ == '__main__':
-    run()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/web/flup_frontend.py	Fri Aug 08 20:49:12 2008 +0200
@@ -0,0 +1,92 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - Flup based WSGI adapters
+
+    @copyright: 2008 MoinMoin:FlorianKrupicka
+    @license: GNU GPL, see COPYING for details.
+"""
+from MoinMoin.web.frontend import ServerFrontEnd
+
+from MoinMoin import log
+logging = log.getLogger(__name__)
+
+class FlupFrontEnd(ServerFrontEnd):
+
+    is_fcgi = False
+
+    def add_options(self):
+        super(FlupFrontEnd, self).add_options()
+        parser = self.parser
+        parser.add_option("--min-spare", dest="min_spare", type="int", metavar='MIN',
+                          help=("Minimum spare threads/processes (when "
+                                "using threaded or forking servers)."))
+        parser.add_option("--max-spare", dest="max_spare", type="int", metavar='MAX',
+                          help=("Maximum spare threads/processes (when "
+                                "using threaded or forking servers)."))
+        parser.add_option("--max-childs", dest="max_childs", type="int", metavar='CHILDS',
+                          help=("Hard upper limit on threads/processes "
+                                "(when using threaded or forking servers)."))
+        parser.add_option("-t", "--type", dest="server_type", metavar='TYPE',
+                          help=("Type of server to use, e.g. single/threaded"
+                                "/forking. Defaults to 'single' when not "
+                                "bound to a socket and to 'threaded' when it is"))
+
+    def run_server(self, application, options):
+        cgi_fallback = False
+
+        server_type = options.server_type
+
+        if not server_type:
+            server_type = (options.port and 'threaded') or 'single'
+
+        if server_type not in self.server_types:
+            raise ArgumentError("Unknown server type '%s'" % options.server_type)
+
+        multi = server_type in ('threaded', 'forking')
+
+        try:
+            mod = self.server_types[server_type]
+            mod = __import__(mod, fromlist=['WSGIServer'])
+            WSGIServer = mod.WSGIServer
+        except ImportError:
+            if self.is_fcgi:
+                from MoinMoin.web._fallback_cgi import WSGIServer
+                cgi_fallback = True
+
+        kwargs = {}
+
+        if options.port:
+            kwargs['bindAddress'] = (options.interface, options.port)
+        elif options.interface.startswith('/') or \
+                options.interface.startswith('./'):
+            kwargs['bindAddress'] = options.interface
+
+        if options.min_spare and multi:
+            kwargs['minSpare'] = options.min_spare
+        if options.max_spare and multi:
+            kwargs['maxSpare'] = options.max_spare
+        if options.max_childs and multi:
+            if server_type == 'threaded':
+                kwargs['maxThreads'] = options.max_childs
+            else:
+                kwargs['maxChildren'] = options.max_childs
+
+        if not cgi_fallback:
+            return WSGIServer(application, **kwargs).run()
+        else:
+            logging.warning('Running as CGI fallback.')
+            return WSGIServer(application).run()
+
+class FCGIFrontEnd(FlupFrontEnd):
+    is_fcgi = True
+    server_types = {'threaded': 'flup.server.fcgi',
+                    'forking': 'flup.server.fcgi_fork',
+                    'single': 'flup.server.fcgi_single'}
+
+class SCGIFrontEnd(FlupFrontEnd):
+    server_types = {'threaded': 'flup.server.scgi',
+                    'forking': 'flup.server.scgi_fork'}
+
+class AJPFrontEnd(FlupFrontEnd):
+    server_types = {'threaded': 'flup.server.ajp',
+                    'forking': 'flup.server.ajp_fork'}
--- a/MoinMoin/web/frontend.py	Fri Aug 08 18:51:22 2008 +0200
+++ b/MoinMoin/web/frontend.py	Fri Aug 08 20:49:12 2008 +0200
@@ -55,3 +55,9 @@
                                 "with '/' or './' it is interpreted as a path "
                                 "to a unix socket. Default: localhost"))
         parser.set_default('interface', 'localhost')
+
+class CGIFrontEnd(FrontEnd):
+    """ Simple WSGI CGI Adapter. """
+    def run_server(self, application, options):
+        from MoinMoin.web._fallback_cgi import WSGIServer
+        return WSGIServer(application).run()
--- a/MoinMoin/web/scgi_interface.py	Fri Aug 08 18:51:22 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-"""
-    MoinMoin - SCGI interface
-
-    @copyright: 2008 MoinMoin:FlorianKrupicka
-    @license: GNU GPL, see COPYING for details.
-"""
-import optparse
-
-from MoinMoin.web.frontend import ServerFrontEnd
-
-from MoinMoin import log
-logging = log.getLogger(__name__)
-
-class SCGIFrontEnd(ServerFrontEnd):
-    def run_server(self, application, options):
-        from flup.server.scgi import WSGIServer
-
-        kwargs = {}
-        if options.port:
-            kwargs['bindAddress'] = (options.interface, options.port)
-        elif options.interface.startswith('/') or \
-                options.interface.startswith('./'):
-            kwargs['bindAddress'] = options.interface
-
-        WSGIServer(application, **kwargs).run()
-
-def run():
-    SCGIFrontEnd().run()
-
-if __name__ == '__main__':
-    run()
--- a/wiki/server/moin.ajp	Fri Aug 08 18:51:22 2008 +0200
+++ b/wiki/server/moin.ajp	Fri Aug 08 20:49:12 2008 +0200
@@ -29,6 +29,7 @@
 #from MoinMoin import log
 #log.load_config('/path/to/logging_configuration_file')
 
-from MoinMoin.web.ajp_interface import run
 
-run()
+from MoinMoin.web.flup_frontend import AJPFrontEnd
+AJPFrontEnd().run()
+
--- a/wiki/server/moin.cgi	Fri Aug 08 18:51:22 2008 +0200
+++ b/wiki/server/moin.cgi	Fri Aug 08 20:49:12 2008 +0200
@@ -31,6 +31,5 @@
 #from MoinMoin import log
 #log.load_config('/path/to/logging_configuration_file')
 
-from MoinMoin.web.cgi_interface import run
-
-run()
+from MoinMoin.web.flup_frontend import FCGIFrontEnd
+FCGIFrontEnd().run()
--- a/wiki/server/moin.scgi	Fri Aug 08 18:51:22 2008 +0200
+++ b/wiki/server/moin.scgi	Fri Aug 08 20:49:12 2008 +0200
@@ -29,6 +29,6 @@
 #from MoinMoin import log
 #log.load_config('/path/to/logging_configuration_file')
 
-from MoinMoin.web.scgi_interface import run
+from MoinMoin.web.flup_frontend import SCGIFrontEnd
+SCGIFrontEnd().run()
 
-run()