简体   繁体   English

为什么我的 python 定时器触发器 function 没有在正确的时间运行?

[英]Why is my python timer trigger function not running at the correct time?

this is probably a noob question.这可能是一个菜鸟问题。 I have an Azure Function that responds to HTTP requests and it works fine, I can call it from a browser or from a Python 3.8 script. I have an Azure Function that responds to HTTP requests and it works fine, I can call it from a browser or from a Python 3.8 script.

I want to make another function that will have Timer Trigger and will call the HTTP trigger function on a schedule.我想制作另一个 function 将有Timer Trigger ,并将按计划调用HTTP trigger function

HTTP Trigger function returns a simple string with execution results. HTTP Trigger function返回一个带有执行结果的简单字符串。

Now my code for Timer trigger function is using Python Requests and it works locally every time, but will work only 1/10 times when deployed to Azure.现在我的Timer trigger function正在使用 Python Requests ,它每次都在本地工作,但在部署到 Z3A580F142203677F135Z30898F63F 时只能工作 1/10 次。 Other times it returns error when it reaches timeout of 30 minutes.有时它会在超时 30 分钟时返回错误。 The whole thing should run only for 1-2 minutes max so I don't understand where it gets stuck.整个过程最多只能运行 1-2 分钟,所以我不明白它卡在哪里了。

When successful it works(I can see in backend of HTTP trigger script ), but in azure logs the logger saves 404 error page html instead of the string that HTTP trigger function should return. When successful it works(I can see in backend of HTTP trigger script ), but in azure logs the logger saves 404 error page html instead of the string that HTTP trigger function should return.

Here is the code for Timer Trigger function:这是定时器触发器 function 的代码:

import datetime
import logging

import azure.functions as func

import requests

def main(mytimer: func.TimerRequest) -> None:
    URL = "https://rob-functions.azurewebsites.net/api/ss_kite_scrape_http"
    r = requests.get(url = URL) 
    data = r.text
    logging.info(f'TIMER TRIGGER HAS RUN. RESULT:{data}')

How to troubleshoot or fix this?如何解决或解决此问题? The logging issue is not so important but the timeout issue has to be fixed somehow and I have no idea where to start since it works perfectly locally.日志记录问题并不那么重要,但超时问题必须以某种方式解决,我不知道从哪里开始,因为它在本地完美运行。

According to some test, I met the issue similar with yours'.根据一些测试,我遇到了与您类似的问题。 I create a HttpTrigger with the default code(I add a line time.sleep(20) in it).我使用默认代码创建了一个 HttpTrigger(我在其中添加了一行time.sleep(20) )。 And then I create a TimerTrigger(the cron expression is 0 */1 * * * * ) with requests module to call the HttpTrigger function.然后我创建了一个 TimerTrigger(cron 表达式为0 */1 * * * * )和requests模块来调用 HttpTrigger function。 The two functions are in one function app, it seems the issue was caused by the two functions interact with each other but I don't know why.这两个功能在一个 function 应用程序中,似乎问题是由两个功能相互交互引起的,但我不知道为什么。 All of the two functions code looks fine.所有两个函数代码看起来都很好。

For a workaround, I create the two functions(HttpTrigger and TimerTrigger) in different function apps and deploy them to two function app in azure.作为解决方法,我在不同的 function 应用程序中创建了两个函数(HttpTrigger 和 TimerTrigger),并将它们部署到 ZCF04A02E37B774FC311A48F605C3C57 中的两个 function 应用程序中。 Then it works fine, they will not interact with each other.然后它工作正常,它们不会相互交互。

Hope it helps~希望有帮助~

The below issue has now apparently been fixed though I've not confirmed尽管我尚未确认,但以下问题现在显然已得到解决


There seems to be some kind of issue with certain cron expressions regarding this.与此有关的某些 cron 表达式似乎存在某种问题。 For example see my GitHub issue here .例如在这里查看我的 GitHub 问题 So I had a cron expression "0-59 * * * *" and this resulted in my function app just stopping polling without an error:所以我有一个 cron 表达式"0-59 * * * *" ,这导致我的 function 应用程序只是停止轮询而没有错误:

在此处输入图像描述

I changed the cron to "0 */2 * * * *" based on information in this blog post and now my function is working as expected.我根据这篇博文中的信息将 cron 更改为"0 */2 * * * *" ,现在我的 function 正在按预期工作。

在此处输入图像描述

In the GitHub issue I've asked for an explanation of this behaviour.GitHub 问题中,我要求解释这种行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM