Example #1
0
    def runserver(self):

        def _shutdown(signum, frame):
            eventlet.spawn_n(runner.stop)

        runner = ServiceRunner(config=config.to_dict())
        # for service_cls in BaseService.__subclasses__():
        #     runner.add_service(service_cls)
        runner.add_service(InvboxService)

        signal.signal(signal.SIGTERM, _shutdown)
        runner.start()
        runnlet = eventlet.spawn(runner.wait)

        while True:
            try:
                runnlet.wait()
            except OSError as exc:
                if exc.errno == errno.EINTR:
                    continue
                raise
            except KeyboardInterrupt:
                print()
                try:
                    runner.stop()
                except KeyboardInterrupt:
                    print()  # as above
                    runner.kill()
            else:
                break
Example #2
0
    def test_config_key(self, service_cls, container_cls):
        config = {'SERVICE_CONTAINER_CLS': "fake_module.ServiceContainerX"}
        runner = ServiceRunner(config)
        runner.add_service(service_cls)

        container = get_container(runner, service_cls)
        assert isinstance(container, container_cls)
Example #3
0
    def test_config_key(self, service_cls, container_cls):
        config = {
            'SERVICE_CONTAINER_CLS': "fake_module.ServiceContainerX"
        }
        runner = ServiceRunner(config)
        runner.add_service(service_cls)

        container = get_container(runner, service_cls)
        assert isinstance(container, container_cls)
def main():
    config = {
        '_log': LOG,
        'AMQP_URI': 'amqp://*****:*****@127.0.0.1:5672'
    }
    service_runner = ServiceRunner(config)
    service_runner.add_service(DatabaseService)

    service_runner.start()

    runnlet = eventlet.spawn(service_runner.wait)

    while True:
        try:
            runnlet.wait()
        except OSError as exc:
            if exc.errno == errno.EINTR:
                continue
            raise
        except KeyboardInterrupt:
            try:
                service_runner.stop()
            except KeyboardInterrupt:
                service_runner.kill()
        else:
            break
Example #5
0
    def test_kwarg_deprecation_warning(self, warnings, service_cls,
                                       container_cls):
        config = {}
        runner = ServiceRunner(config, container_cls=container_cls)
        runner.add_service(service_cls)

        container = get_container(runner, service_cls)
        assert isinstance(container, container_cls)

        # TODO: replace with pytest.warns when eventlet >= 0.19.0 is released
        assert warnings.warn.call_args_list == [call(ANY, DeprecationWarning)]
Example #6
0
    def test_kwarg_deprecation_warning(
        self, warnings, service_cls, container_cls
    ):
        config = {}
        runner = ServiceRunner(config, container_cls=container_cls)
        runner.add_service(service_cls)

        container = get_container(runner, service_cls)
        assert isinstance(container, container_cls)

        # TODO: replace with pytest.warns when eventlet >= 0.19.0 is released
        assert warnings.warn.call_args_list == [call(ANY, DeprecationWarning)]
Example #7
0
def main():
    import logging
    logging.basicConfig(level=logging.DEBUG)

    config = {'AMQP_URI': 'amqp://*****:*****@localhost:5672/'}
    runner = ServiceRunner(config)
    runner.add_service(AdderService)
    runner.start()

    try:
        runner.wait()
    except KeyboardInterrupt:
        runner.stop()
Example #8
0
    def test_kwarg_deprecation_warning(self, warnings, service_cls,
                                       worker_ctx_cls):
        config = {}
        runner = ServiceRunner(config)
        runner.add_service(service_cls, worker_ctx_cls=worker_ctx_cls)

        container = get_container(runner, service_cls)
        entrypoint = list(container.entrypoints)[0]

        worker_ctx = container.spawn_worker(entrypoint, (), {})
        assert isinstance(worker_ctx, worker_ctx_cls)

        # TODO: replace with pytest.warns when eventlet >= 0.19.0 is released
        assert warnings.warn.call_args_list == [call(ANY, DeprecationWarning)]
