pythoncronazure-functionstimer-trigger

Adjust python runtime Azure Function app timer trigger for daylight saving time


My server runs on a start and stop schedule that unfortunately follows daylight saving time. I am looking for a way to dynamically change the CRON schedule on my function app to account for daylight saving time. This is easy in the python code; however, the function app timer trigger doesn't work when you set anything other than a literal string to the schedule parameter of azure.functions.FunctionApp.schedule().

import azure.functions as func
import logging

app = func.FunctionApp()

@app.schedule(
        schedule="<cron_schedule>"
        ,arg_name="myTimer"
        ,run_on_startup=False
        ,use_monitor=True
        ) 

def timer_trigger(myTimer: func.TimerRequest) -> None:
    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')

For example, in @app.schedule(), if you set schedule = "0 35 * * * *", the scheduler triggers on the 35th minute every hour as expected. After verifying that literal strings work, I tried the following approaches which allow for dynamic inputs:

schedule=f"0 {35} * * * *"
var_cron = "0 35 * * * *"
schedule=var_cron
schedule="0 {minutes} * * * *".format(minutes=35)

Even though the Timer Trigger Issue Analysis application insight in the diagnostic hub shows the CRON as 0 35 * * * * for all of these approaches, the app does not actually trigger. Manually running the app in the azure portal returns no errors. Also, the diagnostic view "Functions that are not triggering" returns no errors. From every angle I thought to check, it seems Azure understands what is being passed into the schedule parameter of @app.schedule(). Is there another python approach you can think of trying? Is this a known bug on Azure's side? Any help is very much appreciated.


Solution

  • in @app.schedule(), if you set schedule = "0 35 * * * *", the scheduler triggers on the 35th minute every hour as expected. After verifying that literal strings work

    Use @app.schedule decorator. the schedule parameter must be in a literal string. This means that dynamic inputs, such as variables or functions, cannot be used directly for the schedule.

    The Azure Functions runtime requires a static schedule to properly configure the timer. This can be the cause for the above issue.

    Here is the modified existing function app code with the @app.schedule decorator.

    import datetime  
    import logging  
    import pytz  # Make sure to install pytz if you haven't already  
    
    import azure.functions as func  
    
    app = func.FunctionApp()  
    
    def is_dst(date):  
        """Check if the given date is in Daylight Saving Time."""  
        tz = pytz.timezone("America/Chicago")  # Change to your desired timezone  
        # Ensure the date is naive before localizing  
        naive_date = date.replace(tzinfo=None)  # Remove tzinfo to make it naive  
        localized_date = tz.localize(naive_date)  
        return bool(localized_date.dst().seconds)  
    
    @app.schedule(  
        schedule="0 */1 * * * *",  # Change this to your desired CRON schedule  
        arg_name="myTimer",  
        run_on_startup=False,  
        use_monitor=True  
    )  
    def timer_trigger(myTimer: func.TimerRequest) -> None:  
        # Get the current time in the specified timezone  
        tz = pytz.timezone("America/Chicago")  # Change to your desired timezone  
        current_time = datetime.datetime.now(tz)  
        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)  
    
        # Check if it's currently DST  
        if is_dst(current_time):  
            logging.info('Current time is in Daylight Saving Time.')  
            # Add your logic for DST here  
        else:  
            logging.info('Current time is in Standard Time.')  
            # Add your logic for Standard Time here  
    
        # Log the current local time  
        logging.info('Current local time is %s', current_time.isoformat())
    

    enter image description here

    Deployment status:

    enter image description here

    Succeeded:

    enter image description here