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

妖精的绣舞 提交于 2021-02-16 18:07:16

问题


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 want to make another function that will have Timer Trigger and will call the HTTP trigger function on a schedule.

HTTP Trigger function returns a simple string with execution results.

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. Other times it returns error when it reaches timeout of 30 minutes. The whole thing should run only for 1-2 minutes max so I don't understand where it gets stuck.

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:

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.


回答1:


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). And then I create a TimerTrigger(the cron expression is 0 */1 * * * *) with requests module to call the 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. 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. Then it works fine, they will not interact with each other.

Hope it helps~




回答2:


There seems to be some kind of issue with certain cron expressions regarding this. For example see my GitHub issue here. So I had a cron expression "0-59 * * * *" and this resulted in my function app just stopping polling without an error:

I changed the cron to "0 */2 * * * *" based on information in this blog post and now my function is working as expected.

In the GitHub issue I've asked for an explanation of this behaviour.



来源:https://stackoverflow.com/questions/61035038/why-is-my-python-timer-trigger-function-not-running-at-the-correct-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!