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

# Source Code for Module Products.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  from Products.ZenUtils.Utils import unused
     31
     32 -class ZeoPoolBase(ZenDaemon):
     33      """
     34      A multi-threaded daemon that maintains a pool of zeo connections
     35      that it can hand out to its worker threads.
     36      """
     37
     38
     39 -    def __init__(self, noopts=0, app=None, keeproot=False):
     40          ZenDaemon.__init__(self, noopts, keeproot)
     41          unused(app)
     42          self.opendb()
     43          self.openconn = self.getPoolSize()
     44
     45
     46 -    def getConnection(self, path=None):
     47          """Return a connection from the connection pool. If path is passed
     48          return the object that the path points to.
     49          """
     50          try:
     51              self.poollock.acquire()
     52              if not self.is_connected():
     53                  self.opendb()
     54              connection=self.db.open()
     55              root=connection.root()
     56              app=root['Application']
     57              self._getContext(app)
     58              app._p_jar.sync()
     59              if path:
     60                  return app.getObjByPath(path)
     61              else:
     62                  return app
     63              self.openconn -= 1
     64          finally:
     65              self.poollock.release()
     66
     67
     68 -    def opendb(self):
     69          addr = (self.options.host, self.options.port)
     70          self._storage=ClientStorage.ClientStorage(addr)
     71          self.db=DB(self._storage)
     72          self.poollock = Lock()
     73
     74
     75 -    def closedb(self):
     76          """Close all connections in both free an inuse pools.
     77          """
     78          self.db.close()
     79          self.db = None
     80          self._storage = None
     81
     82
     83 -    def is_connected(self):
     84          """Are we connected to zeo.
     85          """
     86          return not self._storage or self._storage.is_connected()
     87
     88
     89 -    def getPoolSize(self):
     90          """Return the target max pool size for this database.
     91          """
     92          return self.db.getPoolSize()
     93
     94
     95 -    def available(self):
     96          """Return the number of available connection in our pool.
     97          """
     98          if self.db._pools:
     99              pool = self.db._pools[''] # trunk version pool
    100              return len(pool.available)
    101          return 0
    102
    103
    104 -    def _getContext(self, app):
    105          resp = HTTPResponse(stdout=None)
    106          env = {
    107              'SERVER_NAME':'localhost',
    108              'SERVER_PORT':'8080',
    109              'REQUEST_METHOD':'GET'
    110              }
    111          req = HTTPRequest(None, env, resp)
    112          app.__of__(RequestContainer(REQUEST = req))
    113          return app
    114
    115
    116 -    def buildOptions(self):
    117          """basic options setup sub classes can add more options here"""
    118          ZenDaemon.buildOptions(self)
    119          self.parser.add_option('--host',
    120                      dest="host",default="localhost",
    121                      help="hostname of zeo server")
    122          self.parser.add_option('--port',
    123                      dest="port",type="int", default=8100,
    124                      help="port of zeo server")
    125