|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import asyncio |
| 3 | +from asyncio import TimeoutError |
| 4 | +from datetime import datetime, timezone |
| 5 | +import json |
| 6 | +import sys |
| 7 | +import pathlib |
| 8 | +from typing import AsyncIterator, List |
| 9 | + |
| 10 | +from yapapi import props as yp |
| 11 | +from yapapi.log import enable_default_logger |
| 12 | +from yapapi.props.builder import DemandBuilder |
| 13 | +from yapapi.rest import Configuration, Market, Activity, Payment # noqa |
| 14 | +from yapapi.rest.market import OfferProposal |
| 15 | +from examples import utils |
| 16 | + |
| 17 | +examples_dir = pathlib.Path(__file__).resolve().parent.parent |
| 18 | +sys.path.append(str(examples_dir)) |
| 19 | + |
| 20 | + |
| 21 | +class NodeInfo(object): |
| 22 | + def __init__(self, offer: OfferProposal): |
| 23 | + self.node_id = offer.issuer |
| 24 | + self.node_name = offer.props["golem.node.id.name"] |
| 25 | + |
| 26 | + |
| 27 | +async def list_offers(conf: Configuration, subnet_tag: str) -> AsyncIterator[OfferProposal]: |
| 28 | + async with conf.market() as client: |
| 29 | + market_api = Market(client) |
| 30 | + dbuild = DemandBuilder() |
| 31 | + dbuild.add(yp.NodeInfo(name="Scanning Node", subnet_tag=subnet_tag)) |
| 32 | + dbuild.add(yp.Activity(expiration=datetime.now(timezone.utc))) |
| 33 | + |
| 34 | + async with market_api.subscribe(dbuild.properties, dbuild.constraints) as subscription: |
| 35 | + async for event in subscription.events(): |
| 36 | + yield event |
| 37 | + |
| 38 | + |
| 39 | +async def list_nodes(conf: Configuration, subnet_tag: str) -> AsyncIterator[List[NodeInfo]]: |
| 40 | + async for offer in list_offers(conf, subnet_tag): |
| 41 | + yield NodeInfo(offer) |
| 42 | + |
| 43 | + |
| 44 | +async def print_nodes(conf: Configuration, subnet_tag: str): |
| 45 | + async for node in list_nodes(conf, subnet_tag=subnet_tag): |
| 46 | + print(f"{node.node_id} {node.node_name}") |
| 47 | + |
| 48 | + |
| 49 | +def main(): |
| 50 | + parser = utils.build_parser("List Nodes") |
| 51 | + args = parser.parse_args() |
| 52 | + |
| 53 | + subnet = args.subnet_tag |
| 54 | + sys.stderr.write(f"Using subnet: {utils.TEXT_COLOR_YELLOW}{subnet}{utils.TEXT_COLOR_DEFAULT}\n") |
| 55 | + |
| 56 | + enable_default_logger() |
| 57 | + try: |
| 58 | + asyncio.get_event_loop().run_until_complete( |
| 59 | + asyncio.wait_for( |
| 60 | + print_nodes( |
| 61 | + Configuration(), |
| 62 | + subnet_tag=subnet, |
| 63 | + ), |
| 64 | + timeout=4, |
| 65 | + ) |
| 66 | + ) |
| 67 | + except TimeoutError: |
| 68 | + pass |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + main() |
0 commit comments