Example #9
0
def test_runner_waits_raises_error(fake_module):
    class Container(object):
        def __init__(self, service_cls, config):
            pass

        def start(self):
            pass

        def stop(self):
            pass

        def wait(self):
            raise Exception('error in container')

    fake_module.ServiceContainer = Container

    config = {'SERVICE_CONTAINER_CLS': 'fake_module.ServiceContainer'}

    runner = ServiceRunner(config=config)
    runner.add_service(TestService1)
    runner.start()

    with pytest.raises(Exception) as exc_info:
        runner.wait()
    assert exc_info.value.args == ('error in container', )
Example #10
0
def run_worker():
    config = get_nameko_config()

    service_runner = ServiceRunner(config)
    service_runner.add_service(RepoWorker)

    service_runner.start()
    service_runner.wait()
Example #11
0
    def test_kwarg_deprecation_warning(
        self, warnings, service_cls, worker_ctx_cls
    ):
        config = {}
        runner = ServiceRunner(config)
        runner.add_service(service_cls, worker_ctx_cls=worker_ctx_cls)

        container = get_container(runner, service_cls)
        entrypoint = list(container.entrypoints)[0]

        worker_ctx = container.spawn_worker(entrypoint, (), {})
        assert isinstance(worker_ctx, worker_ctx_cls)

        # TODO: replace with pytest.warns when eventlet >= 0.19.0 is released
        assert warnings.warn.call_args_list == [call(ANY, DeprecationWarning)]
Example #12
0
def main():

    logging.basicConfig(level=logging.DEBUG)

    # disable most logging so we can see the console
    logger = logging.getLogger('')
    logger.setLevel(logging.WARNING)

    config = {'AMQP_URI': 'amqp://*****:*****@localhost:5672/chat'}
    runner = ServiceRunner(config)
    runner.add_service(Chat)
    runner.start()
    try:
        runner.wait()
    except KeyboardInterrupt:
        runner.stop()
Example #13
0
def main():
    import logging
    logging.basicConfig(level=logging.DEBUG)

    config = {'AMQP_URI': 'amqp://*****:*****@localhost:5672/'}
    runner = ServiceRunner(config)
    runner.add_service(MessagingPublisher)
    runner.start()

    try:
        runner.wait()
    except KeyboardInterrupt:
        runner.stop()
Example #14
0
def run(services, config, backdoor_port=None, rename=None):
    service_runner = ServiceRunner(config)
    for service_cls in services:
        if rename and rename.get(service_cls.name):
            service_cls.name = rename[service_cls.name]
        service_runner.add_service(service_cls)

    def shutdown(signum, frame):
        # signal handlers are run by the MAINLOOP and cannot use eventlet
        # primitives, so we have to call `stop` in a greenlet
        eventlet.spawn_n(service_runner.stop)

    signal.signal(signal.SIGTERM, shutdown)

    if backdoor_port is not None:
        setup_backdoor(service_runner, backdoor_port)

    service_runner.start()

    # if the signal handler fires while eventlet is waiting on a socket,
    # the __main__ greenlet gets an OSError(4) "Interrupted system call".
    # This is a side-effect of the eventlet hub mechanism. To protect nameko
    # from seeing the exception, we wrap the runner.wait call in a greenlet
    # spawned here, so that we can catch (and silence) the exception.
    runnlet = eventlet.spawn(service_runner.wait)

    while True:
        try:
            runnlet.wait()
        except OSError as exc:
            if exc.errno == errno.EINTR:
                # this is the OSError(4) caused by the signalhandler.
                # ignore and go back to waiting on the runner
                continue
            raise
        except KeyboardInterrupt:
            print()  # looks nicer with the ^C e.g. bash prints in the terminal
            try:
                service_runner.stop()
            except KeyboardInterrupt:
                print()  # as above
                service_runner.kill()
        else:
            # runner.wait completed
            break
def test_runner_waits_raises_error(fake_module):
    class Container(object):
        def __init__(self, service_cls, config):
            pass

        def start(self):
            pass

        def stop(self):
            pass

        def wait(self):
            raise Exception('error in container')

    fake_module.ServiceContainer = Container

    config = {'SERVICE_CONTAINER_CLS': 'fake_module.ServiceContainer'}

    runner = ServiceRunner(config=config)
    runner.add_service(TestService1)
    runner.start()

    with pytest.raises(Exception) as exc_info:
        runner.wait()
    assert exc_info.value.args == ('error in container',)
