运行 计时器触发时 Azure 容器实例未终止

Azure container Instance not terminated when running timer trigger

我已经将一个 dockerized azure 函数计时器触发器部署到 azure 容器实例。定时器触发器计划在 运行 上午 6:00。它 运行 符合预期。我的问题是即使在计时器触发器完成后容器也没有终止。因此 ACI 按 24 小时而不是 5 分钟收费。我已将重启策略设置为从不。

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

定时器触发器文件。

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'])

Azure 容器实例的终止取决于您使用的图像。如果图像包含连续操作,则在您停止之前它不会终止。如果图像中的应用程序只是 运行s 中的一个时间段。例如,5 分钟,则容器实例将在 运行ning 状态持续 5 分钟后终止。或者图像中的应用程序出现问题,导致容器实例终止。重启策略只在容器终止时起作用,它不能终止容器。所以如果你想要一个精确的终止,我推荐第一种情况。

更新:

这是您的选择。如果您不想在 ACI 上每天 24 小时付费,您需要停止或删除 ACI。如果你想运行每周期在ACI中触发定时器,你需要为此付费。也许您可以尝试使用逻辑应用来安排工作流,以便在完成收集数据时创建和删除 ACI。

您关注的 link 只是 运行ACI 中的定时器触发器,而不是函数。如果使用 Azure 函数定时器触发器,则不需要使用 ACI。