在 Azure Functions 中设置 python TimerTrigger 的正确方法?

问题描述

我有一个无法开始工作的 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

    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)