代码之家  ›  专栏  ›  技术社区  ›  Rob S.

Azure函数Python V2计时器触发器未部署,但在VSCode中处于成功状态

  •  0
  • Rob S.  · 技术社区  · 3 年前

    我正在部署一个非常基本的Azure功能应用程序来演示一些关键功能。

    我有两个函数,一个演示HTTP触发器,另一个演示Timer触发器。两者都在本地实例上完美运行。

    import azure.functions as func
    import os
    import datetime
    import logging
    
    app = func.FunctionApp()
    
    @app.function_name(name="HttpTrigger1")
    @app.route(route="keyvaulttest")
    def test_function(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
        utc_timestamp = datetime.datetime.utcnow().replace(
            tzinfo=datetime.timezone.utc).isoformat()
    
        test_phrase = os.getenv("TestEnvFromKeyVault")
    
        logging.info(f'TestEnvFromKeyVault: {test_phrase}')
        logging.info('Python HTTP trigger function ran at %s', utc_timestamp)
     
        return func.HttpResponse(
            test_phrase,
            status_code=200
        )
    
    @app.function_name(name="TestTimer")
    @app.schedule(schedule="0 */5 * * * *", arg_name="test_timer", use_monitor=False) 
    def test_function(test_timer: func.TimerRequest) -> None:
        utc_timestamp = datetime.datetime.utcnow().replace(
            tzinfo=datetime.timezone.utc).isoformat()
    
        test = os.getenv("TestEnvFromKeyVault")
    
        if test_timer.past_due:
            logging.info('The timer is past due!')
    
        logging.info(f'TestEnvFromKeyVault: {test}')
        logging.info('Python timer trigger function ran at %s', utc_timestamp)
    

    当我尝试使用VSCode Azure Function扩展命令“Azure Functions:deploy to FunctionApp”进行部署时,它表示已成功部署。我的HTTP触发器函数已部署并工作,但我的Timer触发器函数未部署。

    12:13:48 PM testapp: Deployment successful. deployer = ms-azuretools-vscode deploymentPath = Functions App ZipDeploy. Extract zip. Remote build.
    

    enter image description here

    0 回复  |  直到 3 年前
        1
  •  1
  •   thatguy1155    2 年前

    我做了上述解决方案所做的(包括配置),但仍然发现自己遇到了OP描述的相同问题。在接到微软支持和一切的电话后,最终删除了这行:

    @app.function_name(name="HttpTrigger1")
    

    …显然,在v2中,函数名是从 def test_function 就像任何其他函数一样,并且上面的行在上传时对读取函数造成干扰。

        2
  •  0
  •   anon anon    3 年前

    我在我的环境中通过在同一个函数类文件中使用HTTP和Timer触发器函数进行了复制-Python版本3.9.13

    本地执行 :

    enter image description here

    配置 :

    1. 创建了Azure功能应用程序(Python)和密钥库。
    2. 已在Azure功能应用程序中启用系统分配的托管身份。
    3. 在密钥库中,添加了具有值的密钥,也在访问配置中>添加策略>密钥和秘密管理,主功能应用程序系统分配的托管身份对象Id。
    4. 添加名为的密钥库名称应用程序设置和Python v2模型应用程序设置 "AzureWebJobsFeatureFlags": "EnableWorkerIndexing" 功能应用程序配置。

    Azure云门户-功能应用程序执行 :

    1. 使用以下命令发布到Azure门户功能应用程序:
    func azure functionapp publish <your_FunctionApp_Name_inTheAzurePortal>
    
    1. 发布到Azure门户功能应用程序后,我在Azure功能应用程序的配置菜单中添加了2个应用程序设置:

    enter image description here

    注意:您可以在发布到Azure门户之前或之后添加它们。

    我可以看到两个函数:

    enter image description here

    Http触发器函数运行 :

    enter image description here

    计时器触发功能运行:

    enter image description here

    代码段 :

    函数_app.py :

    import  azure.functions  as  func
    import  logging
    import  datetime
    import  os
    from  azure.keyvault.secrets  import  SecretClient
    from  azure.identity  import  DefaultAzureCredential
    
    app = func.FunctionApp()
    @app.function_name(name="HttpTrigger1")     #Http_Trigger
    @app.route(route="hello")
    
    def  test_function(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    
    utc_timestamp = datetime.datetime.utcnow().replace(
    tzinfo=datetime.timezone.utc).isoformat()
    
    keyVaultName = os.environ["KEY_VAULT_NAME"]
    logging.info(f'TestEnvFromKeyVault: {keyVaultName}')
    logging.info('Python Http trigger function ran at %s', utc_timestamp)
    return  func.HttpResponse("Hello Krishna, This HTTP triggered function executed successfully.",
    status_code=200
    )
    
    # Timer_Trigger_in_same_function
    
    @app.function_name(name="mytimer")
    @app.schedule(schedule="0 */1 * * * *", arg_name="mytimer", run_on_startup=True,
    use_monitor=False)
    def  test_function(mytimer: func.TimerRequest) -> None:
    
    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)
    

    host.json :

    {
        "version": "2.0",
        "logging": {
        "applicationInsights": {
        "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
            }
        }
    },
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[3.15.0, 4.0.0)"
        }
    }
    

    requirements.txt文件 :

    azure-functions
    azure.keyvault.secrets
    azure.identity
    azure-keyvault
    

    local.settings.json :

    {
    
    "IsEncrypted": false,
    "Values": {
        "FUNCTIONS_WORKER_RUNTIME": "python",
        "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=krishfunappstorage;AccountKey=<Storage-Account-Access_Key>;EndpointSuffix=core.windows.net",
        "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
        "KEY_VAULT_NAME":"krishkv4funapp"
    }
    }
    

    代码示例的以上部分取自以下参考文献 :

    1. 来自此的Python Azure函数代码 MS Doc
    2. 来自此MS的功能应用程序发布命令 Doc 参考