Commits

Young King  committed 4b352ad

fixed unix domain socket support in runfcgi

  • Participants
  • Parent commits 4eb331c

Comments (0)

Files changed (7)

 - ...
 
+0.6.1  (2010-11-18)
+--------------------
+
+- fixed documents.
+
+- fixed unix domain socket support in runfcgi (thks César H. Roldán) .
+
 0.6  (2010-10-21)
 --------------------
 

File docs/conf.py

 # built documents.
 #
 # The short X.Y version.
-version = '0.6'
+version = '0.6.1'
 # The full version, including alpha/beta/rc tags.
-release = '0.6'
+release = '0.6.1'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.

File docs/index.rst

 `````````````````
 you can change the manage.py and modify the line::
 
-  manage.run()
+  manager = Manager(app)
 
 to the line ::
 
-  manage.run(default_help_actions=False)
+  manager = Manager(app,default_help_actions=False)
 
 This will avoid load the default help actions.
 
 ````````````````
 you can change the manage.py and modify the line::
 
-  manage.run()
+  manager = Manager(app)
 
 to the line ::
 
-  manage.run(default_server_actions=True)
+  manager = Manager(app,default_server_actions=True)
 
 This will load the default server actions.
 
-currently,flask-actions has support following servers:
+currently,flask-actions has supported the following servers:
 
 - appengine
 - flup
 
 Running a threaded server on a TCP port::
 
-    ./manage.py runfcgi method=threaded host=127.0.0.1 port=3033
+    ./manage.py runfcgi --method=threaded --host=127.0.0.1 --port=3033
 
 Running a preforked server on a Unix domain socket::
 
-    ./manage.py runfcgi method=prefork socket=/home/user/mysite.sock pidfile=flask.pid
+    ./manage.py runfcgi --method=prefork --socket=/home/user/mysite.sock --pidfile=flask.pid
 
 Run without daemonizing (backgrounding) the process (good for debugging)::
 
-    ./manage.py runfcgi daemonize=false socket=/tmp/mysite.sock maxrequests=1
+    ./manage.py runfcgi --daemonize=false --socket=/tmp/mysite.sock --maxrequests=1
 
 Stopping the FastCGI daemon
 `````````````````````````````

File flaskext/actions/daemonize.py

         "A writeable object that writes to nowhere -- like /dev/null."
         def write(self, s):
             pass
-
-
-class nul:
-    write = staticmethod(lambda s: None)

File flaskext/actions/fastcgi.py

+#!/usr/bin/env python2
+
+"""
+  This file is borrowed from django.core.servers.fastcgi
+
+"""
+import os
+
+FASTCGI_HELP = r"""
+  Run this project as a fastcgi (or some other protocol supported
+  by flup) application. To do this, the flup package from
+  http://www.saddi.com/software/flup/ is required.
+
+   runfcgi [options] [fcgi settings]
+"""
+
+FASTCGI_OPTIONS = {
+    'protocol': 'fcgi',
+    'host': None,
+    'port': None,
+    'socket': None,
+    'method': 'fork',
+    'daemonize': None,
+    'workdir': '/',
+    'pidfile': None,
+    'maxspare': 5,
+    'minspare': 2,
+    'maxchildren': 50,
+    'maxrequests': 0,
+    'debug': None,
+    'outlog': None,
+    'errlog': None,
+    'umask': None,
+}
+
+def fastcgi_help(message=None):
+    print FASTCGI_HELP
+    if message:
+        print message
+    return False
+
+def runfastcgi(application,**kwargs):
+    options = FASTCGI_OPTIONS.copy()
+    options.update(kwargs)
+    try:
+        import flup
+    except ImportError, e:
+        print >> sys.stderr, "ERROR: %s" % e
+        print >> sys.stderr, "  Unable to load the flup package.  In order to run django"
+        print >> sys.stderr, "  as a FastCGI application, you will need to get flup from"
+        print >> sys.stderr, "  http://www.saddi.com/software/flup/   If you've already"
+        print >> sys.stderr, "  installed flup, then make sure you have it in your PYTHONPATH."
+        return False
+
+    flup_module = 'server.%s'%options['protocol']
+    print flup_module
+
+    if options['method'] in ('prefork', 'fork'):
+        wsgi_opts = {
+            'maxSpare': int(options["maxspare"]),
+            'minSpare': int(options["minspare"]),
+            'maxChildren': int(options["maxchildren"]),
+            'maxRequests': int(options["maxrequests"]),
+        }
+        flup_module += '_fork'
+    elif options['method'] in ('thread', 'threaded'):
+        wsgi_opts = {
+            'maxSpare': int(options["maxspare"]),
+            'minSpare': int(options["minspare"]),
+            'maxThreads': int(options["maxchildren"]),
+        }
+    else:
+        return fastcgi_help("ERROR: Implementation must be one of prefork or thread.")
+
+    wsgi_opts['debug'] = options['debug'] is not None
+
+    try:
+        WSGIServer = getattr(__import__('flup.' + flup_module, '', '', flup_module), 'WSGIServer')
+    except ImportError:
+        print "You need to install flup"
+        return False
+
+    if options["host"] and options["port"] and not options["socket"]:
+        wsgi_opts['bindAddress'] = (options["host"], int(options["port"]))
+    elif options["socket"]:
+        wsgi_opts['bindAddress'] = options["socket"]
+    elif not options["socket"] and not options["host"] and not options["port"]:
+        wsgi_opts['bindAddress'] = None
+    else:
+        return fastcgi_help("Invalid combination of host, port, socket.")
+
+    if options["daemonize"] is None:
+        # Default to daemonizing if we're running on a socket/named pipe.
+        daemonize = (wsgi_opts['bindAddress'] is not None)
+    else:
+        if options["daemonize"]:
+            daemonize = True
+        else:
+            daemonize = False
+
+    daemon_kwargs = {}
+    if options['outlog']:
+        daemon_kwargs['out_log'] = options['outlog']
+    if options['errlog']:
+        daemon_kwargs['err_log'] = options['errlog']
+    if options['umask']:
+        daemon_kwargs['umask'] = int(options['umask'])
+
+    if daemonize:
+        from .daemonize import become_daemon
+        become_daemon(our_home_dir=options["workdir"], **daemon_kwargs)
+
+    if options["pidfile"]:
+        fp = open(options["pidfile"], "w")
+        fp.write("%d\n" % os.getpid())
+        fp.close()
+
+    WSGIServer(application, **wsgi_opts).run()