Example #16
0
 def open(url={'AMQP_URI': 'amqp://*****:*****@127.0.0.1'}):
     if not RuleNameko.nameko_fd:
         RuleNameko.url = url
         eventlet.monkey_patch(all=False, os=True, select=True, socket=True, thread=True, time=True)
         RuleNameko.nameko_fd = ServiceRunner(config=url)
         RuleNameko.nameko_fd.add_service(RuleService)
         RuleNameko.nameko_fd.start()
         global service_is_ready
         service_is_ready = False
     return True
Example #17
0
def main():

    logging.basicConfig(level=logging.DEBUG)

    # disable most logging so we can see the console
    logger = logging.getLogger("")
    logger.setLevel(logging.WARNING)

    config = {"AMQP_URI": "amqp://*****:*****@localhost:5672/chat"}
    runner = ServiceRunner(config)
    runner.add_service(Chat)
    runner.start()
    try:
        runner.wait()
    except KeyboardInterrupt:
        runner.stop()
Example #18
0
def test_service_integration():
    config = {'AMQP_URI': 'amqp://*****:*****@localhost:5672/'}
    runner = ServiceRunner(config)
    runner.add_service(ServiceX)
    runner.add_service(ServiceY)
    runner.start()

    container = get_container(runner, ServiceX)

    with entrypoint_hook(container, "remote_method") as entrypoint:
        assert entrypoint("value") == "value-x-y"

    runner.stop()
Example #19
0
def service_container(patched_db):
    config = {'AMQP_URI': settings.NAMEKO_AMQP_URI}
    runner = ServiceRunner(config)
    runner.add_service(NamekoCollectionService)
    runner.start()

    container = get_container(runner, NamekoCollectionService)
    yield container

    runner.stop()
def listen_message(key,
                   callback,
                   context_key=None,
                   timeout=None,
                   block=False,
                   pool_name=None,
                   **kwargs):
    """
    监听消息
    :param key: 消息识别key
    :param callback: 回调函数
    :param context_key: 消息上下文识别key
    :param timeout: 超时关闭监听
    :param block: 是否阻塞
    :param pool_name: 配置池名称
    :return: 无
    """
    message_listener = get_message_listener(key, callback, context_key,
                                            **kwargs)
    config = get_config(settings.NAMEKO_CONFIG, name=pool_name)
    service_runner = ServiceRunner(config)
    service_runner.add_service(message_listener)
    service_runner.start()
    if timeout:

        def stop():
            time.sleep(timeout)
            service_runner.stop()

        if block:
            stop()
        else:
            async_exe(stop)

    return service_runner
Example #21
0
def run(services, config, backdoor_port=None):
    service_runner = ServiceRunner(config)
    for service_cls in services:
        service_runner.add_service(service_cls)

    def shutdown(signum, frame):
        # signal handlers are run by the MAINLOOP and cannot use eventlet
        # primitives, so we have to call `stop` in a greenlet
        eventlet.spawn_n(service_runner.stop)

    signal.signal(signal.SIGTERM, shutdown)

    if backdoor_port is not None:
        setup_backdoor(service_runner, backdoor_port)

    service_runner.start()

    # if the signal handler fires while eventlet is waiting on a socket,
    # the __main__ greenlet gets an OSError(4) "Interrupted system call".
    # This is a side-effect of the eventlet hub mechanism. To protect nameko
    # from seeing the exception, we wrap the runner.wait call in a greenlet
    # spawned here, so that we can catch (and silence) the exception.
    runnlet = eventlet.spawn(service_runner.wait)

    while True:
        try:
            runnlet.wait()
        except OSError as exc:
            if exc.errno == errno.EINTR:
                # this is the OSError(4) caused by the signalhandler.
                # ignore and go back to waiting on the runner
                continue
            raise
        except KeyboardInterrupt:
            print()  # looks nicer with the ^C e.g. bash prints in the terminal
            try:
                service_runner.stop()
            except KeyboardInterrupt:
                print()  # as above
                service_runner.kill()
        else:
            # runner.wait completed
            break
