pythonazureazure-functionstimer-trigger

Proper way to set up python TimerTrigger in Azure Functions?


I have a TimerTrigger Azure Function (running in a Docker container) that I can't get to work. The __init__.py executes, pulls in some custom modules, scrapes the interwebs (with selenium), and outputs to Twitter. Locally all the code works. When packaged into an Azure Function Docker container, locally, I get zilch.

Below, I've put the function.json file, which I think is where my issue lies. I think I might need some more components here, beyond just the TimerTrigger part. There's not great documentation on the internet for python-based Azure Functions & TimerTriggers, beyond what Microsoft has put out (and believe me, I've thoroughly read each of those articles).

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

Beginning of my __init__.py (I basically put all my custom modules, etc. within the function automatically created when initiated the function):

def main(mytimer: func.TimerRequest) -> None: #should be something else?
    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)
    
    # Class instantiation for the handling stuff ----
    name_handle_instance = dc.NameHandle()
    #calls other functions below...

If the Dockerfile would be pertinent:

FROM mcr.microsoft.com/azure-functions/python:3.0-python3.6-appservice

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

# install python dependencies
RUN apt-get update \
    && apt-get install -y \
        build-essential \
        cmake \
        git \
        wget \
        unzip \
    && rm -rf /var/lib/apt/lists/*

RUN pip3 install --upgrade pip
RUN pip3 install --upgrade setuptools    

# chrome install
ARG CHROME_VERSION="google-chrome-stable"
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
  && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
  && apt-get update -qqy \
  && apt-get -qqy install \
    ${CHROME_VERSION:-google-chrome-stable} \
  && rm /etc/apt/sources.list.d/google-chrome.list \
  && rm -rf /var/lib/apt/lists/* /var/cache/apt/*

# install chromedriver used by Selenium
RUN LATEST=$(wget -q -O - http://chromedriver.storage.googleapis.com/LATEST_RELEASE) && \
    wget http://chromedriver.storage.googleapis.com/$LATEST/chromedriver_linux64.zip && \
    unzip chromedriver_linux64.zip && ln -s /chromedriver /usr/local/bin/chromedriver

ENV PATH="/usr/local/bin/chromedriver:${PATH}"

RUN pip install -U selenium
COPY . /home/site/wwwroot
RUN cd /home/site/wwwroot && \
    pip install -r requirements.txt

    

Solution

  • Your Function code is absolutely correct. As you said, locally all the code works. You didn't mention how you deploy to container in your question.

    The problem is in this step: push to Docker container. See this article: Docker Container on Azure Functions with Python

    About how to config Dokerfiles, see this Python in a container