cosmosdb 的计时器触发器无法正常工作

cosmosdb 的计时器触发器无法正常工作

问题内容

我对我的函数应用“timertrigger”有疑问。

我开发了此功能来与 telegram 机器人进行通信,以便在 api 请求后发送消息。

我在本地尝试过该功能应用程序,效果很好。但是,当我尝试使用 cosmosdb 存储信息时,遇到问题并且无法保存信息。

我已经设置了将我的应用程序与 telegram 和 cosmosdb 连接所需的所有变量和内容

try: database_obj = client.get_database_client(database_name) await database_obj.read() return database_obj except exceptions.cosmosresourcenotfounderror: print("creating database") return await client.create_database(database_name) 1. 1. create a container 1. using a good partition key improves the performance of database operations. 1. async def get_or_create_container(database_obj, container_name): try: todo_items_container = database_obj.get_container_client(container_name) await todo_items_container.read() return todo_items_container except exceptions.cosmosresourcenotfounderror: print("creating container with lastname as partition key") return await database_obj.create_container( id=container_name, partition_key=partitionkey(path="/lastname"), offer_throughput=400) except exceptions.cosmoshttpresponseerror: raise 1. async def populate_container_items(container_obj, items_to_create): 1. add items to the container family_items_to_create = items_to_create 1. for family_item in family_items_to_create: inserted_item = await container_obj.create_item(body=family_item) print("inserted item for %s family. item id: %s" %(inserted_item['lastname'], inserted_item['id'])) 1. 1. async def read_items(container_obj, items_to_read): 1. read items (key value lookups by partition key and id, aka point reads) 1. for family in items_to_read: item_response = await container_obj.read_item(item=family['id'], partition_key=family['lastname']) request_charge = container_obj.client_connection.last_response_headers['x-ms-request-charge'] print('read item with id {0}. operation consumed {1} request units'.format(item_response['id'], (request_charge))) 1. 1. 1. async def query_items(container_obj, query_text): 1. enable_cross_partition_query should be set to true as the container is partitioned 1. in this case, we do have to await the asynchronous iterator object since logic 1. within the query_items() method makes network calls to verify the partition key 1. definition in the container 1. query_items_response = container_obj.query_items( query=query_text, enable_cross_partition_query=true ) request_charge = container_obj.client_connection.last_response_headers['x-ms-request-charge'] items = [item async for item in query_items_response] print('query returned {0} items. operation consumed {1} request units'.format(len(items), request_charge)) 1. 1. async def run_sample(): print('aaaa') print('sss {0}'.format(cosmosclient(endpoint,credential=key))) async with cosmosclient(endpoint, credential = key) as client: print('connected to db') try: database_obj = await get_or_create_db(client, database_name) 1. create a container container_obj = await get_or_create_container(database_obj, container_name) family_items_to_create = ["link", "ss", "s", "s"] await populate_container_items(container_obj, family_items_to_create) await read_items(container_obj, family_items_to_create) 1. query these items using the sql query syntax. 1. specifying the partition key value in the query allows cosmos db to retrieve data only from the relevant partitions, which improves performance query = "select * from c " await query_items(container_obj, query) except exceptions.cosmoshttpresponseerror as e: print('nrun_sample has caught an error. {0}'.format(e.message)) finally: print("nquickstart complete") async def main(mytimer: func.timerrequest) -> none: utc_timestamp = datetime.datetime.utcnow().replace( tzinfo=datetime.timezone.utc).isoformat() asyncio.create_task(run_sample()) logging.info(' sono partito') sendnews() if mytimer.past_due: logging.info('the timer is past due!') logging.info('python timer trigger function ran at %s', utc_timestamp)登录后复制