Example #22
0
def test_service_x_y_integration():

    # run services in the normal manner
    config = {'AMQP_URI': 'amqp://*****:*****@localhost:5672/'}
    runner = ServiceRunner(config)
    runner.add_service(ServiceX)
    runner.add_service(ServiceY)
    runner.start()

    # artificially fire the "remote_method" entrypoint on ServiceX
    # and verify response
    container = get_container(runner, ServiceX)
    with entrypoint_hook(container, "remote_method") as entrypoint:
        assert entrypoint("value") == "value-x-y"

    runner.stop()
Example #23
0
def test_busy_check_on_teardown():
    # max_workers needs to be provided, as it's used in a semaphore count
    config = {'max_workers': 4}
    kill_called = Mock()

    class MockedContainer(ServiceContainer):
        def kill(self):
            kill_called()
            super(MockedContainer, self).kill()

    sr = ServiceRunner(config, container_cls=MockedContainer)
    sr.add_service(ExampleService)
    sr.start()
    sr.kill()
    with wait_for_call(5, kill_called) as kill_called_waited:
        assert kill_called_waited.call_count == 1
Example #24
0
def test_service_integration():
    config = {"AMQP_URI": "amqp://*****:*****@localhost:5672/"}
    runner = ServiceRunner(config)
    runner.add_service(ServiceX)
    runner.add_service(ServiceY)
    runner.start()

    container = get_container(runner, ServiceX)

    with entrypoint_hook(container, "remote_method") as entrypoint:
        assert entrypoint("value") == "value-x-y"

    runner.stop()
Example #25
0
def run_all():
    '''
    启动所有服务
    '''
    try:
        services = {}
        for f in root.iterdir():
            if f.is_dir() and f.name.endswith('Service'):
                name = f.name
                service = importlib.import_module(name + '.service')
                services[name] = service
        from nameko.runners import ServiceRunner

        runner = ServiceRunner(
            config={'AMQP_URI': 'amqp://*****:*****@192.168.2.215'})
        for name, service in services.items():
            cls = getattr(service, name)
            runner.add_service(cls)

        runner.start()
        runner.wait()
        # runner.stop()
    except Exception as e:
        runner.kill()
    def test_mail_service_integration(self):
        config = {'AMQP_URI': 'amqp://*****:*****@localhost:5672/'}
        runner = ServiceRunner(config)
        runner.add_service(PaymentService)
        runner.add_service(MailService)

        payment_container = get_container(runner, PaymentService)
        mail_container = get_container(runner, MailService)

        # turns off timer event
        # restrict_entrypoints(payment_container, *[])

        runner.start()

        with entrypoint_hook(payment_container, 'emit_event') as entrypoint:
            with entrypoint_waiter(mail_container, 'on_payment_received'):
                entrypoint()

        assert True
Example #27
0
def test_busy_check_on_teardown():
    # max_workers needs to be provided, as it's used in a semaphore count
    config = {'max_workers': 4}
    kill_called = Mock()

    class MockedContainer(ServiceContainer):
        def kill(self):
            kill_called()
            super(MockedContainer, self).kill()

    sr = ServiceRunner(config, container_cls=MockedContainer)
    sr.add_service(ExampleService)
    sr.start()
    sr.kill()
    with wait_for_call(5, kill_called) as kill_called_waited:
        assert kill_called_waited.call_count == 1
Example #28
0
def test_busy_check_on_teardown():
    # max_workers needs to be provided, as it's used in a semaphore count
    config = MagicMock({'max_workers': 4})
    kill_called = Mock()

    class MockedContainer(ServiceContainer):
        def kill(self, exc):
            kill_called(type(exc))
            super(MockedContainer, self).kill(exc)

    sr = ServiceRunner(config, container_cls=MockedContainer)
    sr.add_service(ExampleService)
    sr.start()
    sr.kill(Exception())
    with wait_for_call(5, kill_called) as kill_called_waited:
        kill_called_waited.assert_called_with(Exception)
