async def run(loop): print("Connecting to NATS...") nc = NATS() await nc.connect("localhost:4222", loop=loop) input_subject = "test-subject-input" try: payload = { "data": { "name": "John Doe" }, } async def message_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print( f"[WRITER] Received a message on '{subject} {reply}': {data}") try: await nc.publish( reply, bytes("{\"success\": true }", encoding='utf-8')) print(f"[WRITER] reply ok") except Exception as err: print(f"[WRITER] exception: {err}") sid = await nc.subscribe("mongo_writer", cb=message_handler) print("Waiting for a metrics message...") # Stop receiving after 3 calls to prediction.save() and 1 call to db.save(). await nc.auto_unsubscribe(sid, 4) print(f"Sending a test message to {input_subject}...") payload["data"] = json.dumps(payload["data"]) msg = await nc.request(input_subject, bytes(json.dumps(payload), encoding='utf-8'), timeout=10) res = json.loads(msg.data.decode()) print("error -> ", res['error']) print("data -> ", res['data']) except ErrTimeout: print("Request timed out") await nc.close()
async def run(loop): nc = NATS() await nc.connect(servers=["nats://127.0.0.1:4222"], io_loop=loop) async def message_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print("Received a message on '{subject} {reply}': {data}".format( subject=subject, reply=reply, data=data)) # Simple publisher and async subscriber via coroutine. sid = await nc.subscribe("foo", cb=message_handler) # Stop receiving after 2 messages. await nc.auto_unsubscribe(sid, 2) await nc.publish("foo", b'Hello') await nc.publish("foo", b'World') await nc.publish("foo", b'!!!!!') async def help_request(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print("Received a message on '{subject} {reply}': {data}".format( subject=subject, reply=reply, data=data)) await nc.publish(reply, b'I can help') # Use queue named 'workers' for distributing requests # among subscribers. await nc.subscribe("help", "workers", help_request) # Send a request and expect a single response # and trigger timeout if not faster than 50 ms. try: response = await nc.timed_request("help", b'help me', 0.050) print("Received response: {message}".format( message=response.data.decode())) except ErrTimeout: print("Request timed out") await asyncio.sleep(1, loop=loop) await nc.close()
async def run(loop): parser = argparse.ArgumentParser() # e.g. nats-pub hello -d "world" -s nats://127.0.0.1:4222 -s nats://127.0.0.1:4223 parser.add_argument('subject', default='hello', nargs='?') parser.add_argument('-d', '--data', default="hello world") parser.add_argument('-s', '--servers', default=[], action='append') parser.add_argument('--creds', default="") args = parser.parse_args() nc = NATS() async def error_cb(e): print("Error:", e) async def closed_cb(): print("Connection to NATS is closed.") async def reconnected_cb(): print("Connected to NATS at {}...".format(nc.connected_url.netloc)) options = { "io_loop": loop, "error_cb": error_cb, "closed_cb": closed_cb, "reconnected_cb": reconnected_cb } if len(args.creds) > 0: options["user_credentials"] = args.creds try: if len(args.servers) > 0: options['servers'] = args.servers await nc.connect(**options) except Exception as e: print(e) show_usage_and_die() print("Connected to NATS at {}...".format(nc.connected_url.netloc)) await nc.publish(args.subject, args.data.encode()) await nc.flush() await nc.close()
async def test_async_publish_and_max_acks_inflight(self): nc = NATS() await nc.connect(loop=self.loop) sc = STAN() await sc.connect("test-cluster", generate_client_id(), nats=nc, max_pub_acks_inflight=5) future = asyncio.Future(loop=self.loop) packs = [] # It will be receiving the ack which we will be controlling manually,. async def cb(ack): nonlocal packs nonlocal future packs.append(ack) if len(packs) >= 5: await asyncio.sleep(0.5, loop=self.loop) for i in range(0, 1024): future = sc.publish("hi", b'hello', ack_handler=cb) try: await asyncio.wait_for(future, 0.2, loop=self.loop) except Exception as e: # Some of them will be timing out since giving up # on being able to publish. break # Gave up with some published acks still awaiting self.assertTrue(sc._pending_pub_acks_queue.qsize() > 1) # Expect to have received all messages already by now. self.assertEqual(len(packs), 5) # Waiting some time will let us receive the rest of the messages. await asyncio.sleep(2.5, loop=self.loop) self.assertEqual(len(packs), 10) await sc.close() await nc.close() self.assertFalse(nc.is_connected)
def test_connect_with_auth(self): nc = NATS() options = { 'servers': [ "nats://*****:*****@127.0.0.1:4223", "nats://*****:*****@127.0.0.1:4224" ], 'io_loop': self.loop } yield from nc.connect(**options) self.assertIn('auth_required', nc._server_info) self.assertIn('max_payload', nc._server_info) self.assertEqual(nc._server_info['max_payload'], nc._max_payload) self.assertTrue(nc.is_connected) yield from nc.close() self.assertTrue(nc.is_closed) self.assertFalse(nc.is_connected)
async def run(loop): nc = NATS() async def closed_cb(): print("Connection to NATS is closed.") await asyncio.sleep(0.1, loop=loop) loop.stop() # It is very likely that the demo server will see traffic from clients other than yours. # To avoid this, start your own locally and modify the example to use it. options = { # "servers": ["nats://127.0.0.1:4222"], "servers": ["nats://demo.nats.io:4222"], "loop": loop, "closed_cb": closed_cb } await nc.connect(**options) print(f"Connected to NATS at {nc.connected_url.netloc}...") async def subscribe_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print("Received a message on '{subject} {reply}': {data}".format( subject=subject, reply=reply, data=data)) # Basic subscription to receive all published messages # which are being sent to a single topic 'discover' await nc.subscribe("help", cb=subscribe_handler) # Subscription on queue named 'workers' so that # one subscriber handles message a request at a time. await nc.subscribe("help.*", "workers", subscribe_handler) def signal_handler(): if nc.is_closed: return print("Disconnecting...") loop.create_task(nc.close()) for sig in ('SIGINT', 'SIGTERM'): loop.add_signal_handler(getattr(signal, sig), signal_handler)
async def run(loop): nats_conn = NATS() subject = 'friends_quote' async def error_cb(e): print('Error:', e) async def closed_cb(): print('Connection to NATS is closed.') async def reconnected_cb(): print(f'Connected to NATS at {nats_conn.connected_url.netloc}...') async def get_quote(): url = 'https://friends-quotes-api.herokuapp.com/quotes/random' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36' } req = requests.get(url, headers=headers) return json.dumps(req.json()).encode() quote = await get_quote() options = { 'loop': loop, 'error_cb': error_cb, 'closed_cb': closed_cb, 'reconnected_cb': reconnected_cb } try: options['servers'] = 'nats://nats:4222' await nats_conn.connect(**options) except Exception as e: print(e) print(f'Connected to NATS at {nats_conn.connected_url.netloc}...') await nats_conn.publish(subject, quote) await nats_conn.flush() await nats_conn.close()
async def nats_init_and_bind(get_queue: QueuePrimitive, send_queue: QueuePrimitive): nats = NATS() await nats.connect(NATS_CONFIG.nats_addr) async def msg_recived(msg): get_queue.queue.put(msg.data.decode()) await nats.subscribe(NATS_CONFIG.room_number, cb=msg_recived) while True: try: txt = send_queue.queue.get(block=False) await nats.publish(NATS_CONFIG.room_number, (NATS_CONFIG.my_name + ">> " + str(txt)).encode()) except Exception: pass await asyncio.sleep(0.1)
def test_connect_with_failed_auth(self): nc = NATS() options = { 'servers': [ "nats://*****:*****@127.0.0.1:4223", ], 'io_loop': self.loop } with self.assertRaises(ErrNoServers): yield from nc.connect(**options) self.assertIn('auth_required', nc._server_info) self.assertTrue(nc._server_info['auth_required']) self.assertFalse(nc.is_connected) yield from nc.close() self.assertTrue(nc.is_closed) self.assertEqual(ErrNoServers, type(nc.last_error)) self.assertEqual(0, nc.stats['reconnects'])
async def run(loop): nc = NATS() #await nc.connect("demo.nats.io:4222", loop=loop) await nc.connect("192.168.5.41:4222", loop=loop, connect_timeout=3) async def message_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print("Received a message on '{subject} {reply}': {data}".format( subject=subject, reply=reply, data=data)) # Simple publisher and async subscriber via coroutine. sid = await nc.subscribe("foo", cb=message_handler) async def help_request(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print("Received a message on '{subject} {reply}': {data}".format( subject=subject, reply=reply, data=data)) await nc.publish(reply, b'I can help') # Use queue named 'workers' for distributing requests # among subscribers. sid = await nc.subscribe("help", "workers", help_request) # Send a request and expect a single response # and trigger timeout if not faster than 1 second. try: response = await nc.request("help", b'help me', timeout=1) print("Received response: {message}".format( message=response.data.decode())) except ErrTimeout: print("Request timed out") # Remove interest in subscription. await nc.unsubscribe(sid) time.sleep(10) # Terminate connection to NATS. await nc.close()
async def nats(server, subject, msg, loop): """ NATS client implemented via asyncio python3 implementation, see https://github.com/nats-io/nats.py """ nc = NATS() try: await nc.connect(server, loop=loop, max_reconnect_attempts=3) except Exception as exp: print("failed to connect to server: error {}".format(str(exp))) traceback.print_exc() return if isinstance(msg, list): for item in msg: await nc.publish(subject, item) else: await nc.publish(subject, msg) await nc.close()
async def call_service(nats_host: str, nats_port: str, service_topic: str, services, response_topic, logger, loop, wait_for_response: bool = True): nc = NATS() await nc.connect(servers=[f"nats://{nats_host}:{nats_port}"], loop=loop) async def message_handler(msg): try: subject = msg.subject data = msg.data.decode() logger.info(f"Received a message on '{subject}': {data}") data = json.loads(data) response = services[data['service_name']](data['data']) if wait_for_response: logger.info(f"Response from service'{data['service_name']}': {response}") await nc.publish(response_topic, response.encode()) except Exception as ex: logger.error(f"Exception in service {data['service_name']}: {ex}") sid = await nc.subscribe(service_topic, cb=message_handler)
async def run(loop): publisher = pubsub_v1.PublisherClient() topic_name = args.topic nc = NATS() async def closed_cb(): print("Connection to NATS is closed.") await asyncio.sleep(0.1, loop=loop) loop.stop() # It is very likely that the demo server will see traffic from clients other than yours. # To avoid this, start your own locally and modify the example to use it. options = { "servers": args.servers, "loop": loop, "closed_cb": closed_cb } await nc.connect(**options) print(f"Connected to NATS at {nc.connected_url.netloc}...") async def subscribe_handler(msg): data = msg.data read_card_holder = ReadCardHolder() read_card_holder.card_holder.ParseFromString(data) row_to_insert = read_card_holder.GetRowToInsert() pubsub_msg = json.dumps(row_to_insert).encode() publisher.publish(topic_name, pubsub_msg) await nc.subscribe(args.subject, cb=subscribe_handler) # Subscription on queue named 'workers' so that # one subscriber handles message a request at a time. await nc.subscribe(args.subject, args.queue, subscribe_handler) def signal_handler(): if nc.is_closed: return print("Disconnecting...") loop.create_task(nc.close()) for sig in ('SIGINT', 'SIGTERM'): loop.add_signal_handler(getattr(signal, sig), signal_handler)
def __init__(self, settings): self.settings = settings models.init_mongoengine(settings) self.nc = NATS() self.cn_report_queue = asyncio.Queue() self.processor_command_queue = asyncio.Queue() self.storage_command_queue = asyncio.Queue() self.running = False self.cn_resource = compute_nodes.ComputeNodeResource() self.command_controller = commands.CommandController( self.settings, self.processor_command_queue, ) self.processor_controller = processors.ProcessorController( self.nc, command_controller=self.command_controller, ) self.storage_controller = storages.StorageController(self.settings)
async def listening(pool): nc = NATS() print("Obj done") loop = asyncio.get_running_loop() print(f"loop1 is {loop}") await nc.connect("localhost:4222", loop=loop) print("connected") async def help_request(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print(f"Received a message on '{subject} {reply}': {data}") future = loop.run_in_executor(pool, task1, data) new_task = add_success_cb(future, cb2) result = asyncio.create_task(new_task) print(result) #future.add_done_callback(cb2) await nc.subscribe("trial", cb=help_request)
async def run(loop): nc = NATS() try: # Setting explicit list of servers in a cluster. await nc.connect(servers=["nats://127.0.0.1:4222", "nats://127.0.0.1:4223", "nats://127.0.0.1:4224"], loop=loop) except ErrNoServers as e: print(e) return async def message_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() for i in range(0, 20): await nc.publish(reply, f"i={i}".encode()) await nc.subscribe("help.>", cb=message_handler) async def request_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print("Received a message on '{subject} {reply}': {data}".format( subject=subject, reply=reply, data=data)) # Signal the server to stop sending messages after we got 10 already. await nc.request( "help.please", b'help', expected=10, cb=request_handler) try: # Flush connection to server, returns when all messages have been processed. # It raises a timeout if roundtrip takes longer than 1 second. await nc.flush(1) except ErrTimeout: print("Flush timeout") await asyncio.sleep(1, loop=loop) # Drain gracefully closes the connection, allowing all subscribers to # handle any pending messages inflight that the server may have sent. await nc.drain()
def run(loop): nc = NATS() try: yield from nc.connect(servers=["nats://127.0.0.1:4222"], io_loop=loop) except ErrNoServers as e: print(e) return @asyncio.coroutine def message_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() for i in range(0, 20): yield from nc.publish(reply, "i={i}".format(i=i).encode()) yield from nc.subscribe("help.>", cb=message_handler) @asyncio.coroutine def request_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print("Received a message on '{subject} {reply}': {data}".format( subject=subject, reply=reply, data=data)) # Signal the server to stop sending messages after we got 10 already. yield from nc.request("help.please", b'help', expected=10, cb=request_handler) try: # Flush connection to server, returns when all messages have been processed. # It raises a timeout if roundtrip takes longer than 1 second. yield from nc.flush(1) except ErrTimeout: print("Flush timeout") yield from asyncio.sleep(1, loop=loop) yield from nc.close()
async def publish(ingress_resource, loop): # client publish to NATS nc = NATS() msg = { "namespace": ingress_resource["metadata"]["namespace"], "name": ingress_resource["metadata"]["name"], "rules": ingress_resource["spec"]["rules"] } try: await nc.connect(servers=[args.nats_address], loop=loop, connect_timeout=args.conn_timeout, max_reconnect_attempts=args.conn_attempts, reconnect_time_wait=args.conn_wait) except ErrNoServers as e: # Could not connect to any server in the cluster. print(e) return async def message_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print("Received a message on '{subject} {reply}': {data}".format( subject=subject, reply=reply, data=data)) # Simple publisher and async subscriber via coroutine. sid = await nc.subscribe(args.topic, cb=message_handler) try: await nc.publish(args.topic, json.dumps(msg).encode('utf-8')) await nc.flush(0.500) output['data'].append(msg) except ErrConnectionClosed as e: print("Connection closed prematurely.") except ErrTimeout as e: print("Timeout occured when publishing msg i={}: {}".format(deploy, e)) await nc.drain()
def run(loop): nc = NATS() @asyncio.coroutine def closed_cb(): print("Connection to NATS is closed.") yield from asyncio.sleep(0.1, loop=loop) loop.stop() options = { "servers": ["nats://127.0.0.1:4222"], "io_loop": loop, "closed_cb": closed_cb } yield from nc.connect(**options) print("Connected to NATS at {}...".format(nc.connected_url.netloc)) @asyncio.coroutine def subscribe_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print("Received a message on '{subject} {reply}': {data}".format( subject=subject, reply=reply, data=data)) # Basic subscription to receive all published messages # which are being sent to a single topic 'discover' yield from nc.subscribe("discover", cb=subscribe_handler) # Subscription on queue named 'workers' so that # one subscriber handles message a request at a time. yield from nc.subscribe("help.*", "workers", subscribe_handler) def signal_handler(): if nc.is_closed: return print("Disconnecting...") loop.create_task(nc.close()) for sig in ('SIGINT', 'SIGTERM'): loop.add_signal_handler(getattr(signal, sig), signal_handler)
async def run(loop): # Use borrowed connection for NATS then mount NATS Streaming # client on top. URL = "nats://[email protected]:4222" clusterID = "fissionMQTrigger" clientID = "clientSub" subj = "test1" nc = NATS() await nc.connect(io_loop=loop, servers=[URL]) # Start session with NATS Streaming cluster. sc = STAN() await sc.connect(clusterID, clientID, nats=nc) future = asyncio.Future(loop=loop) async def cb(msg): nonlocal future # print("Received a message (seq={}): {}".format(msg.seq, msg.data)) decoded = json.loads(msg.data) decoded['end'] = time.time() decoded['duration'] = decoded['end'] - decoded['start'] decoded['e2e_duration'] = decoded['end'] - decoded['trigger'] # print("Received dict: {}".format(decoded)) future.set_result(decoded) # Subscribe to get all messages since beginning. sub = await sc.subscribe(subj, start_at='last_received', cb=cb) await asyncio.wait_for(future, 10, loop=loop) # Stop receiving messages await sub.unsubscribe() # Close NATS Streaming session await sc.close() # We are using a NATS borrowed connection so we need to close manually. await nc.close() return future.result()
async def run(loop): nc = NATS() log.info(f'connecting to nats service: {nats_host:{nats_port}}') await nc.connect(f'{nats_host}:{nats_port}', loop=loop) async def message_handler(msg): subject = msg.subject log.info( f'Received a message on: {subject}, data length: {len(msg.data)} bytes' ) datastream_msg = datastream_pb2.DataStreamMessage() datastream_msg.ParseFromString(msg.data) payload = pickle.loads(datastream_msg.payload[0]) cache.set('values', payload.get('values')) if payload.get('image'): cache.set('image', payload.get('image')) log.info(f'subscribe to subject: {nats_subject}') sid = await nc.subscribe(nats_subject, cb=message_handler)
def test_invalid_subscribe_error(self): nc = NATS() msgs = [] future_error = asyncio.Future(loop=self.loop) @asyncio.coroutine def subscription_handler(msg): msgs.append(msg) @asyncio.coroutine def closed_cb(): nonlocal future_error future_error.set_result(nc.last_error) yield from nc.connect(io_loop=self.loop, closed_cb=closed_cb) yield from nc.subscribe("foo.", cb=subscription_handler) yield from asyncio.wait_for(future_error, 1.0, loop=self.loop) nats_error = future_error.result() self.assertEqual(type(nats_error), NatsError) self.assertEqual(str(nats_error), "nats: 'Invalid Subject'")
def __init__(self, nats_broker_url=None, nats_src_topic=None, nats_dst_topic=None): self.subscribe_id = None self.connected = False self.nats_client = NATS() self._get_config_from_env_var_('nats_broker_url', 'NATS_ENDPOINT', nats_broker_url, 'NATS broker') self._get_config_from_env_var_('nats_src_topic', 'NATS_SRC_TOPIC', nats_src_topic, 'NATS source topic') self._get_config_from_env_var_('nats_dst_topic', 'NATS_DST_TOPIC', nats_dst_topic, 'NATS destination topic') logger.info("broker: {b}, src topic: {s}, dst_topic: {d}".format( b=self.nats_broker_url, s=self.nats_src_topic, d=self.nats_dst_topic))
async def run(loop): nc = NATS() try: await nc.connect(args.nats_address, loop=loop, connect_timeout=args.conn_timeout, max_reconnect_attempts=args.conn_attempts, reconnect_time_wait=args.conn_wait) except Exception as e: exit(e) async def get_pod_events(): w = watch.Watch() for event in w.stream(v1.list_pod_for_all_namespaces): logger.info("Event: %s %s %s" % (event['type'], event['object'].kind, event['object'].metadata.name)) msg = {'type':event['type'],'object':event['raw_object']} if args.enable_output: print(json.dumps(msg)) await nc.publish("k8s_events", json.dumps(msg).encode('utf-8')) await asyncio.sleep(0.1) await get_pod_events() await nc.close()
async def run(loop): nc = NATS() await nc.connect(servers=["nats://127.0.0.1:4222"], loop=loop) async def message_handler(msg): data = json.loads(msg.data.decode()) print(data) sid = await nc.subscribe("updates", cb=message_handler) await nc.flush() await nc.auto_unsubscribe(sid, 2) await nc.publish("updates", json.dumps({ "symbol": "GOOG", "price": 1200 }).encode()) await asyncio.sleep(1, loop=loop) await nc.close()
async def run(loop): print("Connecting to NATS...") nc = NATS() await nc.connect("localhost:4222", loop=loop) async def message_handler(msg): subject = msg.subject reply = msg.reply data = msg.data.decode() print(f"Received a message on '{subject} {reply}': {data}") # Terminate connection to NATS. await nc.close() loop.stop() sid = await nc.subscribe("test-subject-mongo-writer", cb=message_handler) print("Waiting for a metrics message...") # Stop receiving after 1 messages. await nc.auto_unsubscribe(sid, 1)
async def example(): # [begin connect_status] nc = NATS() await nc.connect(servers=["nats://demo.nats.io:4222"], ) # Do something with the connection. print("The connection is connected?", nc.is_connected) while True: if nc.is_reconnecting: print("Reconnecting to NATS...") break await asyncio.sleep(1) await nc.close() print("The connection is closed?", nc.is_closed)
async def run(loop: asyncio.AbstractEventLoop): """ Main funcion, check folder every 10 second and sending supported picture format to system. :param loop: asyncio loop """ nc = NATS() logging.info("Connecting to nats") await nc.connect(servers=["nats://nats-server:4222"], loop=loop) logging.info("Starting loop") while True: try: await scan_folder(nc) # re-scan folder every 10 seconds await asyncio.sleep(10) except Exception as ex: # we dont want shut down whole program logging.error(ex) pass
async def inner(app): async def on_error(e): app.nats_logger.error(f'[NATS] Error: {e}') async def on_close(): app.nats_logger.warning('[NATS] Closed connection to NATS') async def on_reconnect(): app.nats_logger.warning(f'[NATS] Reconnected to NATS: {app.nats_dsn}') async def on_disconnect(): app.nats_logger.warning('[NATS] Disconnected from NATS') app.nats_logger.info(f'[NATS] Connecting to NATS: {app.nats_dsn} ...') nc = NATS() await nc.connect(app.nats_dsn, error_cb=on_error, closed_cb=on_close, reconnected_cb=on_reconnect, disconnected_cb=on_disconnect, max_reconnect_attempts=max_reconnect) app['nats'] = nc app.nats_logger.info('[NATS] Connected to NATS!')
async def call(request): con = NATS() await con.connect(io_loop=loop, servers=[NATS_URL]) if await request.text(): try: data = await request.json() except json.decoder.JSONDecodeError as e: raise HttpBadRequest(e) else: data = {} rpc = request.match_info.get('rpc') logger.debug('=Received http request: {0} {1}'.format(rpc, data)) msg = await con.timed_request(rpc, json.dumps(data).encode(), timeout=NATS_CALL_TIMEOUT) return web.json_response(json.loads(msg.data.decode()))