예제 #1
0
def test_local(local_broadcaster):
    cb_calls = [0, 0]

    def cb1(data):
        cb_calls[0] += 1
        return data + 1

    def cb2():
        cb_calls[1] += 1

    assert broadcast.broadcast("test1", {'data': 1}, expect_answers=True) == []
    assert cb_calls == [0, 0]

    broadcast.subscribe("test1", cb1)
    broadcast.subscribe("test2", cb2)
    assert cb_calls == [0, 0]

    assert broadcast.broadcast("test1", {'data': 1}) is None
    assert cb_calls == [1, 0]

    assert broadcast.broadcast("test2") is None
    assert cb_calls == [1, 1]

    assert broadcast.broadcast("test1", {'data': 12},
                               expect_answers=True) == [13]
    assert cb_calls == [2, 1]

    broadcast.unsubscribe("test1")
    assert broadcast.broadcast("test1", {'data': 1}, expect_answers=True) == []
    assert cb_calls == [2, 1]
예제 #2
0
def init(slave: bool) -> None:
    global MASTER_SOURCE  # pylint: disable=global-statement
    mode.init(slave)
    if slave:
        broadcast.subscribe("slave_fetch", _slave_fetch)
    _update_flag("LOADING")
    _prepare_ssh()
    if os.environ.get("MASTER_CONFIG"):
        LOG.info("Load the master config from environment variable")
        content = yaml.load(os.environ["MASTER_CONFIG"],
                            Loader=yaml.SafeLoader)
    else:
        LOG.info("Load the master config from config file")
        with open("/etc/shared_config_manager/config.yaml",
                  encoding="utf-8") as scm_file:
            content = yaml.load(scm_file, Loader=yaml.SafeLoader)

        def thread() -> None:
            inotify_ = inotify.adapters.Inotify()
            inotify_.add_watch("/etc/shared_config_manager/config.yaml")
            for _, type_names, path, filename in inotify_.event_gen(
                    yield_nones=False):
                LOG.debug("Inotify envent: %s / %s: [%s]", path, filename,
                          ",".join(type_names))
                if "IN_CLOSE_WRITE" in type_names:
                    LOG.info("Reload the master config from config file")
                    with open("/etc/shared_config_manager/config.yaml",
                              encoding="utf-8") as scm_file:
                        config = yaml.load(scm_file, Loader=yaml.SafeLoader)
                    _handle_master_config(config)

        thread_instance = Thread(target=thread, daemon=True)
        thread_instance.start()

    if content.get("sources", False) is not False:
        LOG.info("The master config is inline")
        content["standalone"] = True
        # A fake master source to have auth work
        MASTER_SOURCE = base.BaseSource(MASTER_ID,
                                        content,
                                        is_master=True,
                                        default_key=content.get("key"))
        Thread(target=_handle_master_config,
               args=[content],
               name="master_config_loader",
               daemon=True).start()
    else:
        MASTER_SOURCE = _create_source(MASTER_ID, content, is_master=True)
        LOG.info("Initial loading of the master config")
        MASTER_SOURCE.refresh_or_fetch()
        LOG.info("Loading of the master config finished")
        if not MASTER_SOURCE.get_config().get("standalone", False):
            Thread(target=reload_master_config,
                   name="master_config_loader",
                   daemon=True).start()
예제 #3
0
def init(config: pyramid.config.Configurator) -> None:
    """Install a pyramid  event handler that adds the request information."""
    broadcast.subscribe("c2c_sql_profiler", _setup_profiler)

    config.add_route("c2c_sql_profiler",
                     config_utils.get_base_path(config) + r"/sql_profiler",
                     request_method="GET")
    config.add_view(_sql_profiler_view,
                    route_name="c2c_sql_profiler",
                    renderer="fast_json",
                    http_cache=0)
    LOG.info("Enabled the /sql_profiler API")
예제 #4
0
def test_fallback():
    cb_calls = [0]

    def cb1(value):
        cb_calls[0] += 1
        return value + 1

    try:
        broadcast.subscribe("test1", cb1)

        assert broadcast.broadcast("test1", {'value': 12},
                                   expect_answers=True) == [13]
        assert cb_calls == [1]
    finally:
        broadcast._broadcaster = None  # pylint: disable=W0212
예제 #5
0
def init() -> None:
    """Initialize the debug listeners."""
    broadcast.subscribe("c2c_dump_memory", _dump_memory_impl)
    broadcast.subscribe("c2c_dump_stacks", _dump_stacks_impl)
예제 #6
0
def init() -> None:
    broadcast.subscribe('c2c_dump_memory', _dump_memory_impl)
    broadcast.subscribe('c2c_dump_stacks', _dump_stacks_impl)