Example #29
0
def test_runner_waits_raises_error():
    class Container(object):
        def __init__(self, service_cls, worker_ctx_cls, config):
            pass

        def start(self):
            pass

        def stop(self):
            pass

        def wait(self):
            raise Exception('error in container')

    runner = ServiceRunner(config={}, container_cls=Container)
    runner.add_service(TestService1)
    runner.start()

    with pytest.raises(Exception) as exc_info:
        runner.wait()
    assert exc_info.value.args == ('error in container', )
Example #30
0
def run_service(service_cls):
    def _signal_handler(_signum, _frame):
        """
        Handler on signal
        """
        LOGGER.info('stop service')
        eventlet.spawn(runner.stop)

    params = get_args()
    runner = ServiceRunner(config={})
    service_cls.prepare(params)
    runner.add_service(service_cls)
    signal.signal(signal.SIGTERM, _signal_handler)
    runner.start()
    runnlet = eventlet.spawn(runner.wait)

    LOGGER.info('Start service')
    try:
        runnlet.wait()
    except KeyboardInterrupt:
        LOGGER.info('Stop service')
        runner.stop()
Example #31
0
    def test_mail_service_integration(self):
        config = {'AMQP_URI': 'amqp://*****:*****@localhost:5672/'}
        runner = ServiceRunner(config)
        runner.add_service(PaymentService)
        runner.add_service(MailService)


        payment_container = get_container(runner, PaymentService)
        mail_container = get_container(runner, MailService)

        # turns off timer event
        # restrict_entrypoints(payment_container, *[])

        runner.start()

        with entrypoint_hook(payment_container, 'emit_event') as entrypoint:
            with entrypoint_waiter(mail_container, 'on_payment_received'):
                entrypoint()

        assert True
Example #32
0
def test_runner_waits_raises_error():
    class Container(object):
        def __init__(self, service_cls, worker_ctx_cls, config):
            pass

        def start(self):
            pass

        def stop(self):
            pass

        def wait(self):
            raise Exception('error in container')

    runner = ServiceRunner(config={}, container_cls=Container)
    runner.add_service(TestService1)
    runner.start()

    with pytest.raises(Exception) as exc_info:
        runner.wait()
    assert exc_info.value.args == ('error in container',)
Example #33
0
def main():

    logging.basicConfig(level=logging.DEBUG)

    config = {'AMQP_URI': 'amqp://*****:*****@localhost:5672/'}
    runner = ServiceRunner(config)
    runner.add_service(HelloWorld)
    runner.add_service(FriendlyService)
    runner.start()
    try:
        runner.wait()
    except KeyboardInterrupt:
        runner.stop()
Example #34
0
def test_runner_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):
            events.add(('kill', self.service_cls.name, self.service_cls))

        def wait(self):
            events.add(('wait', self.service_cls.name, self.service_cls))

    config = {}
    runner = ServiceRunner(config, container_cls=Container)

    runner.add_service(TestService1)
    runner.add_service(TestService2)

    runner.start()

    assert events == {
        ('start', 'foobar_1', TestService1),
        ('start', 'foobar_2', TestService2),
    }

    events = set()
    runner.stop()
    assert events == {
        ('stop', 'foobar_1', TestService1),
        ('stop', 'foobar_2', TestService2),
    }

    events = set()
    runner.kill()
    assert events == {
        ('kill', 'foobar_1', TestService1),
        ('kill', 'foobar_2', TestService2),
    }

    events = set()
    runner.wait()
    assert events == {
        ('wait', 'foobar_1', TestService1),
        ('wait', 'foobar_2', TestService2),
    }
Example #35
0
def main():
    runner = ServiceRunner(config={})
    runner.add_service(DSMService)
    runner.start()
