Comment 11 for bug 143422

Revision history for this message
Darryl Dixon (esrever-otua) wrote :

For what it's worth, for all the users of older (Zope <=2.9.8) releases, we did the same thing for the same reason with the following slightly hacky hotfix. It hooks in to Lifetime.loop to only light up a 'HAport' (in Pound terms) once the application was in fact actually ready (Pound watches for the appearance/disappearance of the HAport instead of the usual application port). Because of some other initialisation stuff we do, our Zope startup times were anywhere up to 5-10 minutes, so this was a critical fix for us:

import socket, thread, zLOG
from exceptions import Exception

from App.config import getConfiguration

import Lifetime

_original_loop = Lifetime.loop

def halistenerStarter():
    zLOG.LOG('ZZZHAListener', zLOG.INFO, 'Attempting to initialise')
    try:
        servers = [x.getsockname() for x in getConfiguration().servers]
    except Exception, e:
        zLOG.LOG('ZZZHAListener', zLOG.INFO, 'Dieing on: %s' % str(e))
    if servers:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        # The first one in the config file is the HTTP Server...
        # We could just specify a value for the ip-address variable in
        # the zope.conf and use getConfiguration().ip_address, but
        # this would limit us in some other ways.
        run_ha = True
        try:
            s.bind((servers[0][0], 58080))
        except:
            zLOG.LOG('ZZZHAListener', zLOG.INFO, "Couldn't bind to %s on port 58080 - probably there's something already listening there? Starting up without the HAport" % servers[0][0])
            run_ha = False

        def ha_run(s):
            s.listen(socket.SOMAXCONN)
            while(1):
                try:
                    conn, addr = s.accept()
                    conn.close()
                except socket.error:
                    pass
            s.close()

        if run_ha:
            zLOG.LOG('ZZZHAListener', zLOG.INFO, 'Coming online now')
            thread.start_new_thread(ha_run, (s,))
    else:
        zLOG.LOG('ZZZHAListener', zLOG.WARN, 'Could not find a valid HTTPServer to bind to the IP address of')
    _original_loop()

Lifetime.loop = halistenerStarter