[Zodb-checkins] CVS: StandaloneZODB/ZEO - start.py:1.26.4.2.2.2

Jeremy Hylton jeremy@zope.com
Thu, 25 Apr 2002 17:04:15 -0400


Update of /cvs-repository/StandaloneZODB/ZEO
In directory cvs.zope.org:/tmp/cvs-serv16906

Modified Files:
      Tag: ZEO2-branch
	start.py 
Log Message:
Merge a bunch of changes from ZEO 1.0.

Do a better job of computing INSTANCE_HOME and choosing var directory.

Add -d option (set STUPID_LOG_SEVERITY).

Cleanly log and report fatal errors that occur during startup.

Recover from failures to set signal handlers.  (try/finally should
have been try/except.)

Do not require platform to support getppd() or getpid().

Remove prof option.


=== StandaloneZODB/ZEO/start.py 1.26.4.2.2.1 => 1.26.4.2.2.2 ===
         last=a
 
-    INSTANCE_HOME=os.environ.get('INSTANCE_HOME', directory(me, 4))
+    if os.environ.has_key('INSTANCE_HOME'):
+        INSTANCE_HOME=os.environ['INSTANCE_HOME']
+    elif os.path.isdir(os.path.join(directory(me, 4),'var')):
+        INSTANCE_HOME=directory(me, 4)
+    else:
+        INSTANCE_HOME=os.getcwd()
+
+    if os.path.isdir(os.path.join(INSTANCE_HOME, 'var')):
+        var=os.path.join(INSTANCE_HOME, 'var')
+    else:
+        var=INSTANCE_HOME
 
     zeo_pid=os.environ.get('ZEO_SERVER_PID',
-                           os.path.join(INSTANCE_HOME, 'var', 'ZEO_SERVER.pid')
+                           os.path.join(var, 'ZEO_SERVER.pid')
                            )
 
-    fs=os.path.join(INSTANCE_HOME, 'var', 'Data.fs')
+    fs = os.path.join(var, 'Data.fs')
 
     usage="""%s [options] [filename]
 
@@ -77,6 +87,8 @@
 
        -D -- Run in debug mode
 
+       -d -- Set STUPD_LOG_SEVERITY to -300
+
        -U -- Unix-domain socket file to listen on
 
        -u username or uid number
@@ -114,36 +126,41 @@
     """ % (me, fs)
 
     try:
-        opts, args = getopt.getopt(args, 'p:Dh:U:sS:u:P:')
+        opts, args = getopt.getopt(args, 'p:Dh:U:sS:u:P:d')
     except getopt.error, msg:
         print usage
         print msg
         sys.exit(1)
 
-    port=None
-    debug=0
-    host=''
-    unix=None
-    Z=1
-    UID='nobody'
+    port = None
+    debug = 0
+    host = ''
+    unix =None
+    Z = 1
+    UID = 'nobody'
     prof = None
+    detailed = 0
     for o, v in opts:
-        if o=='-p': port=string.atoi(v)
-        elif o=='-h': host=v
-        elif o=='-U': unix=v
-        elif o=='-u': UID=v
-        elif o=='-D': debug=1
-        elif o=='-s': Z=0
-        elif o=='-P': prof = v
+        if o=='-p':
+            port = int(v)
+        elif o=='-h':
+            host = v
+        elif o=='-U':
+            unix = v
+        elif o=='-u':
+            UID = v
+        elif o=='-D':
+            debug = 1
+        elif o=='-d':
+            detailed = 1
+        elif o=='-s':
+            Z = 0
+        elif o=='-P':
+            prof = v
 
     if prof:
         Z = 0
 
-    try:
-        from ZServer.medusa import asyncore
-        sys.modules['asyncore']=asyncore
-    except: pass
-
     if port is None and unix is None:
         print usage
         print 'No port specified.'
@@ -157,7 +174,10 @@
         fs=args[0]
 
     __builtins__.__debug__=debug
