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 or (at your
      8  # option) any later version as published by 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          with self.poollock:
     51              if not self.is_connected():
     52                  self.opendb()
     53              connection=self.db.open()
     54              root=connection.root()
     55              app=root['Application']
     56              self._getContext(app)
     57              app._p_jar.sync()
     58              if path:
     59                  return app.getObjByPath(path)
     60              else:
     61                  return app
     62              self.openconn -= 1
     63
     64
     65 -    def opendb(self):
     66          addr = (self.options.host, self.options.port)
     67          self._storage=ClientStorage.ClientStorage(addr)
     68          self.db=DB(self._storage)
     69          self.poollock = Lock()
     70
     71
     72 -    def closedb(self):
     73          """Close all connections in both free an inuse pools.
     74          """
     75          self.db.close()
     76          self.db = None
     77          self._storage = None
     78
     79
     80 -    def is_connected(self):
     81          """Are we connected to zeo.
     82          """
     83          return not self._storage or self._storage.is_connected()
     84
     85
     86 -    def getPoolSize(self):
     87          """Return the target max pool size for this database.
     88          """
     89          return self.db.getPoolSize()
     90
     91
     92 -    def available(self):
     93          """Return the number of available connection in our pool.
     94          """
     95          if self.db._pools:
     96              pool = self.db._pools[''] # trunk version pool
     97              return len(pool.available)
     98          return 0
     99
    100
    101 -    def _getContext(self, app):
    102          resp = HTTPResponse(stdout=None)
    103          env = {
    104              'SERVER_NAME':'localhost',
    105              'SERVER_PORT':'8080',
    106              'REQUEST_METHOD':'GET'
    107              }
    108          req = HTTPRequest(None, env, resp)
    109          app.__of__(RequestContainer(REQUEST = req))
    110          return app
    111
    112
    113 -    def buildOptions(self):
    114          """basic options setup sub classes can add more options here"""
    115          ZenDaemon.buildOptions(self)
    116          self.parser.add_option('--host',
    117                      dest="host",default="localhost",
    118                      help="hostname of zeo server")
    119          self.parser.add_option('--port',
    120                      dest="port",type="int", default=8100,
    121                      help="port of zeo server")
    122