File flaskext/actions/server_actions.py

 from werkzeug import script
 
 def runfcgi(application, before_daemon=None):
-    def action( hostname            =   ('h', ''),
-                port                =   ('p', 3001),
-                protocol            =   'scgi',
-                method              =   'threaded',
-                children            =   20,
-                daemonize           =   False,
-                pidfile             =   '/var/run/flask.pid',
-                workdir             =   '.',
-                outlog              =   '/dev/null',
-                errlog              =   '/dev/null',
-                umask               =   022,
+    def action( 
+            protocol            =   'fcgi',
+            hostname            =   ('h', ''),
+            port                =   ('p', 3001),
+            socket              =   '',
+            method              =   'threaded',
+            daemonize           =   False,
+            workdir             =   '.',
+            pidfile             =   '/var/run/flask.pid',
+            maxspare            =    5,
+            minspare            =    2,
+            maxchildren          =    50,
+            maxrequests         =    0,
+            debug               =    False,
+            outlog              =   '/dev/null',
+            errlog              =   '/dev/null',
+            umask               =   022,
         ):
         """run application use flup
         you can choose these arguments:
         protocol :   scgi, fcgi or ajp
         method   :   threaded or fork
+        socket   :   Unix domain socket
         children :   number of threads or processes"""
-        if protocol not in ('scgi', 'fcgi', 'ajp'):
-            sys.exit('invalid protocol: %s'%protocol)
-        flup_module = 'server.%s'%protocol
-
-        kw = {'maxSpare': children, 'minSpare': children}
-
-        if protocol == 'scgi':
-            kw['scriptName'] = '/'
-
-        if method == 'threaded':
-            kw['maxThreads'] = children
-        elif method == 'fork':
-            flup_module += '_fork'
-            kw['maxChildren'] = children
-        else:
-            sys.exit('invalid method: %s'%method)
-
-        try:
-            WSGIServer = getattr(__import__('flup.' + flup_module, '', '', flup_module), 'WSGIServer')
-        except ImportError:
-            print "You need to install flup"
-            sys.exit()
-
-        if os.name!='posix' and outlog=='/dev/null':
-            outlog = None
-        if os.name!='posix' and errlog=='/dev/null':
-            errlog = None
-
-        if daemonize:
-            from .daemonize import become_daemon
-            become_daemon(workdir, outlog, errlog, umask)
-
-        if daemonize and pidfile and os.name=='posix':
-            try:
-                fp = open(pidfile, 'w')
-                fp.write("%d\n" % os.getpid())
-                fp.close()
-            except:
-                pass
-
-        if before_daemon is not None:
-            before_daemon()
-
-        WSGIServer(application, bindAddress=(hostname, port), **kw).run()
+        from .fastcgi import runfastcgi
+        runfastcgi(
+            application         =  application,
+            protocol            =  protocol,
+            hostname            =  hostname,
+            port                =  port,
+            socket              =  socket,
+            method              =  method,
+            daemonize           =  daemonize,
+            workdir             =  workdir,
+            pidfile             =  pidfile,
+            maxspare            =  maxspare,
+            minspare            =  minspare,
+            maxchildren         =  maxchildren,
+            maxrequests         =  maxrequests,
+            debug               =  debug,
+            outlog              =  outlog,
+            errlog              =  errlog,
+            umask               =  umask,
+                )
 
     return action
 
 
 setup(
     name='Flask-Actions',
-    version='0.6',
+    version='0.6.1',
     url='http://blog.flyzen.com',
     license='BSD',
     author='Young King',