skip to Main Content

I have a timer function in Python, that’s supposed to run daily. However there is a business requirement that a certain method runs on a monthly basis.

Is there a way for me to do this?

It would be something like the following in pseudo code:


next_monthly_job = date + "30d"

def main (timer):
    if current_date != next_monthly_job:
        ## do normal stuff
    elif:
        ## do specific monthly stuff
        next_monthly_job = current_date + "30d"

I’m just concerned that the global variable will be overwritten at each trigger, hence never reaching the else statement.

Thanks in advance!

2

Answers


  1. I suppose you’re gonna use Azure Function App. Then it is easy – just configure time trigger (https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=python-v2%2Cin-process&pivots=programming-language-python) for your function. Create two functions, and set the corresponding triggers for each of those.

    Login or Signup to reply.
  2. You can run your Function every month by setting the function.json like below:-

    {
      "scriptFile": "__init__.py",
      "bindings": [
        {
          "name": "mytimer",
          "type": "timerTrigger",
          "direction": "in",
          "schedule": "0 0 0 1 * *"
        }
      ]
    }
    

    And In your function init.py, check the current date to see if you want to execute the regular or customized monthly stuff. To determine whether the timer is past due, utilise the isPastDue property.

    Code:-

    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)
    
    

    Output:-

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search