带有MultiService的Twisted tap插件在启动时需要一些回调

时间:2014-09-30 16:00:06

标签: python twisted

使用以下源代码示例, makeService()启动两项服务:

  1. AMQP Broker :启动 TCPClient 以连接到AMQP Broker
  2. HTTPApi :启动 twisted.web.resource.Resource TCPServer
  3. 启动时,第二个(HTTPApi)需要与AMQP Broker建立就绪/可用连接,否则,HTTPApi将无法启动。

    我正在寻找一种干净的方法,当AMQP Broker准备就绪时如何回调HTTPApi startHTTPApiService(),有关信息,我已经在连接就绪时已经延迟触发: self .components [' amqp-broker-factory']。getChannelReadyDeferred(),但如何在 makeService()中使用它?

    源代码:

    from zope.interface import implements
    from twisted.python import usage
    from twisted.plugin import IPlugin
    from twisted.application import internet, service
    from twisted.web import server
    from nimsaj.queues.factory import AmqpFactory
    from nimsaj.protocols.http.server import HTTPApi
    
    class nimsajServiceMaker:
    
        implements(service.IServiceMaker, IPlugin)
    
        tapname = "nimsaj"
        description = "Network IMS-AJ"
        options = usage.Options
        top_service = None
        components = {}
    
        def startAMQPBrokerService(self):
            "Start AMQP Broker"
    
            self.components['amqp-broker-factory'] = AmqpFactory()
    
            # Add service
            AMQPBrokerService = internet.TCPClient('127.0.0.1', 
                                                   5672, 
                                                   self.components['amqp-broker-factory'])
            AMQPBrokerService.setServiceParent(self.top_service)
    
        def startHTTPApiService(self):
            "Start HTTP Api"
    
            httpApi_f = HTTPApi(self.components['amqp-broker-factory'])
    
            httpApi = internet.TCPServer(80, 
                                         server.Site(httpApi_f), 
                                         interface = '0.0.0.0'
                                         )
            httpApi.setServiceParent(self.top_service)
    
        def makeService(self, options):
            self.top_service = service.MultiService()
            self.options = options
    
            ########################################################
            # Start AMQP Broker
            self.startAMQPBrokerService()
    
            ########################################################
            # Start HTTP Api
            self.startHTTPApiService()
    
            return self.top_service
    
    service_maker = nimsajServiceMaker()
    

1 个答案:

答案 0 :(得分:1)

我是这样做的,我不知道你的情况是否足够。在我的情况下,我只是确保在数据库准备好之前未在应用程序上设置数据存储区服务(因此不回答请求)。如果有更好的方法来处理服务依赖关系,我也想知道。

def setServiceParentWhenDepReady(constraint, service, parent):
    if constraint.running:
        service.setServiceParent(parent)
    else:
        reactor.callLater(0.1, setServiceParentWhenDepReady,
                          *(constraint, service, parent))


def application(config):
    app = Application('appName')

    srv_resolver = SrvResolver()
    app.setComponent(ISrvResolver, srv_resolver)

    postgresservice = PostgresService(app, config)
    postgresservice.setServiceParent(app)
    datastoreservice = RpcService(app, config)
    setServiceParentWhenDepReady(postgresservice, datastoreservice, app)

    return app
相关问题