I have deployed a dockerized azure function timer trigger to azure container instance. The timer trigger is scheduled to run at 6:00 AM. It runs as expected. My problem is the container is not terminated even after the timer trigger is completed. So ACI is charged for 24 hours instead of 5 minutes. I have set the restart policy to Never.
FROM mcr.microsoft.com/azure-functions/python:3.0-python3.7
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
AzureWebJobsStorage="DefaultEndpointsProtocol=https;AccountName=XXXX;AccountKey=XXXXXX;EndpointSuffix=core.windows.net"
ENV SITESPEED_IO_BROWSERTIME__XVFB true
ENV SITESPEED_IO_BROWSERTIME__DOCKER true
ENV WEBSITE_TIME_ZONE="India Standard Time"
RUN apt-get update \
&& apt-get install -y \
build-essential \
cmake \
git \
wget \
unzip \
&& rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y --no-install-recommends \
unixodbc-dev \
unixodbc \
libpq-dev
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/*
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 && rm -rf chromedriver_linux64.zip && ln -s $PWD/chromedriver /usr/local/bin/chromedriver
ENV PATH="/usr/local/bin/chromedriver:${PATH}"
COPY . /home/site/wwwroot
RUN pip install --upgrade pip
COPY ./requirements.txt /app/
RUN cat /app/requirements.txt | xargs -n 1 pip install ; exit 0
RUN cd /home/site/wwwroot && \
pip install -r requirements.txt
The timer trigger file.
import datetime
import logging
import os
import azure.functions as func
import json
from ..utility import ablob_utils
from ..utility.db_utils_sql import DB
from ..utility import mail_trigger
def main(mytimer: func.TimerRequest) -> None:
script_dir = os.path.dirname(__file__)
abs_file_path = os.path.join(script_dir, "../settings.json")
logging.info("Absolute path: %s ", abs_file_path)
with open(abs_file_path) as settings:
logging.info("Settings json value %s", settings)
settingJsonObject = json.load(settings)
utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()
# Trigger Scrapper
scraper_exec = xxxx.xxx_scraper(
ablob_utils.BLOB_DB, settingJsonObject).scrape_rrrr()
if scraper_exec['status']:
# Trigger DB Update
logging.info('insert db function ran at %s', utc_timestamp)
yyyy.cccc(
ablob_utils.BLOB_DB, DB, settingJsonObject).insert_to_db()
logging.info('insert db function completed at %s', utc_timestamp)
logging.info(
'timer trigger function completed at %s', utc_timestamp)
else:
mail_trigger.trigger(scraper_exec['msg'])