[英]Azure Function App timer trigger in Python not working
I can't get an Azure Function App to run my Pyhton script (timer trigger function) inside of Azure.我无法让 Azure 函数应用程序在 Azure 中运行我的 Pyhton 脚本(定时器触发函数)。 Locally, the script works just fine.
在本地,该脚本运行良好。 My script is a simple operation which copies data from a PostgreSQL-database to an Azure SQL Database.
我的脚本是一个简单的操作,它将数据从 PostgreSQL 数据库复制到 Azure SQL 数据库。 (script is also listed below).
(脚本也在下面列出)。
After creating a function with the TimerTrigger template in VS Code, I get this __ init __.py file, looking like this:在 VS Code 中使用 TimerTrigger 模板创建函数后,我得到这个 __ init __.py 文件,如下所示:
import logging
import azure.functions as func
def main(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)
So, as I understand correctly from the (standard generated) function.json file, the init.py file will be the fired at the desired trigger.因此,正如我从(标准生成的)function.json 文件中正确理解的那样,init.py 文件将在所需的触发器上触发。 The function.json file looks like this:
function.json 文件如下所示:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "mytimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "* */10 * * * *"
}
]
}
When I paste in my own script in the __ init __.py file, it works like a charm locallly , but once uploaded to Azure nothing happens, at all .当我在__初始化__.py文件我自己的脚本粘贴,它就像一个魅力locallly,但一旦上传到Azure的什么也没有发生,在所有。 After pasting my own script, the init.py file looks like this (I hid my credentials of course):
粘贴我自己的脚本后,init.py 文件如下所示(我当然隐藏了我的凭据):
import datetime
import logging
import azure.functions as func
from flask import Flask
from flask import render_template
import psycopg2
import pandas as pd
import pyodbc
def main(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)
# Start of script
# Connect PostgreSQL
pg_host = "###"
pg_port = "###"
pg_dbname = "###"
pg_user = "###"
pg_pw = "###"
db_conn = psycopg2.connect(host=pg_host, port=pg_port, dbname=pg_dbname, user=pg_user, password=pg_pw)
pg_cursor = db_conn.cursor()
# Query PostgreSQL
pg_sql = 'select "_id", "minute", "hour", "day", "month", "year", "action", "dashlaneRid", "href", "id", "ip", "roleType", "sessionId", "tag", "textContent", "toggle", "url", "userId", "userPib", "utcEpochMillis", "xtag" from click'
pg_cursor.execute(pg_sql)
query = pg_cursor.fetchall()
# Fetch PostgreSQL query in Pandas DataFrame
click_df = pd.DataFrame(query, columns=[
'_id'
,'minute'
,'hour'
,'day'
,'month'
,'year'
,'action'
,'dashlaneRid'
,'href'
,'id'
,'ip'
,'roleType'
,'sessionId'
,'tag'
,'textContent'
,'toggle'
,'url'
,'userId'
,'userPib'
,'utcEpochMillis'
,'xtag'])
# Azure SQL connect
az_server = '###'
az_db = '###'
az_user = '###'
az_pw = '####'
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+az_server+';DATABASE='+az_db+';UID='+az_user+';PWD='+ az_pw)
az_cursor = cnxn.cursor()
# Truncate table click
az_cursor.execute("TRUNCATE TABLE stage.click;")
cnxn.commit()
print('Table succesfully truncated')
# Fill table click with values
for index, row in click_df.iterrows():
az_cursor.execute("INSERT INTO stage.click"
"(_id, minute, hour, day, month, year, action, dashlaneRid, href, id, ip, roleType, sessionId, tag, textContent, toggle, url, userId, userPib, utcEpochMillis, xtag) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
,row._id, row.minute, row.hour, row.day, row.month, row.year, row.action, row.dashlaneRid, row.href, row.id, row.ip, row.roleType, row.sessionId, row.tag, row.textContent, row.toggle, row.url, row.userId, row.userPib, row.utcEpochMillis, row.xtag)
cnxn.commit()
az_cursor.close()
pg_cursor.close()
print('Table successfully filled')
I configured CORS for the Function App to allow all domains, because when I Test/Run the function, I was getting HTTP error 413 Request entity too large.我为函数应用程序配置了 CORS 以允许所有域,因为当我测试/运行函数时,我收到 HTTP 错误 413 请求实体太大。 That's solved: I now get HTTP response 202 Accepted.
这已经解决了:我现在得到 HTTP 响应 202 Accepted。 However: nothing happens on the SQL Database.
但是:SQL 数据库上没有任何反应。
I probably forgot to mention a few important things, so please ask away.我可能忘了提一些重要的事情,所以请走开。
Thank you Anupum Chand for your suggestions, Based on the discussion in comment section we understood issue got resolved so we are converting the above comments to Answer for this thread to help other community members.感谢Anupum Chand的建议,根据评论部分的讨论,我们了解到问题已解决,因此我们将上述评论转换为该线程的答案,以帮助其他社区成员。
Post enabling the application insights metrics understood that issue is related to the psycopg2 library.启用应用程序洞察指标后了解到该问题与 psycopg2 库有关。 After adding the psycopg2-binary to the requirements.txt file & adding ODBC Driver 17 for SQL Server successfully resolved your issue issue.
将 psycopg2-binary 添加到 requirements.txt 文件并为 SQL Server 添加 ODBC 驱动程序 17 后,成功解决了您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.