英文:
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.
答案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:
Configuration:
- Created Azure Function App (Python) and Key Vault.
- Enabled System Assigned Managed Identity in Azure Function App.
- 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.
- 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:
- Published to Azure Portal Function App using below Command:
func azure functionapp publish <your_FunctionApp_Name_inTheAzurePortal>
- After Publishing to the Azure Portal Function App, I have added 2 app settings in the Configuration Menu of Azure Function App:
Note: You can add them either before or after publishing to the Azure Portal.
And I can see both the Functions:
Http Trigger Function Run:
Timer Trigger Function Run:
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:
- Python Azure Functions Code from this MS Doc.
- 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:
Configuration:
- Created Azure Function App (Python) and Key Vault.
- Enabled System Assigned Managed Identity in Azure Function App.
- 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.
- 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:
- Published to Azure Portal Function App using below Command:
func azure functionapp publish <your_FunctionApp_Name_inTheAzurePortal>
- After Publishing to the Azure Portal Function App, I have added 2 app settings in the Configuration Menu of Azure Function App:
Note: You can add them either before or after publishing to the Azure Portal.
And I can see both the Functions:
Http Trigger Function Run:
Timer Trigger Function Run:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论