【发布时间】: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