Example #36
0
def run_nameko(args):
    # Running nameko imports here so that Eventgen as a module does not require nameko to run.
    import eventlet
    eventlet.monkey_patch()
    from nameko.runners import ServiceRunner
    # In order to make this run locally as well as within a container-ized environment, we're to pull variables
    # from both environment variables and CLI arguments, where CLI will take precendence.
    config = parse_env_vars()
    config = parse_cli_vars(config, args)
    config = rectify_config(config)
    print "Config used: {}".format(config)
    # Wait up to 30s for RMQ service to be up
    wait_for_response(config["AMQP_URI"], config["AMQP_WEBPORT"])
    # Start Nameko service
    runner = ServiceRunner(config=config)
    if args.role == "controller":
        from eventgen_nameko_controller import EventgenController
        runner.add_service(EventgenController)
    else:
        from eventgen_nameko_server import EventgenServer
        runner.add_service(EventgenServer)
    runner.start()
    runnlet = eventlet.spawn(runner.wait)
    while True:
        try:
            runnlet.wait()
        except OSError as exc:
            if exc.errno == errno.EINTR:
                # this is the OSError(4) caused by the signalhandler.
                # ignore and go back to waiting on the runner
                continue
            raise
        except KeyboardInterrupt:
            print()  # looks nicer with the ^C e.g. bash prints in the terminal
            try:
                runner.stop()
            except KeyboardInterrupt:
                print()  # as above
                runner.kill()
        else:
            # runner.wait completed
            break
def main():
    config_dict = get_config_yaml(CONFIG_YAML_PATH)
    c = update_config_yaml(config_dict, CONFIG_YAML_PATH)
    wus = WatchURLService()
    wus.get_config()
    runner = ServiceRunner(c)
    runner.add_service(WatchURLService)
    # container_a = get_container(runner, WatchURLService)
    runner.start()
    try:
        runner.wait()
    except KeyboardInterrupt:
        runner.kill()
    runner.stop()
Example #38
0
 def make_runner(config, *service_classes):
     runner = ServiceRunner(config)
     for service_cls in service_classes:
         runner.add_service(service_cls)
     all_runners.append(runner)
     return runner
Example #39
0
 def make_runner(*service_classes):
     runner = ServiceRunner(rabbit_config)
     for service_cls in service_classes:
         runner.add_service(service_cls)
     all_runners.append(runner)
     return runner
Example #40
0
from nameko.runners import ServiceRunner
from .controllers.data_mongo_controller import DataMongoController
from .settings import Constants

RUNNER = ServiceRunner(config=Constants.NAMEKO_CONFIG)
RUNNER.add_service(DataMongoController)
Example #41
0
    def handle(self, *args, **options):
        runner = ServiceRunner(settings.AMQP_CONFIG)
        runner.add_service(MasterService)

        def shutdown(signum, frame):
            eventlet.spawn_n(runner.kill)
        signal.signal(signal.SIGTERM, shutdown)

        runner.start()
        try:
            runner.wait()
        except KeyboardInterrupt:
            try:
                runner.stop()
            except KeyboardInterrupt:
                runner.kill()
    service_logger.info("%s %s %s ",
                        sys._getframe().f_code.co_name,
                        "RUNNING_PUBLIC_CLOUD_LIST:-",
                        RUNNING_PUBLIC_CLOUD_LIST)
    service_logger.info("%s %s %s ",
                        sys._getframe().f_code.co_name,
                        "RUNNING_PRIVATE_CLOUD_LIST:-",
                        RUNNING_PRIVATE_CLOUD_LIST)
    db_data = get_clouds()
    with ClusterRpcProxy(CONFIG) as rpc:
        rpc.initHandler.dispatchUpdateCloudTable(payload=db_data)
    with ClusterRpcProxy(CONFIG) as rpc:
        rpc.ClusterRegistryService.registerCloudInstance(
            configDict=cloud_config)
    #with ClusterRpcProxy(CONFIG) as rpc:
    #    rpc.AuditorService.loadAuditorTypeRegistry(auditor_dict=cloud_config)


from nameko.containers import ServiceContainer
from nameko.runners import ServiceRunner
from nameko.testing.utils import get_container

runner = ServiceRunner(config=CONFIG)
runner.add_service(InitHandler)
runner.add_service(ClusterRegistryService)
runner.start()

service_logger.info("%s %s %s ",
                    sys._getframe().f_code.co_name, "DB_PATH:-", DB_PATH)
