Ejemplo n.º 1
0
def setup_event_bus():
    """
    Setup the event bus with the provided application data

    """
    global bus
    redis_url = build_redis_url(**config.redis)

    if redis_health_check(redis_url):
        LOGGER.info('Starting event bus on %s', redis_url)
        bus = lightbus.create(
            config=dict(
                service_name='search-engine',
                process_name='search-engine-process',
                bus=dict(
                    schema=dict(
                        transport=dict(
                            redis=dict(
                                url=redis_url
                            )
                        )
                    )
                ),
                apis=dict(
                    default=dict(
                        event_transport=dict(
                            redis=dict(
                                url=redis_url
                            )
                        ),
                        rpc_transport=dict(
                            redis=dict(
                                url=redis_url
                            )
                        ),
                        result_transport=dict(
                            redis=dict(
                                url=redis_url
                            )
                        )
                    )
                )
            ))

        bus.client.register_api(UserEvents())

        UserCreatedListener(name='handle_user_creation',
                            event_api='user',
                            event_name='user_created',
                            storage_config=config.storage).add_to_bus(bus)
        UserDeletedListener(name='handle_user_deletion',
                            event_api='user',
                            event_name='user_deleted',
                            storage_config=config.storage).add_to_bus(bus)

        p = Process(target=event_bus_runner, args=(bus,))
        p.start()
    else:
        LOGGER.error(f'Redis service not available. Exiting...')
        sys.exit(1)
Ejemplo n.º 2
0
def bus(redis_rpc_transport, redis_result_transport, redis_event_transport):
    """Get a redis transport backed by a running redis server."""
    return lightbus.create(
        rpc_transport=redis_rpc_transport,
        result_transport=redis_result_transport,
        event_transport=redis_event_transport,
    )
Ejemplo n.º 3
0
async def test_listen_to_multiple_events_across_multiple_transports(
        loop, redis_server_url, redis_server_b_url, worker: Worker):
    redis_url_a = redis_server_url
    redis_url_b = redis_server_b_url

    logging.warning(f"Server A URL: {redis_url_a}")
    logging.warning(f"Server B URL: {redis_url_b}")

    config = Config.load_dict({
        "bus": {
            "schema": {
                "transport": {
                    "redis": {
                        "url": redis_url_a
                    }
                }
            }
        },
        "apis": {
            "default": {
                "event_transport": {
                    "redis": {
                        "url": redis_url_a
                    }
                }
            },
            "api_b": {
                "event_transport": {
                    "redis": {
                        "url": redis_url_b
                    }
                }
            },
        },
    })

    bus = lightbus.create(config=config)
    bus.client.disable_proxy()
    bus.client.register_api(ApiA())
    bus.client.register_api(ApiB())
    await asyncio.sleep(0.1)

    calls = 0

    def listener(*args, **kwargs):
        nonlocal calls
        calls += 1

    bus.client.listen_for_events(events=[("api_a", "event_a"),
                                         ("api_b", "event_b")],
                                 listener=listener,
                                 listener_name="test")

    async with worker(bus):
        await asyncio.sleep(0.1)
        await bus.api_a.event_a.fire_async()
        await bus.api_b.event_b.fire_async()
        await asyncio.sleep(0.1)

    assert calls == 2
Ejemplo n.º 4
0
async def test_multiple_rpc_transports(loop, redis_server_url,
                                       redis_server_b_url, consume_rpcs):
    """Configure a bus with two redis transports and ensure they write to the correct redis servers"""
    redis_url_a = redis_server_url
    redis_url_b = redis_server_b_url

    logging.warning(f"Server A url: {redis_url_a}")
    logging.warning(f"Server B url: {redis_url_b}")

    config = Config.load_dict({
        "bus": {
            "schema": {
                "transport": {
                    "redis": {
                        "url": redis_url_a
                    }
                }
            }
        },
        "apis": {
            "default": {
                "rpc_transport": {
                    "redis": {
                        "url": redis_url_a
                    }
                },
                "result_transport": {
                    "redis": {
                        "url": redis_url_a
                    }
                },
            },
            "api_b": {
                "rpc_transport": {
                    "redis": {
                        "url": redis_url_b
                    }
                },
                "result_transport": {
                    "redis": {
                        "url": redis_url_b
                    }
                },
            },
        },
    })

    bus = lightbus.create(config=config)
    bus.client.register_api(ApiA())
    bus.client.register_api(ApiB())

    task = asyncio.ensure_future(consume_rpcs(bus))
    await asyncio.sleep(0.1)

    await bus.api_a.rpc_a.call_async()
    await bus.api_b.rpc_b.call_async()
    await asyncio.sleep(0.1)

    await cancel(task)
    await bus.client.close_async()
