Package Products :: Package ZenUtils :: Module ZeoPoolBase
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenUtils.ZeoPoolBase

  1  ############################################################################## 
  2  #  
  3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved. 
  4  #  
  5  # This content is made available according to terms specified in 
  6  # License.zenoss under the directory where your Zenoss product is installed. 
  7  #  
  8  ############################################################################## 
  9   
 10   
 11  __doc__="""ZeoPoolBase 
 12   
 13  $Id: ZC.py,v 1.9 2004/02/16 17:19:31 edahl Exp $""" 
 14   
 15  __version__ = "$Revision: 1.9 $"[11:-2] 
 16   
 17  from threading import Lock 
 18   
 19  from ZEO import ClientStorage 
 20  from ZODB import DB 
 21  from ZPublisher.HTTPRequest import HTTPRequest 
 22  from ZPublisher.HTTPResponse import HTTPResponse 
 23  from ZPublisher.BaseRequest import RequestContainer 
 24   
 25  from ZenDaemon import ZenDaemon 
 26   
 27  from Products.ZenUtils.Utils import unused 
 28   
29 -class ZeoPoolBase(ZenDaemon):
30 """ 31 A multi-threaded daemon that maintains a pool of zeo connections 32 that it can hand out to its worker threads. 33 """ 34 35
36 - def __init__(self, noopts=0, app=None, keeproot=False):
37 ZenDaemon.__init__(self, noopts, keeproot) 38 unused(app) 39 self.opendb() 40 self.openconn = self.getPoolSize()
41 42
43 - def getConnection(self, path=None):
44 """Return a connection from the connection pool. If path is passed 45 return the object that the path points to. 46 """ 47 with self.poollock: 48 if not self.is_connected(): 49 self.opendb() 50 connection=self.db.open() 51 root=connection.root() 52 app=root['Application'] 53 self._getContext(app) 54 app._p_jar.sync() 55 if path: 56 return app.getObjByPath(path) 57 else: 58 return app 59 self.openconn -= 1
60 61
62 - def opendb(self):
63 addr = (self.options.host, self.options.port) 64 self._storage=ClientStorage.ClientStorage(addr) 65 self.db=DB(self._storage) 66 self.poollock = Lock()
67 68
69 - def closedb(self):
70 """Close all connections in both free an inuse pools. 71 """ 72 self.db.close() 73 self.db = None 74 self._storage = None
75 76
77 - def is_connected(self):
78 """Are we connected to zeo. 79 """ 80 return not self._storage or self._storage.is_connected()
81 82
83 - def getPoolSize(self):
84 """Return the target max pool size for this database. 85 """ 86 return self.db.getPoolSize()
87 88
89 - def available(self):
90 """Return the number of available connection in our pool. 91 """ 92 if self.db._pools: 93 pool = self.db._pools[''] # trunk version pool 94 return len(pool.available) 95 return 0
96 97
98 - def _getContext(self, app):
99 resp = HTTPResponse(stdout=None) 100 env = { 101 'SERVER_NAME':'localhost', 102 'SERVER_PORT':'8080', 103 'REQUEST_METHOD':'GET' 104 } 105 req = HTTPRequest(None, env, resp) 106 app.__of__(RequestContainer(REQUEST = req)) 107 return app
108 109
110 - def buildOptions(self):
111 """basic options setup sub classes can add more options here""" 112 ZenDaemon.buildOptions(self) 113 self.parser.add_option('--host', 114 dest="host",default="localhost", 115 help="hostname of zeo server") 116 self.parser.add_option('--port', 117 dest="port",type="int", default=8100, 118 help="port of zeo server")
119