diff options
author | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-01-30 16:47:25 +0000 |
---|---|---|
committer | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-01-30 16:47:25 +0000 |
commit | f853ea99e91034278b108aba3482f6a8e95821c9 (patch) | |
tree | ed24a318f1eac28ceba96bf3ba63b2f81902b483 | |
parent | 93c5a3d003e6bfa59019b8b3ef58b9d07bb835b6 (diff) | |
parent | 31a380115a0f05d9709e016e349fbf91f1c20e2a (diff) | |
download | lorry-controller-f853ea99e91034278b108aba3482f6a8e95821c9.tar.gz |
Merge remote-tracking branch 'origin/richardmaw/ephemeral-port' into liw/daemonise
-rwxr-xr-x | lorry-controller-webapp | 31 | ||||
-rw-r--r-- | yarns.webapp/900-implementations.yarn | 15 |
2 files changed, 31 insertions, 15 deletions
diff --git a/lorry-controller-webapp b/lorry-controller-webapp index 884a3f5..27c6375 100755 --- a/lorry-controller-webapp +++ b/lorry-controller-webapp @@ -20,6 +20,7 @@ import logging import os import sqlite3 import time +import wsgiref.simple_server import bottle import cliapp @@ -171,11 +172,12 @@ class WEBAPP(cliapp.Application): ['wsgi'], 'run in wsgi mode (default is debug mode, for development)') - self.settings.integer( - ['debug-port'], - 'listen on PORT when in debug mode (i.e., not running under WSGI)', - metavar='PORT', - default=8888) + self.settings.string( + ['debug-port-file'], + 'write listening port to FILE when in debug mode ' + '(i.e., not running under WSGI)', + metavar='FILE', + default='webapp.port') self.settings.string( ['debug-host'], @@ -221,10 +223,27 @@ class WEBAPP(cliapp.Application): if self.settings['wsgi']: WSGIServer(webapp).run() else: + server_port_file = self.settings['debug-port-file'] + class DebugServer(wsgiref.simple_server.WSGIServer): + '''WSGI-like server that uses an ephemeral port. + + Rather than use a specified port, or default, the + DebugServer connects to an ephemeral port and writes + its number to debug-port-file, so a non-racy temporary + port can be used. + + ''' + + def __init__(self, (host, port), *args, **kwargs): + wsgiref.simple_server.WSGIServer.__init__( + self, (host, 0), *args, **kwargs) + with open(server_port_file, 'w') as f: + f.write(str(self.server_port) + '\n') + bottle.run( webapp, host=self.settings['debug-host'], - port=self.settings['debug-port'], + server_class=DebugServer, quiet=True, debug=True) diff --git a/yarns.webapp/900-implementations.yarn b/yarns.webapp/900-implementations.yarn index d311362..32262e3 100644 --- a/yarns.webapp/900-implementations.yarn +++ b/yarns.webapp/900-implementations.yarn @@ -24,14 +24,6 @@ but the shell doesn't wait for it to terminate. This way, WEBAPP will be running until it crashes or is explicitly killed. IMPLEMENTS GIVEN a running WEBAPP - # Pick a random port beyond 1024 (i.e., an unreserved one). - port=0 - while [ "$port" -le 1024 ] - do - port=$RANDOM - done - echo "$port" > "$DATADIR/webapp.port" - start-stop-daemon -S -x "$SRCDIR/lorry-controller-webapp" \ -b -p "$DATADIR/webapp.pid" -m --verbose \ -- \ @@ -40,7 +32,12 @@ be running until it crashes or is explicitly killed. --log-level debug \ --log "$DATADIR/webapp.log" \ --debug-host 127.0.0.1 \ - --debug-port "$port" + --debug-port-file "$DATADIR/webapp.port" + + while [ ! -e "$DATADIR/webapp.port" ]; do + sleep 1 + done + port=$(cat "$DATADIR/webapp.port") # Wait for the WEBAPP to actually be ready, i.e., that it's # listening on its assigned port. |