server/pool.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 17 Feb 2011 16:09:06 +0100
changeset 7006 d0f635d3a6bb
parent 6427 c8a5ac2d1eaa
child 7398 26695dd703d8
permissions -rw-r--r--
backport stable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2765
diff changeset
    18
"""CubicWeb server connections pool : the repository has a limited number of
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2765
diff changeset
    19
connections pools, each of them dealing with a set of connections on each source
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2765
diff changeset
    20
used by the repository. A connections pools (`ConnectionsPool`) is an
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2765
diff changeset
    21
abstraction for a group of connection to each source.
5824
de9b7e88660e cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    22
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
import sys
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
    27
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
class ConnectionsPool(object):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
    """handle connections on a set of sources, at some point associated to a
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
    user session
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
    def __init__(self, sources):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
        # dictionnary of (source, connection), indexed by sources'uri
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
        self.source_cnxs = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
        for source in sources:
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
    37
            self.add_source(source)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        if not 'system' in self.source_cnxs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
            self.source_cnxs['system'] = self.source_cnxs[sources[0].uri]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
        self._cursors = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
2765
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    42
    def __getitem__(self, uri):
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    43
        """subscription notation provide access to sources'cursors"""
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    44
        try:
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    45
            cursor = self._cursors[uri]
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    46
        except KeyError:
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    47
            cursor = self.source_cnxs[uri][1].cursor()
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    48
            if cursor is not None:
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    49
                # None possible on sources without cursor support such as ldap
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    50
                self._cursors[uri] = cursor
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    51
        return cursor
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
    52
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
    53
    def add_source(self, source):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
    54
        assert not source.uri in self.source_cnxs
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
    55
        self.source_cnxs[source.uri] = (source, source.get_connection())
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
    56
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
    57
    def remove_source(self, source):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
    58
        source, cnx = self.source_cnxs.pop(source.uri)
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
    59
        cnx.close()
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
    60
        self._cursors.pop(source.uri, None)
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
    61
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
    def commit(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        """commit the current transaction for this user"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
        # FIXME: what happends if a commit fail
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        # would need a two phases commit or like, but I don't know how to do
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        # this using the db-api...
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
        for source, cnx in self.source_cnxs.values():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
            # let exception propagates
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
            cnx.commit()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
    70
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
    def rollback(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        """rollback the current transaction for this user"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
        for source, cnx in self.source_cnxs.values():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
            # catch exceptions, rollback other sources anyway
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
                cnx.rollback()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
            except:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
                source.critical('rollback error', exc_info=sys.exc_info())
5976
00b1b6b906cf [transaction] if error on rollbacking a source, replace the connection by a new one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5824
diff changeset
    79
                # error on rollback, the connection is much probably in a really
00b1b6b906cf [transaction] if error on rollbacking a source, replace the connection by a new one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5824
diff changeset
    80
                # bad state. Replace it by a new one.
00b1b6b906cf [transaction] if error on rollbacking a source, replace the connection by a new one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5824
diff changeset
    81
                self.reconnect(source)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
    def close(self, i_know_what_i_do=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        """close all connections in the pool"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
        if i_know_what_i_do is not True: # unexpected closing safety belt
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
            raise RuntimeError('pool shouldn\'t be closed')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
        for cu in self._cursors.values():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
                cu.close()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
            except:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        for _, cnx in self.source_cnxs.values():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
                cnx.close()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
            except:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
                continue
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
    97
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
    # internals ###############################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
2063
fe4278b50388 fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   100
    def pool_set(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        """pool is being set"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        self.check_connections()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
2063
fe4278b50388 fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   104
    def pool_reset(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        """pool is being reseted"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        for source, cnx in self.source_cnxs.values():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
            source.pool_reset(cnx)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
   108
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
    def sources(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
        """return the source objects handled by this pool"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
        # implementation details of flying insert requires the system source
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
        # first
2765
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   113
        yield self.source_cnxs['system'][0]
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4710
diff changeset
   114
        for uri, (source, cnx) in self.source_cnxs.items():
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
            if uri == 'system':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
            yield source
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        #return [source_cnx[0] for source_cnx in self.source_cnxs.values()]
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
   119
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
    def source(self, uid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        """return the source object with the given uri"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
        return self.source_cnxs[uid][0]
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
   123
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
    def connection(self, uid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
        """return the connection on the source object with the given uri"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
        return self.source_cnxs[uid][1]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
2765
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   128
    def reconnect(self, source=None):
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   129
        """reopen a connection for this source or all sources if none specified
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   130
        """
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   131
        if source is None:
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   132
            sources = self.sources()
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   133
        else:
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   134
            sources = (source,)
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   135
        for source in sources:
4710
4d9ad6a4f261 [pool] properly close existing connection before reconnection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   136
            try:
4d9ad6a4f261 [pool] properly close existing connection before reconnection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   137
                # properly close existing connection if any
4d9ad6a4f261 [pool] properly close existing connection before reconnection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   138
                self.source_cnxs[source.uri][1].close()
4d9ad6a4f261 [pool] properly close existing connection before reconnection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   139
            except:
4d9ad6a4f261 [pool] properly close existing connection before reconnection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   140
                pass
2765
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   141
            source.info('trying to reconnect')
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   142
            self.source_cnxs[source.uri] = (source, source.get_connection())
5e2525d7b1b1 reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
   143
            self._cursors.pop(source.uri, None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
    def check_connections(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        for source, cnx in self.source_cnxs.itervalues():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
            newcnx = source.check_connection(cnx)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
            if newcnx is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
                self.reset_connection(source, newcnx)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
    def reset_connection(self, source, cnx):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
        self.source_cnxs[source.uri] = (source, cnx)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
        self._cursors.pop(source.uri, None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5976
diff changeset
   156
from cubicweb.server.hook import Operation, LateOperation, SingleLastOperation
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2765
diff changeset
   157
from logilab.common.deprecation import class_moved, class_renamed
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2765
diff changeset
   158
Operation = class_moved(Operation)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2765
diff changeset
   159
PreCommitOperation = class_renamed('PreCommitOperation', Operation)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2765
diff changeset
   160
LateOperation = class_moved(LateOperation)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2765
diff changeset
   161
SingleLastOperation = class_moved(SingleLastOperation)