Ejemplo n.º 5
0
async def test_multiple_event_transports(loop, redis_server_url,
                                         redis_server_b_url,
                                         create_redis_client):
    """Configure a bus with two redis transports and ensure they write to the correct redis servers"""
    redis_url_a = redis_server_url
    redis_url_b = redis_server_b_url

    logging.warning(f"Server A URL: {redis_url_a}")
    logging.warning(f"Server B URL: {redis_url_b}")

    config = Config.load_dict({
        "bus": {
            "schema": {
                "transport": {
                    "redis": {
                        "url": redis_url_a
                    }
                }
            }
        },
        "apis": {
            "default": {
                "event_transport": {
                    "redis": {
                        "url": redis_url_a,
                        "stream_use": StreamUse.PER_EVENT.value
                    }
                }
            },
            "api_b": {
                "event_transport": {
                    "redis": {
                        "url": redis_url_b,
                        "stream_use": StreamUse.PER_EVENT.value
                    }
                }
            },
        },
    })

    bus = lightbus.create(config=config)
    bus.client.disable_proxy()
    bus.client.register_api(ApiA())
    bus.client.register_api(ApiB())
    await asyncio.sleep(0.1)

    await bus.api_a.event_a.fire_async()
    await bus.api_b.event_b.fire_async()

    redis_a = await create_redis_client(address=redis_server_url)
    redis_b = await create_redis_client(address=redis_server_b_url)

    assert await redis_a.xrange("api_a.event_a:stream")
    assert await redis_a.xrange("api_b.event_b:stream") == []

    assert await redis_b.xrange("api_a.event_a:stream") == []
    assert await redis_b.xrange("api_b.event_b:stream")

    await bus.client.close_async()
Ejemplo n.º 6
0
def main():
    # Make sure Lightbus formats its logs correctly
    lightbus.configure_logging()

    # Create our lightbus client and our web application
    bus = lightbus.create()
    app = web.Application()

    app.router.add_route("GET", "/", home_view)
    app.on_startup.append(start_listener)
    app.on_cleanup.append(cleanup)

    # Store the bus on `app` as we'll need it
    # in start_listener() and cleanup()
    app.bus = bus

    web.run_app(app, host="127.0.0.1", port=5000)
Ejemplo n.º 7
0
def setup_event_bus():
    """
    Setup the events bus for the provided app

    """
    global bus
    redis_url = build_redis_url(**config.redis)

    if redis_health_check(redis_url):
        LOGGER.info('Starting event bus on %s', redis_url)
        bus = lightbus.create(
            config=dict(
                bus=dict(
                    schema=dict(
                        transport=dict(
                            redis=dict(
                                url=redis_url
                            )
                        )
                    )
                ),
                apis=dict(
                    default=dict(
                        event_transport=dict(
                            redis=dict(
                                url=redis_url
                            )
                        ),
                        rpc_transport=dict(
                            redis=dict(
                                url=redis_url
                            )
                        ),
                        result_transport=dict(
                            redis=dict(
                                url=redis_url
                            )
                        )
                    )
                )
            ))
        bus.client.register_api(UserEvents())
    else:
        LOGGER.error(f'Redis service not available. Exiting...')
        sys.exit(1)
Ejemplo n.º 8
0
import lightbus

bus = lightbus.create()


class MyApi(lightbus.Api):
    class Meta:
        version = 1


bus.client.register_api(MyApi())


class MyMigrations():
    def migrate_1_to_2(self,
                       event: lightbus.EventMessage) -> lightbus.EventMessage:
        pass

    def migrate_2_to_1(self,
                       event: lightbus.EventMessage) -> lightbus.EventMessage:
        pass

    def migrate_2_to_3(self,
                       event: lightbus.EventMessage) -> lightbus.EventMessage:
        pass


@migrations(MyMigrations(), to_version=4)
def my_listener():
    pass
Ejemplo n.º 9
0
# store/bus.py
import lightbus

bus = lightbus.create(flask=True)


class StoreApi(lightbus.Api):
    page_view = lightbus.Event(parameters=("url",))

    class Meta:
        name = "store"
Ejemplo n.º 10
0
def bus(redis_config_file):
    """Get a BusPath instance so we can use the bus"""
    bus = lightbus.create(config_file=redis_config_file)
    yield bus
    bus.client.close()