Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

huangapple go评论69阅读模式
英文:

Azure Functions Python V2 Timer Trigger Does Not Deploy but Status Success in VSCode

问题

I am deploying a very basic Azure Functions App to demonstrate a few key features.

我正在部署一个非常基本的Azure Functions应用程序,以演示一些关键特性。

I have two functions, one demonstrating an HTTP Trigger and the other demonstrating a Timer Trigger. Both run perfectly on local instance.

我有两个函数,一个演示HTTP触发器,另一个演示定时触发器。在本地实例上都运行正常。

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触发函数已部署并正常工作,但我的定时触发函数未部署。

英文:

I am deploying a very basic Azure Functions App to demonstrate a few key features.

I have two functions, one demonstrating an HTTP Trigger and the other demonstrating a Timer Trigger. Both run perfectly on local instance.

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)

When I attempt to deploy using the VSCode Azure Function extension command "Azure Functions: Deploy to FunctionApp" it says it deployed successfully. My HTTP Trigger function is deployed and works, but my Timer Trigger function is not deployed.

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

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

答案1

得分: 1

我做了上面解决方案所做的事情(包括配置),但仍然遇到了与OP描述的相同问题。在与Microsoft支持团队通话并尝试了一切后,最终解决问题的方法是删除以下这行代码:

@app.function_name(name="HttpTrigger1")

...显然在v2版本中,函数名称是从def test_function中读取的,就像任何其他函数一样,上面的代码行会干扰函数上传时的名称读取。

英文:

I did what the above solution did (including the configurations) and still found myself in the same issue described by OP. After getting on a call w Microsoft support and everything, ultimately what did it was deleting the line :

@app.function_name(name="HttpTrigger1")

...apparently in v2 the function name is read from def test_function like any other function would and the above line is causing interference reading the function when uploaded.

答案2

得分: 0

I have reproduced in my environment by taking both the HTTP and Timer Trigger Functions in the same Function Class file - Python Version 3.9.13

Local Execution:

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

Configuration:

  1. Created Azure Function App (Python) and Key Vault.
  2. Enabled System Assigned Managed Identity in Azure Function App.
  3. In Key Vault, Added the Secret with the value and also in Access Configuration > Add Policy > Key and Secret Management, Principal - Function App System Assigned Managed Identity Object Id.
  4. Add the Key Vault Name App Setting and Python v2 model app setting called "AzureWebJobsFeatureFlags": "EnableWorkerIndexing" to the Function App Configuration.

Azure Cloud Portal - Function App Execution:

  1. Published to Azure Portal Function App using below Command:
func azure functionapp publish <your_FunctionApp_Name_inTheAzurePortal>
  1. After Publishing to the Azure Portal Function App, I have added 2 app settings in the Configuration Menu of Azure Function App:

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

Note: You can add them either before or after publishing to the Azure Portal.

And I can see both the Functions:

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

Http Trigger Function Run:

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

Timer Trigger Function Run:

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

Code Snippets:

function_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 file:

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"
}
}

Above parts of code sample taken from the below references:

  1. Python Azure Functions Code from this MS Doc.
  2. Function App Publish Command from this MS Doc Reference
英文:

I have reproduced in my environment by taking both the HTTP and Timer Trigger Functions in the same Function Class file - Python Version 3.9.13

Local Execution:

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

Configuration:

  1. Created Azure Function App (Python) and Key Vault.
  2. Enabled System Assigned Managed Identity in Azure Function App.
  3. In Key Vault, Added the Secret with the value and also in Access Configuration > Add Policy > Key and Secret Management, Principal - Function App System Assigned Managed Identity Object Id.
  4. Add the Key Vault Name App Setting and Python v2 model app setting called &quot;AzureWebJobsFeatureFlags&quot;: &quot;EnableWorkerIndexing&quot; to the Function App Configuration.

Azure Cloud Portal - Function App Execution:

  1. Published to Azure Portal Function App using below Command:
func azure functionapp publish &lt;your_FunctionApp_Name_inTheAzurePortal&gt;
  1. After Publishing to the Azure Portal Function App, I have added 2 app settings in the Configuration Menu of Azure Function App:

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

Note: You can add them either before or after publishing to the Azure Portal.

And I can see both the Functions:

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

Http Trigger Function Run:

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

Timer Trigger Function Run:

Azure Functions Python V2定时触发器在VSCode中部署失败,但状态显示成功。

Code Snippets:

function_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=&quot;HttpTrigger1&quot;)		#Http_Trigger
@app.route(route=&quot;hello&quot;)

def  test_function(req: func.HttpRequest) -&gt; func.HttpResponse:
logging.info(&#39;Python HTTP trigger function processed a request.&#39;)

utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()

keyVaultName = os.environ[&quot;KEY_VAULT_NAME&quot;]
logging.info(f&#39;TestEnvFromKeyVault: {keyVaultName}&#39;)
logging.info(&#39;Python Http trigger function ran at %s&#39;, utc_timestamp)
return  func.HttpResponse(&quot;Hello Krishna, This HTTP triggered function executed successfully.&quot;,
status_code=200
)

# Timer_Trigger_in_same_function

@app.function_name(name=&quot;mytimer&quot;)
@app.schedule(schedule=&quot;0 */1 * * * *&quot;, arg_name=&quot;mytimer&quot;, run_on_startup=True,
use_monitor=False)
def  test_function(mytimer: func.TimerRequest) -&gt; None:

utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()

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

host.json:

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

requirements.txt file:

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

local.settings.json:

{

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

Above parts of code sample taken from the below references:

  1. Python Azure Functions Code from this MS Doc.
  2. Function App Publish Command from this MS Doc Reference

huangapple
  • 本文由 发表于 2023年2月6日 02:19:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定