server/sources/pyrorql.py
changeset 9445 65d93a4fd11c
parent 9444 6012cf57f48e
child 9446 18a186b02970
equal deleted inserted replaced
9444:6012cf57f48e 9445:65d93a4fd11c
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """Source to query another RQL repository using pyro"""
       
    19 
       
    20 __docformat__ = "restructuredtext en"
       
    21 _ = unicode
       
    22 
       
    23 # module is lazily imported
       
    24 import warnings
       
    25 warnings.warn('Imminent drop of pyrorql source. Switch to datafeed now!',
       
    26               DeprecationWarning)
       
    27 
       
    28 import threading
       
    29 from Pyro.errors import PyroError, ConnectionClosedError
       
    30 
       
    31 from cubicweb import ConnectionError
       
    32 from cubicweb.server.sources import ConnectionWrapper
       
    33 
       
    34 from cubicweb.server.sources.remoterql import RemoteSource
       
    35 
       
    36 class PyroRQLSource(RemoteSource):
       
    37     """External repository source, using Pyro connection"""
       
    38 
       
    39     def get_connection(self):
       
    40         try:
       
    41             return self._get_connection()
       
    42         except (ConnectionError, PyroError) as ex:
       
    43             self.critical("can't get connection to source %s: %s", self.uri, ex)
       
    44             return ConnectionWrapper()
       
    45 
       
    46     def check_connection(self, cnx):
       
    47         """check connection validity, return None if the connection is still valid
       
    48         else a new connection
       
    49         """
       
    50         # we have to transfer manually thread ownership. This can be done safely
       
    51         # since the connections set holding the connection is affected to one
       
    52         # session/thread and can't be called simultaneously
       
    53         try:
       
    54             cnx._repo._transferThread(threading.currentThread())
       
    55         except AttributeError:
       
    56             # inmemory connection
       
    57             pass
       
    58         try:
       
    59             return super(PyroRQLSource, self).check_connection(cnx)
       
    60         except ConnectionClosedError:
       
    61             # try to reconnect
       
    62             return self.get_connection()
       
    63