init_cloud_framework()
def main():
    config_dict = get_config_yaml(CONFIG_YAML_PATH)
    c = update_config_yaml(config_dict, CONFIG_YAML_PATH)
    runner = ServiceRunner(c)
    runner.add_service(FetchURLService)
    # container_a = get_container(runner, FetchURLService)
    runner.start()
    try:
        runner.wait()
    except KeyboardInterrupt:
        runner.kill()
    runner.stop()
Example #44
0
def test_runner_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):
            events.add(('kill', self.service_cls.name, self.service_cls))

        def wait(self):
            events.add(('wait', self.service_cls.name, self.service_cls))

    config = {}
    runner = ServiceRunner(config, container_cls=Container)

    runner.add_service(TestService1)
    runner.add_service(TestService2)

    runner.start()

    assert events == {
        ('start', 'foobar_1', TestService1),
        ('start', 'foobar_2', TestService2),
    }

    events = set()
    runner.stop()
    assert events == {
        ('stop', 'foobar_1', TestService1),
        ('stop', 'foobar_2', TestService2),
    }

    events = set()
    runner.kill()
    assert events == {
        ('kill', 'foobar_1', TestService1),
        ('kill', 'foobar_2', TestService2),
    }

    events = set()
    runner.wait()
    assert events == {
        ('wait', 'foobar_1', TestService1),
        ('wait', 'foobar_2', TestService2),
    }
Example #45
0
from nameko.runners import ServiceRunner
from nameko.utils import get_container


class ServiceA(object):
    name = "service_a"


class ServiceB(object):
    name = "service_b"


# create a runner for ServiceA and ServiceB
runner = ServiceRunner(config={})
runner.add_service(ServiceA)
runner.add_service(ServiceB)

# ``get_container`` will return the container for a particular service
container_a = get_container(runner, ServiceA)

# start both services
runner.start()

# stop both services
runner.stop()
Example #46
0
    buf = BufferB()
    T = 1

    @timer(interval=T)  # how to use a value from constructor?
    def send(self):
        if self.buf["at0"] == 0:
            self.buf["at0"] = datetime.strptime(self.microA_rpc.get_t0(),
                                                "%Y-%m-%d %H:%M:%S.%f")

        self.buf["value"] += self.buf["S"] * self.buf["F"]
        key = str(datetime.now() - self.buf["time"])
        self.microA_rpc.put(key, self.buf["value"])

        print self.buf['at0']


if __name__ == '__main__':
    config = {'AMQP_URI': "amqp://*****:*****@localhost"}
    print "inside run_services"
    # with run_services(config, MicroB) as runner:
    #     runner.start()
    #     runner.wait()
    #     runner.stop()

    runner = ServiceRunner(config=config)
    runner.add_service(MicroB)

    runner.start()
    runner.wait()
    runner.stop()
Example #47
0
    def handle(self, *args, **options):
        runner = ServiceRunner(settings.AMQP_CONFIG)
        runner.add_service(MasterService)

        def shutdown(signum, frame):
            eventlet.spawn_n(runner.kill)

        signal.signal(signal.SIGTERM, shutdown)

        runner.start()
        try:
            runner.wait()
        except KeyboardInterrupt:
            try:
                runner.stop()
            except KeyboardInterrupt:
                runner.kill()
import sys, os
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + '/../../')

from service.test_service import PaymentService
from mailer.service import Mailer
from nameko.runners import ServiceRunner


config = {'AMQP_URI': 'amqp://*****:*****@localhost:5672/'}
runner = ServiceRunner(config)

# Add Mailer service. Dependant Payment Service will be started automatically.
runner.add_service(Mailer)

runner.start()
Example #49
0
from nameko.runners import ServiceRunner
from nameko.testing.utils import get_container


class ServiceA(object):
    name = "service_a"


class ServiceB(object):
    name = "service_b"


# create a runner for ServiceA and ServiceB
runner = ServiceRunner(config={})
runner.add_service(ServiceA)
runner.add_service(ServiceB)

# ``get_container`` will return the container for a particular service
container_a = get_container(runner, ServiceA)

# start both services
runner.start()

