allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533) stable
authorAlexandre Fayolle <alexandre.fayolle@logilab.fr>
Tue, 29 Mar 2011 08:47:09 +0200
branchstable
changeset 7134 01544b6d98fa
parent 7133 110f38b322ef
child 7135 2164f111efa4
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533) This is achieved by configuring the pyro-ns-host to __NO_PYRONS__ (which is an illegal value for a host name) The Pyro URI for the server can be retrieved by reading the pyro_uri attribute of the Repository
cwconfig.py
dbapi.py
server/repository.py
--- a/cwconfig.py	Tue Mar 29 09:02:49 2011 +0200
+++ b/cwconfig.py	Tue Mar 29 08:47:09 2011 +0200
@@ -325,7 +325,8 @@
          {'type' : 'string',
           'default': '',
           'help': 'Pyro name server\'s host. If not set, will be detected by a \
-broadcast query. It may contains port information using <host>:<port> notation.',
+broadcast query. It may contains port information using <host>:<port> notation. \
+Use "NO_PYRONS" to create a Pyro server but not register to a pyro nameserver',
           'group': 'pyro', 'level': 1,
           }),
         ('pyro-ns-group',
--- a/dbapi.py	Tue Mar 29 09:02:49 2011 +0200
+++ b/dbapi.py	Tue Mar 29 08:47:09 2011 +0200
@@ -102,11 +102,14 @@
         return Repository(config, vreg=vreg)
     else: # method == 'pyro'
         # resolve the Pyro object
-        from logilab.common.pyro_ext import ns_get_proxy
+        from logilab.common.pyro_ext import ns_get_proxy, get_proxy
         pyroid = database or config['pyro-instance-id'] or config.appid
         try:
-            return ns_get_proxy(pyroid, defaultnsgroup=config['pyro-ns-group'],
-                                nshost=config['pyro-ns-host'])
+            if config['pyro-ns-host'] == 'NO_PYRONS':
+                return get_proxy(pyroid)
+            else:
+                return ns_get_proxy(pyroid, defaultnsgroup=config['pyro-ns-group'],
+                                    nshost=config['pyro-ns-host'])
         except Exception, ex:
             raise ConnectionError(str(ex))
 
--- a/server/repository.py	Tue Mar 29 09:02:49 2011 +0200
+++ b/server/repository.py	Tue Mar 29 08:47:09 2011 +0200
@@ -59,6 +59,7 @@
      security_enabled
 from cubicweb.server.ssplanner import EditedEntity
 
+
 def prefill_entity_caches(entity, relations):
     session = entity._cw
     # prefill entity relation caches
@@ -134,6 +135,7 @@
             vreg = cwvreg.CubicWebVRegistry(config)
         self.vreg = vreg
         self.pyro_registered = False
+        self.pyro_uri = None
         self.info('starting repository from %s', self.config.apphome)
         # dictionary of opened sessions
         self._sessions = {}
@@ -415,7 +417,9 @@
                 self.exception('error while closing %s' % pool)
                 continue
         if self.pyro_registered:
-            pyro_unregister(self.config)
+            if self._use_pyrons():
+                pyro_unregister(self.config)
+            self.pyro_uri = None
         hits, misses = self.querier.cache_hit, self.querier.cache_miss
         try:
             self.info('rql st cache hit/miss: %s/%s (%s%% hits)', hits, misses,
@@ -1419,20 +1423,32 @@
         config['pyro-instance-id'] = appid
         return appid
 
+    def _use_pyrons(self):
+        """return True if the pyro-ns-host is set to something else
+        than NO_PYRONS, meaning we want to go through a pyro
+        nameserver"""
+        return self.config['pyro-ns-host'] != 'NO_PYRONS'
+
     def pyro_register(self, host=''):
         """register the repository as a pyro object"""
         from logilab.common import pyro_ext as pyro
         daemon = pyro.register_object(self, self.pyro_appid,
                                       daemonhost=self.config['pyro-host'],
-                                      nshost=self.config['pyro-ns-host'])
+                                      nshost=self.config['pyro-ns-host'],
+                                      use_pyrons=self._use_pyrons())
         self.info('repository registered as a pyro object %s', self.pyro_appid)
+        self.pyro_uri =  pyro.get_object_uri(self.pyro_appid)
+        self.info('pyro uri is: %s', self.pyro_uri)
         self.pyro_registered = True
         # register a looping task to regularly ensure we're still registered
         # into the pyro name server
-        self.looping_task(60*10, self._ensure_pyro_ns)
+        if self._use_pyrons():
+            self.looping_task(60*10, self._ensure_pyro_ns)
         return daemon
 
     def _ensure_pyro_ns(self):
+        if not self._use_pyrons():
+            return
         from logilab.common import pyro_ext as pyro
         pyro.ns_reregister(self.pyro_appid, nshost=self.config['pyro-ns-host'])
         self.info('repository re-registered as a pyro object %s',