async def test_consumer_will_exit_when_kafka_consumer_stops(event_loop): async def event_processor(message): await asyncio.sleep(0.1) with mock.patch('eventlib.KafkaClient', spec=pykafka.KafkaClient) as KafkaClient: client = KafkaClient.return_value client.topics = {'my-topic': mock.MagicMock(spec=pykafka.topic.Topic)} client.topics['my-topic'].get_simple_consumer.return_value = FakeConsumer(_running=True, value=0) asyncio.ensure_future(eventlib.consume_events('my-topic', 'my-group', 'dummyaddr:9092', event_processor)) await asyncio.sleep(0.2) await eventlib.stop_consuming_events('my-topic') assert eventlib.has_consumer('my-topic') == False
def run(): """ Entry point to this microservice. """ args = get_cli_parser().parse_args() # schedule the internal event consumer # that will run until we terminate this service asyncio.ensure_future(consume_events(topic=args.topic.encode('utf-8'), group=args.group, addr=args.broker, callback=event_handler)) # let's start the REST server that will # serve the view's resource srv = loop.run_until_complete(webserver(args.addr, args.port)) loop.run_until_complete(route_to_resource(bookshelf_view)) # finally, let's advertize this service # to the rest of the world svc = loop.run_until_complete(register_service( id=args.id, name=args.name, port=args.port, address=args.addr, tags=args.tags ) ) try: loop.run_forever() except KeyboardInterrupt: pass finally: loop.run_until_complete(deregister_service(svc)) loop.run_until_complete(stop_consuming_events()) srv.close() loop.run_until_complete(srv.wait_closed()) # give the time for remaining requests to complete loop.run_until_complete(asyncio.sleep(2)) loop.close()
# running = True # c = Consumer({'bootstrap.servers': 'localhost:9092', 'group.id': 'mygroup'}) # c.subscribe(['test']) # while running: # msg = c.poll() # if not msg.error(): # print('Received message: %s' % msg.value()) # c.close() from eventlib import consume_events,stop_consuming_events,start_producer,send_event import asyncio async def consume_test( payload): print (payload) import asyncio start_producer(topic="test",brokers="localhost:9092") asyncio.ensure_future(consume_events("test","test-group","localhost:9092",consume_test)) for i in range(10): asyncio.ensure_future(send_event("test",{"test":"test"})) asyncio.get_event_loop().run_forever()
# from confluent_kafka import Consumer # running = True # c = Consumer({'bootstrap.servers': 'localhost:9092', 'group.id': 'mygroup'}) # c.subscribe(['test']) # while running: # msg = c.poll() # if not msg.error(): # print('Received message: %s' % msg.value()) # c.close() from eventlib import consume_events, stop_consuming_events, start_producer, send_event import asyncio async def consume_test(payload): print(payload) import asyncio start_producer(topic="test", brokers="localhost:9092") asyncio.ensure_future( consume_events("test", "test-group", "localhost:9092", consume_test)) for i in range(10): asyncio.ensure_future(send_event("test", {"test": "test"})) asyncio.get_event_loop().run_forever()