# stop both services
runner.stop()
def run_all():
    services = {}
    for f in root.iterdir():
        if f.is_dir() and f.name.endswith("Service") and f.name.startswith(
                "Admin"):
            name = f.name
            service = importlib.import_module(name + '.service')
            services[name] = service
    from nameko.runners import ServiceRunner

    runner = ServiceRunner(config={"AMQP_URI": "amqp://*****:*****@ip"})
    for name, service in services.items():
        cls = getattr(service, name)
        print(name, service, cls)
        runner.add_service(cls)

    print('wsadsa')

    runner.start()
    try:
        runner.wait()
    except KeyboardInterrupt:
        runner.kill()
        runner.stop()
def run():
    config = {'AMQP_URI': 'amqp://localhost'}
    runner = ServiceRunner(config)
    runner.add_service(AlarmService)
    runner.add_service(HttpEntrypointService)
    runner.add_service(LightService)
    runner.add_service(LocalStorageService)
    runner.add_service(SpotifyService)
    runner.add_service(VolumeService)
    runner.start()

    runnlet = eventlet.spawn(runner.wait)

    try:
        runnlet.wait()
    except KeyboardInterrupt:
        try:
            runner.stop()
        except KeyboardInterrupt:
            runner.kill()
def test_runner_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):
            events.add(('kill', self.service_cls.name, self.service_cls))

        def wait(self):
            events.add(('wait', self.service_cls.name, self.service_cls))

    fake_module.ServiceContainer = Container

    config = {'SERVICE_CONTAINER_CLS': 'fake_module.ServiceContainer'}
    runner = ServiceRunner(config)

    runner.add_service(TestService1)
    runner.add_service(TestService2)

    runner.start()

    assert events == {
        ('start', 'foobar_1', TestService1),
        ('start', 'foobar_2', TestService2),
    }

    events = set()
    runner.stop()
    assert events == {
        ('stop', 'foobar_1', TestService1),
        ('stop', 'foobar_2', TestService2),
    }

    events = set()
    runner.kill()
    assert events == {
        ('kill', 'foobar_1', TestService1),
        ('kill', 'foobar_2', TestService2),
    }

    events = set()
    runner.wait()
    assert events == {
        ('wait', 'foobar_1', TestService1),
        ('wait', 'foobar_2', TestService2),
    }
                else:
                    print("Suspicious node :", cluster['properties']['name'],
                          "connected to Auditor : ",
                          node['properties']['name'])
            elif 'type' in node['properties'] and node['properties'][
                    'type'] == "Policies":
                #Check if policy version in evaMapBAse is same as current version in GraphDB
                'TODO'
        return evaMapBase


from nameko.containers import ServiceContainer
from nameko.runners import ServiceRunner
from nameko.testing.utils import get_container

runner = ServiceRunner(config=CONFIG)
runner.add_service(SecurityPostureMap)
runner.add_service(SecurityPostureGraph)
runner.start()
sec = SecurityPostureGraph()
sec.populate_components()

if __name__ == '__main__':
    main()


def main():
    print("Get Catalogs")
    SecurityPostureGraph.getCrispCatalogs()
    samplePosture = None
    with open('crisp/sample_posture.json') as f:
        return "pong"


if __name__ == "__main__":
    print("Starting scraper microservice")

    while True:
        try:
            print("Looking for config")
            CONFIG = {"AMQP_URI": "pyamqp://*****:*****@localhost"}
            environ_amqp_uri = os.environ.get("AMQP_URI")
            if environ_amqp_uri:
                CONFIG["AMQP_URI"] = environ_amqp_uri

            print("Using the following host address: " + CONFIG["AMQP_URI"])
            service_runner = ServiceRunner(CONFIG)
            service_runner.add_service(
                ScrapeStackOverflowJobListingsMicroService)
            runlet = eventlet.spawn(service_runner.wait)

            def shutdown(signum, frame):
                # signal handlers are run by the MAINLOOP and cannot use eventlet
                # primitives, so we have to call `stop` in a greenlet
                print("Received SIGTERM")
                eventlet.spawn_n(service_runner.stop)

            signal.signal(signal.SIGTERM, shutdown)

            print("Starting service")
            service_runner.start()
            print("Started service")