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

Source Code for Module ZenUtils.ZeoPoolBase

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