0

我是天蓝色函数和整个天蓝色的初学者。我希望我的代码按照我的计划每小时或每天运行一次,因此选择继续使用 azure 函数。

我的目标是从 API 获取数据,然后将其保存到 azure 中的 cosmos db mongo api。

我可以通过普通的 python 代码来实现我的目标,但是当我将它集成到 azure 函数中时。我发现很难运行它。

以下是我的代码供您参考。

__init__.py

print("Starting Importing Libraries")

import os
import pandas as pd
import numpy as np
import requests
import json
import pymongo
from azure.storage.blob import BlockBlobService
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.datafactory import DataFactoryManagementClient
from azure.mgmt.datafactory.models import *
from datetime import datetime, timedelta
import time
import azure.functions as func


print("Completing Importing Libraries")

def execute_timer():
    #Business Logic to take the data from API and insert it to mongo db.
    data_insert = mycol.insert_many(final_df)
    return data_insert

def main(mytimer: func.TimerRequest, outdoc: func.Out[func.Document]):
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')
    logging.info('Python timer trigger function ran at %s', utc_timestamp)

    try:
        # Get Blog feeds
        outdata = {}
        outdata['items'] = execute_timer()
        # logging.info(outdata)  # for debug

        # Store output data using Cosmos DB output binding
        outdoc.set(func.Document.from_json(json.dumps(outdata)))
    except Exception as e:
        logging.error('Error:')
        logging.error(e)

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    },
    {
      "direction": "out",
      "type": "cosmosDB",
      "name": "outdoc",
      "databaseName": "DB",
      "collectionName": "example_test",
      "leaseCollectionName": "leases",
      "createLeaseCollectionIfNotExists": true,
      "connectionStringSetting": "mongodb://*****",
      "createIfNotExists": true
    }
  ]
}

当我在天蓝色部署后运行它时,即测试+运行:

below error is obtained:

020-08-20T04:48:18Z   [Information]   Executing 'Functions.TimerTrigger_2' (Reason='This function was programmatically called via the host APIs.', Id=123586b0-41d5-4ebc-87eb-bee1c8cd3ae2)
2020-08-20T04:48:18Z   [Verbose]   Sending invocation id:123586b0-41d5-4ebc-87eb-bee1c8cd3ae2
2020-08-20T04:48:18Z   [Verbose]   Posting invocation id:123586b0-41d5-4ebc-87eb-bee1c8cd3ae2 on workerId:767e33e5-dc7f-400c-977b-fdaa108904f2
2020-08-20T04:48:18Z   [Error]   Executed 'Functions.TimerTrigger_2' (Failed, Id=123586b0-41d5-4ebc-87eb-bee1c8cd3ae2, Duration=27ms)
2020-08-20T04:50:00Z   [Information]   Executing 'Functions.TimerTrigger_2' (Reason='Timer fired at 2020-08-20T04:50:00.0009948+00:00', Id=04302a29-7521-42aa-a3c0-0b6d6a139d63)
2020-08-20T04:50:00Z   [Verbose]   Sending invocation id:04302a29-7521-42aa-a3c0-0b6d6a139d63
2020-08-20T04:50:00Z   [Verbose]   Posting invocation id:04302a29-7521-42aa-a3c0-0b6d6a139d63 on workerId:767e33e5-dc7f-400c-977b-fdaa108904f2

另外,当我运行它时,在我的本地。它不会导航到 execute_timer() 执行。

我在这里做什么?

此外,当我尝试在终端窗口中以“func host start”启动主机功能时,在窗口中会出现以下错误。

func : The term 'func' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or 
if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ func host start
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (func:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

有什么我在这里做的,或者我需要做些什么。请建议!

4

0 回答 0