[Zodb-checkins] CVS: StandaloneZODB/Tools - zeopack.py:1.4

Jeremy Hylton jeremy@zope.com
Wed, 30 Jan 2002 16:42:18 -0500


Update of /cvs-repository/StandaloneZODB/Tools
In directory cvs.zope.org:/tmp/cvs-serv13820

Modified Files:
	zeopack.py 
Log Message:
Don't block forever is server is not up.

Refactor main() to report all errors and exit with non-zero status.




=== StandaloneZODB/Tools/zeopack.py 1.3 => 1.4 ===
 """
 
+import getopt
+import socket
+import sys
+import time
+
 from ZEO.ClientStorage import ClientStorage
 
-def main(addr, storage, days):
+WAIT = 10 # wait no more than 10 seconds for client to connect
+
+def connect(storage):
+    # The connect-on-startup logic that ZEO provides isn't too useful
+    # for this script.  We'd like to client to attempt to startup, but
+    # fail if it can't get through to the server after a reasonable
+    # amount of time.  There's no external support for this, so we'll
+    # expose the ZEO 1.0 internals.  (consenting adults only)
+    t0 = time.time()
+    while t0 + WAIT > time.time():
+        storage._call.connect()
+        if storage._connected:
+            return
+    raise RuntimeError, "Unable to connect to ZEO server"
+
+def pack(addr, storage, days):
     cs = ClientStorage(addr, storage=storage, wait_for_server_on_startup=1)
     # _startup() is an artifact of the way ZEO 1.0 works.  The
     # ClientStorage doesn't get fully initialized until registerDB()
     # is called.  The only thing we care about, though, is that
     # registerDB() calls _startup().
-    cs._startup()
+    connect(cs)
     cs.pack(wait=1, days=days)
 
 def usage(exit=1):
@@ -34,11 +54,7 @@
     print " ".join(sys.argv)
     sys.exit(exit)
 
-if __name__ == "__main__":
-    import getopt
-    import socket
-    import sys
-
+def main():
     host = None
     port = None
     unix = None
@@ -69,5 +85,12 @@
         if port is None:
             usage()
         addr = host, port
-    
-    main(addr, storage, days)
+        
+    pack(addr, storage, days)
+
+if __name__ == "__main__":
+    try:
+        main()
+    except Exception, err:
+        print err
+        sys.exit(1)