autocat3/ConnectionPool.py

108 lines
3.0 KiB
Python
Raw Permalink Normal View History

2019-03-28 13:45:03 +00:00
#!/usr/bin/env python
# -*- mode: python; indent-tabs-mode: nil; -*- coding: utf-8 -*-
"""
ConnectionPool.py
Copyright 2010 by Marcello Perathoner
Distributable under the GNU General Public License Version 3 or newer.
"""
from __future__ import unicode_literals
import logging
import psycopg2
2021-04-14 19:21:40 +00:00
2019-03-28 13:45:03 +00:00
import sqlalchemy.pool as pool
2021-04-14 21:57:51 +00:00
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
2019-03-28 13:45:03 +00:00
import cherrypy
from cherrypy.process import plugins
2021-04-14 21:57:51 +00:00
DUMMY_SQL_URL = "postgresql://127.0.0.1:5432/gutenberg"
2019-03-28 13:45:03 +00:00
2021-04-14 19:21:40 +00:00
class ConnectionCreator():
2019-03-28 13:45:03 +00:00
""" Creates connections for the connection pool. """
2021-04-14 19:21:40 +00:00
def __init__(self, params):
2019-03-28 13:45:03 +00:00
self.params = params
2021-04-14 19:21:40 +00:00
def __call__(self):
cherrypy.log(
2019-03-28 13:45:03 +00:00
"Connecting to database '%(database)s' on '%(host)s:%(port)d' as user '%(user)s'."
2021-04-14 19:21:40 +00:00
% self.params, context='POSTGRES', severity=logging.INFO)
conn = psycopg2.connect(**self.params)
conn.cursor().execute('SET statement_timeout = 5000')
2019-03-28 13:45:03 +00:00
return conn
2021-04-14 19:21:40 +00:00
class ConnectionPool(plugins.SimplePlugin):
2019-03-28 13:45:03 +00:00
"""A WSPBus plugin that controls a SQLAlchemy engine/connection pool."""
2021-04-14 19:21:40 +00:00
def __init__(self, bus, params=None):
plugins.SimplePlugin.__init__(self, bus)
2019-03-28 13:45:03 +00:00
self.params = params
self.name = 'sqlalchemy'
self.pool = None
2021-04-14 19:21:40 +00:00
def _start(self):
2019-03-28 13:45:03 +00:00
""" Init the connection pool. """
2021-04-14 19:21:40 +00:00
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))
2021-04-14 21:57:51 +00:00
my_pool = pool.QueuePool(ConnectionCreator(self.params),
pool_size=pool_size,
max_overflow=max_overflow,
timeout=timeout,
recycle=recycle)
engine = create_engine(DUMMY_SQL_URL, echo=False, pool=my_pool)
Session = sessionmaker(bind=engine)
2021-04-23 23:46:35 +00:00
return engine, Session
2021-04-14 21:57:51 +00:00
def connect(self):
2019-03-28 13:45:03 +00:00
""" Return a connection. """
2021-04-23 23:46:35 +00:00
return self.pool.raw_connection()
2019-03-28 13:45:03 +00:00
2021-04-14 19:21:40 +00:00
def start(self):
2019-03-28 13:45:03 +00:00
""" Called on engine start. """
if self.pool is None:
2021-04-14 19:21:40 +00:00
self.bus.log("Creating the SQL connectors ...")
self.pool, self.Session = self._start()
2019-03-28 13:45:03 +00:00
else:
2021-04-14 19:21:40 +00:00
self.bus.log("SQL connectors already exists.")
2019-03-28 13:45:03 +00:00
2021-04-14 19:21:40 +00:00
def stop(self):
2019-03-28 13:45:03 +00:00
""" Called on engine stop. """
if self.pool is not None:
2021-04-14 19:21:40 +00:00
self.bus.log("Disposing the SQL connection pool.")
2021-04-14 21:57:51 +00:00
self.Session = None
2021-04-14 19:21:40 +00:00
self.pool.dispose()
2019-03-28 13:45:03 +00:00
self.pool = None
2021-04-14 19:21:40 +00:00
def graceful(self):
2019-03-28 13:45:03 +00:00
""" Called on engine restart. """
if self.pool is not None:
2021-04-14 19:21:40 +00:00
self.bus.log("Restarting the SQL connection pool ...")
self.pool.dispose()
2021-04-23 23:46:35 +00:00
self.pool, self.Session = self._start()
2019-03-28 13:45:03 +00:00
2021-04-14 19:21:40 +00:00
cherrypy.process.plugins.ConnectionPool = ConnectionPool