Esempio n. 1
0
def register_server(method, period):
    """ Registers the given method with a clockserver.

    Note that due to it's implementation there's no guarantee that the method
    will be called on time every time. The clockserver checks if a call is due
    every time a request comes in, or every 30 seconds when the asyncore.pollss
    method reaches it's timeout (see Lifetime/__init__.py and
    Python Stdlib/asyncore.py).

    """

    assert method not in _clockservers, "%s is already being used" % method

    with locks['_clockservers']:
        _clockservers[method] = ClockServer(
            method, period, host='localhost', logger=ClockLogger(method)
        )

    return _clockservers[method]
Esempio n. 2
0
    def create(self):
        from zope.component import provideUtility

        from collective.zamqp.interfaces import IBrokerConnection
        from collective.zamqp.connection import BrokerConnection

        connection = BrokerConnection(connection_id=self.connection_id,
                                      hostname=self.hostname,
                                      port=self.port,
                                      virtual_host=self.virtual_host,
                                      username=self.username,
                                      password=self.password,
                                      heartbeat=self.heartbeat,
                                      prefetch_count=self.prefetch_count,
                                      tx_select=self.tx_select)

        # set expected ZServer-properties to support debugtoolbar
        connection.server_name = "ZAMQP Broker Connection"
        connection.ip = None

        provideUtility(connection, IBrokerConnection, name=self.connection_id)

        if self.keepalive:
            from collective.zamqp.utils import logger
            logger.default(u"Setting up keepalive (%s s) for connection '%s'",
                           self.keepalive, self.connection_id)

            # register a ping producer, a ping consumer, a ping view and a ping
            # clock-server to keep the connection alive

            from collective.zamqp.interfaces import IProducer, IConsumer
            from collective.zamqp import keepalive

            name = "%s.ping" % self.connection_id

            # the producer
            producer = keepalive.PingProducer(self.connection_id)
            provideUtility(producer, IProducer, name=name)

            # the consumer
            consumer = keepalive.PingConsumer(self.connection_id)
            provideUtility(consumer, IConsumer, name=name)

            from zope.interface import Interface
            from zope.component import provideAdapter

            from OFS.interfaces import IApplication

            # the view
            ping = lambda context, request: lambda: keepalive.ping(name)
            provideAdapter(ping,
                           adapts=(IApplication, Interface),
                           provides=Interface,
                           name=name)

            # the clock-server
            from ZServer.AccessLogger import access_logger
            from ZServer.ClockServer import ClockServer
            clock = ClockServer(method="/%s" % name,
                                period=self.keepalive,
                                host="localhost",
                                logger=access_logger)

            # just in case, store the created utilities, view and server
            connection._keepalive = {
                "producer": producer,
                "consumer": consumer,
                "view": ping,
                "clock": clock
            }

        if self.producer:
            # generate default producer with the name of the connection
            from collective.zamqp.interfaces import IProducer
            from collective.zamqp.producer import Producer

            producer = Producer(self.connection_id,
                                exchange="",
                                routing_key="",
                                durable=False,
                                auto_declare=False)
            provideUtility(producer, IProducer, name=self.connection_id)

        # set expected ZServer-properties to support debugtoolbar
        connection.server_name = "ZAMQP Broker Connection"
        connection.ip = None

        return connection
Esempio n. 3
0
 def create(self):
     from ZServer.ClockServer import ClockServer
     from ZServer.AccessLogger import access_logger
     return ClockServer(self.method, self.period, self.user,
                        self.password, self.hostheader, access_logger)