예제 #1
0
파일: models.py 프로젝트: Drunkar/redash
 def __init__(self):
     self.database_config = dict(settings.DATABASE_CONFIG)
     self.database_config['register_hstore'] = False
     self.database_name = self.database_config.pop('name')
     self.database = MeteredPostgresqlExtDatabase(self.database_name, **self.database_config)
     self.app = None
     self.pid = os.getpid()
예제 #2
0
파일: models.py 프로젝트: Drunkar/redash
class Database(object):
    def __init__(self):
        self.database_config = dict(settings.DATABASE_CONFIG)
        self.database_config['register_hstore'] = False
        self.database_name = self.database_config.pop('name')
        self.database = MeteredPostgresqlExtDatabase(self.database_name, **self.database_config)
        self.app = None
        self.pid = os.getpid()

    def init_app(self, app):
        self.app = app
        self.register_handlers()

    def connect_db(self):
        self._check_pid()
        self.database.reset_metrics()
        self.database.connect()

    def close_db(self, exc):
        self._check_pid()
        if not self.database.is_closed():
            self.database.close()

    def _check_pid(self):
        current_pid = os.getpid()
        if self.pid != current_pid:
            logging.info("New pid detected (%d!=%d); resetting database lock.", self.pid, current_pid)
            self.pid = os.getpid()
            self.database._conn_lock = threading.Lock()

    def register_handlers(self):
        self.app.before_request(self.connect_db)
        self.app.teardown_request(self.close_db)