0

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'])
Karthikeyan VK
  • 5,310
  • 3
  • 37
  • 50

1 Answers1

1

The terminate of the Azure Container Instance depends on what image you use. If the image contains a continuous operation, then it won't terminate until you stop it. If the application in the image just runs in a period. For example, 5 minutes, Then the container instance will terminate after in the running state for 5 minutes. Or the application in the image has something wrong and it causes the container instance to terminate. The restart policy just works when the container terminates, it cannot terminate the container. So if you want an exact terminate, I recommend the first situation.

Update:

It's a selection for you. If you do not want to pay for 24 hours every day on ACI, you need to stop or delete the ACI. If you want to run the timer trigger in the ACI every period, you need to pay for it. Maybe you can try the logic app for schedule workflow on creating and deleting the ACI when finish collects data.

The link you follow just runs the timer trigger inside the ACI, not the function. If you use the Azure function timer trigger, then you do not need to use the ACI.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • Have updated with more details with docker and function src code. It is not a continuous job, it needs to run at 6:00 AM every day. It might run for 5-10 minutes – Karthikeyan VK Jun 11 '20 at 04:54
  • I deployed to the aci with azure functions timer trigger configured. Do i answer ur question? – Karthikeyan VK Jun 11 '20 at 06:36
  • @KarthikeyanVK Do you mean the ACI is created to trigger the Azure function time trigger? If this, will the ACI shut down after the job you set? And how long does the job run? – Charles Xu Jun 11 '20 at 06:42
  • I followed this article. https://medium.com/@sergiibielskyi/how-to-make-own-timer-trigger-using-docker-container-azure-function-aci-3a7fef8a3221 . My scenario almost the same – Karthikeyan VK Jun 11 '20 at 07:47
  • @KarthikeyanVK As I see, it just uses the Function CLI to create the time trigger for the image. actually the image is the solution. And it will always run and collect the data for every time trigger. – Charles Xu Jun 11 '20 at 08:15
  • is there another way to run the timer trigger for the application in aci. I dont want to pay for 24 hours. – Karthikeyan VK Jun 11 '20 at 12:05
  • @KarthikeyanVK You can add code to check if the data collection is finished, if yes, shutdown the ACI and delete it. If no, continue to run it until the finish. – Charles Xu Jun 12 '20 at 01:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215787/discussion-between-karthikeyan-vk-and-charles-xu). – Karthikeyan VK Jun 12 '20 at 02:49