master
eric 2021-04-14 15:21:40 -04:00
parent 16dfb27724
commit df6142c005
1 changed files with 31 additions and 31 deletions

View File

@ -15,45 +15,40 @@ from __future__ import unicode_literals
import logging
import psycopg2
import sqlalchemy.pool as pool
import cherrypy
from cherrypy.process import plugins
class ConnectionCreator ():
class ConnectionCreator():
""" Creates connections for the connection pool. """
def __init__ (self, params):
def __init__(self, params):
self.params = params
def __call__ (self):
cherrypy.log (
def __call__(self):
cherrypy.log(
"Connecting to database '%(database)s' on '%(host)s:%(port)d' as user '%(user)s'."
% self.params, context = 'POSTGRES', severity = logging.INFO)
conn = psycopg2.connect (**self.params)
conn.cursor ().execute ('SET statement_timeout = 5000')
% self.params, context='POSTGRES', severity=logging.INFO)
conn = psycopg2.connect(**self.params)
conn.cursor().execute('SET statement_timeout = 5000')
return conn
class ConnectionPool (plugins.SimplePlugin):
class ConnectionPool(plugins.SimplePlugin):
"""A WSPBus plugin that controls a SQLAlchemy engine/connection pool."""
def __init__ (self, bus, params = None):
plugins.SimplePlugin.__init__ (self, bus)
def __init__(self, bus, params=None):
plugins.SimplePlugin.__init__(self, bus)
self.params = params
self.name = 'sqlalchemy'
self.pool = None
def _start (self):
def _start(self):
""" Init the connection pool. """
pool_size = cherrypy.config.get ('sqlalchemy.pool_size', 5)
max_overflow = cherrypy.config.get ('sqlalchemy.max_overflow', 10)
timeout = cherrypy.config.get ('sqlalchemy.timeout', 30)
recycle = cherrypy.config.get ('sqlalchemy.recycle', 3600)
self.bus.log ("... pool_size = %d, max_overflow = %d" % (pool_size, max_overflow))
return pool.QueuePool (ConnectionCreator (self.params),
pool_size = pool_size,
@ -63,38 +58,43 @@ class ConnectionPool (plugins.SimplePlugin):
def connect (self):
pool_size = cherrypy.config.get('sqlalchemy.pool_size', 5)
max_overflow = cherrypy.config.get('sqlalchemy.max_overflow', 10)
timeout = cherrypy.config.get('sqlalchemy.timeout', 30)
recycle = cherrypy.config.get('sqlalchemy.recycle', 3600)
self.bus.log("... pool_size = %d, max_overflow = %d" % (pool_size, max_overflow))
""" Return a connection. """
return self.pool.connect ()
return self.pool.connect()
def start (self):
def start(self):
""" Called on engine start. """
if self.pool is None:
self.bus.log ("Creating the SQL connection pool ...")
self.pool = self._start ()
self.bus.log("Creating the SQL connectors ...")
self.pool, self.Session = self._start()
else:
self.bus.log ("An SQL connection pool already exists.")
# start.priority = 80
self.bus.log("SQL connectors already exists.")
def stop (self):
def stop(self):
""" Called on engine stop. """
if self.pool is not None:
self.bus.log ("Disposing the SQL connection pool.")
self.pool.dispose ()
self.bus.log("Disposing the SQL connection pool.")
self.pool.dispose()
self.pool = None
def graceful (self):
def graceful(self):
""" Called on engine restart. """
if self.pool is not None:
self.bus.log ("Restarting the SQL connection pool ...")
self.pool.dispose ()
self.pool = self._start ()
self.bus.log("Restarting the SQL connection pool ...")
self.pool.dispose()
self.pool = self._start()
cherrypy.process.plugins.ConnectionPool = ConnectionPool
cherrypy.process.plugins.ConnectionPool = ConnectionPool