google 应用引擎中的 cron 作业不工作
cron job in google app engine not working
我已经从这个 google cloud tutorial 获取了 Flask 上的基本 python 3 教程网站,我能够设置它并且网站工作正常。
此外,我还想 运行 一个 python 脚本,它每天 运行 收集一些数据,但 cron 作业不起作用。我还添加了 login: admin 来限制任何人直接使用 url
cron.yaml
cron:
- description: test dispatch vs target
url: /cronapp
schedule: every 5 hours
app.yaml
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 3
handlers:
- url: /cronapp
script: cronapp.py
login: admin
我称此为 http://myproject.appspot.com/cronapp 也不起作用,returns 404。
我究竟做错了什么 ?任何帮助表示赞赏
您的 app.yaml 文件混淆了标准环境 Handlers element into a flexible environment configuration,因此它可能被忽略了。您可能会在开发人员控制台的应用程序日志中看到 cron 请求(不过可能有错误)。
您需要在应用 代码中为 /cronapp
添加处理程序,而不是在 app.yaml
中。不完全确定你是怎么做到的(我仍然只使用标准环境),这取决于你的应用 and/or 它的框架。查看 Hello World code review 中的烧瓶示例。
更新:
我可能不完全正确,我的回答基于文档,但我只是注意到一些不一致的地方(我为此向 Google 发送了一些文档反馈)。
灵活的环境 Securing URLs for cron (which appears mostly copied from the standard environment equivalent) 提到了几个解决方案:
- 确实基于
login: admin
选项 handler
:
You can restrict a URL by adding login: admin
to the handler
configuration section in app.yaml
. For more information see
Securing URLs
但是handler
中没有提到Configuring your App with app.yaml and the Securing URLs是指向一个不存在的标签。所以我不确定这是否真的有效。
- 第二个基于
X-Appengine-Cron
头(与标准环境相同):
Requests from the Cron Service will also contain a HTTP header:
X-Appengine-Cron: true
The X-Appengine-Cron
header is set internally by Google App Engine.
If your request handler finds this header it can trust that the
request is a cron request. If the header is present in an external
user request to your app, it is stripped, except for requests from
logged in administrators of the application, who are allowed to set
the header for testing purposes.
但是在Removed headers中提到:
In addition, some selected headers that match the following pattern
are removed from the request:
X-Appengine-*
目前还不清楚这是否延伸至 X-Appengine-Cron
。这值得一试。这是我在(标准环境,基于 webapp2 的)cron 处理程序代码中的检查:
if self.request.headers.get('X-AppEngine-Cron') is None:
self.abort(403) # HTTPForbidden
我已经从这个 google cloud tutorial 获取了 Flask 上的基本 python 3 教程网站,我能够设置它并且网站工作正常。
此外,我还想 运行 一个 python 脚本,它每天 运行 收集一些数据,但 cron 作业不起作用。我还添加了 login: admin 来限制任何人直接使用 url
cron.yaml
cron:
- description: test dispatch vs target
url: /cronapp
schedule: every 5 hours
app.yaml
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 3
handlers:
- url: /cronapp
script: cronapp.py
login: admin
我称此为 http://myproject.appspot.com/cronapp 也不起作用,returns 404。 我究竟做错了什么 ?任何帮助表示赞赏
您的 app.yaml 文件混淆了标准环境 Handlers element into a flexible environment configuration,因此它可能被忽略了。您可能会在开发人员控制台的应用程序日志中看到 cron 请求(不过可能有错误)。
您需要在应用 代码中为 /cronapp
添加处理程序,而不是在 app.yaml
中。不完全确定你是怎么做到的(我仍然只使用标准环境),这取决于你的应用 and/or 它的框架。查看 Hello World code review 中的烧瓶示例。
更新:
我可能不完全正确,我的回答基于文档,但我只是注意到一些不一致的地方(我为此向 Google 发送了一些文档反馈)。
灵活的环境 Securing URLs for cron (which appears mostly copied from the standard environment equivalent) 提到了几个解决方案:
- 确实基于
login: admin
选项handler
:
You can restrict a URL by adding
login: admin
to the handler configuration section inapp.yaml
. For more information see Securing URLs
但是handler
中没有提到Configuring your App with app.yaml and the Securing URLs是指向一个不存在的标签。所以我不确定这是否真的有效。
- 第二个基于
X-Appengine-Cron
头(与标准环境相同):
Requests from the Cron Service will also contain a HTTP header:
X-Appengine-Cron: true
The
X-Appengine-Cron
header is set internally by Google App Engine. If your request handler finds this header it can trust that the request is a cron request. If the header is present in an external user request to your app, it is stripped, except for requests from logged in administrators of the application, who are allowed to set the header for testing purposes.
但是在Removed headers中提到:
In addition, some selected headers that match the following pattern are removed from the request:
X-Appengine-*
目前还不清楚这是否延伸至 X-Appengine-Cron
。这值得一试。这是我在(标准环境,基于 webapp2 的)cron 处理程序代码中的检查:
if self.request.headers.get('X-AppEngine-Cron') is None:
self.abort(403) # HTTPForbidden