def test_contextual_lifecycle(): events = set() class Container(object): def __init__(self, service_cls, worker_ctx_cls, config): self.service_name = service_cls.__name__ self.service_cls = service_cls self.worker_ctx_cls = worker_ctx_cls def start(self): events.add(('start', self.service_cls.name, self.service_cls)) def stop(self): events.add(('stop', self.service_cls.name, self.service_cls)) def kill(self, exc=None): events.add(('kill', self.service_cls.name, self.service_cls)) def wait(self): events.add(('wait', self.service_cls.name, self.service_cls)) config = {} with run_services(config, TestService1, TestService2, container_cls=Container): # Ensure the services were started assert events == { ('start', 'foobar_1', TestService1), ('start', 'foobar_2', TestService2), } # ...and that they were stopped assert events == { ('start', 'foobar_1', TestService1), ('start', 'foobar_2', TestService2), ('stop', 'foobar_1', TestService1), ('stop', 'foobar_2', TestService2), } events = set() with run_services(config, TestService1, TestService2, container_cls=Container, kill_on_exit=True): # Ensure the services were started assert events == { ('start', 'foobar_1', TestService1), ('start', 'foobar_2', TestService2), } # ...and that they were killed assert events == { ('kill', 'foobar_1', TestService1), ('kill', 'foobar_2', TestService2), ('start', 'foobar_1', TestService1), ('start', 'foobar_2', TestService2), }
def test_contextual_lifecycle(fake_module): events = set() class Container(object): def __init__(self, service_cls, config): self.service_name = service_cls.__name__ self.service_cls = service_cls def start(self): events.add(('start', self.service_cls.name, self.service_cls)) def stop(self): events.add(('stop', self.service_cls.name, self.service_cls)) def kill(self, exc=None): events.add(('kill', self.service_cls.name, self.service_cls)) fake_module.ServiceContainer = Container config = {'SERVICE_CONTAINER_CLS': 'fake_module.ServiceContainer'} with run_services(config, TestService1, TestService2): # Ensure the services were started assert events == { ('start', 'foobar_1', TestService1), ('start', 'foobar_2', TestService2), } # ...and that they were stopped assert events == { ('start', 'foobar_1', TestService1), ('start', 'foobar_2', TestService2), ('stop', 'foobar_1', TestService1), ('stop', 'foobar_2', TestService2), } events = set() with run_services(config, TestService1, TestService2, kill_on_exit=True): # Ensure the services were started assert events == { ('start', 'foobar_1', TestService1), ('start', 'foobar_2', TestService2), } # ...and that they were killed assert events == { ('kill', 'foobar_1', TestService1), ('kill', 'foobar_2', TestService2), ('start', 'foobar_1', TestService1), ('start', 'foobar_2', TestService2), }
def main(self): if not self.stateless: self.state.update_environment() self.state.update(started=epoch(), stopped=None, ip_addr=self.state.node_address) self.logger.info('This instance is %s on %s', self.state.node_id, self.state.cluster_id) stopped = False while stopped is False: store = ServiceStore(from_modules=self.modules) self.logger.info('Starting nameko containers with %s', store.services) with run_services(self.config, *store.services, kill_on_exit=True): while True: try: if not self.stateless: self.state.confirm_master() time.sleep(1 if self.auto_reload else 5) if self.auto_reload and store.should_reload(): self.logger.info('Server reload!') if not self.stateless: self.state.update(stopped=epoch()) break if not self.stateless: self.state.update(last_seen=epoch()) except KeyboardInterrupt: self.logger.warning( 'Stopping nameko containers (someone hit ^C)') if not self.stateless: self.state.update(stopped=epoch()) stopped = True break
def test_worker_ctx_cls_warning(self, warnings): class WorkerContextX(WorkerContext): pass config = {} with run_services(config, worker_ctx_cls=WorkerContext): pass # TODO: replace with pytest.warns when eventlet >= 0.19.0 is released assert warnings.warn.call_args_list == [ # from contextual runner # (no calls to ServiceRunner.add_service in this test) call(ANY, DeprecationWarning), ]
def test_container_cls_warning(self, warnings): class ServiceContainerX(ServiceContainer): pass config = {} with run_services(config, container_cls=ServiceContainerX): pass # TODO: replace with pytest.warns when eventlet >= 0.19.0 is released assert warnings.warn.call_args_list == [ # from contextual runner call(ANY, DeprecationWarning), # from underlying ServiceRunner constructor call(ANY, DeprecationWarning), ]
def get_dependency(self, worker_ctx): return self.database class MicroA(object): name = 'microA' buf = BufferA() @rpc def put(self, key, value): self.buf[key] = value print key print value @rpc def get(self, key): return 0 @rpc def get_t0(self): # print "t0: ", self.buf["time"] return str(self.buf["time"]) if __name__ == '__main__': config = {'AMQP_URI': "amqp://*****:*****@localhost"} # config = {} with run_services(config, MicroA) as runner: runner.start() runner.wait() runner.stop()