async def add_slave(self, sid: str, key: str, translations: dict): events.get_listener(self.app).subscribe( exchange_name=self.app['config']['sync_exchange'], routing=key, on_message=partial(_receive, self.app, sid, translations) ) LOGGER.info(f'Added remote subscription. key = {key}, local object = {sid}')
async def test_subscribe_callbacks(app, client, mocker): cb = Mock() content = dict(key='var') pika_msg = Mock() pika_msg.routing_key = 'message_key' pika_msg.body = json.dumps(content) sub = events.get_listener(app).subscribe('brewblox', 'router', on_message=cb) await sub._relay(pika_msg) cb.assert_called_once_with(sub, 'message_key', content) assert pika_msg.ack.call_count == 1 cb2 = Mock() sub.on_message = cb2 await sub._relay(pika_msg) cb2.assert_called_once_with(sub, 'message_key', content) assert cb.call_count == 1 assert pika_msg.ack.call_count == 2 # Shouldn't blow up cb2.side_effect = ValueError await sub._relay(pika_msg) # Should be tolerated sub.on_message = None await sub._relay(pika_msg)
def setup(app): writer = InfluxWriter(app) relay = EventRelay(listener=events.get_listener(app), writer=writer) client = QueryClient(app) app[WRITER_KEY] = writer app[RELAY_KEY] = relay app[CLIENT_KEY] = client app.router.add_routes(routes)
def test_main(mocker, app): mocker.patch(TESTED + '.service.run') mocker.patch(TESTED + '.service.create_app').return_value = app main.main() assert None not in [ commander.get_commander(app), datastore.get_datastore(app), device.get_controller(app), events.get_listener(app), broadcaster.get_broadcaster(app) ]
def add_events(app: web.Application): """Add event handling Subscriptions can be made at any time using `EventListener.subscribe()`. They will be declared on the remote amqp server whenever the listener is connected. Message interest can be specified by setting exchange name, and routing key. For `direct` and `fanout` exchanges, messages must match routing key exactly. For `topic` exchanges (the default), routing keys can be multiple values, separated with dots (.). Routing keys can use regex and wildcards. The simple wildcards are `*` and `#`. `*` matches a single level. "controller.*.sensor" subscriptions will receive (example) routing keys: - controller.block.sensor - controller.container.sensor But not: - controller - controller.nested.block.sensor - controller.block.sensor.nested `#` is a greedier wildcard: it will match as few or as many values as it can Plain # subscriptions will receive all messages published to that exchange. A subscription of "controller.#" will receive: - controller - controller.block.sensor - controller.container.nested.sensor For more information on this, see https://www.rabbitmq.com/tutorials/tutorial-four-python.html and https://www.rabbitmq.com/tutorials/tutorial-five-python.html """ # Enable the task scheduler # This is required for the `events` feature scheduler.setup(app) # Enable event handling # Event subscription / publishing will be enabled after you call this function events.setup(app) # Get the standard event listener # This can be used to register as many subscriptions as you want listener = events.get_listener(app) # Subscribe to all events on 'brewblox' exchange listener.subscribe('brewblox', '#', on_message=on_message)
async def test_setup(app, client): assert events.get_listener(app) assert events.get_publisher(app)
def __init__(self, app: web.Application): super().__init__(app) self._listener = events.get_listener(app) self._writer = influx.get_data_writer(app)