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

2

4

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.

Hamrnand answered 4/4, 2020 at 21:28 Comment(7)
May I know the frequency of your timer trigger or the cron expression of your timer trigger ?Cotinga
Do you have an authLevel attribute set to 'function' in your httpTrigger Function? Because you are not passing any ?code= value in your URL (you need your Function key if you used default settings when you created the app).Demolish
@HuryShen Yes, the frequency that I tested with is "0 1 10-20/1 * * *". (1 minute past each hour between 10:00 and 20:00 UTC). It does trigger properly, but sometimes it will timeout.Hamrnand
@DjerroNeth I don't use any parameters for the HTTP trigger function and authLevel is set to Anonymous. You can try the URL in the above code in your browser, it should work and return a string after a 10-20 seconds.Hamrnand
Same here. It does not happen if one HttpTrigger function calls another HttpTrigger function, but with another trigger (I tried BlobTrigger and QueueTrigger) I have the same issue. Moreover, I do not see any logs from the second (chained) function.Bechuanaland
this has a issue on github Timer Trigger doesn't start because UnscheduledInvocationReasonInamorato
there is also a related issue (though doesn't mention python here) Azure Functions: timer trigger and consumption plan issueInamorato
C
4

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~

Cotinga answered 9/4, 2020 at 9:36 Comment(4)
Thanks, I will try this next week and let you know the results.Hamrnand
Hi @Hamrnand May I know if it can solve your problem ?Cotinga
Thanks Hury! Sorry I wasn't able to test this sooner. Your solution worked perfectly. Placed the same timer trigger in a different function app and it works 10/10 times now. Weird how Microsoft has designed functions to conflict with each other like this. Thanks again!Hamrnand
This is a great workaround, but it does make it much more cumbersome to keep the code organized (e.g. with CI/CD pipeline). I still wonder why this happens.Bechuanaland
I
0

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. 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:

enter image description here

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

enter image description here

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

Inamorato answered 28/1, 2021 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.