hooks/zmq.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # -*- coding: utf-8 -*-
       
     2 # copyright 2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     4 #
       
     5 # This file is part of CubicWeb.
       
     6 #
       
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     8 # terms of the GNU Lesser General Public License as published by the Free
       
     9 # Software Foundation, either version 2.1 of the License, or (at your option)
       
    10 # any later version.
       
    11 #
       
    12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    15 # details.
       
    16 #
       
    17 # You should have received a copy of the GNU Lesser General Public License along
       
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    19 
       
    20 from cubicweb.server import hook
       
    21 
       
    22 class ZMQStopHook(hook.Hook):
       
    23     __regid__ = 'zmqstop'
       
    24     events = ('server_shutdown',)
       
    25 
       
    26     def __call__(self):
       
    27         self.repo.app_instances_bus.stop()
       
    28 
       
    29 class ZMQStartHook(hook.Hook):
       
    30     __regid__ = 'zmqstart'
       
    31     events = ('server_startup',)
       
    32     order = -1
       
    33 
       
    34     def __call__(self):
       
    35         config = self.repo.config
       
    36         address_pub = config.get('zmq-address-pub')
       
    37         address_sub = config.get('zmq-address-sub')
       
    38         if not address_pub and not address_sub:
       
    39             return
       
    40         from cubicweb.server import cwzmq
       
    41         self.repo.app_instances_bus = cwzmq.ZMQComm()
       
    42         if address_pub:
       
    43             self.repo.app_instances_bus.add_publisher(address_pub)
       
    44         def clear_cache_callback(msg):
       
    45             self.debug('clear_caches: %s', ' '.join(msg))
       
    46             self.repo.clear_caches(msg[1:])
       
    47         self.repo.app_instances_bus.add_subscription('delete', clear_cache_callback)
       
    48         for address in address_sub:
       
    49             self.repo.app_instances_bus.add_subscriber(address)
       
    50         self.repo.app_instances_bus.start()