-    if debug: os.environ['Z_DEBUG_MODE']='1'
+    if debug:
+        os.environ['Z_DEBUG_MODE'] = '1'
+    if detailed:
+        os.environ['STUPID_LOG_SEVERITY'] = '-300'
 
     from zLOG import LOG, INFO, ERROR
 
@@ -199,54 +219,77 @@
             import zdaemon
             zdaemon.run(sys.argv, '')
 
-    storages={}
-    for o, v in opts:
-        if o=='-S':
-            n, m = string.split(v,'=')
-            if string.find(m,':'):
-                # we got an attribute name
-                m, a = string.split(m,':')
-            else:
-                # attribute name must be same as storage name
-                a=n
-            storages[n]=get_storage(m,a)
-
-    if not storages:
-        import ZODB.FileStorage
-        storages['1']=ZODB.FileStorage.FileStorage(fs)
-
-    # Try to set up a signal handler
     try:
-        import signal
 
-        signal.signal(signal.SIGTERM,
-                      lambda sig, frame, s=storages: shutdown(s)
-                      )
-        signal.signal(signal.SIGINT,
-                      lambda sig, frame, s=storages: shutdown(s, 0)
-                      )
-        signal.signal(signal.SIGHUP, rotate_logs_handler)
-
-    finally: pass
-
-    items=storages.items()
-    items.sort()
-    for kv in items:
-        LOG('ZEO Server', INFO, 'Serving %s:\t%s' % kv)
+        import ZEO.StorageServer, asyncore
+        
+        storages={}
+        for o, v in opts:
+            if o=='-S':
+                n, m = string.split(v,'=')
+                if string.find(m,':'):
+                    # we got an attribute name
+                    m, a = string.split(m,':')
+                else:
+                    # attribute name must be same as storage name
+                    a=n
+                storages[n]=get_storage(m,a)
+
+        if not storages:
+            import ZODB.FileStorage
+            storages['1']=ZODB.FileStorage.FileStorage(fs)
 
-    if not unix: unix=host, port
+        # Try to set up a signal handler
+        try:
+            import signal
+
+            try:
+                signal.signal(signal.SIFXFSZ, signal.SIG_IGN)
+            except AttributeError:
+                pass
+            signal.signal(signal.SIGTERM,
+                          lambda sig, frame, s=storages: shutdown(s))
+            signal.signal(signal.SIGINT,
+                          lambda sig, frame, s=storages: shutdown(s, 0))
+            try:
+                signal.signal(signal.SIGHUP, rotate_logs_handler)
+            except:
+                pass
+        except:
+            pass
+
+        items=storages.items()
+        items.sort()
+        for kv in items:
+            LOG('ZEO Server', INFO, 'Serving %s:\t%s' % kv)
+
+        if not unix: unix=host, port
 
-    if prof:
-        cmds = \
-        "StorageServer.StorageServer(unix, storages);" \
-        'open(zeo_pid,"w").write("%s %s" % (os.getppid(), os.getpid()));' \
-        "asyncore.loop()"
-        import profile
-        profile.run(cmds, prof)
-    else:
         StorageServer.StorageServer(unix, storages)
-        open(zeo_pid,'w').write("%s %s" % (os.getppid(), os.getpid()))
-        asyncore.loop()
+        
+        try:
+            ppid, pid = os.getppid(), os.getpid()
+        except:
+            pass # getpid not supported
+        else:
+            open(zeo_pid,'w').write("%s %s" % (ppid, pid))
+            
+    except:
+        # Log startup exception and tell zdaemon not to restart us.
+        info = sys.exc_info()
+        try:
+            import zLOG
+            zLOG.LOG("z2", zLOG.PANIC, "Startup exception",
+                     error=info)
+        except:
+            pass
+
+        import traceback
+        apply(traceback.print_exception, info)
+            
+        sys.exit(0)
+
+    asyncore.loop()
 
 def rotate_logs():
     import zLOG