def run(self): container = create_container(self.config) install_plugins(container, self.config.get('plugins', {})) install_interfaces(container, self.config.get('interfaces', {})) for cls_name in self.args.get('--interface', ()): cls = import_object(cls_name) container.install(cls) if self.args.get('--debug'): from gevent.backdoor import BackdoorServer backdoor = BackdoorServer(('127.0.0.1', 5005), locals={'container': container}) gevent.spawn(backdoor.serve_forever) def handle_signal(): logger.info('caught SIGINT/SIGTERM, pid=%s', os.getpid()) container.stop() container.join() sys.exit(0) gevent.signal(signal.SIGINT, handle_signal) gevent.signal(signal.SIGTERM, handle_signal) setproctitle('iris-instance (identity: %s, endpoint: %s, config: %s)' % ( container.identity, container.endpoint, self.config.source, )) container.start(register=not self.args.get('--isolated', False)) if self.args.get('--reload'): set_source_change_callback(container.stop) container.join()
def install_interfaces(container, interfaces): for name, instance_config in six.iteritems(interfaces): try: cls_name = instance_config['class'] except KeyError: print("no instance class for '%s'" % name) sys.exit(1) cls = import_object(cls_name) instance = container.install(cls) instance.apply_config(instance_config)
def test_import_object(self): from iris.core.container import ServiceContainer cls = import_object('iris.core.container:ServiceContainer') self.assertIs(cls, ServiceContainer)
def create_instance(self, key, default_class=None, **kwargs): config = self.get(key, {}) path = config.pop('class', default_class) cls = import_object(path) return cls.from_config(config, **kwargs)
def install_plugins(container, plugins): for name, plugin_config in six.iteritems(plugins): cls = import_object(plugin_config['class']) container.install_plugin(cls, **plugin_config)