【问题标题】:Proper way to set up python TimerTrigger in Azure Functions?在 Azure Functions 中设置 python TimerTrigger 的正确方法?
【发布时间】:2021-07-28 01:16:33
【问题描述】:

我有一个无法开始工作的 TimerTrigger Azure 函数(在 Docker 容器中运行)。 __init__.py 执行,引入一些自定义模块,抓取互联网(使用 selenium),并输出到 Twitter。在本地,所有代码都有效。在本地打包到 Azure Function Docker 容器中时,我得到了 zilch。

下面,我放了function.json 文件,我认为这是我的问题所在。我想我可能需要更多的组件,而不仅仅是 TimerTrigger 部分。互联网上没有关于基于 python 的 Azure Functions 和 TimerTriggers 的优秀文档,超出了 Microsoft 发布的内容(相信我,我已经彻底阅读了这些文章中的每一篇)。

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

开始我的__init__.py(我基本上把我所有的自定义模块等都放在了启动函数时自动创建的函数中):

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...

如果 Dockerfile 是相关的:

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

    

【问题讨论】:

  • "schedule": "0 * * * * *" 这说明什么?你想每小时跑一次吗?
  • 每分钟,用于测试目的。一旦一切正常,将其更改为每天几次
  • 不应该是这样吗? 0 */1 * * * *?
  • 是的,如果它每分钟,它应该是 1 而不是 0

标签: python azure azure-functions timer-trigger


【解决方案1】:

您的函数代码绝对正确。正如您所说,所有代码在本地都有效。您没有在问题中提及如何部署到容器。

问题出在这一步:推送到Docker容器。见这篇文章:Docker Container on Azure Functions with Python

关于如何配置Dockerfiles,请看Python in a container

【讨论】:

  • 好吧,真棒,将深入研究文章。我在上面添加了我的 Dockerfile,如果这有帮助的话。我一直在本地构建我的图像(基本上是:docker build --tag <YOUR_DOCKER_HUBB_ID>/ocrfunctionsimage:v1.0.0 .),然后在容器中本地运行它。当我这样做时,我对触发器没有任何操作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-29
相关资源
最近更新 更多